[R] sem and "simple variables"

steven northway northway at interchange.ubc.ca
Thu Nov 13 02:40:48 CET 2008


salutations!
i am doing some longitudinal modeling with sem and thought calculating some
"simple variables" would make my model more readable.  this is the smallest
subset of my model that illustrates the resulting problem.

i have 2 observed exogenous variables (c1, d2) and 4 observed endogenous
variables (dc1, dd1, dc2, dd2).  c1 is the observed state at time 1, dc1 is
the change in c from time 1 to time 2 and dc2 is the change in c from time 2
to time 3.  i want to predict dc1 and dd1 from both c1 and d1.

similarly i want to predict dc2 and dd2 from c2 and d2.  to do this i
calculate c2 as a "simple variable" : an unobservable variable with known
parameters (c2=c1+dc1) and 0 variance. this works with many formulations but
throws a singularity on others.

instead, where ever i use c2 i could just use c1 and dc1 coercing the
estimated parameters to be the same.
bellow are the two versions of this model, one calculating and using c2 as a
simple variable(ram.try2) and the other not(ram.try1).

it is likely i have just misunderstood something in the syntax or the
formulation.  if someone could point me in the right direction i will
appreciate it!

yours aye, steven northway



# longitudinal data model  (only part of the full model, but illustrates my
trouble)
# below are two identical model specifications one with (ram.try2) and one
without (ram.try1)
# calculated endogenous variables (simple variables?). the simple variables
are defined as latent
# variables with fixed path weights and variances of 0.
# these variables are used for clarity and convenience.
#  (d2i=d1+dd1; c2i=c1_dc1)
#  however, they result in
#          Error in solve.default(C[ind, ind]) :
#             Lapack routine dgesv: system is exactly singular
# the model that circumvents theses variables (ram.try1), by repeating their
calculations
#   where ever they would be used, solves without trouble.

library(sem)

s.try <- matrix(c(
    1.000000000,  0,            0,            0,            0,
0,             0,
    8.179735935, 69.283293668,  0,            0,            0,
0,             0,
    1.488597708, 12.801059848,  2.462201404,  0,            0,
0,             0,
    0.064416230,  0.538518090,  0.101546492,  0.009998932,  0,
0,             0,
   -0.015114110, -0.049081308, -0.027940473, -0.000722700,  0.0319872465,
0,             0,
    0.077287545,  0.609258638,  0.108936725,  0.008417597, -0.0003804653,
0.0114547847,  0,
    0.002547743,  0.001505935, -0.002749403,  0.003495991, -0.0048058784,
0.0028064159,  0.015747189
     ), ncol=7, byrow=TRUE)
rownames(s.try) <- colnames(s.try) <-
c('one','d1','c1','dd1','dc1','dd2','dc2')

ram.try1 <- matrix(c(         # this version works by avoiding creating d2
and c2
#  'd1  -> d2', NA, 1,
#  'dd1  -> d2', NA, 1,
#  'd2<->d2',NA,0,
#  'c1  -> c2', NA, 1,
#  'dc1  -> c2', NA, 1,
#  'c2<->c2',NA,0,
  'one -> d1',  'Md', 8.2,
  'one -> c1',  'Mc', 1.5,
  'd1  <-> d1', 'Sd_d', NA,
  'c1  <-> c1', 'Sc_c', NA,
  'd1  <-> c1', 'Sd_c', NA,
  'one -> dd1',  'Idd', .1,
  'one -> dc1',  'Idc', -.2,
  'd1  -> dd1', 'd_dd', -.002,
  'c1  -> dc1', 'c_dc', -.2,
  'd1  -> dc1', 'd_dc', -.0007,
  'c1  -> dd1', 'c_dd', .015,
  'dd1 <-> dd1', 'Sdd_dd',  NA,
  'dc1 <-> dc1', 'Sdc_dc',  NA,
  'dd1 <-> dc1', 'Sdd_dc',  NA,
#  'd2  -> dd2', 'd_dd', -.002,
  'd1  -> dd2', 'd_dd', -.002,
  'dd1  -> dd2', 'd_dd', -.002,
#  'c2  -> dc2', 'c_dc', -.2,
  'c1  -> dc2', 'c_dc', -.2,
  'dc1  -> dc2', 'c_dc', -.2,
#  'd2  -> dc2', 'd_dc', -.0007,
  'd1  -> dc2', 'd_dc', -.0007,
  'dd1  -> dc2', 'd_dc', -.0007,
#  'c2  -> dd2', 'c_dd', .015,
  'c1  -> dd2', 'c_dd', .015,
  'dc1  -> dd2', 'c_dd', .015,
  'one -> dd2',  'Idd', .1,
  'one -> dc2',  'Idc', -.2,
  'dd2 <-> dd2', 'Sdd_dd',  NA,
  'dc2 <-> dc2', 'Sdc_dc',  NA,
  'dd2 <-> dc2', 'Sdd_dc',  NA
  ),ncol=3, byrow=TRUE)
sem.try1 <- sem(ram.try1,s.try,N=80,raw=TRUE,fixed.x=c('one')  )

ram.try2 <- matrix(c(                #this version fails ....
  'd1  -> d2', NA, 1,
  'dd1  -> d2', NA, 1,
  'd2<->d2',NA,0,
  'c1  -> c2', NA, 1,
  'dc1  -> c2', NA, 1,
  'c2<->c2',NA,0,
  'one -> d1',  'Md', 8.2,
  'one -> c1',  'Mc', 1.5,
  'd1  <-> d1', 'Sd_d', NA,
  'c1  <-> c1', 'Sc_c', NA,
  'd1  <-> c1', 'Sd_c', NA,
  'one -> dd1',  'Idd', .1,
  'one -> dc1',  'Idc', -.2,
  'd1  -> dd1', 'd_dd', -.002,
  'c1  -> dc1', 'c_dc', -.2,
  'd1  -> dc1', 'd_dc', -.0007,
  'c1  -> dd1', 'c_dd', .015,
  'dd1 <-> dd1', 'Sdd_dd',  NA,
  'dc1 <-> dc1', 'Sdc_dc',  NA,
  'dd1 <-> dc1', 'Sdd_dc',  NA,
  'd2  -> dd2', 'd_dd', -.002,
#  'd1  -> dd2', 'd_dd', -.002,
#  'dd1  -> dd2', 'd_dd', -.002,
  'c2  -> dc2', 'c_dc', -.2,
#  'c1  -> dc2', 'c_dc', -.2,
#  'dc1  -> dc2', 'c_dc', -.2,
  'd2  -> dc2', 'd_dc', -.0007,                     # remove either to get
rid of singularity
#  'd1  -> dc2', 'd_dc', -.0007,
#  'dd1  -> dc2', 'd_dc', -.0007,
  'c2  -> dd2', 'c_dd', .015,                       # remove either to get
rid of singularity
#  'c1  -> dd2', 'c_dd', .015,
#  'dc1  -> dd2', 'c_dd', .015,
  'one -> dd2',  'Idd', .1,
  'one -> dc2',  'Idc', -.2,
  'dd2 <-> dd2', 'Sdd_dd',  NA,
  'dc2 <-> dc2', 'Sdc_dc',  NA,
  'dd2 <-> dc2', 'Sdd_dc',  NA
  ),ncol=3, byrow=TRUE)
sem.try2 <- sem(ram.try2,s.try,N=80,raw=TRUE,fixed.x=c('one')  )



summary(sem.try1)
summary(sem.try2)



More information about the R-help mailing list