lavaan ERROR: missing observed variables in dataset: ..constant..

346 views
Skip to first unread message

Andreas Schmidt

unread,
Aug 20, 2020, 8:52:02 AM8/20/20
to lav...@googlegroups.com
Hello,


I'm a first time user of R and the lavaan package so it might be an obvious mistake, thanks in advance for your help and patience. I have collected data of 732 students who took a math test of originally 24 items, in later versions two items were dismissed, that's why I'm only including 22 items (data see attached file).

Long story short - I want to perform a cfa, analyzing how appropriate a one-factor-model fits the data. 

My R script is shown below, I am using R-4.0.2 for Windows, RStudio Desktop 1.3.1073, lavaan 0.6-1:

library(lavaan)
library(readxl)
mathtest <- read_excel("C:/Users/andis/Downloads/mathtest.xlsx")

mathknowledge <- '
f1 =~ "MA1"+"MA2"+"MA3"+"MA4"+"MA5"+"MA6"+"MA7"+"MA8"+"MA9"+"MA10"+"MA11"+"MA13"+"MA14"+"MA15"+"MA16"+"MA17"+"MA18"+"MA20"+"MA21"+"MA22"+"MA23"+"MA24"
'
model_cfa <- cfa(model = mathknowledge, data = mathtest)
fit <- cfa(model=mathknowledge, data= mathtest)


> model_cfa <- cfa(model = mathknowledge, data = mathtest)
lavaan ERROR: missing observed variables in dataset: ..constant..


What can I do?


Thank you very much for your time.


Regards,

Andreas
mathtest.xlsx

balal izanloo

unread,
Aug 20, 2020, 9:33:22 AM8/20/20
to lav...@googlegroups.com
Hi
i have changed some parts in your syntax and it is working now. HTH

library(lavaan)
library(readxl)
mathtest <- read_excel("C:/Users/Yasine/Downloads/mathtest.xlsx")
head(mathtest)
dim(mathtest)
mathknowledge <- '
f1 =~ MA1+MA2+MA3+MA4+MA5+MA6+MA7+MA8+MA9+MA10+MA11+MA13+MA14+MA15+MA16+MA17+MA18+MA20+MA21+MA22+MA23+MA24

'
model_cfa <- cfa(model = mathknowledge, data = mathtest)
model_cfa
summary(model_cfa,fit.measures=TRUE,standardized=TRUE)
lavaan 0.6-6 ended normally after 64 iterations

  Estimator                                         ML
  Optimization method                           NLMINB
  Number of free parameters                         44
                                                     
  Number of observations                           732
                                                     
Model Test User Model:
                                                     
  Test statistic                               362.653
  Degrees of freedom                               209
  P-value (Chi-square)                           0.000

Model Test Baseline Model:

  Test statistic                              2213.580
  Degrees of freedom                               231
  P-value                                        0.000

User Model versus Baseline Model:

  Comparative Fit Index (CFI)                    0.922
  Tucker-Lewis Index (TLI)                       0.914

Loglikelihood and Information Criteria:

  Loglikelihood user model (H0)              -9654.783
  Loglikelihood unrestricted model (H1)      -9473.457
                                                     
  Akaike (AIC)                               19397.567
  Bayesian (BIC)                             19599.781
  Sample-size adjusted Bayesian (BIC)        19460.067

Root Mean Square Error of Approximation:

  RMSEA                                          0.032
  90 Percent confidence interval - lower         0.026
  90 Percent confidence interval - upper         0.037
  P-value RMSEA <= 0.05                          1.000

Standardized Root Mean Square Residual:

  SRMR                                           0.038

Parameter Estimates:

  Standard errors                             Standard
  Information                                 Expected
  Information saturated (h1) model          Structured

Latent Variables:
                   Estimate  Std.Err  z-value  P(>|z|)   Std.lv  Std.all
  f1 =~                                                                
    MA1               1.000                               0.173    0.346
    MA2               0.895    0.141    6.364    0.000    0.155    0.344
    MA3               0.883    0.126    6.986    0.000    0.153    0.411
    MA4               1.147    0.164    6.972    0.000    0.198    0.409
    MA5               0.931    0.141    6.607    0.000    0.161    0.368
    MA6               1.364    0.183    7.437    0.000    0.236    0.474
    MA7               0.719    0.117    6.166    0.000    0.124    0.325
    MA8               1.270    0.176    7.229    0.000    0.220    0.443
    MA9               1.408    0.187    7.522    0.000    0.243    0.487
    MA10              1.093    0.152    7.182    0.000    0.189    0.436
    MA11              1.010    0.151    6.706    0.000    0.175    0.379
    MA13              0.877    0.135    6.507    0.000    0.152    0.358
    MA14              1.246    0.172    7.226    0.000    0.216    0.443
    MA15              1.158    0.168    6.898    0.000    0.200    0.400
    MA16              1.698    0.211    8.034    0.000    0.294    0.589
    MA17              1.229    0.170    7.216    0.000    0.213    0.441
    MA18              1.023    0.155    6.615    0.000    0.177    0.369
    MA20              1.156    0.162    7.120    0.000    0.200    0.428
    MA21              1.003    0.157    6.405    0.000    0.173    0.348
    MA22              1.382    0.184    7.501    0.000    0.239    0.484
    MA23              0.909    0.144    6.328    0.000    0.157    0.340
    MA24              1.377    0.185    7.455    0.000    0.238    0.477

Variances:
                   Estimate  Std.Err  z-value  P(>|z|)   Std.lv  Std.all
   .MA1               0.219    0.012   18.588    0.000    0.219    0.880
   .MA2               0.179    0.010   18.597    0.000    0.179    0.882
   .MA3               0.115    0.006   18.320    0.000    0.115    0.831
   .MA4               0.195    0.011   18.328    0.000    0.195    0.832
   .MA5               0.166    0.009   18.507    0.000    0.166    0.865
   .MA6               0.192    0.011   17.977    0.000    0.192    0.776
   .MA7               0.131    0.007   18.659    0.000    0.131    0.894
   .MA8               0.198    0.011   18.157    0.000    0.198    0.804
   .MA9               0.190    0.011   17.887    0.000    0.190    0.762
   .MA10              0.152    0.008   18.193    0.000    0.152    0.810
   .MA11              0.182    0.010   18.464    0.000    0.182    0.857
   .MA13              0.157    0.008   18.546    0.000    0.157    0.872
   .MA14              0.191    0.011   18.160    0.000    0.191    0.804
   .MA15              0.210    0.011   18.370    0.000    0.210    0.840
   .MA16              0.162    0.010   17.005    0.000    0.162    0.653
   .MA17              0.187    0.010   18.167    0.000    0.187    0.805
   .MA18              0.199    0.011   18.503    0.000    0.199    0.864
   .MA20              0.178    0.010   18.236    0.000    0.178    0.817
   .MA21              0.219    0.012   18.583    0.000    0.219    0.879
   .MA22              0.187    0.010   17.910    0.000    0.187    0.766
   .MA23              0.189    0.010   18.609    0.000    0.189    0.884
   .MA24              0.193    0.011   17.959    0.000    0.193    0.773
    f1                0.030    0.007    4.403    0.000    1.000    1.000




--
You received this message because you are subscribed to the Google Groups "lavaan" group.
To unsubscribe from this group and stop receiving emails from it, send an email to lavaan+un...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/lavaan/CAJ2TfY-6xdZkzL6wO-63LEpj0z0%2Bb%2BCFiu8-uc3a8kka2_A2TQ%40mail.gmail.com.
run-me.R

Andreas S

unread,
Aug 20, 2020, 9:38:28 AM8/20/20
to lavaan
Thank you so much for your help, it helped me a lot!!!
Reply all
Reply to author
Forward
0 new messages