Question

I keep getting an error when I try to compile a .rnw file with a model specified by specifyModel() from the sem package. knitr does not like the comma in F1 -> X1, lam1, NA.

Here is an example that reproduces the error:

\documentclass{article}

\begin{document}

<<specify, include=FALSE, tidy=FALSE>>=
# Simple CFA Model
  # from: http://www.statmethods.net/advstats/factor.html
    library(sem)
    model.mydata <- specifyModel() 
      F1 ->  X1, lam1, NA
      F1 ->  X2, lam2, NA 
      F1 ->  X3, lam3, NA 
      F2 ->  X4, lam4, NA 
      F2 ->  X5, lam5, NA 
      F2 ->  X6, lam6, 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 
      F1 <-> F1, NA,    1 
      F2 <-> F2, NA,    1 
      F1 <-> F2, F1F2, NA

    model.mydata
# end
@

\end{document}

Here is the error:

#Quitting from lines 5-27 (test.rnw) 
#Error in parse(text = x, srcfile = src) : <text>:5:16: unexpected ','
#5:     model.mydata <- specifyModel() 
#6:       F1 ->  X1,

Any thoughts?

Was it helpful?

Solution

If you don't specify the file in specifyModel it will attempt to read from the standard input stream (stdin) -- while this may be possible within knitr I think it would be easier to read the model specifications in from a file.

For example

model <- readLines(con = textConnection('F1 ->  X1, lam1, NA
F1 ->  X2, lam2, NA 
F1 ->  X3, lam3, NA 
F2 ->  X4, lam4, NA 
F2 ->  X5, lam5, NA 
F2 ->  X6, lam6, 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 
F1 <-> F1, NA,    1 
F2 <-> F2, NA,    1 
F1 <-> F2, F1F2, NA'))
cat(model, file = 'model.spec', sep = '\n')
model.mydata <- specifyModel(file = 'model.spec') 

Alternatively you could simply create the model.spec file and read it in.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top