rr-lavaanfactor-analysissem

How can I deal with the Lavaan error: "syntax error in lavaan model syntax"?


I am trying to do a CFA for the first time. Lavaan gives the following error.

Error in lavParseModelString(model) : 
  lavaan ERROR: syntax error in lavaan model syntax

My code looks simplified like this:

mycfa <- 'Construct =~ A +
                       B +
                       C +
                       D +
                       E +
                       F +
                       G +
                       H
                       '
fit <- cfa(mycfa, data = mydataframe)

I would guess that regression dependencies and covariances are in my model or would lavaan output otherwise? Does anyone have a tip for me on how to proceed.


Solution

  • I believe your problem is related with the encoding of your characters in the model. You should use Unicode characters (avoid Non-ASCII Characters). I would suggest pasting as simple text or rewriting the model. After that It is quite simple:

    library(lavaan)
    #> This is lavaan 0.6-8
    #> lavaan is FREE software! Please report any bugs.
    
    mydataframe <- HolzingerSwineford1939[, paste0("x",1:8)]
    names(mydataframe) <- LETTERS[1:8]
    
    mycfa <- 'Construct =~ A +
                           B +
                           C +
                           D +
                           E +
                           F +
                           G +
                           H
                           '
    fit <- cfa(mycfa, data = mydataframe)
    summary(fit)
    #> lavaan 0.6-8 ended normally after 30 iterations
    #> 
    #>   Estimator                                         ML
    #>   Optimization method                           NLMINB
    #>   Number of model parameters                        16
    #>                                                       
    #>   Number of observations                           301
    #>                                                       
    #> Model Test User Model:
    #>                                                       
    #>   Test statistic                               209.040
    #>   Degrees of freedom                                20
    #>   P-value (Chi-square)                           0.000
    #> 
    #> Parameter Estimates:
    #> 
    #>   Standard errors                             Standard
    #>   Information                                 Expected
    #>   Information saturated (h1) model          Structured
    #> 
    #> Latent Variables:
    #>                    Estimate  Std.Err  z-value  P(>|z|)
    #>   Construct =~                                        
    #>     A                 1.000                           
    #>     B                 0.505    0.157    3.215    0.001
    #>     C                 0.476    0.151    3.161    0.002
    #>     D                 2.006    0.276    7.273    0.000
    #>     E                 2.203    0.304    7.258    0.000
    #>     F                 1.862    0.257    7.250    0.000
    #>     G                 0.367    0.141    2.593    0.010
    #>     H                 0.371    0.133    2.801    0.005
    #> 
    #> Variances:
    #>                    Estimate  Std.Err  z-value  P(>|z|)
    #>    .A                 1.115    0.093   11.926    0.000
    #>    .B                 1.320    0.108   12.195    0.000
    #>    .C                 1.220    0.100   12.198    0.000
    #>    .D                 0.370    0.047    7.804    0.000
    #>    .E                 0.476    0.059    8.087    0.000
    #>    .F                 0.352    0.043    8.230    0.000
    #>    .G                 1.150    0.094   12.224    0.000
    #>    .H                 0.988    0.081   12.215    0.000
    #>     Construct         0.244    0.067    3.640    0.000
    

    Created on 2021-04-23 by the reprex package (v2.0.0)