Example:
library(lavaan)
library(semPlot)
model <- '
# latent variables
ind60 =~ x1 + x2 + x3
dem60 =~ y1 + y2 + y3 + y4
dem65 =~ y5 + y6 + y7 + y8
# regressions
dem60 ~ ind60
dem65 ~ ind60 + dem60
# residual covariances
y1 ~~ y5
y2 ~~ y4 + y6
y3 ~~ y7
y4 ~~ y8
y6 ~~ y8
'
fit <- sem(model, data = PoliticalDemocracy)
summary(fit, fit.measures = TRUE)
## lavaan (0.5-22) converged normally after 68 iterations
##
## Number of observations 75
##
## Estimator ML
## Minimum Function Test Statistic 38.125
## Degrees of freedom 35
## P-value (Chi-square) 0.329
##
## Model test baseline model:
##
## Minimum Function Test Statistic 730.654
## Degrees of freedom 55
## P-value 0.000
##
## User model versus baseline model:
##
## Comparative Fit Index (CFI) 0.995
## Tucker-Lewis Index (TLI) 0.993
##
## Loglikelihood and Information Criteria:
##
## Loglikelihood user model (H0) -1547.791
## Loglikelihood unrestricted model (H1) -1528.728
##
## Number of free parameters 31
## Akaike (AIC) 3157.582
## Bayesian (BIC) 3229.424
## Sample-size adjusted Bayesian (BIC) 3131.720
##
## Root Mean Square Error of Approximation:
##
## RMSEA 0.035
## 90 Percent Confidence Interval 0.000 0.092
## P-value RMSEA <= 0.05 0.611
##
## Standardized Root Mean Square Residual:
##
## SRMR 0.044
##
## Parameter Estimates:
##
## Information Expected
## Standard Errors Standard
##
## Latent Variables:
## Estimate Std.Err z-value P(>|z|)
## ind60 =~
## x1 1.000
## x2 2.180 0.139 15.742 0.000
## x3 1.819 0.152 11.967 0.000
## dem60 =~
## y1 1.000
## y2 1.257 0.182 6.889 0.000
## y3 1.058 0.151 6.987 0.000
## y4 1.265 0.145 8.722 0.000
## dem65 =~
## y5 1.000
## y6 1.186 0.169 7.024 0.000
## y7 1.280 0.160 8.002 0.000
## y8 1.266 0.158 8.007 0.000
##
## Regressions:
## Estimate Std.Err z-value P(>|z|)
## dem60 ~
## ind60 1.483 0.399 3.715 0.000
## dem65 ~
## ind60 0.572 0.221 2.586 0.010
## dem60 0.837 0.098 8.514 0.000
##
## Covariances:
## Estimate Std.Err z-value P(>|z|)
## .y1 ~~
## .y5 0.624 0.358 1.741 0.082
## .y2 ~~
## .y4 1.313 0.702 1.871 0.061
## .y6 2.153 0.734 2.934 0.003
## .y3 ~~
## .y7 0.795 0.608 1.308 0.191
## .y4 ~~
## .y8 0.348 0.442 0.787 0.431
## .y6 ~~
## .y8 1.356 0.568 2.386 0.017
##
## Variances:
## Estimate Std.Err z-value P(>|z|)
## .x1 0.082 0.019 4.184 0.000
## .x2 0.120 0.070 1.718 0.086
## .x3 0.467 0.090 5.177 0.000
## .y1 1.891 0.444 4.256 0.000
## .y2 7.373 1.374 5.366 0.000
## .y3 5.067 0.952 5.324 0.000
## .y4 3.148 0.739 4.261 0.000
## .y5 2.351 0.480 4.895 0.000
## .y6 4.954 0.914 5.419 0.000
## .y7 3.431 0.713 4.814 0.000
## .y8 3.254 0.695 4.685 0.000
## ind60 0.448 0.087 5.173 0.000
## .dem60 3.956 0.921 4.295 0.000
## .dem65 0.172 0.215 0.803 0.422
semPaths(fit, "std", edge.label.cex = 0.5, curvePivot = TRUE, layout = "tree")
Reading Non-renters data:
library(foreign)
nonrental <- read.spss(file = "Horn_NR.sav", to.data.frame = TRUE)
nonrental_data <- apply(nonrental, 2, factor, levels = c("Stämmer inte alls", "Stämmer mycket dåligt",
"Stämmer dåligt", "Stämmer varken bra eller dåligt",
"Stämmer bra", "Stämmer mycket bra",
"Stämmer helt och hållet"), ordered = TRUE)
nonrental_data <- as.data.frame(nonrental_data)
Non-renters model:
model <- '
# latent variables
NOISE =~ H3 + H6 + LH9
RELAX =~ LH4 + H4 + LH1
APART =~ L2 + L3 + L5
FUNC =~ L1 + L4 + L7
OVERAL =~ P1 + P2 + P3
VMF =~ P22 + P23 + P24
'
fit <- sem(model, data = nonrental_data)
## Warning in lav_data_full(data = data, group = group, group.label =
## group.label, : lavaan WARNING: unordered factor(s) with more than 2 levels
## detected in data: H3 H6 LH9 LH4 H4 LH1 L2 L3 L5 L1 L4 L7 P1 P2 P3 P22 P23
## P24
## Warning in lav_object_post_check(lavobject): lavaan WARNING: some estimated
## ov variances are negative
## Warning in lav_object_post_check(lavobject): lavaan WARNING: covariance matrix of latent variables
## is not positive definite;
## use inspect(fit,"cov.lv") to investigate.
summary(fit, fit.measures = TRUE)
## lavaan (0.5-22) converged normally after 226 iterations
##
## Used Total
## Number of observations 102 112
##
## Estimator ML
## Minimum Function Test Statistic 140.365
## Degrees of freedom 120
## P-value (Chi-square) 0.099
##
## Model test baseline model:
##
## Minimum Function Test Statistic 306.755
## Degrees of freedom 153
## P-value 0.000
##
## User model versus baseline model:
##
## Comparative Fit Index (CFI) 0.868
## Tucker-Lewis Index (TLI) 0.831
##
## Loglikelihood and Information Criteria:
##
## Loglikelihood user model (H0) NA
## Loglikelihood unrestricted model (H1) NA
##
## Number of free parameters 51
## Akaike (AIC) NA
## Bayesian (BIC) NA
##
## Root Mean Square Error of Approximation:
##
## RMSEA 0.041
## 90 Percent Confidence Interval 0.000 0.066
## P-value RMSEA <= 0.05 0.695
##
## Standardized Root Mean Square Residual:
##
## SRMR 0.083
##
## Parameter Estimates:
##
## Information Expected
## Standard Errors Standard
##
## Latent Variables:
## Estimate Std.Err z-value P(>|z|)
## NOISE =~
## H3 1.000
## H6 17.065 51.486 0.331 0.740
## LH9 12.044 36.063 0.334 0.738
## RELAX =~
## LH4 1.000
## H4 0.187 0.273 0.684 0.494
## LH1 0.041 0.082 0.501 0.616
## APART =~
## L2 1.000
## L3 0.691 0.383 1.806 0.071
## L5 1.466 0.537 2.732 0.006
## FUNC =~
## L1 1.000
## L4 0.767 0.220 3.485 0.000
## L7 0.501 0.196 2.556 0.011
## OVERAL =~
## P1 1.000
## P2 1.350 0.365 3.698 0.000
## P3 0.594 0.195 3.049 0.002
## VMF =~
## P22 1.000
## P23 1.519 0.591 2.571 0.010
## P24 1.060 0.398 2.662 0.008
##
## Covariances:
## Estimate Std.Err z-value P(>|z|)
## NOISE ~~
## RELAX 0.006 0.036 0.158 0.874
## APART -0.004 0.023 -0.185 0.854
## FUNC 0.054 0.164 0.329 0.742
## OVERAL 0.009 0.035 0.247 0.805
## VMF 0.007 0.028 0.259 0.796
## RELAX ~~
## APART 0.597 0.404 1.477 0.140
## FUNC 1.200 0.538 2.230 0.026
## OVERAL -0.057 0.443 -0.130 0.897
## VMF 0.883 0.429 2.058 0.040
## APART ~~
## FUNC 1.569 0.562 2.793 0.005
## OVERAL 0.682 0.362 1.885 0.059
## VMF 0.283 0.233 1.215 0.224
## FUNC ~~
## OVERAL 1.259 0.494 2.548 0.011
## VMF 0.417 0.311 1.344 0.179
## OVERAL ~~
## VMF 0.055 0.245 0.225 0.822
##
## Variances:
## Estimate Std.Err z-value P(>|z|)
## .H3 5.028 0.705 7.132 0.000
## .H6 2.807 1.777 1.580 0.114
## .LH9 4.780 1.093 4.374 0.000
## .LH4 -5.228 15.842 -0.330 0.741
## .H4 6.146 1.021 6.020 0.000
## .LH1 6.857 0.959 7.148 0.000
## .L2 5.961 0.893 6.675 0.000
## .L3 6.592 0.937 7.039 0.000
## .L5 5.745 1.051 5.465 0.000
## .L1 4.413 0.801 5.508 0.000
## .L4 5.184 0.789 6.573 0.000
## .L7 5.074 0.726 6.988 0.000
## .P1 4.090 0.843 4.853 0.000
## .P2 2.001 1.139 1.757 0.079
## .P3 5.576 0.828 6.731 0.000
## .P22 4.728 0.789 5.995 0.000
## .P23 2.784 0.953 2.921 0.003
## .P24 3.743 0.692 5.411 0.000
## NOISE 0.010 0.057 0.168 0.867
## RELAX 11.406 15.831 0.720 0.471
## APART 0.665 0.515 1.292 0.196
## FUNC 1.771 0.793 2.235 0.025
## OVERAL 2.505 0.952 2.630 0.009
## VMF 1.084 0.638 1.697 0.090
semPaths(fit, "std", edge.label.cex = 0.5, curvePivot = TRUE, layout = "tree")