1
$\begingroup$

I'm having difficulty with some syntax in R using the sem package. I'm trying to do the following:

  1. Create four latent variables with three (attitudinal) indicators each
  2. Regress these latent variables on an observed (behavioral) variable
  3. Compare this to a model where I regress four latent variables with three indicators each on a higher-order latent variable

Using the sem package in R, my code for doing steps 1-2 is:

mydata.cov<-cov(mydata)
model.mydata <- specify.model() 
F1 ->  X1, lam1, NA
F1 ->  X2, lam2, NA 
F1 ->  X3, lam3, NA
F2 ->  X4, lam4, NA 
F2 ->  X5, lam5, NA
F2 ->  X6, lam6, NA
F3 ->  X7, lam7, NA
F3 ->  X8, lam8, NA
F3 ->  X9, lam9, NA 
F4 ->  X10, lam10, NA 
F4 ->  X11, lam11, NA
F4 ->  X12, lam12, NA
behav -> F1, lam13, NA
behav -> F2, lam14, NA
behav -> F3, lam15, NA
behav -> F4, lam16, NA
X1 <-> X1, e1,   NA 
X2 <-> X2, e2,   NA 
X3 <-> X3, e3,   NA 
X4 <-> X4, e4,   NA 
X5 <-> X5, e5,   NA 
X6 <-> X6, e6,   NA 
X7 <-> X7, e7,   NA 
X8 <-> X8, e8,   NA 
X9 <-> X9, e9,   NA 
X10 <-> X10, e10,   NA 
X11 <-> X11, e11,   NA 
X12 <-> X12, e12,   NA 
F1 <-> F1, e13, NA 
F2 <-> F2, e14, NA 
F3 <-> F3, e15, NA 
F4 <-> F4, e16,  NA 
behav <-> behav, NA, 1

Presumably, the second model would be similar code:

model2.mydata <- specify.model() 
F1 ->  X1, lam1, NA
F1 ->  X2, lam2, NA 
F1 ->  X3, lam3, NA
F2 ->  X4, lam4, NA 
F2 ->  X5, lam5, NA
F2 ->  X6, lam6, NA
F3 ->  X7, lam7, NA
F3 ->  X8, lam8, NA
F3 ->  X9, lam9, NA 
F4 ->  X10, lam10, NA 
F4 ->  X11, lam11, NA
F4 ->  X12, lam12, NA
F5 -> F1, lam13, NA
F5 -> F2, lam14, NA
F5 -> F3, lam15, NA
F5 -> F4, lam16, NA
X1 <-> X1, e1,   NA 
X2 <-> X2, e2,   NA 
X3 <-> X3, e3,   NA 
X4 <-> X4, e4,   NA 
X5 <-> X5, e5,   NA 
X6 <-> X6, e6,   NA 
X7 <-> X7, e7,   NA 
X8 <-> X8, e8,   NA 
X9 <-> X9, e9,   NA 
X10 <-> X10, e10,   NA 
X11 <-> X11, e11,   NA 
X12 <-> X12, e12,   NA 
F1 <-> F1, e13, NA 
F2 <-> F2, e14, NA 
F3 <-> F3, e15, NA 
F4 <-> F4, e16,  NA 
F5 <-> F5, NA, 1

model.sem2 <- sem(model2.mydata, mydata.cov, nrow(labor_coef))
# print results (fit indices, parameters, hypothesis tests) 
summary(model2.sem)
stdCoef(model2.sem)

Unfortunately, neither syntax appears to work. Both models should be identified, and R just gives me a generic error message when I run the summary command:

Updated: Now, the model will run, but summary returns the following: Error in summary.objectiveML(data.sem) : coefficient covariances cannot be computed.

Thoughts?

$\endgroup$
4
  • $\begingroup$ what are the error messages? one issue , though prob a typo - behav goes onto F1 three times & same for F5. You may find it easier to define model with cfa: eg cfa.mod <- cfa() 1: F1: x1 , x2 , x2 $\endgroup$ – user20650 Apr 8 '13 at 21:05
  • $\begingroup$ Thanks for catching the typo! (I changed my variable names for clarity). The error message says: Error in classifyVariables(model) : incorrectly specified model $\endgroup$ – roody Apr 8 '13 at 22:22
  • $\begingroup$ Updated: Now, the model will run, but summary returns the following: Error in summary.objectiveML(data.sem) : coefficient covariances cannot be computed $\endgroup$ – roody Apr 8 '13 at 23:40
  • $\begingroup$ model is probably under-identified. Try setting factor variances to one $\endgroup$ – user20650 Apr 8 '13 at 23:45
1
$\begingroup$

Sorry i don't use specify model so can't spot the error (although perhaps set variances of factors to 1). I think cfa command might be a bit easier for specification. A wee example below:

library(MASS)    
library(sem)

# Create data
sigma <- matrix(c(1.0, 0.2, 0.4, 0.5, 0.6, 0.3,
                  0.2, 1.0, 0.4, 0.5, 0.2, 0.6,
                  0.4, 0.4, 1.0, 0.3, 0.2, 0.6,
                  0.5, 0.5, 0.3, 1.0, 0.7, 0.2, 
                  0.6, 0.2, 0.2, 0.7, 1.0, 0.4, 
                  0.3, 0.6, 0.6, 0.2, 0.4, 1.0), 
                  nrow=6, byrow=T)    

df <- data.frame(mvrnorm(200 , c(10,15,20,25,30,35) , sigma))    
names(df) <- paste("X",1:6,sep="")    
df$age <- rnorm(100,40,15)

my.cor <- cor(df)

# Specify model
cfa.mod <- cfa(reference.indicators=FALSE, covs=NULL)    
F1: X1,X2,X3    
F2: X4,X5,X6    
age: F1,F2

# Look at model parameters
cfa.mod

# estimate model
cfa.sem <- sem(cfa.mod, S=my.cor, N=200) 
summary(cfa.sem) 
$\endgroup$
2
  • 1
    $\begingroup$ Wow. That is SO MUCH EASIER. From the bottom of the heart of a crazed dissertating doctoral candidate...THANK YOU! $\endgroup$ – roody Apr 8 '13 at 23:45
  • $\begingroup$ Nice example @user20650. I will put that in my back pocket! $\endgroup$ – doug.numbers May 16 '13 at 2:14

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.