简体   繁体   English

sem 包的 knitr 和 specificModel()

[英]knitr and specifyModel() of sem package

I keep getting an error when I try to compile a .rnw file with a model specified by specifyModel() from the sem package.当我尝试使用sem包中的指定模型specifyModel()指定的模型编译.rnw文件时,我不断收到错误specifyModel() knitr does not like the comma in F1 -> X1, lam1, NA . knitr不喜欢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?有什么想法吗?

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.如果您没有在specifyModel指定文件,它将尝试从标准输入流( stdin )中读取——虽然这在knitr可能是可能的,但我认为从文件中读取模型规范会更容易。

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.或者,您可以简单地创建model.spec文件并将其读入。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM