Error Message in Francis Huangs Article on Multilevel Confirmatory Factor Analysis

Hi there,
I am running a Mulitlevel Confirmatory Factorial Analysis in R based on the paper of Francis Huang. Iam working with the syntax from the article and the example data set used in the article.

Here is the Article: Huang, F. L. (2017). Conducting multilevel confirmatory factor analysis using R. Unpublished Manuscript). http://faculty. missouri. edu/huangf/data/mcfa/MCFA% 20in% 20R% 20HUANG. pdf .

Watch out! The webadress for downloading the data and the R function is obsolete in the paper. In the syntax below the webadresses are correct.

# load lavaan package
Library(lavaan)

# That’s an r function
source('http://web.missouri.edu/huangf/data/mcfa/mcfa.R')

#dataset containing only the grouping variable and the variables of interest.
raw <- read.csv("http://web.missouri.edu/huangf/data/mcfa/raw.csv")

#the function is applied to the dataset specifying that sid (school id) is the clustering #variable
x <- mcfa.input("sid", raw)

############## STEP 1
# one factor model and two factor model with results
onefactor <- 'f1 =~ x1 + x2 + x3 + x4 + x5 + x6'
twofactor <- 'f1 =~ x1 + x2 + x4; f2= ~x3 + x5 + x6'

results1 <- cfa(onefactor, sample.cov = x$pw.cov, sample.nobs = x$n - x$G)
summary(results1, fit.measures = T, standardized = T)

results2 <- cfa(twofactor, sample.cov = x$pw.cov, sample.nobs = x$n - x$G)
summary(results2, fit.measures = T, standardized = T)

############# STEP 2
# A null model is specified where both the SPW and SB matrices are used in a  
# multigroup setup using the factor structure defined at step 1 on both matrices with 
# all equality constraints set to be equal. We do not really have two groups but 
# the multigroup setup will be used to analyze both the `within group' and the 
#`between  group' matrices simultaneously. In lavaan, multiple input covariance 
#matrices and the sample sizes for each are stored in a list object:

combined.cov <- list(within = x$pw.cov, between = x$b.cov)
combined.n <- list(within = x$n - x$G, between = x$G)

#The first object in the list refers to group one and the second object refers to group 
#two and we create two new objects (i.e., combined.cov and combined.n) that 
#contain the two covariance matrices (i.e., SPW and SB) and the sample size for each 
#(n - G and G, respectively).



# A model imposing the equality constraints must be specified. In this step, the 
#model specification expands quite a bit. In lavaan, the equality constrains are imposed 
#for the particular variable by indicating c(a,a)*variable where c() is the concatenate 
#function, a is a label assigned by the user to indicate that loading a for group one is set to 
#be equal for loading a in group two. The same label names instruct lavaan to use the 
#same estimates between groups or in other words, specify equality constraints. To 
#specify equal factor loadings for both factors for the within and between models, 
#we indicate: f1 =~ x1 + c(a,a)*x2 + c(b,b)*x4; f2 =~ x3 + c(c,c)*x5 + c(d,d)*x6. The loadings 
#for x1 and x3 are auto matically set to 1 so do not need to be specified.

nullmodel <- '
+ f1 =~ x1 + c(a,a)*x2 + c(b,b)*x4
+ f2 =~ x3 + c(c,c)*x5 + c(d,d)*x6
+ x1 ~~ c(e,e)*x1
+ x2 ~~ c(f,f)*x2
+ x3 ~~ c(g,g)*x3
+ x4 ~~ c(h,h)*x4
+ x5 ~~ c(i,i)*x5
+ x6 ~~ c(j,j)*x6
+ f1 ~~ c(k,k)*f1
+ f2 ~~ c(l,l)*f2
+ f1 ~~ c(m,m)*f2
+ '

results3 <- cfa(nullmodel, sample.cov = combined.cov,
+ sample.nobs = combined.n)

#Thats the error message
Error: Unexpected '=' in:
"results3 <- cfa(nullmodel, sample.cov = combined.cov, + sample.nobs ="

With my data I am received the same error message.

Any ideas? Help is appreciated.
Best regards Konstantin

The way the code is printed on the paper is not beginner-friendly, you are copying the console prompt by mistake, this part of the code shouldn't have leading "+" symbols on it.

nullmodel <- '
f1 =~ x1 + c(a,a)*x2 + c(b,b)*x4
f2 =~ x3 + c(c,c)*x5 + c(d,d)*x6
x1 ~~ c(e,e)*x1
x2 ~~ c(f,f)*x2
x3 ~~ c(g,g)*x3
x4 ~~ c(h,h)*x4
x5 ~~ c(i,i)*x5
x6 ~~ c(j,j)*x6
f1 ~~ c(k,k)*f1
f2 ~~ c(l,l)*f2
f1 ~~ c(m,m)*f2
'

results3 <- cfa(nullmodel, sample.cov = combined.cov, sample.nobs = combined.n)
summary(results3, fit.measures = T, standardized = T)
#> lavaan 0.6-11 ended normally after 28 iterations
#> 
#>   Estimator                                         ML
#>   Optimization method                           NLMINB
#>   Number of model parameters                        26
#>   Number of equality constraints                    13
#>                                                       
#>   Number of observations per group:                   
#>     within                                        3640
#>     between                                        254
#>                                                       
#> Model Test User Model:
#>                                                       
#>   Test statistic                              1238.556
#>   Degrees of freedom                                29
#>   P-value (Chi-square)                           0.000
#>   Test statistic for each group:
#>     within                                     295.906
#>     between                                    942.650
#> 
#> Model Test Baseline Model:
#> 
#>   Test statistic                             11054.075
#>   Degrees of freedom                                30
#>   P-value                                        0.000
#> 
#> User Model versus Baseline Model:
#> 
#>   Comparative Fit Index (CFI)                    0.890
#>   Tucker-Lewis Index (TLI)                       0.886
#> 
#> Loglikelihood and Information Criteria:
#> 
#>   Loglikelihood user model (H0)             -28058.005
#>   Loglikelihood unrestricted model (H1)     -27438.727
#>                                                       
#>   Akaike (AIC)                               56142.010
#>   Bayesian (BIC)                             56223.483
#>   Sample-size adjusted Bayesian (BIC)        56182.175
#> 
#> Root Mean Square Error of Approximation:
#> 
#>   RMSEA                                          0.146
#>   90 Percent confidence interval - lower         0.139
#>   90 Percent confidence interval - upper         0.153
#>   P-value RMSEA <= 0.05                          0.000
#> 
#> Standardized Root Mean Square Residual:
#> 
#>   SRMR                                           0.229
#> 
#> Parameter Estimates:
#> 
#>   Standard errors                             Standard
#>   Information                                 Expected
#>   Information saturated (h1) model          Structured
#> 
#> 
#> Group 1 [within]:
#> 
#> Latent Variables:
#>                    Estimate  Std.Err  z-value  P(>|z|)   Std.lv  Std.all
#>   f1 =~                                                                 
#>     x1                1.000                               0.789    0.900
#>     x2         (a)    1.131    0.017   67.165    0.000    0.892    0.904
#>     x4         (b)    0.684    0.020   33.599    0.000    0.540    0.514
#>   f2 =~                                                                 
#>     x3                1.000                               0.801    0.686
#>     x5         (c)    1.171    0.026   45.548    0.000    0.938    0.850
#>     x6         (d)    1.204    0.026   45.968    0.000    0.965    0.867
#> 
#> Covariances:
#>                    Estimate  Std.Err  z-value  P(>|z|)   Std.lv  Std.all
#>   f1 ~~                                                                 
#>     f2         (m)    0.448    0.016   28.013    0.000    0.708    0.708
#> 
#> Variances:
#>                    Estimate  Std.Err  z-value  P(>|z|)   Std.lv  Std.all
#>    .x1         (e)    0.146    0.008   19.404    0.000    0.146    0.190
#>    .x2         (f)    0.178    0.010   18.684    0.000    0.178    0.183
#>    .x3         (g)    0.724    0.019   38.582    0.000    0.724    0.530
#>    .x4         (h)    0.810    0.019   42.494    0.000    0.810    0.735
#>    .x5         (i)    0.339    0.013   26.264    0.000    0.339    0.278
#>    .x6         (j)    0.307    0.013   23.764    0.000    0.307    0.248
#>     f1         (k)    0.622    0.018   33.871    0.000    1.000    1.000
#>     f2         (l)    0.642    0.028   23.107    0.000    1.000    1.000
#> 
#> 
#> Group 2 [between]:
#> 
#> Latent Variables:
#>                    Estimate  Std.Err  z-value  P(>|z|)   Std.lv  Std.all
#>   f1 =~                                                                 
#>     x1                1.000                               0.789    0.900
#>     x2         (a)    1.131    0.017   67.165    0.000    0.892    0.904
#>     x4         (b)    0.684    0.020   33.599    0.000    0.540    0.514
#>   f2 =~                                                                 
#>     x3                1.000                               0.801    0.686
#>     x5         (c)    1.171    0.026   45.548    0.000    0.938    0.850
#>     x6         (d)    1.204    0.026   45.968    0.000    0.965    0.867
#> 
#> Covariances:
#>                    Estimate  Std.Err  z-value  P(>|z|)   Std.lv  Std.all
#>   f1 ~~                                                                 
#>     f2         (m)    0.448    0.016   28.013    0.000    0.708    0.708
#> 
#> Variances:
#>                    Estimate  Std.Err  z-value  P(>|z|)   Std.lv  Std.all
#>    .x1         (e)    0.146    0.008   19.404    0.000    0.146    0.190
#>    .x2         (f)    0.178    0.010   18.684    0.000    0.178    0.183
#>    .x3         (g)    0.724    0.019   38.582    0.000    0.724    0.530
#>    .x4         (h)    0.810    0.019   42.494    0.000    0.810    0.735
#>    .x5         (i)    0.339    0.013   26.264    0.000    0.339    0.278
#>    .x6         (j)    0.307    0.013   23.764    0.000    0.307    0.248
#>     f1         (k)    0.622    0.018   33.871    0.000    1.000    1.000
#>     f2         (l)    0.642    0.028   23.107    0.000    1.000    1.000
1 Like

Hi Andresrcs,
great. Thanks a lot for your help.
Without the "+" it is working.
Super!
Thanks a lot...

This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.

If you have a query related to it or one of the replies, start a new topic and refer back with a link.