****I am running a CFA in R using the Lavaan pkg for the first time. I have got everything up and running however for some reason my none of my factor loadings are set to 1 like they are supposed to be. I want to know why Lavaan isn't automatically setting one of the loadings to one for each of the factors.
This is the code I used:****
model1<-'comm=~relimport+relthink+relhurt
Ind=~attend+prayer+relread
relimport~~relthink'
fit1 <-cfa(model1, data=SIM1, std.lv=TRUE)
summary (fit1, ci=T, standardized=T, fit.measures=T )
modindices(fit1, minimum.value=10, sort=TRUE)
lavaanPlot(model=fit1, node_options=list(shape="box", fontname= "Helvetica"),
edge_options=list(color="grey"), coefs=TRUE, stand=TRUE)
Here is my output:
lavaan 0.6.13 ended normally after 30 iterations
Estimator ML
Optimization method NLMINB
Number of model parameters 14
Used Total
Number of observations 796 1769
Model Test User Model:
Test statistic 2.707
Degrees of freedom 7
P-value (Chi-square) 0.911
Model Test Baseline Model:
Test statistic 1394.558
Degrees of freedom 15
P-value 0.000
User Model versus Baseline Model:
Comparative Fit Index (CFI) 1.000
Tucker-Lewis Index (TLI) 1.007
Loglikelihood and Information Criteria:
Loglikelihood user model (H0) -7374.779
Loglikelihood unrestricted model (H1) -7373.425
Akaike (AIC) 14777.558
Bayesian (BIC) 14843.072
Sample-size adjusted Bayesian (SABIC) 14798.615
Root Mean Square Error of Approximation:
RMSEA 0.000
90 Percent confidence interval - lower 0.000
90 Percent confidence interval - upper 0.017
P-value H_0: RMSEA <= 0.050 1.000
P-value H_0: RMSEA >= 0.080 0.000
Standardized Root Mean Square Residual:
SRMR 0.008
Parameter Estimates:
Standard errors Standard
Information Expected
Information saturated (h1) model Structured
Latent Variables:
Estimate Std.Err z-value P(>|z|) ci.lower ci.upper Std.lv
comm =~
relimport 0.796 0.050 15.875 0.000 0.698 0.894 0.796
relthink 0.735 0.062 11.784 0.000 0.613 0.857 0.735
relhurt 0.660 0.061 10.827 0.000 0.540 0.779 0.660
Ind =~
attend 0.685 0.048 14.408 0.000 0.591 0.778 0.685
prayer 1.605 0.065 24.794 0.000 1.478 1.732 1.605
relread 1.134 0.052 21.960 0.000 1.033 1.235 1.134
Std.all
0.926
0.672
0.455
0.523
0.844
0.757
Covariances:
Estimate Std.Err z-value P(>|z|) ci.lower ci.upper Std.lv
.relimport ~~
.relthink -0.007 0.069 -0.104 0.917 -0.143 0.129 -0.007
comm ~~
Ind 0.609 0.043 14.108 0.000 0.525 0.694 0.609
Std.all
-0.027
0.609
Variances:
Estimate Std.Err z-value P(>|z|) ci.lower ci.upper Std.lv
.relimport 0.106 0.071 1.489 0.137 -0.033 0.245 0.106
.relthink 0.658 0.084 7.874 0.000 0.494 0.822 0.658
.relhurt 1.668 0.097 17.268 0.000 1.479 1.857 1.668
.attend 1.242 0.068 18.253 0.000 1.109 1.376 1.242
.prayer 1.040 0.125 8.286 0.000 0.794 1.286 1.040
.relread 0.955 0.075 12.676 0.000 0.807 1.103 0.955
comm 1.000 1.000 1.000 1.000
Ind 1.000 1.000 1.000 1.000
Std.all
0.143
0.549
0.793
0.726
0.288
0.426
I figured out that std.lv=TRUE was literally telling R-Lavaan not to set the first factor to one. So problem fixed!
I want to obtain a monotonic effect for an ordinal predictor with the function lmer() from the package lme4. My reference is the estimate that can be obtained with mo() in the brms package (a more developed review of monotonic effects for ordinal predictors can be found here https://psyarxiv.com/9qkhj/).
Below a reprex with the desired estimate (leaving aside the different statistical approach behind both packages) in m1, and what happens by default (m2) when an ordered factor is used lmer()
library(brms)
library(lme4)
sleepstudy$Days <- factor(sleepstudy$Days, ordered = T)
m1 <- brm(Reaction ~ mo(Days) + (1 | Subject), data = sleepstudy, chains = 2)
#> Compiling Stan program...
summary(m1)
#> Family: gaussian
#> Links: mu = identity; sigma = identity
#> Formula: Reaction ~ mo(Days) + (1 | Subject)
#> Data: sleepstudy (Number of observations: 180)
#> Samples: 2 chains, each with iter = 2000; warmup = 1000; thin = 1;
#> total post-warmup samples = 2000
#>
#> Group-Level Effects:
#> ~Subject (Number of levels: 18)
#> Estimate Est.Error l-95% CI u-95% CI Rhat Bulk_ESS Tail_ESS
#> sd(Intercept) 39.73 7.85 27.73 58.76 1.01 538 751
#>
#> Population-Level Effects:
#> Estimate Est.Error l-95% CI u-95% CI Rhat Bulk_ESS Tail_ESS
#> Intercept 257.11 10.87 235.14 277.72 1.00 468 774
#> moDays 10.12 1.01 8.16 12.09 1.00 1603 1315
#>
#> Simplex Parameters:
#> Estimate Est.Error l-95% CI u-95% CI Rhat Bulk_ESS Tail_ESS
#> moDays1[1] 0.07 0.06 0.00 0.20 1.00 1343 747
#> moDays1[2] 0.07 0.05 0.00 0.20 1.00 1275 524
#> moDays1[3] 0.13 0.07 0.01 0.29 1.00 1337 591
#> moDays1[4] 0.10 0.07 0.00 0.28 1.00 1600 850
#> moDays1[5] 0.16 0.09 0.01 0.34 1.00 1285 658
#> moDays1[6] 0.09 0.06 0.00 0.24 1.00 1543 840
#> moDays1[7] 0.09 0.07 0.00 0.25 1.00 1534 992
#> moDays1[8] 0.16 0.08 0.02 0.32 1.00 1897 906
#> moDays1[9] 0.13 0.08 0.01 0.31 1.00 1839 936
#>
#> Family Specific Parameters:
#> Estimate Est.Error l-95% CI u-95% CI Rhat Bulk_ESS Tail_ESS
#> sigma 31.25 1.81 27.93 35.19 1.00 1726 1341
#>
#> Samples were drawn using sampling(NUTS). For each parameter, Bulk_ESS
#> and Tail_ESS are effective sample size measures, and Rhat is the potential
#> scale reduction factor on split chains (at convergence, Rhat = 1).
m2 <- lmer(Reaction ~ Days + (1 | Subject), data = sleepstudy)
summary(m2)
#> Linear mixed model fit by REML ['lmerMod']
#> Formula: Reaction ~ Days + (1 | Subject)
#> Data: sleepstudy
#>
#> REML criterion at convergence: 1731.8
#>
#> Scaled residuals:
#> Min 1Q Median 3Q Max
#> -3.3473 -0.5293 0.0317 0.5328 4.2570
#>
#> Random effects:
#> Groups Name Variance Std.Dev.
#> Subject (Intercept) 1375.5 37.09
#> Residual 987.6 31.43
#> Number of obs: 180, groups: Subject, 18
#>
#> Fixed effects:
#> Estimate Std. Error t value
#> (Intercept) 298.508 9.050 32.985
#> Days.L 95.074 7.407 12.835
#> Days.Q 7.744 7.407 1.045
#> Days.C -0.705 7.407 -0.095
#> Days^4 5.889 7.407 0.795
#> Days^5 1.754 7.407 0.237
#> Days^6 -6.036 7.407 -0.815
#> Days^7 -1.695 7.407 -0.229
#> Days^8 -4.161 7.407 -0.562
#> Days^9 6.435 7.407 0.869
#>
#> Correlation of Fixed Effects:
#> (Intr) Days.L Days.Q Days.C Days^4 Days^5 Days^6 Days^7 Days^8
#> Days.L 0.000
#> Days.Q 0.000 0.000
#> Days.C 0.000 0.000 0.000
#> Days^4 0.000 0.000 0.000 0.000
#> Days^5 0.000 0.000 0.000 0.000 0.000
#> Days^6 0.000 0.000 0.000 0.000 0.000 0.000
#> Days^7 0.000 0.000 0.000 0.000 0.000 0.000 0.000
#> Days^8 0.000 0.000 0.000 0.000 0.000 0.000 0.000 0.000
#> Days^9 0.000 0.000 0.000 0.000 0.000 0.000 0.000 0.000 0.000
How could an equivalent monotonic effect of moDays in m1 can be obtained with lme4?
It is not possible to do this with lme4::lmer(), but an alternative can be implemented with the package glmmTMB
.
I got the following answer by Ben Bolker in the R mixed-models mailing list:
"You can't constrain fixed-effect parameters in lme4::lmer (they are profiled out of the likelihood function, so they're not accessible to be constrained), but in glmmTMB you could do this"
data("sleepstudy", package= "lme4")
ss <- within(sleepstudy, {
Days <- factor(Days)
contrasts(Days) <- MASS::contr.sdif
})
library(glmmTMB)
m1 <- glmmTMB(Reaction ~ Days + (1 | Subject), data = ss)
m2 <- update(m1,
control = glmmTMBControl(
optArgs = list(lower=c(-Inf, rep(0,9), rep(-Inf, 2)))))
I need beta coefficients and residual variance for multiple stock. My question is, how can I create a loop for multiple linear regression and extract the aforementioned coefficients into the output?
Here is what my data looks like, MR is my independent variable and rest of the columns are dependent variables, to each of which I have to perform a linear regression separately.
Thank you very much!
//Edit:
> dput(head(Beta_market_model_test))
structure(list(...1 = structure(c(1422748800, 1425168000, 1427846400,
1430438400, 1433116800, 1435708800), tzone = "UTC", class = c("POSIXct",
"POSIXt")), R1 = c(-0.0225553678146582, 0.084773882172773, -0.00628335525823254,
0.189767902403849, -0.129765571642446, -0.02268699227135), R2 = c(-0.000634819869861802,
0.0566396021070485, 0.0504313735522286, -0.0275926732076482,
0.0473125483284236, -0.0501700832780339), R3 = c(-0.0607564272876455,
0.0915928283206455, -0.116429377153136, 0.0338313435925748, -0.0731748018356279,
-0.082292041771696), R4 = c(0.036716647443291, 0.0409790469126645,
-0.0594941218382615, 0.0477272727272728, 0.0115690527838033,
-0.0187634024303074), R5 = c(0.00286365940192601, 0.0128875748616479,
0.000174637626924046, 0.0238214018458469, 0.0120599342185406,
-0.0627587867116033), R6 = c(-0.0944601447872712, 0.090838356632893,
-0.0577132600192821, 0.136928528648433, -0.0137770071043408,
0.0214549609033041), MR = c(-0.0388483879770769, 0.0858362570727453,
-0.0178553084990147, 0.0567646974926548, -0.0391124787432181,
-0.014626289866472)), row.names = c(NA, -6L), class = c("tbl_df",
"tbl", "data.frame"))
We could use cbind to specify the dependent variables in lm
model <- lm(cbind(R1, R2, R3, R4, R5, R6) ~ MR, data = df1)
s1 <- summary(model)
NOTE: We assume that the 'R1' to 'R6' are numeric columns i.e. the , should be replaced with . while reading into R
Update
If there are many columns and are in the range of sequece, extract those columns and convert to matrix
dep_data <- as.matrix(Beta_market_model_test[startsWith(
names(Beta_market_model_test), "R")])
model <- lm(dep_data ~ MR, data = Beta_market_model_test)
Checking the summary
summary(model)
Response R1 :
Call:
lm(formula = R1 ~ MR, data = Beta_market_model_test)
Residuals:
1 2 3 4 5 6
0.03757 -0.06851 0.01791 0.08624 -0.06919 -0.00402
Coefficients:
Estimate Std. Error t value Pr(>|t|)
(Intercept) 0.006368 0.028060 0.227 0.8316
MR 1.711625 0.577571 2.963 0.0414 *
---
Signif. codes: 0 ‘***’ 0.001 ‘**’ 0.01 ‘*’ 0.05 ‘.’ 0.1 ‘ ’ 1
Residual standard error: 0.06831 on 4 degrees of freedom
Multiple R-squared: 0.6871, Adjusted R-squared: 0.6088
F-statistic: 8.782 on 1 and 4 DF, p-value: 0.04141
Response R2 :
Call:
lm(formula = R2 ~ MR, data = Beta_market_model_test)
Residuals:
1 2 3 4 5 6
-0.01047 0.03882 0.03925 -0.04355 0.03750 -0.06155
Coefficients:
Estimate Std. Error t value Pr(>|t|)
(Intercept) 0.01232 0.02079 0.593 0.585
MR 0.06402 0.42797 0.150 0.888
Residual standard error: 0.05062 on 4 degrees of freedom
Multiple R-squared: 0.005564, Adjusted R-squared: -0.243
F-statistic: 0.02238 on 1 and 4 DF, p-value: 0.8883
Response R3 :
Call:
lm(formula = R3 ~ MR, data = Beta_market_model_test)
Residuals:
1 2 3 4 5 6
0.035081 0.014541 -0.049701 -0.002909 0.023029 -0.020041
Coefficients:
Estimate Std. Error t value Pr(>|t|)
(Intercept) -0.04197 0.01431 -2.934 0.04266 *
MR 1.38661 0.29449 4.709 0.00925 **
---
Signif. codes: 0 ‘***’ 0.001 ‘**’ 0.01 ‘*’ 0.05 ‘.’ 0.1 ‘ ’ 1
Residual standard error: 0.03483 on 4 degrees of freedom
Multiple R-squared: 0.8472, Adjusted R-squared: 0.8089
F-statistic: 22.17 on 1 and 4 DF, p-value: 0.009249
Response R4 :
Call:
lm(formula = R4 ~ MR, data = Beta_market_model_test)
Residuals:
1 2 3 4 5 6
0.0438966 0.0002996 -0.0603723 0.0182067 0.0188503 -0.0208810
Coefficients:
Estimate Std. Error t value Pr(>|t|)
(Intercept) 0.007732 0.016804 0.46 0.669
MR 0.383843 0.345886 1.11 0.329
Residual standard error: 0.04091 on 4 degrees of freedom
Multiple R-squared: 0.2354, Adjusted R-squared: 0.04425
F-statistic: 1.232 on 1 and 4 DF, p-value: 0.3293
Response R5 :
Call:
lm(formula = R5 ~ MR, data = Beta_market_model_test)
Residuals:
1 2 3 4 5 6
0.013692 -0.001676 0.006728 0.015178 0.022942 -0.056863
Coefficients:
Estimate Std. Error t value Pr(>|t|)
(Intercept) -0.002917 0.013351 -0.218 0.838
MR 0.203653 0.274801 0.741 0.500
Residual standard error: 0.0325 on 4 degrees of freedom
Multiple R-squared: 0.1207, Adjusted R-squared: -0.09909
F-statistic: 0.5492 on 1 and 4 DF, p-value: 0.4998
Response R6 :
Call:
lm(formula = R6 ~ MR, data = Beta_market_model_test)
Residuals:
1 2 3 4 5 6
-0.04498 -0.03837 -0.03832 0.04938 0.03608 0.03622
Coefficients:
Estimate Std. Error t value Pr(>|t|)
(Intercept) 0.006197 0.020555 0.302 0.7781
MR 1.433135 0.423083 3.387 0.0276 *
---
Signif. codes: 0 ‘***’ 0.001 ‘**’ 0.01 ‘*’ 0.05 ‘.’ 0.1 ‘ ’ 1
Residual standard error: 0.05004 on 4 degrees of freedom
Multiple R-squared: 0.7415, Adjusted R-squared: 0.6769
F-statistic: 11.47 on 1 and 4 DF, p-value: 0.0276
We could get the summary output in a data.frame easily in a tabular format with tidy from broom
library(purrr)
library(broom)
map_dfr(summary(model), tidy, .id = 'dep_var')
# A tibble: 12 x 6
# dep_var term estimate std.error statistic p.value
# <chr> <chr> <dbl> <dbl> <dbl> <dbl>
# 1 Response R1 (Intercept) 0.00637 0.0281 0.227 0.832
# 2 Response R1 MR 1.71 0.578 2.96 0.0414
# 3 Response R2 (Intercept) 0.0123 0.0208 0.593 0.585
# 4 Response R2 MR 0.0640 0.428 0.150 0.888
# 5 Response R3 (Intercept) -0.0420 0.0143 -2.93 0.0427
# 6 Response R3 MR 1.39 0.294 4.71 0.00925
# 7 Response R4 (Intercept) 0.00773 0.0168 0.460 0.669
# 8 Response R4 MR 0.384 0.346 1.11 0.329
# 9 Response R5 (Intercept) -0.00292 0.0134 -0.218 0.838
#10 Response R5 MR 0.204 0.275 0.741 0.500
#11 Response R6 (Intercept) 0.00620 0.0206 0.302 0.778
#12 Response R6 MR 1.43 0.423 3.39 0.0276
Or to get other output with glance
map_dfr(summary(model), glance, .id = 'dep_var')
I'm just posting this to ask a question about my code:
library(dplyr)
library(tidyr)
library(broom)
df %>%
select(-...1) %>%
pivot_longer(R1:R6) %>%
group_by(name) %>%
nest(data = c(MR, value)) %>%
mutate(model = map(data, ~ lm(MR ~ value, data = .)),
glance = map(model, ~ glance(.x))) %>%
unnest(glance) %>%
select(- c(data, model))
# A tibble: 6 x 13
# Groups: name [6]
name r.squared adj.r.squared sigma statistic p.value df logLik AIC BIC deviance
<chr> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl>
1 R1 0.687 0.609 0.0331 8.78 0.0414 1 13.2 -20.3 -20.9 0.00438
2 R2 0.00556 -0.243 0.0590 0.0224 0.888 1 9.69 -13.4 -14.0 0.0139
3 R3 0.847 0.809 0.0231 22.2 0.00925 1 15.3 -24.6 -25.2 0.00214
4 R4 0.235 0.0443 0.0517 1.23 0.329 1 10.5 -15.0 -15.6 0.0107
5 R5 0.121 -0.0991 0.0555 0.549 0.500 1 10.1 -14.1 -14.7 0.0123
6 R6 0.742 0.677 0.0301 11.5 0.0276 1 13.7 -21.5 -22.1 0.00362
# ... with 2 more variables: df.residual <int>, nobs <int>
Update
Thanks to my dear friend #akrun who always provides me with valuable suggestions.
In case you would like to avoid pivoting the data as with a really big data the pivoting could increase the rows to a degree that it would exceed the limitations, you can use the following code as well:
library(dplyr)
library(tidyr)
library(broom)
df %>%
select(-1) %>%
summarise(across(-MR, ~ list(lm(reformulate('MR', response = cur_column()),
data = df) %>%
summary))) %>%
unclass %>%
map_dfr(~ tidy(.x[[1]]))
# A tibble: 12 x 5
term estimate std.error statistic p.value
<chr> <dbl> <dbl> <dbl> <dbl>
1 (Intercept) 0.00637 0.0281 0.227 0.832
2 MR 1.71 0.578 2.96 0.0414
3 (Intercept) 0.0123 0.0208 0.593 0.585
4 MR 0.0640 0.428 0.150 0.888
5 (Intercept) -0.0420 0.0143 -2.93 0.0427
6 MR 1.39 0.294 4.71 0.00925
7 (Intercept) 0.00773 0.0168 0.460 0.669
8 MR 0.384 0.346 1.11 0.329
9 (Intercept) -0.00292 0.0134 -0.218 0.838
10 MR 0.204 0.275 0.741 0.500
11 (Intercept) 0.00620 0.0206 0.302 0.778
12 MR 1.43 0.423 3.39 0.0276
I have cross-sectional data and I am trying to specify a model with multiple mediations.
My independent variable (IV) is measured by a tool with 24 items, which make up 5 subscales (latent variables), which in turn load onto a total "higher-order" factor. I try to estimate this model in two different ways: (1) A five-factor model (without a higher order factor) in which all 5 subscales are allowed to correlate and (2) a higher-order model with a TOTAL latent variable made up of those 5 suscales.The first model has five correlated latents (FNR, FOB...FAA) factors, with variance fixed to 1.
My first model has an IV (FTOTAL), 4 mediators (ER 1, 2, 3 and 4), and one DV (PH). The relationship between ER 4 and the IV is mediated by one of the other mediators (ER3), making it a mediated mediation. I was able to specify the SEM for the first model with the higher order TOTAL factor without a problem, and the code is shown below:
"#Measurements model
FNR =~ FNR1 + FNR2 + FNR3 +FNR4 +FNR5
FOB =~ FOB1 + FOB2 +FOB3 +FOB4
FDS =~ FDS1 +FDS2 +FDS3 + FDS4 + FDS5
FNJ =~ FNJ1 + FNJ2 + FNJ3 +FNJ4 + FNJ5
FAA =~ FAA1 + FAA2 +FAA3 + FAA4 +FAA5
FTOTAL =~ FNR + FOB + FDS + FNJ+ FAA
#Regressions
ER3~ a*FTOTAL
ER4~ b*RSTOTAL +FTOTAL
ER1 ~ u1*FTOTAL
ER2 ~ u2*FTOTAL
PHQTOTAL ~ z1*ER1 + z2*ER2 + d*FTOTAL + c*ER4 + ER3
indirect1 := u1 * z1
indirect2 := u2 * z2
indirect3 := a*b*c
total := d + (u1 * z1) + (u2 * z2) + a*b*c
#Residual correlations
CRTOTAL~~SUPTOTAL+SLEEPTOTAL+RSTOTAL
SUPTOTAL~~SLEEPTOTAL+RSTOTAL
"
fitPHtotal <- sem(model = multipleMediationPH, data = SEMDATA, std.lv=TRUE)
summary(fitPHtotal)
However, I cannot figure out how to specify the model with all the 5 subscales as independent variables in the same model. I tried following the same logic and but including the 5 IVs in the model and it did not work out. Could anyone suggest a solution? or is the only way to just run 5 different models with one subscale as an IV at a time?
Thank you in advance for the help.
Since you did not provide data. I show you how to do it using the HolzingerSwineford1939 which comes with the lavaan package.
First, a mediation using a second-order latent factor (3 first-order factors):
library(lavaan)
#> This is lavaan 0.6-8
#> lavaan is FREE software! Please report any bugs.
model_2L <- "
visual =~ x1 + x2 + x3
textual =~ x4 + x5 + x6
speed =~ x7 + x8 + x9
higher =~ visual + textual + speed
#grade will be your Y
#higher order latent factor will be your X
#agemo will be your M
grade ~ c*higher + b*agemo
agemo ~ a*higher
# indirect effect (a*b)
ab := a*b
# total effect
total := c + (a*b)
"
fit_2L <- sem(model = model_2L, data = HolzingerSwineford1939)
summary(object = fit_2L, std=T)
#> lavaan 0.6-8 ended normally after 48 iterations
#>
#> Estimator ML
#> Optimization method NLMINB
#> Number of model parameters 26
#>
#> Used Total
#> Number of observations 300 301
#>
#> Model Test User Model:
#>
#> Test statistic 116.110
#> Degrees of freedom 40
#> 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|) Std.lv Std.all
#> visual =~
#> x1 1.000 0.849 0.727
#> x2 0.621 0.109 5.680 0.000 0.527 0.448
#> x3 0.824 0.124 6.641 0.000 0.699 0.619
#> textual =~
#> x4 1.000 0.990 0.851
#> x5 1.117 0.066 16.998 0.000 1.106 0.859
#> x6 0.922 0.056 16.563 0.000 0.913 0.834
#> speed =~
#> x7 1.000 0.648 0.595
#> x8 1.130 0.148 7.612 0.000 0.732 0.726
#> x9 1.010 0.135 7.465 0.000 0.655 0.649
#> higher =~
#> visual 1.000 0.673 0.673
#> textual 0.849 0.185 4.586 0.000 0.490 0.490
#> speed 0.810 0.179 4.519 0.000 0.714 0.714
#>
#> Regressions:
#> Estimate Std.Err z-value P(>|z|) Std.lv Std.all
#> grade ~
#> higher (c) 0.421 0.089 4.730 0.000 0.241 0.482
#> agemo (b) -0.004 0.008 -0.519 0.604 -0.004 -0.029
#> agemo ~
#> higher (a) 0.322 0.469 0.687 0.492 0.184 0.053
#>
#> Variances:
#> Estimate Std.Err z-value P(>|z|) Std.lv Std.all
#> .x1 0.641 0.110 5.822 0.000 0.641 0.471
#> .x2 1.108 0.102 10.848 0.000 1.108 0.799
#> .x3 0.786 0.094 8.398 0.000 0.786 0.616
#> .x4 0.373 0.048 7.750 0.000 0.373 0.276
#> .x5 0.436 0.058 7.453 0.000 0.436 0.263
#> .x6 0.364 0.044 8.369 0.000 0.364 0.304
#> .x7 0.767 0.080 9.629 0.000 0.767 0.646
#> .x8 0.482 0.070 6.924 0.000 0.482 0.474
#> .x9 0.589 0.068 8.686 0.000 0.589 0.579
#> .grade 0.192 0.020 9.767 0.000 0.192 0.768
#> .agemo 11.881 0.972 12.220 0.000 11.881 0.997
#> .visual 0.394 0.111 3.535 0.000 0.547 0.547
#> .textual 0.745 0.101 7.397 0.000 0.760 0.760
#> .speed 0.206 0.062 3.312 0.001 0.490 0.490
#> higher 0.327 0.097 3.375 0.001 1.000 1.000
#>
#> Defined Parameters:
#> Estimate Std.Err z-value P(>|z|) Std.lv Std.all
#> ab -0.001 0.004 -0.366 0.715 -0.001 -0.002
#> total 0.420 0.089 4.728 0.000 0.240 0.481
Second, a mediation using a three first-order factors. Three indirect effects and three total effects are estimated:
library(lavaan)
model_1L <- "
visual =~ x1 + x2 + x3
textual =~ x4 + x5 + x6
speed =~ x7 + x8 + x9
#grade will be your Y
#higher order latent factor will be your X
#agemo will be your M
grade ~ c1*visual + c2*textual + c3*speed + b*agemo
agemo ~ a1*visual + a2*textual + a3*speed
# indirect effect (a*b)
a1b := a1*b
a2b := a2*b
a3b := a3*b
# total effect
total1 := c1 + (a1*b)
total2 := c2 + (a2*b)
total3 := c3 + (a3*b)
"
fit_1L <- sem(model = model_1L, data = HolzingerSwineford1939)
summary(object = fit_1L, std=T)
#> lavaan 0.6-8 ended normally after 55 iterations
#>
#> Estimator ML
#> Optimization method NLMINB
#> Number of model parameters 30
#>
#> Used Total
#> Number of observations 300 301
#>
#> Model Test User Model:
#>
#> Test statistic 101.925
#> Degrees of freedom 36
#> 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|) Std.lv Std.all
#> visual =~
#> x1 1.000 0.904 0.775
#> x2 0.555 0.100 5.564 0.000 0.501 0.426
#> x3 0.724 0.109 6.657 0.000 0.655 0.580
#> textual =~
#> x4 1.000 0.993 0.853
#> x5 1.108 0.065 17.017 0.000 1.101 0.855
#> x6 0.921 0.055 16.667 0.000 0.915 0.836
#> speed =~
#> x7 1.000 0.668 0.613
#> x8 1.115 0.142 7.840 0.000 0.744 0.737
#> x9 0.945 0.125 7.540 0.000 0.631 0.625
#>
#> Regressions:
#> Estimate Std.Err z-value P(>|z|) Std.lv Std.all
#> grade ~
#> visual (c1) 0.012 0.048 0.246 0.806 0.011 0.021
#> textual (c2) 0.048 0.035 1.376 0.169 0.047 0.095
#> speed (c3) 0.295 0.063 4.689 0.000 0.197 0.394
#> agemo (b) -0.003 0.008 -0.361 0.718 -0.003 -0.020
#> agemo ~
#> visual (a1) 0.354 0.355 0.996 0.319 0.320 0.093
#> textual (a2) -0.233 0.256 -0.912 0.362 -0.231 -0.067
#> speed (a3) 0.098 0.421 0.232 0.817 0.065 0.019
#>
#> Covariances:
#> Estimate Std.Err z-value P(>|z|) Std.lv Std.all
#> visual ~~
#> textual 0.412 0.074 5.565 0.000 0.459 0.459
#> speed 0.265 0.058 4.554 0.000 0.438 0.438
#> textual ~~
#> speed 0.180 0.052 3.448 0.001 0.271 0.271
#>
#> Variances:
#> Estimate Std.Err z-value P(>|z|) Std.lv Std.all
#> .x1 0.545 0.115 4.747 0.000 0.545 0.400
#> .x2 1.135 0.102 11.115 0.000 1.135 0.819
#> .x3 0.846 0.091 9.322 0.000 0.846 0.664
#> .x4 0.368 0.048 7.698 0.000 0.368 0.272
#> .x5 0.447 0.058 7.657 0.000 0.447 0.270
#> .x6 0.361 0.043 8.343 0.000 0.361 0.301
#> .x7 0.741 0.079 9.422 0.000 0.741 0.624
#> .x8 0.465 0.069 6.724 0.000 0.465 0.456
#> .x9 0.620 0.067 9.217 0.000 0.620 0.609
#> .grade 0.201 0.018 11.307 0.000 0.201 0.806
#> .agemo 11.813 0.969 12.191 0.000 11.813 0.991
#> visual 0.817 0.147 5.564 0.000 1.000 1.000
#> textual 0.986 0.113 8.752 0.000 1.000 1.000
#> speed 0.446 0.091 4.906 0.000 1.000 1.000
#>
#> Defined Parameters:
#> Estimate Std.Err z-value P(>|z|) Std.lv Std.all
#> a1b -0.001 0.003 -0.344 0.731 -0.001 -0.002
#> a2b 0.001 0.002 0.335 0.738 0.001 0.001
#> a3b -0.000 0.002 -0.183 0.855 -0.000 -0.000
#> total1 0.011 0.048 0.226 0.821 0.010 0.020
#> total2 0.048 0.035 1.399 0.162 0.048 0.096
#> total3 0.295 0.063 4.685 0.000 0.197 0.394
Created on 2021-03-30 by the reprex package (v1.0.0)
I'm running a non-recursive model with Lavaan. However, 2 things happened that I didn't quite understand. First, gooodness-of-fit indices and some standard errors were "NA". Second, the two coefficients between two variables of different directions were not consistent (non-recursive part: ResidentialMobility--Author): one was positive, and another one was negative (at least they should be in the same direction; otherwise, how to explain?). Can someone help me out? Please let me know if you want me to clarify it more. Thanks!
model01<-'ResidentialMobility~a*Coun
SavingMotherPercentage~e*Affect
SavingMotherPercentage~f*Author
SavingMotherPercentage~g*Recipro
Affect~b*ResidentialMobility
Author~c*ResidentialMobility
Recipro~d*ResidentialMobility
ResidentialMobility~h*Affect
ResidentialMobility~i*Author
ResidentialMobility~j*Recipro
Affect~~Author+Recipro+ResidentialMobility
Author~~Recipro+ResidentialMobility
Recipro~~ResidentialMobility
Coun~SavingMotherPercentage
ab:=a*b
ac:=a*c
ad:=a*d
be:=b*e
cf:=c*f
dg:=d*g
'
fit <- cfa(model01, estimator = "MLR", data = data01, missing = "FIML")
summary(fit, standardized = TRUE, fit.measures = TRUE)
Output:
lavaan (0.5-21) converged normally after 93 iterations
Used Total
Number of observations 502 506
Number of missing patterns 4
Estimator ML Robust
Minimum Function Test Statistic NA NA
Degrees of freedom -2 -2
Minimum Function Value 0.0005232772506
Scaling correction factor
for the Yuan-Bentler correction
User model versus baseline model:
Comparative Fit Index (CFI) NA NA
Tucker-Lewis Index (TLI) NA NA
Loglikelihood and Information Criteria:
Loglikelihood user model (H0) -5057.346 -5057.346
Loglikelihood unrestricted model (H1) -5057.084 -5057.084
Number of free parameters 29 29
Akaike (AIC) 10172.693 10172.693
Bayesian (BIC) 10295.032 10295.032
Sample-size adjusted Bayesian (BIC) 10202.984 10202.984
Root Mean Square Error of Approximation:
RMSEA NA NA
90 Percent Confidence Interval NA NA NA NA
P-value RMSEA <= 0.05 NA NA
Standardized Root Mean Square Residual:
SRMR 0.006 0.006
Parameter Estimates:
Information Observed
Standard Errors Robust.huber.white
Regressions:
Estimate Std.Err z-value P(>|z|) Std.lv Std.all
ResidentialMobility ~
Coun (a) -1.543 0.255 -6.052 0.000 -1.543 -0.540
SavingMotherPercentage ~
Affect (e) 3.093 1.684 1.837 0.066 3.093 0.122
Author (f) 2.618 0.923 2.835 0.005 2.618 0.145
Recipro (g) 0.061 1.344 0.046 0.964 0.061 0.003
Affect ~
RsdntlMblt (b) -0.311 0.075 -4.125 0.000 -0.311 -0.570
Author ~
RsdntlMblt (c) -0.901 0.119 -7.567 0.000 -0.901 -1.180
Recipro ~
RsdntlMblt (d) -0.313 0.082 -3.841 0.000 -0.313 -0.512
ResidentialMobility ~
Affect (h) -0.209 0.193 -1.082 0.279 -0.209 -0.114
Author (i) 0.475 0.192 2.474 0.013 0.475 0.363
Recipro (j) 0.178 0.346 0.514 0.607 0.178 0.109
Coun ~
SvngMthrPr 0.003 0.001 2.225 0.026 0.003 0.108
Covariances:
Estimate Std.Err z-value P(>|z|) Std.lv Std.all
.Affect ~~
.Author 0.667 0.171 3.893 0.000 0.667 0.534
.Recipro 0.669 0.119 5.623 0.000 0.669 0.773
.ResidentialMobility ~~
.Affect 0.624 0.144 4.347 0.000 0.624 0.474
.Author ~~
.Recipro 0.565 0.173 3.267 0.001 0.565 0.416
.ResidentialMobility ~~
.Author 1.029 0.288 3.572 0.000 1.029 0.499
.Recipro 0.564 0.304 1.851 0.064 0.564 0.395
Intercepts:
Estimate Std.Err z-value P(>|z|) Std.lv Std.all
.ResidentlMblty 1.813 NA 1.813 1.270
.SvngMthrPrcntg 29.591 7.347 4.027 0.000 29.591 1.499
.Affect 5.701 0.169 33.797 0.000 5.701 7.320
.Author 5.569 0.275 20.259 0.000 5.569 5.109
.Recipro 5.149 0.186 27.642 0.000 5.149 5.889
.Coun 0.367 0.069 5.336 0.000 0.367 0.735
Variances:
Estimate Std.Err z-value P(>|z|) Std.lv Std.all
.ResidentlMblty 2.169 0.259 8.378 0.000 2.169 1.064
.SvngMthrPrcntg 363.792 23.428 15.528 0.000 363.792 0.934
.Affect 0.797 0.129 6.153 0.000 0.797 1.314
.Author 1.957 0.343 5.713 0.000 1.957 1.647
.Recipro 0.941 0.126 7.439 0.000 0.941 1.231
.Coun 0.242 0.004 54.431 0.000 0.242 0.969
Defined Parameters:
Estimate Std.Err z-value P(>|z|) Std.lv Std.all
ab 0.480 0.120 3.991 0.000 0.480 0.308
ac 1.390 0.261 5.328 0.000 1.390 0.637
ad 0.483 0.133 3.640 0.000 0.483 0.276
be -0.962 0.548 -1.757 0.079 -0.962 -0.070
cf -2.359 0.851 -2.771 0.006 -2.359 -0.171
dg -0.019 0.421 -0.046 0.964 -0.019 -0.001
Why you get NA I think is because you have specified a model with -2 in degrees of freedom. You should specify the model differently so that you get a positive number of degrees of freedom.