├── CFA_HolzingerSwineford1939
├── HolzingerSwineford1939.csv
├── HolzingerSwineford1939.dat
├── Jasp.R
├── Mplus.inp
├── Mplus_simplified.inp
├── OpenMx.R
├── lavaan.R
├── lavaan_simplified.R
├── mplus.R
├── mplus.dgm
├── mplus.out
├── mplus_simplified.dgm
├── mplus_simplified.out
├── psychonetrics.R
└── umx.R
├── CFA_fit_examples
├── Jasp
│ ├── StarWars.csv
│ └── StarWars.jasp
├── Mplus
│ ├── StarWars_Mplus.csv
│ ├── StarWars_mPlus.inp
│ ├── StarWars_mPlus.out
│ └── semPlot.R
├── Onyx
│ ├── StarWars.csv
│ └── StarWars_Onyx.xml
├── OpenMx
│ ├── StarWars.csv
│ └── StarWars_OpenMx.R
├── StarWars_questionaire.pdf
├── lavaan
│ ├── StarWars.csv
│ ├── StarWars.pdf
│ ├── StarWars_lavaan.R
│ └── StarWars_semPlot.R
└── psychonetrics
│ ├── StarWars.csv
│ └── StarWars_psychonetrics.R
├── Latent_growth_examples
├── Jasp
│ ├── LGC_Jasp.jasp
│ ├── covmat_bivariate.csv
│ ├── means_bivariate.csv
│ ├── readme.txt
│ ├── simulate_data.R
│ └── simulatedData.csv
├── Onyx
│ ├── LGC_Onyx_bivariate.xml
│ ├── LGC_Onyx_unvariate.xml
│ ├── covmat_bivariate.csv
│ ├── means_bivariate.csv
│ ├── readme.txt
│ ├── simulate_data.R
│ └── simulatedData.csv
├── lavaan
│ ├── LGC_lavaan_bivariate.R
│ ├── LGC_lavaan_univariate.R
│ ├── covmat_bivariate.csv
│ ├── covmat_univariate.csv
│ ├── means_bivariate.csv
│ └── means_univariate.csv
└── psychonetrics
│ ├── LGC_psychonetrics_bivariate.R
│ ├── LGC_psychonetrics_univariate.R
│ ├── LGC_psychonetrics_univariate.html
│ ├── covmat_bivariate.csv
│ ├── covmat_univariate.csv
│ ├── means_bivariate.csv
│ └── means_univariate.csv
├── Measurement_invariance_examples
├── Jasp
│ ├── StarWars.csv
│ ├── StarWars_Jasp.jasp
│ └── readme.txt
├── StarWars_questionaire.pdf
├── lavaan
│ ├── StarWars.csv
│ ├── StarWars_lavaan.R
│ └── StarWars_lavaan.html
└── psychonetrics
│ ├── StarWars.csv
│ ├── StarWars_psychonetrics.R
│ └── StarWars_psychonetrics.html
├── README.md
├── SEM_fit_examples
├── lavaan_rawdata.R
├── lavaan_sumstat.R
├── psychonetrics_rawdata.R
├── psychonetrics_sumstat.R
├── semplot_lavaan.R
└── semplot_psychonetrics.R
├── psychonetrics
└── SHARE panel example
│ ├── SHARE_summary_statistics.RData
│ ├── SHAREresults.pdf
│ ├── dataPreparationCodes.R
│ └── shareAnalysis.R
├── qgraph_path_diagrams
├── 1factor_1indicator
│ ├── 1fac1ind.pdf
│ └── 1factor_1indicator.R
├── 1factor_2indicators
│ ├── 1fac2ind.pdf
│ └── 1factor_2indicators.R
├── 1factor_3indicators
│ ├── 1fac1ind.pdf
│ └── 1factor_3indicators.R
├── 2factor_6indicator
│ ├── 2fac6ind.pdf
│ └── 2factor_6indicators.R
├── 2factor_6indicator_allcrossloadings
│ ├── 2fac6ind_allcross.pdf
│ └── 2factor_6indicators_allcrossloadings.R
├── 2factor_6indicator_crossloading
│ ├── 2fac6indcross.pdf
│ └── 2factor_6indicators_crossloading.R
├── 2factor_6indicator_residual
│ ├── 2fac6ind_resid.pdf
│ └── 2factor_6indicators_residual.R
├── Path models
│ ├── ThreeNodePathDiagram.R
│ └── ThreeNodePathDiagram.pdf
├── baseline
│ ├── baseline.R
│ └── baseline.pdf
├── efa_loadings
│ ├── loadings.pdf
│ └── loadingsPlot.R
├── latentgrowth
│ ├── latentgrowth.R
│ └── latentgrowth.pdf
├── meanstructure1
│ ├── meanstructure1.R
│ └── meanstructure1.pdf
└── meanstructure2
│ ├── meanstructure2.R
│ └── meanstructure2.pdf
└── semPlot
├── semPlotExample1.pdf
└── semPlot_modelMatrices.R
/CFA_HolzingerSwineford1939/Jasp.R:
--------------------------------------------------------------------------------
1 | library("lavaan")
2 |
3 | # Load data:
4 | data("HolzingerSwineford1939")
5 | Data <- HolzingerSwineford1939[,c("x1","x2","x3","x4","x5","x6","x7","x8","x9")]
6 |
7 | # Write for Jasp:
8 | write.csv(Data, file = "HolzingerSwineford1939.csv", row.names = FALSE)
--------------------------------------------------------------------------------
/CFA_HolzingerSwineford1939/Mplus.inp:
--------------------------------------------------------------------------------
1 | TITLE: Your title goes here
2 | DATA: FILE = "E:/Github/SEM-code-examples/HolzingerSwineford1939.dat";
3 | VARIABLE:
4 | NAMES = x1 x2 x3 x4 x5 x6 x7 x8 x9;
5 | MISSING=.;
6 | MODEL:
7 | ! Factor loadings:
8 | visual BY x1@1 x2 x3;
9 | textual BY x4@1 x5 x6;
10 | speed BY x7@1 x8 x9;
11 |
12 | ! Factor variances:
13 | visual WITH visual textual speed;
14 | textual WITH visual textual speed;
15 | speed WITH visual textual speed;
16 |
17 | ! Residual variances:
18 | x1 WITH x1;
19 | x2 WITH x2;
20 | x3 WITH x3;
21 | x4 WITH x4;
22 | x5 WITH x5;
23 | x6 WITH x6;
24 | x7 WITH x7;
25 | x8 WITH x8;
26 | x9 WITH x9;
--------------------------------------------------------------------------------
/CFA_HolzingerSwineford1939/Mplus_simplified.inp:
--------------------------------------------------------------------------------
1 | TITLE: Your title goes here
2 | DATA: FILE = "E:/Github/SEM-code-examples/HolzingerSwineford1939.dat";
3 | VARIABLE:
4 | NAMES = x1 x2 x3 x4 x5 x6 x7 x8 x9;
5 | MISSING=.;
6 | MODEL:
7 | ! Factor loadings:
8 | visual BY x1 x2 x3;
9 | textual BY x4 x5 x6;
10 | speed BY x7 x8 x9;
--------------------------------------------------------------------------------
/CFA_HolzingerSwineford1939/OpenMx.R:
--------------------------------------------------------------------------------
1 | # Load the package:
2 | library("lavaan")
3 | library("OpenMx")
4 |
5 | # Load data:
6 | data("HolzingerSwineford1939")
7 | Data <- HolzingerSwineford1939[,c("x1","x2","x3","x4","x5","x6","x7","x8","x9")]
8 |
9 | ### PATH SPECIFICATION ###
10 |
11 | # Data:
12 | dataCov <- mxData(observed=cov(Data), type="cov", numObs = nrow(Data))
13 |
14 | # residual variances
15 | resVars <- mxPath( from=c("x1","x2","x3","x4","x5","x6","x7","x8","x9"), arrows=2,
16 | free=TRUE, values=rep(1,9),
17 | labels=c("e1","e2","e3","e4","e5","e6","e7","e8","e9") )
18 | # latent variance
19 | latVar <- mxPath( from=c("visual","textual","speed"), arrows=2,
20 | free=TRUE, values=1, labels = c("var_visual", "var_textual", "var_speed"))
21 |
22 | # Latent covariances:
23 | latCov <- mxPath( from=c("visual","visual","textual"),
24 | to = c("textual","speed","speed"),
25 | arrows=2,
26 | free=TRUE, values=1, labels = c("cov_visual_textual", "cov_visual_speed", "cov)textual_speed"))
27 |
28 |
29 | # factor loadings
30 | facLoads <- mxPath( from=c("visual","visual","visual","textual","textual","textual","speed","speed","speed"),
31 | to=c("x1","x2","x3","x4","x5","x6","x7","x8","x9"), arrows=1,
32 | free=c(FALSE,TRUE,TRUE,FALSE,TRUE,TRUE,FALSE,TRUE,TRUE),
33 | values=rep(1,9),
34 | labels =c("l1","l2","l3","l4","l5","l6","l7","l8","l9") )
35 |
36 | # Create the model:
37 | oneFactorModel <- mxModel("HolzingerSwineford1939", type="RAM",
38 | manifestVars=c("x1","x2","x3","x4","x5","x6","x7","x8","x9"),
39 | latentVars=c("visual","textual","speed"),
40 | dataCov, resVars, latVar, latCov, facLoads)
41 |
42 | # Run the model:
43 | oneFactorFit <- mxRun(oneFactorModel)
44 |
45 | # Inspect:
46 | summary(oneFactorFit)
47 | coef(oneFactorFit)
48 |
49 |
50 | ### MATRIX SPECIFICATION ###
51 |
52 | # Data:
53 | dataCov <- mxData(observed=cov(Data), type="cov", numObs = nrow(Data))
54 |
55 | # Lambda:
56 | Lambda <- mxMatrix(type="Full", nrow=9, ncol = 3,
57 | free = c(
58 | FALSE, FALSE, FALSE,
59 | TRUE, FALSE, FALSE,
60 | TRUE, FALSE, FALSE,
61 | FALSE, FALSE, FALSE,
62 | FALSE, TRUE, FALSE,
63 | FALSE, TRUE, FALSE,
64 | FALSE, FALSE, FALSE,
65 | FALSE, FALSE, TRUE,
66 | FALSE, FALSE, TRUE
67 | ),
68 | values = c(
69 | 1, 0, 0,
70 | 1, 0, 0,
71 | 1, 0, 0,
72 | 0, 1, 0,
73 | 0, 1, 0,
74 | 0, 1, 0,
75 | 0, 0, 1,
76 | 0, 0, 1,
77 | 0, 0, 1
78 | ), byrow = TRUE, name = "Lambda",
79 | dimnames = list(c("x1","x2","x3","x4","x5","x6","x7","x8","x9"), c("visual","textual","speed")))
80 |
81 | # Psi:
82 | Psi <- mxMatrix(type="Symm", nrow=3,ncol=3,free=TRUE, name = "Psi",
83 | dimnames = list(c("visual","textual","speed"),c("visual","textual","speed")))
84 |
85 | # Theta:
86 | Theta <- mxMatrix(type = "Diag", free = TRUE, nrow = 9, ncol = 9, values = 1, name = "Theta",
87 | dimnames = list(c("x1","x2","x3","x4","x5","x6","x7","x8","x9"),c("x1","x2","x3","x4","x5","x6","x7","x8","x9")))
88 |
89 | # Expectation (model for sigma)
90 | Sigma <- mxAlgebra(Lambda %*% Psi %*% t(Lambda) + Theta, name = "Sigma",
91 | dimnames = list(c("x1","x2","x3","x4","x5","x6","x7","x8","x9"),c("x1","x2","x3","x4","x5","x6","x7","x8","x9")))
92 | expFunction <- mxExpectationNormal(covariance = "Sigma")
93 |
94 | # Estimator:
95 | funML <- mxFitFunctionML()
96 |
97 | oneFactorModel <- mxModel("HolzingerSwineford1939",
98 | manifestVars=c("x1","x2","x3","x4","x5","x6","x7","x8","x9"),
99 | latentVars=c("visual","textual","speed"),
100 | dataCov, Lambda, Psi, Theta, funML, Sigma, expFunction)
101 |
102 | # Run the model:
103 | oneFactorFit <- mxRun(oneFactorModel)
104 |
105 | # Inspect:
106 | summary(oneFactorFit)
107 | coef(oneFactorFit)
108 |
109 |
--------------------------------------------------------------------------------
/CFA_HolzingerSwineford1939/lavaan.R:
--------------------------------------------------------------------------------
1 | # Load the package:
2 | library("lavaan")
3 |
4 | # Load data:
5 | data("HolzingerSwineford1939")
6 | Data <- HolzingerSwineford1939
7 |
8 | # Model:
9 | Model <- '
10 | # Factor loadings:
11 | visual =~ 1*x1 + x2 + x3
12 | textual =~ 1*x4 + x5 + x6
13 | speed =~ 1*x7 + x8 + x9
14 |
15 | # Factor variances:
16 | visual ~~ visual + textual + speed
17 | textual ~~ textual + speed
18 | speed ~~ speed
19 |
20 | # Residual variances:
21 | x1 ~~ x1
22 | x2 ~~ x2
23 | x3 ~~ x3
24 | x4 ~~ x4
25 | x5 ~~ x5
26 | x6 ~~ x6
27 | x7 ~~ x7
28 | x8 ~~ x8
29 | x9 ~~ x9
30 | '
31 |
32 | # Fit in lavaan:
33 | fit <- lavaan(Model, Data)
34 |
35 | # Inspect fit:
36 | fit
37 |
38 | # Parameter estimates:
39 | parameterestimates(fit)
40 |
41 | # Fit measures:
42 | fitMeasures(fit)
43 |
44 | # Modification indices:
45 | modificationindices(fit) %>% arrange(-mi)
--------------------------------------------------------------------------------
/CFA_HolzingerSwineford1939/lavaan_simplified.R:
--------------------------------------------------------------------------------
1 | # Load the package:
2 | library("lavaan")
3 | library("dplyr")
4 |
5 | # Load data:
6 | data("HolzingerSwineford1939")
7 | Data <- HolzingerSwineford1939
8 |
9 | # Model:
10 | Model <- '
11 | visual =~ x1 + x2 + x3
12 | textual =~ x4 + x5 + x6
13 | speed =~ x7 + x8 + x9
14 | '
15 |
16 | # Fit in lavaan:
17 | fit <- cfa(Model, Data)
18 |
19 | # Inspect fit:
20 | fit
21 |
22 | # Parameter estimates:
23 | parameterestimates(fit)
24 |
25 | # Fit measures:
26 | fitMeasures(fit)
27 |
28 | # Modification indices:
29 | modificationindices(fit) %>% arrange(-mi)
30 |
31 | # Model with extra residual covariance:
32 | # Model:
33 | Model2 <- '
34 | visual =~ x1 + x2 + x3
35 | textual =~ x4 + x5 + x6
36 | speed =~ x7 + x8 + x9
37 | visual =~ x9
38 | '
39 |
40 | # Fit in lavaan:
41 | fit2 <- cfa(Model2, Data)
42 |
43 | # Compare models:
44 | anova(fit, fit2)
45 |
--------------------------------------------------------------------------------
/CFA_HolzingerSwineford1939/mplus.R:
--------------------------------------------------------------------------------
1 | library("lavaan")
2 | library("MplusAutomation") # <- is NOT Mplus, but a wrapper around mplus
3 |
4 | # Load data:
5 | data("HolzingerSwineford1939")
6 | Data <- HolzingerSwineford1939[,c("x1","x2","x3","x4","x5","x6","x7","x8","x9")]
7 |
8 | # Export for Mplus:
9 | prepareMplusData(Data, filename = paste0(getwd(),"/HolzingerSwineford1939.dat"))
10 |
--------------------------------------------------------------------------------
/CFA_HolzingerSwineford1939/mplus.dgm:
--------------------------------------------------------------------------------
1 | VERSION 1.1.0
2 | INPUT
3 | TITLE:
4 | Your title goes here
5 | DATA:
6 | FILE = "E:/Github/SEM-code-examples/HolzingerSwineford1939.dat";
7 | VARIABLE:
8 | NAMES = x1 x2 x3 x4 x5 x6 x7 x8 x9;
9 | MISSING=.;
10 | MODEL:
11 | visual BY x1@1 x2 x3;
12 | textual BY x4@1 x5 x6;
13 | speed BY x7@1 x8 x9;
14 | visual WITH visual textual speed;
15 | textual WITH visual textual speed;
16 | speed WITH visual textual speed;
17 | x1 WITH x1;
18 | x2 WITH x2;
19 | x3 WITH x3;
20 | x4 WITH x4;
21 | x5 WITH x5;
22 | x6 WITH x6;
23 | x7 WITH x7;
24 | x8 WITH x8;
25 | x9 WITH x9;
26 | INPUT_END
27 | 2
28 | 0
29 | 0
30 | 0
31 | ML
32 | 1
33 | GENERAL
34 | 0
35 | 9
36 | 3
37 | 0
38 | x1 x2 x3 x4 x5 x6 x7 x8 x9 visual textual speed
39 | 1
40 | -1 -1 -1 -1 -1 -1 -1 -1 -1 0 0 0
41 | 0
42 | 3
43 | visual
44 | x1 x2 x3
45 | textual
46 | x4 x5 x6
47 | speed
48 | x7 x8 x9
49 | 1 1 visual x1 1.000 0.000
50 | 1 1 visual x2 0.553 0.109
51 | 1 1 visual x3 0.729 0.117
52 | 1 1 textual x4 1.000 0.000
53 | 1 1 textual x5 1.113 0.065
54 | 1 1 textual x6 0.926 0.056
55 | 1 1 speed x7 1.000 0.000
56 | 1 1 speed x8 1.180 0.150
57 | 1 1 speed x9 1.082 0.195
58 | 2 1 visual textual 0.408 0.080
59 | 2 1 visual speed 0.262 0.055
60 | 2 1 textual speed 0.173 0.049
61 | 4 1 x1 0.549 0.119
62 | 4 1 x2 1.134 0.104
63 | 4 1 x3 0.844 0.095
64 | 4 1 x4 0.371 0.048
65 | 4 1 x5 0.446 0.058
66 | 4 1 x6 0.356 0.043
67 | 4 1 x7 0.799 0.088
68 | 4 1 x8 0.488 0.092
69 | 4 1 x9 0.566 0.091
70 | 5 1 visual 0.809 0.150
71 | 5 1 textual 0.979 0.112
72 | 5 1 speed 0.384 0.092
73 |
--------------------------------------------------------------------------------
/CFA_HolzingerSwineford1939/mplus_simplified.dgm:
--------------------------------------------------------------------------------
1 | VERSION 1.1.0
2 | INPUT
3 | TITLE:
4 | Your title goes here
5 | DATA:
6 | FILE = "E:/Github/SEM-code-examples/HolzingerSwineford1939.dat";
7 | VARIABLE:
8 | NAMES = x1 x2 x3 x4 x5 x6 x7 x8 x9;
9 | MISSING=.;
10 | MODEL:
11 | visual BY x1 x2 x3;
12 | textual BY x4 x5 x6;
13 | speed BY x7 x8 x9;
14 | INPUT_END
15 | 2
16 | 0
17 | 0
18 | 0
19 | ML
20 | 1
21 | GENERAL
22 | 0
23 | 9
24 | 3
25 | 0
26 | x1 x2 x3 x4 x5 x6 x7 x8 x9 visual textual speed
27 | 1
28 | -1 -1 -1 -1 -1 -1 -1 -1 -1 0 0 0
29 | 0
30 | 3
31 | visual
32 | x1 x2 x3
33 | textual
34 | x4 x5 x6
35 | speed
36 | x7 x8 x9
37 | 1 1 visual x1 1.000 0.000
38 | 1 1 visual x2 0.553 0.109
39 | 1 1 visual x3 0.729 0.117
40 | 1 1 textual x4 1.000 0.000
41 | 1 1 textual x5 1.113 0.065
42 | 1 1 textual x6 0.926 0.056
43 | 1 1 speed x7 1.000 0.000
44 | 1 1 speed x8 1.180 0.150
45 | 1 1 speed x9 1.082 0.195
46 | 2 1 textual visual 0.408 0.080
47 | 2 1 speed visual 0.262 0.055
48 | 2 1 speed textual 0.173 0.049
49 | 4 1 x1 0.549 0.119
50 | 4 1 x2 1.134 0.104
51 | 4 1 x3 0.844 0.095
52 | 4 1 x4 0.371 0.048
53 | 4 1 x5 0.446 0.058
54 | 4 1 x6 0.356 0.043
55 | 4 1 x7 0.799 0.088
56 | 4 1 x8 0.488 0.092
57 | 4 1 x9 0.566 0.091
58 | 5 1 visual 0.809 0.150
59 | 5 1 textual 0.979 0.112
60 | 5 1 speed 0.384 0.092
61 |
--------------------------------------------------------------------------------
/CFA_HolzingerSwineford1939/mplus_simplified.out:
--------------------------------------------------------------------------------
1 | Mplus VERSION 8
2 | MUTHEN & MUTHEN
3 | 04/02/2020 6:11 PM
4 |
5 | INPUT INSTRUCTIONS
6 |
7 | TITLE: Your title goes here
8 | DATA: FILE = "E:/Github/SEM-code-examples/HolzingerSwineford1939.dat";
9 | VARIABLE:
10 | NAMES = x1 x2 x3 x4 x5 x6 x7 x8 x9;
11 | MISSING=.;
12 | MODEL:
13 | ! Factor loadings:
14 | visual BY x1 x2 x3;
15 | textual BY x4 x5 x6;
16 | speed BY x7 x8 x9;
17 |
18 |
19 |
20 | INPUT READING TERMINATED NORMALLY
21 |
22 |
23 |
24 | Your title goes here
25 |
26 | SUMMARY OF ANALYSIS
27 |
28 | Number of groups 1
29 | Number of observations 301
30 |
31 | Number of dependent variables 9
32 | Number of independent variables 0
33 | Number of continuous latent variables 3
34 |
35 | Observed dependent variables
36 |
37 | Continuous
38 | X1 X2 X3 X4 X5 X6
39 | X7 X8 X9
40 |
41 | Continuous latent variables
42 | VISUAL TEXTUAL SPEED
43 |
44 |
45 | Estimator ML
46 | Information matrix OBSERVED
47 | Maximum number of iterations 1000
48 | Convergence criterion 0.500D-04
49 | Maximum number of steepest descent iterations 20
50 | Maximum number of iterations for H1 2000
51 | Convergence criterion for H1 0.100D-03
52 |
53 | Input data file(s)
54 | E:/Github/SEM-code-examples/HolzingerSwineford1939.dat
55 |
56 | Input data format FREE
57 |
58 |
59 | SUMMARY OF DATA
60 |
61 | Number of missing data patterns 1
62 |
63 |
64 | COVARIANCE COVERAGE OF DATA
65 |
66 | Minimum covariance coverage value 0.100
67 |
68 |
69 | PROPORTION OF DATA PRESENT
70 |
71 |
72 | Covariance Coverage
73 | X1 X2 X3 X4 X5
74 | ________ ________ ________ ________ ________
75 | X1 1.000
76 | X2 1.000 1.000
77 | X3 1.000 1.000 1.000
78 | X4 1.000 1.000 1.000 1.000
79 | X5 1.000 1.000 1.000 1.000 1.000
80 | X6 1.000 1.000 1.000 1.000 1.000
81 | X7 1.000 1.000 1.000 1.000 1.000
82 | X8 1.000 1.000 1.000 1.000 1.000
83 | X9 1.000 1.000 1.000 1.000 1.000
84 |
85 |
86 | Covariance Coverage
87 | X6 X7 X8 X9
88 | ________ ________ ________ ________
89 | X6 1.000
90 | X7 1.000 1.000
91 | X8 1.000 1.000 1.000
92 | X9 1.000 1.000 1.000 1.000
93 |
94 |
95 |
96 | UNIVARIATE SAMPLE STATISTICS
97 |
98 |
99 | UNIVARIATE HIGHER-ORDER MOMENT DESCRIPTIVE STATISTICS
100 |
101 | Variable/ Mean/ Skewness/ Minimum/ % with Percentiles
102 | Sample Size Variance Kurtosis Maximum Min/Max 20%/60% 40%/80% Median
103 |
104 | X1 4.936 -0.256 0.667 0.33% 4.000 4.667 5.000
105 | 301.000 1.358 0.330 8.500 0.33% 5.333 5.833
106 | X2 6.088 0.472 2.250 0.33% 5.250 5.750 6.000
107 | 301.000 1.382 0.355 9.250 1.66% 6.250 7.000
108 | X3 2.250 0.385 0.250 0.33% 1.250 1.750 2.125
109 | 301.000 1.275 -0.894 4.500 1.99% 2.375 3.375
110 | X4 3.061 0.269 0.000 0.33% 2.000 2.667 3.000
111 | 301.000 1.351 0.101 6.333 0.33% 3.333 4.000
112 | X5 4.341 -0.352 1.000 0.66% 3.000 4.000 4.500
113 | 301.000 1.660 -0.536 7.000 0.33% 4.750 5.500
114 | X6 2.186 0.862 0.143 0.66% 1.286 1.857 2.000
115 | 301.000 1.196 0.842 6.143 0.33% 2.286 2.857
116 | X7 4.186 0.250 1.304 0.33% 3.304 3.783 4.087
117 | 301.000 1.183 -0.289 7.435 0.33% 4.391 5.130
118 | X8 5.527 0.528 3.050 0.33% 4.700 5.250 5.500
119 | 301.000 1.022 1.199 10.000 0.33% 5.700 6.300
120 | X9 5.374 0.205 2.778 0.33% 4.500 5.083 5.417
121 | 301.000 1.015 0.312 9.250 0.33% 5.611 6.222
122 |
123 |
124 | THE MODEL ESTIMATION TERMINATED NORMALLY
125 |
126 |
127 |
128 | MODEL FIT INFORMATION
129 |
130 | Number of Free Parameters 30
131 |
132 | Loglikelihood
133 |
134 | H0 Value -3737.745
135 | H1 Value -3695.092
136 |
137 | Information Criteria
138 |
139 | Akaike (AIC) 7535.490
140 | Bayesian (BIC) 7646.703
141 | Sample-Size Adjusted BIC 7551.560
142 | (n* = (n + 2) / 24)
143 |
144 | Chi-Square Test of Model Fit
145 |
146 | Value 85.306
147 | Degrees of Freedom 24
148 | P-Value 0.0000
149 |
150 | RMSEA (Root Mean Square Error Of Approximation)
151 |
152 | Estimate 0.092
153 | 90 Percent C.I. 0.071 0.114
154 | Probability RMSEA <= .05 0.001
155 |
156 | CFI/TLI
157 |
158 | CFI 0.931
159 | TLI 0.896
160 |
161 | Chi-Square Test of Model Fit for the Baseline Model
162 |
163 | Value 918.852
164 | Degrees of Freedom 36
165 | P-Value 0.0000
166 |
167 | SRMR (Standardized Root Mean Square Residual)
168 |
169 | Value 0.060
170 |
171 |
172 |
173 | MODEL RESULTS
174 |
175 | Two-Tailed
176 | Estimate S.E. Est./S.E. P-Value
177 |
178 | VISUAL BY
179 | X1 1.000 0.000 999.000 999.000
180 | X2 0.553 0.109 5.067 0.000
181 | X3 0.729 0.117 6.220 0.000
182 |
183 | TEXTUAL BY
184 | X4 1.000 0.000 999.000 999.000
185 | X5 1.113 0.065 17.128 0.000
186 | X6 0.926 0.056 16.481 0.000
187 |
188 | SPEED BY
189 | X7 1.000 0.000 999.000 999.000
190 | X8 1.180 0.150 7.851 0.000
191 | X9 1.082 0.195 5.542 0.000
192 |
193 | TEXTUAL WITH
194 | VISUAL 0.408 0.080 5.124 0.000
195 |
196 | SPEED WITH
197 | VISUAL 0.262 0.055 4.735 0.000
198 | TEXTUAL 0.173 0.049 3.518 0.000
199 |
200 | Intercepts
201 | X1 4.936 0.067 73.473 0.000
202 | X2 6.088 0.068 89.855 0.000
203 | X3 2.250 0.065 34.579 0.000
204 | X4 3.061 0.067 45.694 0.000
205 | X5 4.341 0.074 58.452 0.000
206 | X6 2.186 0.063 34.667 0.000
207 | X7 4.186 0.063 66.766 0.000
208 | X8 5.527 0.058 94.854 0.000
209 | X9 5.374 0.058 92.545 0.000
210 |
211 | Variances
212 | VISUAL 0.809 0.150 5.405 0.000
213 | TEXTUAL 0.979 0.112 8.729 0.000
214 | SPEED 0.384 0.092 4.168 0.000
215 |
216 | Residual Variances
217 | X1 0.549 0.119 4.612 0.000
218 | X2 1.134 0.104 10.875 0.000
219 | X3 0.844 0.095 8.881 0.000
220 | X4 0.371 0.048 7.739 0.000
221 | X5 0.446 0.058 7.703 0.000
222 | X6 0.356 0.043 8.200 0.000
223 | X7 0.799 0.088 9.129 0.000
224 | X8 0.488 0.092 5.321 0.000
225 | X9 0.566 0.091 6.249 0.000
226 |
227 |
228 | QUALITY OF NUMERICAL RESULTS
229 |
230 | Condition Number for the Information Matrix 0.412E-02
231 | (ratio of smallest to largest eigenvalue)
232 |
233 |
234 | DIAGRAM INFORMATION
235 |
236 | Use View Diagram under the Diagram menu in the Mplus Editor to view the diagram.
237 | If running Mplus from the Mplus Diagrammer, the diagram opens automatically.
238 |
239 | Diagram output
240 | e:\github\sem-code-examples\cfa_holzingerswineford1939\mplus_simplified.dgm
241 |
242 | Beginning Time: 18:11:32
243 | Ending Time: 18:11:32
244 | Elapsed Time: 00:00:00
245 |
246 |
247 |
248 | MUTHEN & MUTHEN
249 | 3463 Stoner Ave.
250 | Los Angeles, CA 90066
251 |
252 | Tel: (310) 391-9971
253 | Fax: (310) 391-8971
254 | Web: www.StatModel.com
255 | Support: Support@StatModel.com
256 |
257 | Copyright (c) 1998-2017 Muthen & Muthen
258 |
--------------------------------------------------------------------------------
/CFA_HolzingerSwineford1939/psychonetrics.R:
--------------------------------------------------------------------------------
1 | # Load the packages:
2 | library("lavaan")
3 | library("psychonetrics")
4 | library("dplyr")
5 |
6 | # Load data:
7 | data("HolzingerSwineford1939")
8 | Data <- HolzingerSwineford1939
9 |
10 | # Model:
11 | lambda <- simplestructure(
12 | c("visual",
13 | "visual",
14 | "visual",
15 | "textual",
16 | "textual",
17 | "textual",
18 | "speed",
19 | "speed",
20 | "speed"))
21 |
22 | # Form psychonetrics model:
23 | mod <- lvm(Data, lambda = lambda, vars = paste0("x",1:9),
24 | latents = colnames(lambda))
25 |
26 | # Run the model:
27 | mod <- runmodel(mod)
28 |
29 | # Inspect fit:
30 | mod
31 |
32 | # Parameters:
33 | mod %>% parameters
34 |
35 | # Fit measures:
36 | mod %>% fit
37 |
38 | # Modification indices:
39 | mod %>% MIs
40 |
41 | # Add a parameter:
42 | mod2 <- mod %>% freepar("lambda", 9, 3) %>% runmodel
43 |
44 | # Compare models:
45 | compare(
46 | original = mod,
47 | adjusted = mod2
48 | )
49 |
--------------------------------------------------------------------------------
/CFA_HolzingerSwineford1939/umx.R:
--------------------------------------------------------------------------------
1 | # Load the package:
2 | library("umx")
3 | library("lavaan")
4 |
5 | # Load data:
6 | data("HolzingerSwineford1939")
7 | Data <- HolzingerSwineford1939
8 |
9 | # Model:
10 | Model <- '
11 | visual =~ x1 + x2 + x3
12 | textual =~ x4 + x5 + x6
13 | speed =~ x7 + x8 + x9
14 | '
15 |
16 | # Fit in lavaan:
17 | fit <- umxRAM(Model,data = Data)
18 |
19 | # Inspect fit:
20 | umxSummary(fit)
21 |
--------------------------------------------------------------------------------
/CFA_fit_examples/Jasp/StarWars.csv:
--------------------------------------------------------------------------------
1 | "Q1","Q2","Q3","Q4","Q5","Q6","Q7","Q8","Q9","Q10","Q11","Q12","Q13"
2 | 2,1,1,4,4,2,4,4,5,2,1,25,1
3 | 2,4,2,4,1,2,3,1,3,1,2,23,1
4 | 1,2,4,4,1,1,1,1,4,1,2,24,1
5 | 2,3,2,3,2,1,3,1,3,1,2,26,1
6 | 4,5,3,4,2,3,4,1,5,3,1,23,1
7 | 5,4,3,3,2,3,5,2,3,3,2,23,2
8 | 2,2,2,2,1,4,4,1,3,2,2,21,1
9 | 3,5,1,2,2,2,4,1,3,2,2,22,1
10 | 1,1,2,5,1,3,3,1,2,1,1,23,1
11 | 1,4,4,2,2,3,1,1,4,1,2,23,1
12 | 2,4,1,5,2,4,4,1,4,1,1,22,1
13 | 3,5,2,5,2,2,4,5,2,4,1,29,1
14 | 2,5,5,4,2,1,2,4,4,1,2,22,1
15 | 5,5,3,1,2,2,5,1,3,1,2,24,1
16 | 1,4,2,5,1,2,1,1,4,2,2,24,1
17 | 1,3,1,4,1,2,3,1,4,3,1,24,1
18 | 2,2,2,5,4,3,3,4,2,2,1,23,1
19 | 3,4,2,2,2,1,2,2,4,3,2,19,1
20 | 3,2,1,3,1,3,3,4,4,3,2,26,1
21 | 2,5,3,3,3,4,3,2,4,1,1,23,1
22 | 2,3,2,5,1,1,2,3,3,1,2,23,1
23 | 4,3,3,4,3,3,4,4,3,1,2,22,2
24 | 1,2,2,3,2,2,2,2,3,1,1,43,1
25 | 5,5,3,4,2,2,4,3,3,2,2,25,2
26 | 5,5,2,4,3,2,4,2,3,2,2,25,1
27 | 1,1,2,1,2,2,2,1,4,3,1,25,1
28 | 2,2,4,1,1,1,1,1,5,1,1,23,1
29 | 4,4,3,2,5,3,4,2,3,2,2,25,1
30 | 1,3,1,2,4,2,4,3,4,2,1,21,1
31 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44,1
236 | 1,5,3,2,2,1,1,1,4,2,1,19,1
237 | 1,5,5,2,1,1,2,1,3,1,2,44,1
238 | 1,1,4,5,2,2,2,2,2,1,1,43,1
239 | 1,4,2,5,2,2,1,3,2,2,1,29,1
240 | 1,4,1,2,1,3,5,2,3,1,2,20,1
241 | 1,5,4,3,1,1,1,2,3,2,1,43,1
242 | 1,5,1,4,1,1,1,3,5,1,1,22,1
243 | 1,4,2,5,2,2,1,1,2,3,1,42,1
244 | 1,5,2,5,1,1,1,1,5,1,1,37,1
245 | 1,5,3,5,5,3,1,1,4,1,1,34,1
246 | 3,5,2,3,3,2,2,3,3,2,1,24,1
247 | 5,5,5,3,5,5,5,4,5,3,2,24,2
248 | 1,5,1,2,1,1,1,1,2,2,1,38,1
249 | 5,5,5,3,5,5,5,5,5,5,1,50,2
250 | 1,5,1,5,2,2,2,2,3,1,2,46,1
251 | 5,5,2,5,2,1,1,1,3,1,1,42,1
252 | 1,5,3,2,1,1,2,1,2,1,1,39,1
253 | 1,5,2,4,1,1,1,1,3,1,1,42,1
254 | 1,4,1,5,1,1,3,1,3,1,2,45,1
255 | 1,5,4,2,1,2,2,1,2,2,2,57,1
256 | 2,5,2,5,2,2,5,1,3,1,1,19,1
257 | 3,5,5,5,2,3,4,2,4,2,1,23,1
258 | 2,5,2,3,4,4,4,2,2,2,1,23,1
259 | 5,5,3,3,1,3,3,5,3,3,2,24,2
260 | 2,5,2,4,4,2,4,1,5,3,1,41,1
261 | 2,3,2,2,1,5,4,5,3,4,2,52,1
262 | 4,4,3,4,2,3,5,2,2,1,2,24,1
263 | 3,5,3,2,2,2,3,1,1,2,2,35,1
264 | 2,4,2,4,2,2,4,1,2,1,1,24,1
265 | 2,3,3,1,1,2,2,2,4,1,1,26,1
266 | 2,5,4,3,5,2,3,1,3,2,2,25,1
267 | 2,2,4,3,4,3,3,5,3,3,1,25,1
268 | 1,5,1,5,5,1,1,1,3,1,1,22,1
269 | 2,5,4,4,4,1,3,3,5,3,1,43,1
270 | 3,5,3,2,3,3,4,2,3,3,1,42,1
271 | 4,5,1,2,1,2,2,1,4,4,1,32,1
272 | 1,4,2,4,2,2,4,1,3,2,1,34,1
273 |
--------------------------------------------------------------------------------
/CFA_fit_examples/Jasp/StarWars.jasp:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/SachaEpskamp/SEM-code-examples/1fc4500c21c8df785101d6ca0832efb9aef45f95/CFA_fit_examples/Jasp/StarWars.jasp
--------------------------------------------------------------------------------
/CFA_fit_examples/Mplus/StarWars_Mplus.csv:
--------------------------------------------------------------------------------
1 | 2,1,1,4,4,2,4,4,5,2,1,25,1
2 | 2,4,2,4,1,2,3,1,3,1,2,23,1
3 | 1,2,4,4,1,1,1,1,4,1,2,24,1
4 | 2,3,2,3,2,1,3,1,3,1,2,26,1
5 | 4,5,3,4,2,3,4,1,5,3,1,23,1
6 | 5,4,3,3,2,3,5,2,3,3,2,23,2
7 | 2,2,2,2,1,4,4,1,3,2,2,21,1
8 | 3,5,1,2,2,2,4,1,3,2,2,22,1
9 | 1,1,2,5,1,3,3,1,2,1,1,23,1
10 | 1,4,4,2,2,3,1,1,4,1,2,23,1
11 | 2,4,1,5,2,4,4,1,4,1,1,22,1
12 | 3,5,2,5,2,2,4,5,2,4,1,29,1
13 | 2,5,5,4,2,1,2,4,4,1,2,22,1
14 | 5,5,3,1,2,2,5,1,3,1,2,24,1
15 | 1,4,2,5,1,2,1,1,4,2,2,24,1
16 | 1,3,1,4,1,2,3,1,4,3,1,24,1
17 | 2,2,2,5,4,3,3,4,2,2,1,23,1
18 | 3,4,2,2,2,1,2,2,4,3,2,19,1
19 | 3,2,1,3,1,3,3,4,4,3,2,26,1
20 | 2,5,3,3,3,4,3,2,4,1,1,23,1
21 | 2,3,2,5,1,1,2,3,3,1,2,23,1
22 | 4,3,3,4,3,3,4,4,3,1,2,22,2
23 | 1,2,2,3,2,2,2,2,3,1,1,43,1
24 | 5,5,3,4,2,2,4,3,3,2,2,25,2
25 | 5,5,2,4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,1,31,1
128 | 3,4,2,1,2,4,3,2,3,3,2,33,1
129 | 1,5,2,5,1,1,1,1,2,1,1,27,1
130 | 1,4,2,1,2,3,2,2,4,3,2,22,1
131 | 5,5,3,3,2,5,4,2,3,3,1,32,1
132 | 5,5,3,3,3,3,3,3,3,3,2,33,1
133 | 1,2,5,4,2,1,2,1,1,2,2,24,1
134 | 4,5,3,2,1,5,4,1,3,2,2,36,1
135 | 1,5,4,1,3,2,1,1,5,4,1,43,1
136 | 4,4,3,4,5,4,2,1,4,2,2,48,1
137 | 3,5,4,2,2,4,4,3,3,3,2,27,1
138 | 4,3,2,1,3,3,5,1,2,4,2,30,1
139 | 3,5,2,2,2,5,4,1,3,1,1,34,1
140 | 1,5,3,3,2,1,1,2,4,1,1,23,1
141 | 3,5,3,4,2,5,3,4,4,3,1,50,1
142 | 5,5,3,5,5,3,5,1,3,2,1,24,1
143 | 2,5,2,3,1,1,2,2,5,3,2,49,1
144 | 1,5,5,3,2,1,1,1,3,2,1,42,1
145 | 3,5,4,2,2,3,3,1,3,2,1,36,1
146 | 1,5,5,1,1,1,1,1,2,1,2,32,1
147 | 2,5,5,3,2,2,3,2,5,3,1,42,1
148 | 2,5,4,2,2,2,1,1,2,2,2,36,1
149 | 5,5,3,3,3,3,3,3,3,4,2,25,2
150 | 2,5,3,3,3,2,2,1,3,2,2,29,1
151 | 4,5,3,4,2,3,5,1,2,2,2,33,1
152 | 4,5,2,1,2,2,5,1,1,1,1,39,1
153 | 3,5,3,2,2,3,2,4,3,2,1,42,2
154 | 2,5,2,4,2,3,4,1,4,2,2,27,1
155 | 4,4,3,4,3,4,5,2,3,2,2,21,1
156 | 4,1,3,3,2,3,3,2,3,3,2,60,1
157 | 2,4,5,3,4,2,4,1,5,4,1,37,1
158 | 2,2,2,2,2,1,1,1,1,1,2,27,1
159 | 2,1,2,4,3,3,2,4,3,1,1,25,1
160 | 5,5,3,3,5,3,3,4,3,3,2,39,2
161 | 2,5,1,4,3,2,2,1,2,3,2,38,1
162 | 5,5,3,3,1,2,3,1,3,3,2,37,2
163 | 2,5,5,3,3,2,4,2,2,2,2,30,1
164 | 3,5,3,3,5,4,2,4,2,2,1,31,1
165 | 4,5,3,1,2,3,3,3,4,3,1,35,1
166 | 4,5,2,3,1,2,3,3,3,3,2,45,1
167 | 5,5,3,2,4,4,4,5,3,2,2,43,1
168 | 2,3,4,2,3,2,4,1,2,1,2,53,1
169 | 3,5,2,2,1,4,1,5,5,4,2,29,1
170 | 1,4,2,4,3,4,5,1,3,2,1,31,1
171 | 5,4,3,5,4,5,2,4,3,2,2,38,1
172 | 3,5,1,5,2,5,5,3,2,5,1,54,1
173 | 5,1,3,3,3,4,3,5,3,3,1,19,1
174 | 1,4,2,4,1,1,4,3,1,1,1,21,1
175 | 2,1,1,5,2,2,4,3,5,1,1,22,1
176 | 4,5,4,4,4,2,2,3,3,3,1,30,1
177 | 2,5,5,4,3,2,4,1,3,1,1,39,1
178 | 3,4,3,3,2,2,2,1,3,2,2,15,2
179 | 2,5,3,2,3,3,2,2,3,3,2,28,1
180 | 5,5,4,2,2,5,4,4,4,2,1,37,1
181 | 5,5,3,1,3,3,1,5,3,1,1,41,2
182 | 3,4,5,2,1,3,2,3,5,3,1,50,1
183 | 3,4,2,1,2,3,5,2,3,3,2,21,1
184 | 3,4,3,5,2,2,4,2,2,1,1,33,1
185 | 4,3,4,4,2,4,3,1,2,1,1,28,1
186 | 1,5,4,5,1,2,3,2,5,1,2,22,1
187 | 3,5,3,4,2,3,2,3,2,2,1,25,1
188 | 1,2,4,3,2,1,4,5,5,1,1,34,1
189 | 2,2,2,3,2,1,4,3,4,3,1,30,1
190 | 4,5,4,2,2,5,4,1,5,2,1,26,1
191 | 2,4,2,2,2,2,4,2,5,2,1,35,1
192 | 3,3,3,4,3,3,4,3,3,2,2,23,2
193 | 2,2,2,5,1,1,3,2,4,1,1,21,1
194 | 3,4,3,4,2,3,3,2,3,2,1,61,1
195 | 5,3,3,4,2,3,3,1,3,3,2,28,2
196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,1,1,2,2,2,4,1,1,26,1
265 | 2,5,4,3,5,2,3,1,3,2,2,25,1
266 | 2,2,4,3,4,3,3,5,3,3,1,25,1
267 | 1,5,1,5,5,1,1,1,3,1,1,22,1
268 | 2,5,4,4,4,1,3,3,5,3,1,43,1
269 | 3,5,3,2,3,3,4,2,3,3,1,42,1
270 | 4,5,1,2,1,2,2,1,4,4,1,32,1
271 | 1,4,2,4,2,2,4,1,3,2,1,34,1
272 |
--------------------------------------------------------------------------------
/CFA_fit_examples/Mplus/StarWars_mPlus.inp:
--------------------------------------------------------------------------------
1 | TITLE: Star Wars model
2 | DATA: FILE IS StarWars_Mplus.csv; ! Note that this data has no first row with variable names...
3 | VARIABLE: NAMES ARE Q1-Q13; USEVARIABLES ARE Q1-Q10;
4 | MODEL:
5 |
6 | ! Factor Loadings:
7 | Prequels BY Q2-Q4 Q1;
8 | Original BY Q5-Q7 Q1;
9 | Sequels BY Q8-Q10 Q1;
10 |
11 | ! Like lavaan, Mplus does a lot of things automatically, such as
12 | ! adding (residual) variances and fixing the first loading to 1
13 | ! Variances can further be specified with the WITH command. For
14 | ! example, this adds the residual variance:
15 | Q4 WITH Q10;
16 |
17 | OUTPUT:
18 | STANDARDIZED; ! This adds standardized parameters
19 | MODINDICES (8); ! This adds modification indices above 8!
--------------------------------------------------------------------------------
/CFA_fit_examples/Mplus/semPlot.R:
--------------------------------------------------------------------------------
1 | # Load packages:
2 | library("dplyr") # I always load this
3 | library("semPlot")
4 |
5 | # Location of output file:
6 | outFile <- "StarWars_mPlus.out"
7 |
8 | # Plot with semPlot:
9 | semPaths(outFile, "std", "est", intercepts = FALSE)
10 |
11 | # We can make this nicer. First let's define the node labels:
12 | nodeNames <- c(
13 | "I am a huge Star Wars\nfan! (star what?)",
14 | "I would trust this person\nwith my democracy.",
15 | "I enjoyed the story of\nAnakin's early life.",
16 | "The special effects in\nthis scene are awful (Battle of Geonosis).",
17 | "I would trust this person\nwith my life.",
18 | "I found Darth Vader's big\nreveal in 'Empire' one of the greatest\nmoments in movie history.",
19 | "The special effects in\nthis scene are amazing (Death Star\nExplosion).",
20 | "If possible, I would\ndefinitely buy this\ndroid.",
21 | "The story in the Star\nWars sequels is an improvement to\nthe previous movies.",
22 | "The special effects in\nthis scene are marvellous (Starkiller\nBase Firing).",
23 | "Prequel trilogy",
24 | "Original trilogy",
25 | "Sequel trilogy"
26 | )
27 |
28 | # Now we can plot:
29 | semPaths(outFile,
30 | what = "std", # this argument controls what the color of edges represent. In this case, standardized parameters
31 | whatLabels = "est", # This argument controls what the edge labels represent. In this case, parameter estimates
32 | style = "lisrel", # This will plot residuals as arrows, closer to what we use in class
33 | residScale = 8, # This makes the residuals larger
34 | theme = "colorblind", # qgraph colorblind friendly theme
35 | nCharNodes = 0, # Setting this to 0 disables abbreviation of nodes
36 | manifests = paste0("Q",1:10), # Names of manifests, to order them appropriatly.
37 | reorder = FALSE, # This disables the default reordering
38 | nodeNames = nodeNames, # Add a legend with node names
39 | legend.cex = 0.25, # Makes the legend smaller
40 | rotation = 2, # Rotates the plot
41 | layout = "tree2", # tree layout options are "tree", "tree2", and "tree3"
42 | cardinal = "lat cov", # This makes the latent covariances connet at a cardinal center point
43 | curvePivot = TRUE, # Changes curve into rounded straight lines
44 | sizeMan = 4, # Size of manifest variables
45 | sizeLat = 10, # Size of latent varibales
46 | intercepts = FALSE # Disables intercepts
47 | )
48 |
49 | # Some other things we can do with semPlot is do some algebra:
50 | semMatrixAlgebra(outFile, Lambda) # Obtain factor loadings
51 | semMatrixAlgebra(outFile, Psi) # Obtain latent variance-covariance matrix
52 | semMatrixAlgebra(outFile, Theta) # Obtain residual variance-covariance matrix
53 | semMatrixAlgebra(outFile, Lambda %*% Psi %*% t(Lambda) + Theta) # Obtain Sigma
54 |
55 | # And we can generate lavaan syntax:
56 | semSyntax(outFile)
57 |
--------------------------------------------------------------------------------
/CFA_fit_examples/Onyx/StarWars.csv:
--------------------------------------------------------------------------------
1 | "Q1","Q2","Q3","Q4","Q5","Q6","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,51,1
206 | 1,5,1,3,1,1,3,3,3,3,1,46,1
207 | 5,4,3,4,2,3,4,5,3,3,2,20,2
208 | 5,5,3,2,3,3,3,3,3,2,1,56,2
209 | 5,5,5,2,3,5,4,5,5,5,2,55,2
210 | 1,5,3,2,3,2,3,2,5,2,1,21,1
211 | 1,4,2,3,4,2,4,4,2,2,2,45,1
212 | 2,4,3,5,2,4,5,2,3,1,1,21,1
213 | 2,4,4,4,3,4,4,2,5,2,2,23,1
214 | 2,5,2,1,2,2,1,1,1,1,1,27,1
215 | 1,5,3,4,1,1,2,1,2,3,2,53,1
216 | 1,5,2,4,3,1,1,1,1,2,1,37,1
217 | 3,2,3,4,3,4,2,3,4,2,2,23,1
218 | 3,5,5,3,5,3,3,5,3,3,2,59,2
219 | 1,5,3,3,5,3,3,1,3,3,1,26,1
220 | 2,5,2,3,2,3,3,1,5,3,2,18,1
221 | 1,3,1,5,2,1,1,2,3,1,1,63,1
222 | 5,3,3,4,2,5,5,5,3,3,2,23,2
223 | 4,3,3,2,2,3,3,2,2,2,2,22,1
224 | 3,5,2,4,2,2,2,1,2,1,2,27,1
225 | 3,3,3,3,4,2,4,5,3,1,2,23,2
226 | 5,5,3,3,5,3,3,5,3,5,2,29,2
227 | 2,4,2,5,2,3,2,2,3,1,2,22,1
228 | 5,3,3,2,3,3,5,3,3,2,1,32,2
229 | 5,5,5,4,3,3,4,3,3,1,2,25,2
230 | 3,3,3,3,3,3,3,3,3,3,2,24,2
231 | 4,5,3,2,5,3,4,3,3,2,1,21,2
232 | 5,5,3,2,5,5,5,5,3,2,1,27,2
233 | 4,5,3,4,3,3,3,3,3,5,2,20,2
234 | 1,5,2,2,1,1,1,3,4,2,1,49,1
235 | 1,5,1,5,2,1,1,2,3,1,1,44,1
236 | 1,5,3,2,2,1,1,1,4,2,1,19,1
237 | 1,5,5,2,1,1,2,1,3,1,2,44,1
238 | 1,1,4,5,2,2,2,2,2,1,1,43,1
239 | 1,4,2,5,2,2,1,3,2,2,1,29,1
240 | 1,4,1,2,1,3,5,2,3,1,2,20,1
241 | 1,5,4,3,1,1,1,2,3,2,1,43,1
242 | 1,5,1,4,1,1,1,3,5,1,1,22,1
243 | 1,4,2,5,2,2,1,1,2,3,1,42,1
244 | 1,5,2,5,1,1,1,1,5,1,1,37,1
245 | 1,5,3,5,5,3,1,1,4,1,1,34,1
246 | 3,5,2,3,3,2,2,3,3,2,1,24,1
247 | 5,5,5,3,5,5,5,4,5,3,2,24,2
248 | 1,5,1,2,1,1,1,1,2,2,1,38,1
249 | 5,5,5,3,5,5,5,5,5,5,1,50,2
250 | 1,5,1,5,2,2,2,2,3,1,2,46,1
251 | 5,5,2,5,2,1,1,1,3,1,1,42,1
252 | 1,5,3,2,1,1,2,1,2,1,1,39,1
253 | 1,5,2,4,1,1,1,1,3,1,1,42,1
254 | 1,4,1,5,1,1,3,1,3,1,2,45,1
255 | 1,5,4,2,1,2,2,1,2,2,2,57,1
256 | 2,5,2,5,2,2,5,1,3,1,1,19,1
257 | 3,5,5,5,2,3,4,2,4,2,1,23,1
258 | 2,5,2,3,4,4,4,2,2,2,1,23,1
259 | 5,5,3,3,1,3,3,5,3,3,2,24,2
260 | 2,5,2,4,4,2,4,1,5,3,1,41,1
261 | 2,3,2,2,1,5,4,5,3,4,2,52,1
262 | 4,4,3,4,2,3,5,2,2,1,2,24,1
263 | 3,5,3,2,2,2,3,1,1,2,2,35,1
264 | 2,4,2,4,2,2,4,1,2,1,1,24,1
265 | 2,3,3,1,1,2,2,2,4,1,1,26,1
266 | 2,5,4,3,5,2,3,1,3,2,2,25,1
267 | 2,2,4,3,4,3,3,5,3,3,1,25,1
268 | 1,5,1,5,5,1,1,1,3,1,1,22,1
269 | 2,5,4,4,4,1,3,3,5,3,1,43,1
270 | 3,5,3,2,3,3,4,2,3,3,1,42,1
271 | 4,5,1,2,1,2,2,1,4,4,1,32,1
272 | 1,4,2,4,2,2,4,1,3,2,1,34,1
273 |
--------------------------------------------------------------------------------
/CFA_fit_examples/OpenMx/StarWars.csv:
--------------------------------------------------------------------------------
1 | "Q1","Q2","Q3","Q4","Q5","Q6","Q7","Q8","Q9","Q10","Q11","Q12","Q13"
2 | 2,1,1,4,4,2,4,4,5,2,1,25,1
3 | 2,4,2,4,1,2,3,1,3,1,2,23,1
4 | 1,2,4,4,1,1,1,1,4,1,2,24,1
5 | 2,3,2,3,2,1,3,1,3,1,2,26,1
6 | 4,5,3,4,2,3,4,1,5,3,1,23,1
7 | 5,4,3,3,2,3,5,2,3,3,2,23,2
8 | 2,2,2,2,1,4,4,1,3,2,2,21,1
9 | 3,5,1,2,2,2,4,1,3,2,2,22,1
10 | 1,1,2,5,1,3,3,1,2,1,1,23,1
11 | 1,4,4,2,2,3,1,1,4,1,2,23,1
12 | 2,4,1,5,2,4,4,1,4,1,1,22,1
13 | 3,5,2,5,2,2,4,5,2,4,1,29,1
14 | 2,5,5,4,2,1,2,4,4,1,2,22,1
15 | 5,5,3,1,2,2,5,1,3,1,2,24,1
16 | 1,4,2,5,1,2,1,1,4,2,2,24,1
17 | 1,3,1,4,1,2,3,1,4,3,1,24,1
18 | 2,2,2,5,4,3,3,4,2,2,1,23,1
19 | 3,4,2,2,2,1,2,2,4,3,2,19,1
20 | 3,2,1,3,1,3,3,4,4,3,2,26,1
21 | 2,5,3,3,3,4,3,2,4,1,1,23,1
22 | 2,3,2,5,1,1,2,3,3,1,2,23,1
23 | 4,3,3,4,3,3,4,4,3,1,2,22,2
24 | 1,2,2,3,2,2,2,2,3,1,1,43,1
25 | 5,5,3,4,2,2,4,3,3,2,2,25,2
26 | 5,5,2,4,3,2,4,2,3,2,2,25,1
27 | 1,1,2,1,2,2,2,1,4,3,1,25,1
28 | 2,2,4,1,1,1,1,1,5,1,1,23,1
29 | 4,4,3,2,5,3,4,2,3,2,2,25,1
30 | 1,3,1,2,4,2,4,3,4,2,1,21,1
31 | 1,5,2,5,2,1,2,3,3,1,1,25,1
32 | 2,2,3,5,3,2,4,2,3,2,1,24,1
33 | 4,5,3,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53,1
170 | 3,5,2,2,1,4,1,5,5,4,2,29,1
171 | 1,4,2,4,3,4,5,1,3,2,1,31,1
172 | 5,4,3,5,4,5,2,4,3,2,2,38,1
173 | 3,5,1,5,2,5,5,3,2,5,1,54,1
174 | 5,1,3,3,3,4,3,5,3,3,1,19,1
175 | 1,4,2,4,1,1,4,3,1,1,1,21,1
176 | 2,1,1,5,2,2,4,3,5,1,1,22,1
177 | 4,5,4,4,4,2,2,3,3,3,1,30,1
178 | 2,5,5,4,3,2,4,1,3,1,1,39,1
179 | 3,4,3,3,2,2,2,1,3,2,2,15,2
180 | 2,5,3,2,3,3,2,2,3,3,2,28,1
181 | 5,5,4,2,2,5,4,4,4,2,1,37,1
182 | 5,5,3,1,3,3,1,5,3,1,1,41,2
183 | 3,4,5,2,1,3,2,3,5,3,1,50,1
184 | 3,4,2,1,2,3,5,2,3,3,2,21,1
185 | 3,4,3,5,2,2,4,2,2,1,1,33,1
186 | 4,3,4,4,2,4,3,1,2,1,1,28,1
187 | 1,5,4,5,1,2,3,2,5,1,2,22,1
188 | 3,5,3,4,2,3,2,3,2,2,1,25,1
189 | 1,2,4,3,2,1,4,5,5,1,1,34,1
190 | 2,2,2,3,2,1,4,3,4,3,1,30,1
191 | 4,5,4,2,2,5,4,1,5,2,1,26,1
192 | 2,4,2,2,2,2,4,2,5,2,1,35,1
193 | 3,3,3,4,3,3,4,3,3,2,2,23,2
194 | 2,2,2,5,1,1,3,2,4,1,1,21,1
195 | 3,4,3,4,2,3,3,2,3,2,1,61,1
196 | 5,3,3,4,2,3,3,1,3,3,2,28,2
197 | 5,4,2,1,4,5,4,5,2,4,2,25,1
198 | 5,5,3,3,5,5,3,2,3,3,2,32,2
199 | 5,5,3,4,5,5,4,3,3,4,2,28,1
200 | 5,3,3,4,3,5,3,2,3,2,2,54,1
201 | 1,5,3,2,1,1,2,2,2,1,2,44,1
202 | 5,5,2,4,5,5,3,1,5,1,1,31,1
203 | 4,5,3,3,2,3,2,4,3,3,1,45,1
204 | 2,4,1,4,1,2,4,3,3,1,2,23,1
205 | 2,5,4,2,2,2,2,2,3,1,2,51,1
206 | 1,5,1,3,1,1,3,3,3,3,1,46,1
207 | 5,4,3,4,2,3,4,5,3,3,2,20,2
208 | 5,5,3,2,3,3,3,3,3,2,1,56,2
209 | 5,5,5,2,3,5,4,5,5,5,2,55,2
210 | 1,5,3,2,3,2,3,2,5,2,1,21,1
211 | 1,4,2,3,4,2,4,4,2,2,2,45,1
212 | 2,4,3,5,2,4,5,2,3,1,1,21,1
213 | 2,4,4,4,3,4,4,2,5,2,2,23,1
214 | 2,5,2,1,2,2,1,1,1,1,1,27,1
215 | 1,5,3,4,1,1,2,1,2,3,2,53,1
216 | 1,5,2,4,3,1,1,1,1,2,1,37,1
217 | 3,2,3,4,3,4,2,3,4,2,2,23,1
218 | 3,5,5,3,5,3,3,5,3,3,2,59,2
219 | 1,5,3,3,5,3,3,1,3,3,1,26,1
220 | 2,5,2,3,2,3,3,1,5,3,2,18,1
221 | 1,3,1,5,2,1,1,2,3,1,1,63,1
222 | 5,3,3,4,2,5,5,5,3,3,2,23,2
223 | 4,3,3,2,2,3,3,2,2,2,2,22,1
224 | 3,5,2,4,2,2,2,1,2,1,2,27,1
225 | 3,3,3,3,4,2,4,5,3,1,2,23,2
226 | 5,5,3,3,5,3,3,5,3,5,2,29,2
227 | 2,4,2,5,2,3,2,2,3,1,2,22,1
228 | 5,3,3,2,3,3,5,3,3,2,1,32,2
229 | 5,5,5,4,3,3,4,3,3,1,2,25,2
230 | 3,3,3,3,3,3,3,3,3,3,2,24,2
231 | 4,5,3,2,5,3,4,3,3,2,1,21,2
232 | 5,5,3,2,5,5,5,5,3,2,1,27,2
233 | 4,5,3,4,3,3,3,3,3,5,2,20,2
234 | 1,5,2,2,1,1,1,3,4,2,1,49,1
235 | 1,5,1,5,2,1,1,2,3,1,1,44,1
236 | 1,5,3,2,2,1,1,1,4,2,1,19,1
237 | 1,5,5,2,1,1,2,1,3,1,2,44,1
238 | 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2,4,2,2,4,1,3,2,1,34,1
273 |
--------------------------------------------------------------------------------
/CFA_fit_examples/OpenMx/StarWars_OpenMx.R:
--------------------------------------------------------------------------------
1 | # Load packages:
2 | library("dplyr") # I always load this
3 | library("semPlot")
4 |
5 | # For info on OpenMx, see:
6 | # https://openmx.ssri.psu.edu/
7 | # Install with:
8 | #
9 | library("OpenMx")
10 |
11 | # This code show an example of matrix specification in OpenMx, which is a clear benefit of OpenMx.
12 | # You can also use path specification (see website above), but that generally does not work as nice
13 | # as lavaan in my oppinion.
14 |
15 | # Read the data:
16 | Data <- read.csv("StarWars.csv", sep = ",")
17 |
18 | # This data encodes the following variables:
19 | # Q1: I am a huge Star Wars fan! (star what?)
20 | # Q2: I would trust this person with my democracy.
21 | # Q3: I enjoyed the story of Anakin's early life.
22 | # Q4: The special effects in this scene are awful (Battle of Geonosis).
23 | # Q5: I would trust this person with my life.
24 | # Q6: I found Darth Vader'ss big reveal in "Empire" one of the greatest moments in movie history.
25 | # Q7: The special effects in this scene are amazing (Death Star Explosion).
26 | # Q8: If possible, I would definitely buy this droid.
27 | # Q9: The story in the Star Wars sequels is an improvement to the previous movies.
28 | # Q10: The special effects in this scene are marvellous (Starkiller Base Firing).
29 | # Q11: What is your gender?
30 | # Q12: How old are you?
31 | # Q13: Have you seen any of the Star Wars movies?
32 |
33 | # Observed variables:
34 | obsvars <- paste0("Q",1:10)
35 |
36 | # Latents:
37 | latents <- c("Prequels","Original","Sequels")
38 |
39 | # Set the data (summary statistics, raw data is also possible):
40 | # Max likelihood cov mat:
41 | n <- nrow(Data)
42 | covMat <- (n-1)/n * cov(Data[,obsvars])
43 | dataRaw <- mxData(observed=covMat, type="cov", numObs = nrow(Data))
44 |
45 | # Lambda matrix:
46 | Lambda <- matrix(0, 10, 3)
47 | Lambda[1:4,1] <- 1
48 | Lambda[c(1,5:7),2] <- 1
49 | Lambda[c(1,8:10),3] <- 1
50 | mxLambda <- mxMatrix("Full",
51 | nrow = 10,
52 | ncol = 3,
53 | free = Lambda!=0,
54 | values = Lambda,
55 | name = "lambda",
56 | dimnames = list(obsvars, latents)
57 | )
58 |
59 | # Psi matrix:
60 | diag <- diag(3)
61 | mxPsi <- mxMatrix("Symm",
62 | nrow = 3,
63 | ncol = 3,
64 | values = diag,
65 | free = diag != 1,
66 | name = "psi",
67 | dimnames = list(latents, latents)
68 | )
69 |
70 | # Theta matrix:
71 | mxTheta <- mxMatrix("Diag",
72 | nrow = 10,
73 | ncol = 10,
74 | name = "theta",
75 | free = TRUE,
76 | dimnames = list(obsvars, obsvars),
77 | lbound = 0 # Lower bound
78 | )
79 |
80 | # Implied variance--covariance matrix:
81 | mxSigma <- mxAlgebra(lambda %*% psi %*% t(lambda) + theta, name = "sigma")
82 |
83 | # Expectation (this tells OpenMx that sigma is the expected cov matrix):
84 | exp <- mxExpectationNormal( covariance="sigma",dimnames=obsvars)
85 |
86 | # Fit function (max likelihood)
87 | funML <- mxFitFunctionML()
88 |
89 | # Combine everything in a big model:
90 | model <- mxModel("Star Wars",
91 | dataRaw,
92 | mxLambda,
93 | mxPsi,
94 | mxTheta,
95 | mxSigma,
96 | exp,
97 | funML)
98 |
99 | # Run model:
100 | model <- mxRun(model)
101 |
102 | # Look at model summary:
103 | summary(model)
104 |
105 | # chi-square: χ² ( df=30 ) = 34.56062, p = 0.2589784
106 | # very similar to lavaan and psychonetrics
107 |
108 | # Modification indices:
109 | MIs <- mxMI(model)
110 | sort(MIs$MI, decreasing = TRUE)[1:10]
111 |
--------------------------------------------------------------------------------
/CFA_fit_examples/StarWars_questionaire.pdf:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/SachaEpskamp/SEM-code-examples/1fc4500c21c8df785101d6ca0832efb9aef45f95/CFA_fit_examples/StarWars_questionaire.pdf
--------------------------------------------------------------------------------
/CFA_fit_examples/lavaan/StarWars.csv:
--------------------------------------------------------------------------------
1 | "Q1","Q2","Q3","Q4","Q5","Q6","Q7","Q8","Q9","Q10","Q11","Q12","Q13"
2 | 2,1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,2,27,1
139 | 4,3,2,1,3,3,5,1,2,4,2,30,1
140 | 3,5,2,2,2,5,4,1,3,1,1,34,1
141 | 1,5,3,3,2,1,1,2,4,1,1,23,1
142 | 3,5,3,4,2,5,3,4,4,3,1,50,1
143 | 5,5,3,5,5,3,5,1,3,2,1,24,1
144 | 2,5,2,3,1,1,2,2,5,3,2,49,1
145 | 1,5,5,3,2,1,1,1,3,2,1,42,1
146 | 3,5,4,2,2,3,3,1,3,2,1,36,1
147 | 1,5,5,1,1,1,1,1,2,1,2,32,1
148 | 2,5,5,3,2,2,3,2,5,3,1,42,1
149 | 2,5,4,2,2,2,1,1,2,2,2,36,1
150 | 5,5,3,3,3,3,3,3,3,4,2,25,2
151 | 2,5,3,3,3,2,2,1,3,2,2,29,1
152 | 4,5,3,4,2,3,5,1,2,2,2,33,1
153 | 4,5,2,1,2,2,5,1,1,1,1,39,1
154 | 3,5,3,2,2,3,2,4,3,2,1,42,2
155 | 2,5,2,4,2,3,4,1,4,2,2,27,1
156 | 4,4,3,4,3,4,5,2,3,2,2,21,1
157 | 4,1,3,3,2,3,3,2,3,3,2,60,1
158 | 2,4,5,3,4,2,4,1,5,4,1,37,1
159 | 2,2,2,2,2,1,1,1,1,1,2,27,1
160 | 2,1,2,4,3,3,2,4,3,1,1,25,1
161 | 5,5,3,3,5,3,3,4,3,3,2,39,2
162 | 2,5,1,4,3,2,2,1,2,3,2,38,1
163 | 5,5,3,3,1,2,3,1,3,3,2,37,2
164 | 2,5,5,3,3,2,4,2,2,2,2,30,1
165 | 3,5,3,3,5,4,2,4,2,2,1,31,1
166 | 4,5,3,1,2,3,3,3,4,3,1,35,1
167 | 4,5,2,3,1,2,3,3,3,3,2,45,1
168 | 5,5,3,2,4,4,4,5,3,2,2,43,1
169 | 2,3,4,2,3,2,4,1,2,1,2,53,1
170 | 3,5,2,2,1,4,1,5,5,4,2,29,1
171 | 1,4,2,4,3,4,5,1,3,2,1,31,1
172 | 5,4,3,5,4,5,2,4,3,2,2,38,1
173 | 3,5,1,5,2,5,5,3,2,5,1,54,1
174 | 5,1,3,3,3,4,3,5,3,3,1,19,1
175 | 1,4,2,4,1,1,4,3,1,1,1,21,1
176 | 2,1,1,5,2,2,4,3,5,1,1,22,1
177 | 4,5,4,4,4,2,2,3,3,3,1,30,1
178 | 2,5,5,4,3,2,4,1,3,1,1,39,1
179 | 3,4,3,3,2,2,2,1,3,2,2,15,2
180 | 2,5,3,2,3,3,2,2,3,3,2,28,1
181 | 5,5,4,2,2,5,4,4,4,2,1,37,1
182 | 5,5,3,1,3,3,1,5,3,1,1,41,2
183 | 3,4,5,2,1,3,2,3,5,3,1,50,1
184 | 3,4,2,1,2,3,5,2,3,3,2,21,1
185 | 3,4,3,5,2,2,4,2,2,1,1,33,1
186 | 4,3,4,4,2,4,3,1,2,1,1,28,1
187 | 1,5,4,5,1,2,3,2,5,1,2,22,1
188 | 3,5,3,4,2,3,2,3,2,2,1,25,1
189 | 1,2,4,3,2,1,4,5,5,1,1,34,1
190 | 2,2,2,3,2,1,4,3,4,3,1,30,1
191 | 4,5,4,2,2,5,4,1,5,2,1,26,1
192 | 2,4,2,2,2,2,4,2,5,2,1,35,1
193 | 3,3,3,4,3,3,4,3,3,2,2,23,2
194 | 2,2,2,5,1,1,3,2,4,1,1,21,1
195 | 3,4,3,4,2,3,3,2,3,2,1,61,1
196 | 5,3,3,4,2,3,3,1,3,3,2,28,2
197 | 5,4,2,1,4,5,4,5,2,4,2,25,1
198 | 5,5,3,3,5,5,3,2,3,3,2,32,2
199 | 5,5,3,4,5,5,4,3,3,4,2,28,1
200 | 5,3,3,4,3,5,3,2,3,2,2,54,1
201 | 1,5,3,2,1,1,2,2,2,1,2,44,1
202 | 5,5,2,4,5,5,3,1,5,1,1,31,1
203 | 4,5,3,3,2,3,2,4,3,3,1,45,1
204 | 2,4,1,4,1,2,4,3,3,1,2,23,1
205 | 2,5,4,2,2,2,2,2,3,1,2,51,1
206 | 1,5,1,3,1,1,3,3,3,3,1,46,1
207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,5,4,3,1,1,1,2,3,2,1,43,1
242 | 1,5,1,4,1,1,1,3,5,1,1,22,1
243 | 1,4,2,5,2,2,1,1,2,3,1,42,1
244 | 1,5,2,5,1,1,1,1,5,1,1,37,1
245 | 1,5,3,5,5,3,1,1,4,1,1,34,1
246 | 3,5,2,3,3,2,2,3,3,2,1,24,1
247 | 5,5,5,3,5,5,5,4,5,3,2,24,2
248 | 1,5,1,2,1,1,1,1,2,2,1,38,1
249 | 5,5,5,3,5,5,5,5,5,5,1,50,2
250 | 1,5,1,5,2,2,2,2,3,1,2,46,1
251 | 5,5,2,5,2,1,1,1,3,1,1,42,1
252 | 1,5,3,2,1,1,2,1,2,1,1,39,1
253 | 1,5,2,4,1,1,1,1,3,1,1,42,1
254 | 1,4,1,5,1,1,3,1,3,1,2,45,1
255 | 1,5,4,2,1,2,2,1,2,2,2,57,1
256 | 2,5,2,5,2,2,5,1,3,1,1,19,1
257 | 3,5,5,5,2,3,4,2,4,2,1,23,1
258 | 2,5,2,3,4,4,4,2,2,2,1,23,1
259 | 5,5,3,3,1,3,3,5,3,3,2,24,2
260 | 2,5,2,4,4,2,4,1,5,3,1,41,1
261 | 2,3,2,2,1,5,4,5,3,4,2,52,1
262 | 4,4,3,4,2,3,5,2,2,1,2,24,1
263 | 3,5,3,2,2,2,3,1,1,2,2,35,1
264 | 2,4,2,4,2,2,4,1,2,1,1,24,1
265 | 2,3,3,1,1,2,2,2,4,1,1,26,1
266 | 2,5,4,3,5,2,3,1,3,2,2,25,1
267 | 2,2,4,3,4,3,3,5,3,3,1,25,1
268 | 1,5,1,5,5,1,1,1,3,1,1,22,1
269 | 2,5,4,4,4,1,3,3,5,3,1,43,1
270 | 3,5,3,2,3,3,4,2,3,3,1,42,1
271 | 4,5,1,2,1,2,2,1,4,4,1,32,1
272 | 1,4,2,4,2,2,4,1,3,2,1,34,1
273 |
--------------------------------------------------------------------------------
/CFA_fit_examples/lavaan/StarWars.pdf:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/SachaEpskamp/SEM-code-examples/1fc4500c21c8df785101d6ca0832efb9aef45f95/CFA_fit_examples/lavaan/StarWars.pdf
--------------------------------------------------------------------------------
/CFA_fit_examples/lavaan/StarWars_lavaan.R:
--------------------------------------------------------------------------------
1 | # Load packages:
2 | library("dplyr") # I always load this
3 | library("lavaan")
4 |
5 | # Read the data:
6 | Data <- read.csv("StarWars.csv", sep = ",")
7 |
8 | # This data encodes the following variables:
9 | # Q1: I am a huge Star Wars fan! (star what?)
10 | # Q2: I would trust this person with my democracy.
11 | # Q3: I enjoyed the story of Anakin's early life.
12 | # Q4: The special effects in this scene are awful (Battle of Geonosis).
13 | # Q5: I would trust this person with my life.
14 | # Q6: I found Darth Vader'ss big reveal in "Empire" one of the greatest moments in movie history.
15 | # Q7: The special effects in this scene are amazing (Death Star Explosion).
16 | # Q8: If possible, I would definitely buy this droid.
17 | # Q9: The story in the Star Wars sequels is an improvement to the previous movies.
18 | # Q10: The special effects in this scene are marvellous (Starkiller Base Firing).
19 | # Q11: What is your gender?
20 | # Q12: How old are you?
21 | # Q13: Have you seen any of the Star Wars movies?
22 |
23 | # Three factor model for trilogies using lavaan:
24 | Model <- '
25 | # Factor loadings:
26 | Prequels =~ Q1 + Q2 + Q3 + Q4
27 | Original =~ Q1 + Q5 + Q6 + Q7
28 | Sequels =~ Q1 + Q8 + Q9 + Q10
29 |
30 | # Reisdual variances:
31 | Q1 ~~ Q1
32 | Q2 ~~ Q2
33 | Q3 ~~ Q3
34 | Q4 ~~ Q4
35 | Q5 ~~ Q5
36 | Q6 ~~ Q6
37 | Q7 ~~ Q7
38 | Q8 ~~ Q8
39 | Q9 ~~ Q9
40 | Q10 ~~ Q10
41 |
42 | # Factor variances / covariances:
43 | Prequels ~~ 1*Prequels # Scaling (alternative is constraining a factor loading to 1)
44 | Prequels ~~ Original
45 | Prequels ~~ Sequels
46 | Original ~~ 1*Original # Scaling
47 | Original ~~ Sequels
48 | Sequels ~~ 1*Sequels # Scaling
49 | '
50 |
51 | # Fit model in lavaan:
52 | fit <- lavaan(Model, Data)
53 |
54 | # Look at fit:
55 | fit
56 |
57 | # We can do this easier with the cfa function!
58 | Model <- '
59 | Prequels =~ Q1 + Q2 + Q3 + Q4
60 | Original =~ Q1 + Q5 + Q6 + Q7
61 | Sequels =~ Q1 + Q8 + Q9 + Q10
62 | '
63 |
64 | # Fit in lavaan:
65 | fit <- cfa(Model, Data, std.lv=TRUE) # Automatically sets first factor loading to 1. Use std.lv = TRUE for latent variable variance scaling!
66 |
67 | # Look at fit:
68 | fit
69 |
70 | # Look at parameters:
71 | parameterEstimates(fit)
72 |
73 | # Let's look at the top 10 modification indices:
74 | modificationindices(fit) %>% arrange(-mi) %>% head(10) # These are some dplyr tricks
75 |
76 | # We could add Q4 ~~ Q10 (special effects of prequels and sequels)
77 | Model2 <- '
78 | Prequels =~ Q2 + Q3 + Q4 + Q1
79 | Original =~ Q5 + Q6 + Q7 + Q1
80 | Sequels =~ Q8 + Q9 + Q10 + Q1
81 | Q4 ~~ Q10
82 | '
83 |
84 | # Fit in lavaan:
85 | fit2 <- cfa(Model2, Data, std.lv=TRUE) # Automatically sets first factor loading to 1. Use std.lv = TRUE for latent variable variance scaling!
86 |
87 | # Compare fit:
88 | anova(fit,fit2)
89 | # Fit 2 has better AIC, BIC and fits significantly better, so it would be prefered.
90 | # However, we could make a good argument *not* to add this parameter, as the model
91 | # already fit well and the previous model was *theoretical*.
92 |
93 | # look at the parameter estimates:
94 | parameterestimates(fit2)
95 |
96 | # Some things to note: the star wars fandom questionary only strongly loads on the original factor, and relatively low correlations with the prequel trilogy
97 |
98 | # Look at fit measures:
99 | fitMeasures(fit2) # All really good!
100 |
101 | # Finally, we could also fit the model using only the covariances!
102 | covMat <- cov(Data[,1:10])
103 | fit2b <- cfa(Model2, sample.cov = covMat, sample.nobs = nrow(Data), std.lv=TRUE)
104 |
105 | # Gives the same mode:
106 | anova(fit2, fit2b)
107 |
--------------------------------------------------------------------------------
/CFA_fit_examples/lavaan/StarWars_semPlot.R:
--------------------------------------------------------------------------------
1 | # Load packages:
2 | library("dplyr") # I always load this
3 | library("lavaan")
4 | library("semPlot")
5 |
6 | # Read the data:
7 | Data <- read.csv("StarWars.csv", sep = ",")
8 |
9 | # Final model:
10 | Model2 <- '
11 | Prequels =~ Q2 + Q3 + Q4 + Q1
12 | Original =~ Q5 + Q6 + Q7 + Q1
13 | Sequels =~ Q8 + Q9 + Q10 + Q1
14 | Q4 ~~ Q10
15 | '
16 |
17 | # Let's fit the model using scaling in latent variable variance, it makes raw parameters a bit easier to interpret:
18 | fit2b <- cfa(Model2, Data, std.lv=TRUE)
19 |
20 | # Plot with semPlot:
21 | semPaths(fit2b, "std", "est")
22 |
23 | # We can make this nicer. First let's define the node labels:
24 | nodeNames <- c(
25 | "I am a huge Star Wars\nfan! (star what?)",
26 | "I would trust this person with\nmy democracy (Jar-Jar Binks).",
27 | "I enjoyed the story of\nAnakin's early life.",
28 | "The special effects in\nthis scene are awful (Battle of Geonosis).",
29 | "I would trust this person\nwith my life (Han Solo).",
30 | "I found Darth Vader's big\nreveal in 'Empire' one of the greatest\nmoments in movie history.",
31 | "The special effects in\nthis scene are amazing (Death Star\nExplosion).",
32 | "If possible, I would definitely buy\nthis droid (BB-8).",
33 | "The story in the Star\nWars sequels is an improvement to\nthe previous movies.",
34 | "The special effects in\nthis scene are marvellous (Starkiller\nBase Firing).",
35 | "Prequel trilogy",
36 | "Original trilogy",
37 | "Sequel trilogy"
38 | )
39 |
40 | # Now we can plot:
41 | semPaths(fit2b,
42 | what = "std", # this argument controls what the color of edges represent. In this case, standardized parameters
43 | whatLabels = "est", # This argument controls what the edge labels represent. In this case, parameter estimates
44 | style = "lisrel", # This will plot residuals as arrows, closer to what we use in class
45 | residScale = 8, # This makes the residuals larger
46 | theme = "colorblind", # qgraph colorblind friendly theme
47 | nCharNodes = 0, # Setting this to 0 disables abbreviation of nodes
48 | manifests = paste0("Q",1:10), # Names of manifests, to order them appropriatly.
49 | reorder = FALSE, # This disables the default reordering
50 | nodeNames = nodeNames, # Add a legend with node names
51 | legend.cex = 0.5, # Makes the legend smaller
52 | rotation = 2, # Rotates the plot
53 | layout = "tree2", # tree layout options are "tree", "tree2", and "tree3"
54 | cardinal = "lat cov", # This makes the latent covariances connet at a cardinal center point
55 | curvePivot = TRUE, # Changes curve into rounded straight lines
56 | sizeMan = 4, # Size of manifest variables
57 | sizeLat = 10, # Size of latent variables
58 | mar = c(2,5,2,5.5), # Figure margins
59 | filetype = "pdf", width = 8, height = 6, filename = "StarWars" # Save to PDF
60 | )
61 |
62 | # Some other things we can do with semPlot is do some algebra:
63 | semMatrixAlgebra(fit2b, Lambda) # Obtain factor loadings
64 | semMatrixAlgebra(fit2b, Psi) # Obtain latent variance-covariance matrix
65 | semMatrixAlgebra(fit2b, Theta) # Obtain residual variance-covariance matrix
66 | semMatrixAlgebra(fit2b, Lambda %*% Psi %*% t(Lambda) + Theta) # Obtain Sigma
67 |
68 | # And we can generate lavaan syntax:
69 | semSyntax(fit2b)
70 |
--------------------------------------------------------------------------------
/CFA_fit_examples/psychonetrics/StarWars.csv:
--------------------------------------------------------------------------------
1 | "Q1","Q2","Q3","Q4","Q5","Q6","Q7","Q8","Q9","Q10","Q11","Q12","Q13"
2 | 2,1,1,4,4,2,4,4,5,2,1,25,1
3 | 2,4,2,4,1,2,3,1,3,1,2,23,1
4 | 1,2,4,4,1,1,1,1,4,1,2,24,1
5 | 2,3,2,3,2,1,3,1,3,1,2,26,1
6 | 4,5,3,4,2,3,4,1,5,3,1,23,1
7 | 5,4,3,3,2,3,5,2,3,3,2,23,2
8 | 2,2,2,2,1,4,4,1,3,2,2,21,1
9 | 3,5,1,2,2,2,4,1,3,2,2,22,1
10 | 1,1,2,5,1,3,3,1,2,1,1,23,1
11 | 1,4,4,2,2,3,1,1,4,1,2,23,1
12 | 2,4,1,5,2,4,4,1,4,1,1,22,1
13 | 3,5,2,5,2,2,4,5,2,4,1,29,1
14 | 2,5,5,4,2,1,2,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117 | 2,5,2,4,1,3,2,2,3,2,1,57,1
118 | 4,4,4,2,1,3,4,3,3,5,2,52,1
119 | 5,3,3,1,4,3,2,1,3,1,1,30,2
120 | 5,5,5,4,2,5,4,5,5,3,2,22,2
121 | 2,5,4,4,2,2,2,2,2,1,2,33,1
122 | 5,5,3,3,3,5,3,3,3,3,2,23,2
123 | 1,5,4,1,2,1,2,1,4,2,2,37,1
124 | 1,5,4,3,2,2,4,1,3,1,1,32,1
125 | 2,5,2,4,2,4,2,1,3,1,1,38,1
126 | 2,2,3,3,2,3,3,4,3,2,1,27,1
127 | 1,4,1,4,2,2,2,1,3,1,1,27,1
128 | 3,5,5,2,3,4,4,5,2,3,1,31,1
129 | 3,4,2,1,2,4,3,2,3,3,2,33,1
130 | 1,5,2,5,1,1,1,1,2,1,1,27,1
131 | 1,4,2,1,2,3,2,2,4,3,2,22,1
132 | 5,5,3,3,2,5,4,2,3,3,1,32,1
133 | 5,5,3,3,3,3,3,3,3,3,2,33,1
134 | 1,2,5,4,2,1,2,1,1,2,2,24,1
135 | 4,5,3,2,1,5,4,1,3,2,2,36,1
136 | 1,5,4,1,3,2,1,1,5,4,1,43,1
137 | 4,4,3,4,5,4,2,1,4,2,2,48,1
138 | 3,5,4,2,2,4,4,3,3,3,2,27,1
139 | 4,3,2,1,3,3,5,1,2,4,2,30,1
140 | 3,5,2,2,2,5,4,1,3,1,1,34,1
141 | 1,5,3,3,2,1,1,2,4,1,1,23,1
142 | 3,5,3,4,2,5,3,4,4,3,1,50,1
143 | 5,5,3,5,5,3,5,1,3,2,1,24,1
144 | 2,5,2,3,1,1,2,2,5,3,2,49,1
145 | 1,5,5,3,2,1,1,1,3,2,1,42,1
146 | 3,5,4,2,2,3,3,1,3,2,1,36,1
147 | 1,5,5,1,1,1,1,1,2,1,2,32,1
148 | 2,5,5,3,2,2,3,2,5,3,1,42,1
149 | 2,5,4,2,2,2,1,1,2,2,2,36,1
150 | 5,5,3,3,3,3,3,3,3,4,2,25,2
151 | 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1,1,3,1,1,42,1
254 | 1,4,1,5,1,1,3,1,3,1,2,45,1
255 | 1,5,4,2,1,2,2,1,2,2,2,57,1
256 | 2,5,2,5,2,2,5,1,3,1,1,19,1
257 | 3,5,5,5,2,3,4,2,4,2,1,23,1
258 | 2,5,2,3,4,4,4,2,2,2,1,23,1
259 | 5,5,3,3,1,3,3,5,3,3,2,24,2
260 | 2,5,2,4,4,2,4,1,5,3,1,41,1
261 | 2,3,2,2,1,5,4,5,3,4,2,52,1
262 | 4,4,3,4,2,3,5,2,2,1,2,24,1
263 | 3,5,3,2,2,2,3,1,1,2,2,35,1
264 | 2,4,2,4,2,2,4,1,2,1,1,24,1
265 | 2,3,3,1,1,2,2,2,4,1,1,26,1
266 | 2,5,4,3,5,2,3,1,3,2,2,25,1
267 | 2,2,4,3,4,3,3,5,3,3,1,25,1
268 | 1,5,1,5,5,1,1,1,3,1,1,22,1
269 | 2,5,4,4,4,1,3,3,5,3,1,43,1
270 | 3,5,3,2,3,3,4,2,3,3,1,42,1
271 | 4,5,1,2,1,2,2,1,4,4,1,32,1
272 | 1,4,2,4,2,2,4,1,3,2,1,34,1
273 |
--------------------------------------------------------------------------------
/CFA_fit_examples/psychonetrics/StarWars_psychonetrics.R:
--------------------------------------------------------------------------------
1 | # Load packages:
2 | library("dplyr") # I always load this
3 | library("psychonetrics")
4 |
5 | # Read the data:
6 | Data <- read.csv("StarWars.csv", sep = ",")
7 |
8 | # This data encodes the following variables:
9 | # Q1: I am a huge Star Wars fan! (star what?)
10 | # Q2: I would trust this person with my democracy.
11 | # Q3: I enjoyed the story of Anakin's early life.
12 | # Q4: The special effects in this scene are awful (Battle of Geonosis).
13 | # Q5: I would trust this person with my life.
14 | # Q6: I found Darth Vader'ss big reveal in "Empire" one of the greatest moments in movie history.
15 | # Q7: The special effects in this scene are amazing (Death Star Explosion).
16 | # Q8: If possible, I would definitely buy this droid.
17 | # Q9: The story in the Star Wars sequels is an improvement to the previous movies.
18 | # Q10: The special effects in this scene are marvellous (Starkiller Base Firing).
19 | # Q11: What is your gender?
20 | # Q12: How old are you?
21 | # Q13: Have you seen any of the Star Wars movies?
22 |
23 | # Factor loadings matrix:
24 | Lambda <- matrix(0, 10, 3)
25 | Lambda[1:4,1] <- 1
26 | Lambda[c(1,5:7),2] <- 1
27 | Lambda[c(1,8:10),3] <- 1
28 |
29 | # Observed variables:
30 | obsvars <- paste0("Q",1:10)
31 |
32 | # Latents:
33 | latents <- c("Prequels","Original","Sequels")
34 |
35 | # Make model:
36 | mod1 <- lvm(Data, lambda = Lambda, vars = obsvars,
37 | identification = "variance", latents = latents)
38 |
39 | # Run model:
40 | mod1 <- mod1 %>% runmodel
41 |
42 | # Look at fit:
43 | mod1
44 |
45 | # Look at parameter estimates:
46 | mod1 %>% parameters
47 |
48 | # Look at modification indices:
49 | mod1 %>% MIs
50 |
51 | # Add and refit:
52 | mod2 <- mod1 %>% freepar("sigma_epsilon","Q10","Q4") %>% runmodel
53 |
54 | # Compare:
55 | compare(original = mod1, adjusted = mod2)
56 |
57 | # Fit measures:
58 | mod2 %>% fit
59 |
60 | # Some extra things we can do: stepup search (alpha = 0.01 & BIC optimization):
61 | mod3 <- mod2 %>% stepup(matrices = c("lambda", "sigma_epsilon"))
62 |
63 | # The same in this case:
64 | compare(stepup = mod3, adjusted = mod2)
65 |
66 | # Bootstrap the difference:
67 | mod1_withdata <- lvm(Data, lambda = Lambda, vars = obsvars,
68 | identification = "variance", latents = latents,
69 | storedata = TRUE)
70 |
71 | # Bootstrap data and run:
72 | mod1_boot <- mod1_withdata %>% bootstrap %>% runmodel
73 |
74 | # Add parameter and run:
75 | mod2_boot <- mod1_boot %>% freepar("sigma_epsilon","Q10","Q4") %>% runmodel
76 |
77 | # Compare:
78 | compare(original_bootstrapped = mod1_boot,
79 | adjusted_bootstrapped = mod2_boot)
80 |
81 |
82 | # Latent network model:
83 | lnm <- lvm(Data, lambda = Lambda, vars = obsvars,
84 | latents = latents, identification = "variance",
85 | latent = "ggm")
86 |
87 | # Run model:
88 | lnm <- lnm %>% runmodel
89 |
90 | # Look at parameters:
91 | lnm %>% parameters
92 |
93 | # Remove non-sig latent edge:
94 | lnm <- lnm %>% prune(alpha = 0.05)
95 |
96 | # Compare to the original CFA model:
97 | compare(cfa = mod1, lnm = lnm)
98 |
99 | # Plot network:
100 | library("qgraph")
101 | qgraph(lnm@modelmatrices[[1]]$omega_zeta, labels = latents,
102 | theme = "colorblind", vsize = 10)
103 |
104 | ### NOT IN THE VIDEO ###
105 | # A wrapper for the latent network model is the lnm function:
106 | lnm2 <- lnm(Data, lambda = Lambda, vars = obsvars,
107 | latents = latents, identification = "variance")
108 | lnm2 <- lnm2 %>% runmodel %>% prune(alpha = 0.05)
109 | compare(lnm, lnm2) # Is the same as the model before.
110 |
111 | # I could also estimate a "residual network model", which adds partial correlations to the residual level:
112 | # This can be done using lvm(..., residal = "ggm") or with rnm(...)
113 | rnm <- rnm(Data, lambda = Lambda, vars = obsvars,
114 | latents = latents, identification = "variance")
115 | # Stepup search:
116 | rnm <- rnm %>% stepup
117 |
118 | # It will estimate the same model (with link Q10 - Q4) as above. In the case of only one partial correlation,
119 | # There is no difference between residual covariances (SEM) or residual partial correlations (RNM).
120 |
121 |
122 | # For more information on latent and residual network models, see:
123 | # Epskamp, S., Rhemtulla, M.T., & Borsboom, D. Generalized Network Psychometrics: Combining Network and Latent Variable Models
124 | # (2017). Psychometrika. doi:10.1007/s11336-017-9557-x
125 |
126 | # Extra: Estimating a GGM from variance-covariance matrices
127 | # All psychonetrics functions (e.g., lvm, lnm, rnm...) allow input via a covariance matrix, with the "covs" and "nobs" arguments.
128 | # The following fits a baseline GGM network with no edges:
129 | S <- (nrow(Data) - 1)/ (nrow(Data)) * cov(Data[,1:10])
130 | ggmmod <- ggm(covs = S, nobs = nrow(Data), omega = "empty")
131 |
132 | # Run model with stepup search and pruning:
133 | ggmmod <- ggmmod %>% stepup %>% prune
134 |
135 | # Fit measures:
136 | ggmmod %>% fit
137 |
138 | # Plot network:
139 | nodeNames <- c(
140 | "I am a huge Star Wars\nfan! (star what?)",
141 | "I would trust this person\nwith my democracy.",
142 | "I enjoyed the story of\nAnakin's early life.",
143 | "The special effects in\nthis scene are awful (Battle of\nGeonosis).",
144 | "I would trust this person\nwith my life.",
145 | "I found Darth Vader's big\nreveal in 'Empire' one of the greatest\nmoments in movie history.",
146 | "The special effects in\nthis scene are amazing (Death Star\nExplosion).",
147 | "If possible, I would\ndefinitely buy this\ndroid.",
148 | "The story in the Star\nWars sequels is an improvement to\nthe previous movies.",
149 | "The special effects in\nthis scene are marvellous (Starkiller\nBase Firing)."
150 | )
151 | library("qgraph")
152 | qgraph(as.matrix(ggmmod@modelmatrices[[1]]$omega), nodeNames = nodeNames, legend.cex = 0.25,
153 | theme = "colorblind", layout = "spring")
154 |
155 | # We can actually compare this model statistically (note they are not nested) to the latent variable model:
156 | compare(original_cfa = mod1, adjusted_cfa = mod2, exploratory_ggm = ggmmod)
157 | # The latent variable model fits better!
158 |
159 |
--------------------------------------------------------------------------------
/Latent_growth_examples/Jasp/LGC_Jasp.jasp:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/SachaEpskamp/SEM-code-examples/1fc4500c21c8df785101d6ca0832efb9aef45f95/Latent_growth_examples/Jasp/LGC_Jasp.jasp
--------------------------------------------------------------------------------
/Latent_growth_examples/Jasp/covmat_bivariate.csv:
--------------------------------------------------------------------------------
1 | "alc1","alc2","alc3","alc4","cig1","cig2","cig3","cig4"
2 | 1.004,0.615761158891985,0.535582200869297,0.418415621773375,0.650160301218092,0.659412211279106,0.63485823368686,0.53281791213134
3 | 0.615761158891985,0.922,0.586815972516086,0.499881510232175,0.494706091745392,0.534327356020633,0.552961319343406,0.518512231740004
4 | 0.535582200869297,0.586815972516086,0.832,0.52100086559621,0.40637925143885,0.406967540720387,0.444557775772733,0.431142763102896
5 | 0.418415621773375,0.499881510232175,0.52100086559621,0.846,0.375110009823785,0.41037726252803,0.501368520900545,0.597788171338309
6 | 0.650160301218092,0.494706091745392,0.40637925143885,0.375110009823785,1.305,1.20342710622621,1.14722860996621,0.968481690978203
7 | 0.659412211279106,0.534327356020633,0.406967540720387,0.41037726252803,1.20342710622621,1.536,1.2954962724763,1.19545632721233
8 | 0.63485823368686,0.552961319343406,0.444557775772733,0.501368520900545,1.14722860996621,1.2954962724763,1.645,1.37284467590474
9 | 0.53281791213134,0.518512231740004,0.431142763102896,0.597788171338309,0.968481690978203,1.19545632721233,1.37284467590474,1.888
10 |
--------------------------------------------------------------------------------
/Latent_growth_examples/Jasp/means_bivariate.csv:
--------------------------------------------------------------------------------
1 | "mean"
2 | 2.271
3 | 2.56
4 | 2.694
5 | 2.965
6 | 1.847
7 | 2.043
8 | 2.227
9 | 2.51
10 |
--------------------------------------------------------------------------------
/Latent_growth_examples/Jasp/readme.txt:
--------------------------------------------------------------------------------
1 | Jasp supports estimating models from covariance matrices but not yet from means. Because
2 | of this, the code simulate_data.R simulates a dataset with the same covariance and mean
3 | structure. If you have the raw data (typically you do!) you do not need to do this!
4 |
5 | When fitting a latent growth curve model in Jasp, there are some options that are
6 | important:
7 |
8 | 1. Set in options:
9 | [x] Include mean strructure
10 |
11 | 2. Set in advanced:
12 | [x] Fix manufest intercepts to zero
13 | [ ] Fix latent intercepts to zero
14 |
15 | Next, you can use the same models as in lavaan. For the univariate latent growth model:
16 |
17 | i_alc =~ 1*alc1 + 1*alc2 + 1*alc3 + 1*alc4
18 | s_alc =~ 1*alc1 + 2*alc2 + 3*alc3 + 4*alc4
19 |
20 | and for the bivariate latent growth model:
21 |
22 | # Alcohol:
23 | i_alc =~ 1*alc1 + 1*alc2 + 1*alc3 + 1*alc4
24 | s_alc =~ 1*alc1 + 2*alc2 + 3*alc3 + 4*alc4
25 |
26 | # Cigarettes:
27 | i_cig =~ 1*cig1 + 1*cig2 + 1*cig3 + 1*cig4
28 | s_cig =~ 1*cig1 + 2*cig2 + 3*cig3 + 4*cig4
--------------------------------------------------------------------------------
/Latent_growth_examples/Jasp/simulate_data.R:
--------------------------------------------------------------------------------
1 | library("dplyr")
2 | library("MASS")
3 |
4 | # Read the data, subset of summary statistics reported by:
5 |
6 | # Duncan, S. C., & Duncan, T. E. (1996). A multivariate latent
7 | # growth curve analysis of adolescent substance use. Structural
8 | # Equation Modeling: A Multidisciplinary Journal, 3(4), 323-347.
9 |
10 | # Sample size: 321
11 |
12 | covMat <- as.matrix(read.csv("covmat_bivariate.csv"))
13 | rownames(covMat) <- colnames(covMat)
14 | means <- read.csv("means_bivariate.csv")[,1]
15 |
16 | # Generate a dataset with same means and cov matrix:
17 | library(MASS)
18 | simData <- mvrnorm(321, mu = means, Sigma = covMat, empirical = TRUE)
19 | names(simData) <- colnames(covMat)
20 | write.csv(simData,"simulatedData.csv", row.names = FALSE)
21 |
22 |
--------------------------------------------------------------------------------
/Latent_growth_examples/Onyx/LGC_Onyx_unvariate.xml:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
5 |
6 |
7 |
8 |
9 |
10 |
11 |
12 |
13 |
14 |
15 |
16 |
17 |
18 |
19 |
20 |
21 |
22 |
23 |
24 |
25 |
26 |
27 |
28 |
29 |
30 |
--------------------------------------------------------------------------------
/Latent_growth_examples/Onyx/covmat_bivariate.csv:
--------------------------------------------------------------------------------
1 | "alc1","alc2","alc3","alc4","cig1","cig2","cig3","cig4"
2 | 1.004,0.615761158891985,0.535582200869297,0.418415621773375,0.650160301218092,0.659412211279106,0.63485823368686,0.53281791213134
3 | 0.615761158891985,0.922,0.586815972516086,0.499881510232175,0.494706091745392,0.534327356020633,0.552961319343406,0.518512231740004
4 | 0.535582200869297,0.586815972516086,0.832,0.52100086559621,0.40637925143885,0.406967540720387,0.444557775772733,0.431142763102896
5 | 0.418415621773375,0.499881510232175,0.52100086559621,0.846,0.375110009823785,0.41037726252803,0.501368520900545,0.597788171338309
6 | 0.650160301218092,0.494706091745392,0.40637925143885,0.375110009823785,1.305,1.20342710622621,1.14722860996621,0.968481690978203
7 | 0.659412211279106,0.534327356020633,0.406967540720387,0.41037726252803,1.20342710622621,1.536,1.2954962724763,1.19545632721233
8 | 0.63485823368686,0.552961319343406,0.444557775772733,0.501368520900545,1.14722860996621,1.2954962724763,1.645,1.37284467590474
9 | 0.53281791213134,0.518512231740004,0.431142763102896,0.597788171338309,0.968481690978203,1.19545632721233,1.37284467590474,1.888
10 |
--------------------------------------------------------------------------------
/Latent_growth_examples/Onyx/means_bivariate.csv:
--------------------------------------------------------------------------------
1 | "mean"
2 | 2.271
3 | 2.56
4 | 2.694
5 | 2.965
6 | 1.847
7 | 2.043
8 | 2.227
9 | 2.51
10 |
--------------------------------------------------------------------------------
/Latent_growth_examples/Onyx/readme.txt:
--------------------------------------------------------------------------------
1 | Onyx supports estimating models from summary statistics (covariance matrix), but it is
2 | not trivial. Because of this, the code simulate_data.R simulates a dataset with the same
3 | covariance and mean structure. If you have the raw data (typically you do!) you do not
4 | need to do this!
5 |
6 | Steps:
7 |
8 | 1. Open Onyx
9 | 2. Drag simulatedData.csv to Onyx
10 | 3. Drag LGC_Onyx_unvariate.xml to Onyx. I created this model using:
11 | 1. right-click
12 | 2. Create new model
13 | 3. Create new LGCM
14 | 4. Edit labels and values if needed
15 | 4. Right click the data in Onyx, and select:
16 | 1. Send data to model ->
17 | 2. Univariate latent growth
18 | 5. The model is now estimating, check results with:
19 | 1. right click
20 | 2. Show estimate summary
21 | 6. For the bivariate LGCM, repeat 3 - 5 for the file LGC_Onyx_bivariate.xml
--------------------------------------------------------------------------------
/Latent_growth_examples/Onyx/simulate_data.R:
--------------------------------------------------------------------------------
1 | library("dplyr")
2 | library("MASS")
3 |
4 | # Read the data, subset of summary statistics reported by:
5 |
6 | # Duncan, S. C., & Duncan, T. E. (1996). A multivariate latent
7 | # growth curve analysis of adolescent substance use. Structural
8 | # Equation Modeling: A Multidisciplinary Journal, 3(4), 323-347.
9 |
10 | # Sample size: 321
11 |
12 | covMat <- as.matrix(read.csv("covmat_bivariate.csv"))
13 | rownames(covMat) <- colnames(covMat)
14 | means <- read.csv("means_bivariate.csv")[,1]
15 |
16 | # Generate a dataset with same means and cov matrix:
17 | library(MASS)
18 | simData <- mvrnorm(321, mu = means, Sigma = covMat, empirical = TRUE)
19 | names(simData) <- colnames(covMat)
20 | write.csv(simData,"simulatedData.csv", row.names = FALSE)
21 |
22 |
--------------------------------------------------------------------------------
/Latent_growth_examples/lavaan/LGC_lavaan_bivariate.R:
--------------------------------------------------------------------------------
1 | library("dplyr")
2 | library("lavaan")
3 |
4 | # Read the data, subset of summary statistics reported by:
5 |
6 | # Duncan, S. C., & Duncan, T. E. (1996). A multivariate latent
7 | # growth curve analysis of adolescent substance use. Structural
8 | # Equation Modeling: A Multidisciplinary Journal, 3(4), 323-347.
9 |
10 | # Sample size: 321
11 |
12 | covMat <- as.matrix(read.csv("covmat_bivariate.csv"))
13 | rownames(covMat) <- colnames(covMat)
14 | means <- read.csv("means_bivariate.csv")[,1]
15 |
16 | # Lavaan growth model:
17 | mod <- '
18 | # Alcohol:
19 | i_alc =~ 1*alc1 + 1*alc2 + 1*alc3 + 1*alc4
20 | s_alc =~ 0*alc1 + 1*alc2 + 2*alc3 + 3*alc4
21 |
22 | # Cigarettes:
23 | i_cig =~ 1*cig1 + 1*cig2 + 1*cig3 + 1*cig4
24 | s_cig =~ 0*cig1 + 1*cig2 + 2*cig3 + 3*cig4
25 | '
26 |
27 | # Fit model:
28 | # If you have raw data you can ignore the argument sample.cov, sample.mean and sample.nobs and use
29 | # the argument "data" instead"
30 | fit <- growth(mod, sample.cov = covMat, sample.mean = means, sample.nobs = 321)
31 |
32 | # Look at fit:
33 | fitMeasures(fit)
34 |
35 | # Look at parameter estimates:
36 | parameterEstimates(fit)
37 |
38 | # Latent correlations:
39 | cov2cor(lavInspect(fit, "est")$psi)
40 |
41 | # Small hack to make nicer path diagram:
42 | library("semPlot")
43 | semPathsMod <- semPlotModel(fit)
44 | semPathsMod@Vars$exogenous[grepl("cig",semPathsMod@Vars$name)] <- TRUE
45 |
46 | # Plot diagram:
47 | semPaths(semPathsMod,
48 | intercepts = FALSE,
49 | curve = 0,
50 | nCharNodes = 0
51 | )
--------------------------------------------------------------------------------
/Latent_growth_examples/lavaan/LGC_lavaan_univariate.R:
--------------------------------------------------------------------------------
1 | library("dplyr")
2 | library("lavaan")
3 |
4 | # Read the data, subset of summary statistics reported by:
5 |
6 | # Duncan, S. C., & Duncan, T. E. (1996). A multivariate latent
7 | # growth curve analysis of adolescent substance use. Structural
8 | # Equation Modeling: A Multidisciplinary Journal, 3(4), 323-347.
9 |
10 | # Sample size: 321
11 |
12 | covMat <- as.matrix(read.csv("covmat_univariate.csv"))
13 | rownames(covMat) <- colnames(covMat)
14 | means <- read.csv("means_univariate.csv")[,1]
15 |
16 | # Lavaan growth model:
17 | mod <- '
18 | # Alcohol:
19 | i_alc =~ 1*alc1 + 1*alc2 + 1*alc3 + 1*alc4
20 | s_alc =~ 1*alc1 + 2*alc2 + 3*alc3 + 4*alc4
21 | '
22 |
23 | # Fit model:
24 | # If you have raw data you can ignore the argument sample.cov, sample.mean and sample.nobs and use
25 | # the argument "data" instead"
26 | fit <- growth(mod, sample.cov = covMat, sample.mean = means, sample.nobs = 321)
27 |
28 | # Look at fit:
29 | fitMeasures(fit)
30 |
31 | # Look at parameter estimates:
32 | parameterEstimates(fit)
33 |
34 | # Latent correlations:
35 | cov2cor(lavInspect(fit, "est")$psi)
36 |
37 | # Plot diagram:
38 | library("semPlot")
39 | semPaths(fit,
40 | fixedStyle = 1,
41 | intercepts = FALSE,
42 | nCharNodes = 0
43 | )
44 |
--------------------------------------------------------------------------------
/Latent_growth_examples/lavaan/covmat_bivariate.csv:
--------------------------------------------------------------------------------
1 | "alc1","alc2","alc3","alc4","cig1","cig2","cig3","cig4"
2 | 1.004,0.615761158891985,0.535582200869297,0.418415621773375,0.650160301218092,0.659412211279106,0.63485823368686,0.53281791213134
3 | 0.615761158891985,0.922,0.586815972516086,0.499881510232175,0.494706091745392,0.534327356020633,0.552961319343406,0.518512231740004
4 | 0.535582200869297,0.586815972516086,0.832,0.52100086559621,0.40637925143885,0.406967540720387,0.444557775772733,0.431142763102896
5 | 0.418415621773375,0.499881510232175,0.52100086559621,0.846,0.375110009823785,0.41037726252803,0.501368520900545,0.597788171338309
6 | 0.650160301218092,0.494706091745392,0.40637925143885,0.375110009823785,1.305,1.20342710622621,1.14722860996621,0.968481690978203
7 | 0.659412211279106,0.534327356020633,0.406967540720387,0.41037726252803,1.20342710622621,1.536,1.2954962724763,1.19545632721233
8 | 0.63485823368686,0.552961319343406,0.444557775772733,0.501368520900545,1.14722860996621,1.2954962724763,1.645,1.37284467590474
9 | 0.53281791213134,0.518512231740004,0.431142763102896,0.597788171338309,0.968481690978203,1.19545632721233,1.37284467590474,1.888
10 |
--------------------------------------------------------------------------------
/Latent_growth_examples/lavaan/covmat_univariate.csv:
--------------------------------------------------------------------------------
1 | "alc1","alc2","alc3","alc4"
2 | 1.004,0.615761158891985,0.535582200869297,0.418415621773375
3 | 0.615761158891985,0.922,0.586815972516086,0.499881510232175
4 | 0.535582200869297,0.586815972516086,0.832,0.52100086559621
5 | 0.418415621773375,0.499881510232175,0.52100086559621,0.846
6 |
--------------------------------------------------------------------------------
/Latent_growth_examples/lavaan/means_bivariate.csv:
--------------------------------------------------------------------------------
1 | "mean"
2 | 2.271
3 | 2.56
4 | 2.694
5 | 2.965
6 | 1.847
7 | 2.043
8 | 2.227
9 | 2.51
10 |
--------------------------------------------------------------------------------
/Latent_growth_examples/lavaan/means_univariate.csv:
--------------------------------------------------------------------------------
1 | "mean"
2 | 2.271
3 | 2.56
4 | 2.694
5 | 2.965
6 |
--------------------------------------------------------------------------------
/Latent_growth_examples/psychonetrics/LGC_psychonetrics_bivariate.R:
--------------------------------------------------------------------------------
1 | library("dplyr")
2 | library("devtools")
3 | library("psychonetrics")
4 |
5 | # Read the data, subset of summary statistics reported by:
6 |
7 | # Duncan, S. C., & Duncan, T. E. (1996). A multivariate latent
8 | # growth curve analysis of adolescent substance use. Structural
9 | # Equation Modeling: A Multidisciplinary Journal, 3(4), 323-347.
10 |
11 | # Sample size: 321
12 |
13 | covMat <- as.matrix(read.csv("covmat_bivariate.csv"))
14 | rownames(covMat) <- colnames(covMat)
15 | means <- read.csv("means_bivariate.csv")[,1]
16 |
17 | # Design matrix. This matrix controls the design of the latent grwoth model.
18 | # It must contain character strings with the names of your variables.
19 | # The rows indicate variables and the columns indicare time points.
20 | # E.g., here the first row indicates "alcohol", the columns indicate time points
21 | # 1, 2, 3 and 4, and the unique values of the first row indicate the names of the variables
22 | # indicating alcohol at time 1, 2, 3 and 4:
23 | vars <- matrix(colnames(covMat), nrow = 2, ncol = 4, byrow = TRUE)
24 | rownames(vars) <- c("alc","cig")
25 |
26 | # Form model (note I rescale the cov matrix here manually to max likelihood estimate):
27 | # If you have raw data you can ignore the argument covs, means and nobs and use
28 | # the argument "data" instead"
29 | mod <- latentgrowth(vars, covs = covMat, means = means, nobs = 321)
30 |
31 | # Run model:
32 | mod <- mod %>% runmodel
33 |
34 |
35 | mod %>% parameters
36 |
37 | # Look at fit:
38 | mod
39 | mod %>% fit
40 |
41 | # Look at parameters:
42 | mod %>% parameters
43 |
--------------------------------------------------------------------------------
/Latent_growth_examples/psychonetrics/LGC_psychonetrics_univariate.R:
--------------------------------------------------------------------------------
1 | library("dplyr")
2 | library("devtools")
3 | library("psychonetrics")
4 |
5 | #' Read the data, subset of summary statistics reported by:
6 |
7 | #' Duncan, S. C., & Duncan, T. E. (1996). A multivariate latent
8 | #' growth curve analysis of adolescent substance use. Structural
9 | #' Equation Modeling: A Multidisciplinary Journal, 3(4), 323-347.
10 |
11 | #' Sample size: 321
12 |
13 | covMat <- as.matrix(read.csv("covmat_univariate.csv"))
14 | rownames(covMat) <- colnames(covMat)
15 | means <- read.csv("means_univariate.csv")[,1]
16 |
17 | #' I have implemented a wrapper called 'latentgrowth' that automates the model specification
18 | #' This requires a Design matrix. This matrix controls the design of the latent growth model.
19 | #' It must contain character strings with the names of your variables.
20 | #' The rows indicate variables and the columns indicare time points.
21 | #' E.g., here the row indicates "alcohol", the columns indicate time points
22 | #' 1, 2, 3 and 4, and the unique values indicate the names of the variables
23 | #' indicating alcohol at time 1, 2, 3 and 4:
24 | vars <- matrix(colnames(covMat), nrow = 1, ncol = 4, byrow = TRUE)
25 | rownames(vars) <- "alc"
26 |
27 | #' If you have raw data you can ignore the argument covs, means and nobs and use
28 | #' the argument "data" instead". We can construct the model (note I rescale the cov matrix
29 | #' here manually to max likelihood estimate):
30 | mod <- latentgrowth(vars, covs = (321-1)/321 * covMat, means = means, nobs = 321)
31 |
32 | #' Run model:
33 | mod <- mod %>% runmodel
34 |
35 | #' Look at fit:
36 | mod
37 | mod %>% fit
38 |
39 | #' Look at parameters:
40 | mod %>% parameters
41 |
42 | #' Latent correlations:
43 | cov2cor(getmatrix(mod,"sigma_zeta"))
44 |
45 | #' We could also specify the model manually via lvm:
46 | mod_lvm <- lvm(covs = (321-1)/321 * covMat, means = means, nobs = 321,
47 | lambda = matrix(1,4,2))
48 |
49 | #' Fix intercept loadings:
50 | mod_lvm <- mod_lvm %>% fixpar("lambda",row=1:4,col=1,value = 1)
51 |
52 | #' Fix slope loadings:
53 | mod_lvm <- mod_lvm %>%
54 | fixpar("lambda",row=1,col=2,value = 1) %>%
55 | fixpar("lambda",row=2,col=2,value = 2) %>%
56 | fixpar("lambda",row=3,col=2,value = 3) %>%
57 | fixpar("lambda",row=4,col=2,value = 4)
58 |
59 | #' Fix intercepts:
60 | mod_lvm <- mod_lvm %>% fixpar("nu", row = 1:4, col = 1, value = 1)
61 |
62 | #' Free latent means:
63 | mod_lvm <- mod_lvm %>% freepar("nu_eta", row = 1:2, col = 1)
64 |
65 | #' Run the model:
66 | mod_lvm <- mod_lvm %>% runmodel
67 |
68 | #' This model is the same!
69 | compare(mod, mod_lvm)
70 |
--------------------------------------------------------------------------------
/Latent_growth_examples/psychonetrics/covmat_bivariate.csv:
--------------------------------------------------------------------------------
1 | "alc1","alc2","alc3","alc4","cig1","cig2","cig3","cig4"
2 | 1.004,0.615761158891985,0.535582200869297,0.418415621773375,0.650160301218092,0.659412211279106,0.63485823368686,0.53281791213134
3 | 0.615761158891985,0.922,0.586815972516086,0.499881510232175,0.494706091745392,0.534327356020633,0.552961319343406,0.518512231740004
4 | 0.535582200869297,0.586815972516086,0.832,0.52100086559621,0.40637925143885,0.406967540720387,0.444557775772733,0.431142763102896
5 | 0.418415621773375,0.499881510232175,0.52100086559621,0.846,0.375110009823785,0.41037726252803,0.501368520900545,0.597788171338309
6 | 0.650160301218092,0.494706091745392,0.40637925143885,0.375110009823785,1.305,1.20342710622621,1.14722860996621,0.968481690978203
7 | 0.659412211279106,0.534327356020633,0.406967540720387,0.41037726252803,1.20342710622621,1.536,1.2954962724763,1.19545632721233
8 | 0.63485823368686,0.552961319343406,0.444557775772733,0.501368520900545,1.14722860996621,1.2954962724763,1.645,1.37284467590474
9 | 0.53281791213134,0.518512231740004,0.431142763102896,0.597788171338309,0.968481690978203,1.19545632721233,1.37284467590474,1.888
10 |
--------------------------------------------------------------------------------
/Latent_growth_examples/psychonetrics/covmat_univariate.csv:
--------------------------------------------------------------------------------
1 | "alc1","alc2","alc3","alc4"
2 | 1.004,0.615761158891985,0.535582200869297,0.418415621773375
3 | 0.615761158891985,0.922,0.586815972516086,0.499881510232175
4 | 0.535582200869297,0.586815972516086,0.832,0.52100086559621
5 | 0.418415621773375,0.499881510232175,0.52100086559621,0.846
6 |
--------------------------------------------------------------------------------
/Latent_growth_examples/psychonetrics/means_bivariate.csv:
--------------------------------------------------------------------------------
1 | "mean"
2 | 2.271
3 | 2.56
4 | 2.694
5 | 2.965
6 | 1.847
7 | 2.043
8 | 2.227
9 | 2.51
10 |
--------------------------------------------------------------------------------
/Latent_growth_examples/psychonetrics/means_univariate.csv:
--------------------------------------------------------------------------------
1 | "mean"
2 | 2.271
3 | 2.56
4 | 2.694
5 | 2.965
6 |
--------------------------------------------------------------------------------
/Measurement_invariance_examples/Jasp/StarWars.csv:
--------------------------------------------------------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43,1
239 | 1,4,2,5,2,2,1,3,2,2,1,29,1
240 | 1,4,1,2,1,3,5,2,3,1,2,20,1
241 | 1,5,4,3,1,1,1,2,3,2,1,43,1
242 | 1,5,1,4,1,1,1,3,5,1,1,22,1
243 | 1,4,2,5,2,2,1,1,2,3,1,42,1
244 | 1,5,2,5,1,1,1,1,5,1,1,37,1
245 | 1,5,3,5,5,3,1,1,4,1,1,34,1
246 | 3,5,2,3,3,2,2,3,3,2,1,24,1
247 | 5,5,5,3,5,5,5,4,5,3,2,24,2
248 | 1,5,1,2,1,1,1,1,2,2,1,38,1
249 | 5,5,5,3,5,5,5,5,5,5,1,50,2
250 | 1,5,1,5,2,2,2,2,3,1,2,46,1
251 | 5,5,2,5,2,1,1,1,3,1,1,42,1
252 | 1,5,3,2,1,1,2,1,2,1,1,39,1
253 | 1,5,2,4,1,1,1,1,3,1,1,42,1
254 | 1,4,1,5,1,1,3,1,3,1,2,45,1
255 | 1,5,4,2,1,2,2,1,2,2,2,57,1
256 | 2,5,2,5,2,2,5,1,3,1,1,19,1
257 | 3,5,5,5,2,3,4,2,4,2,1,23,1
258 | 2,5,2,3,4,4,4,2,2,2,1,23,1
259 | 5,5,3,3,1,3,3,5,3,3,2,24,2
260 | 2,5,2,4,4,2,4,1,5,3,1,41,1
261 | 2,3,2,2,1,5,4,5,3,4,2,52,1
262 | 4,4,3,4,2,3,5,2,2,1,2,24,1
263 | 3,5,3,2,2,2,3,1,1,2,2,35,1
264 | 2,4,2,4,2,2,4,1,2,1,1,24,1
265 | 2,3,3,1,1,2,2,2,4,1,1,26,1
266 | 2,5,4,3,5,2,3,1,3,2,2,25,1
267 | 2,2,4,3,4,3,3,5,3,3,1,25,1
268 | 1,5,1,5,5,1,1,1,3,1,1,22,1
269 | 2,5,4,4,4,1,3,3,5,3,1,43,1
270 | 3,5,3,2,3,3,4,2,3,3,1,42,1
271 | 4,5,1,2,1,2,2,1,4,4,1,32,1
272 | 1,4,2,4,2,2,4,1,3,2,1,34,1
273 |
--------------------------------------------------------------------------------
/Measurement_invariance_examples/Jasp/StarWars_Jasp.jasp:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/SachaEpskamp/SEM-code-examples/1fc4500c21c8df785101d6ca0832efb9aef45f95/Measurement_invariance_examples/Jasp/StarWars_Jasp.jasp
--------------------------------------------------------------------------------
/Measurement_invariance_examples/Jasp/readme.txt:
--------------------------------------------------------------------------------
1 | These instructions use Jasp version 0.9.2
2 |
3 | Step 1: Open StarWars.csv into Jasp, make all variables ordinal if needed
4 |
5 | Step 2: Press the "+" right of the variable labels (not the gray + on the very top right)
6 |
7 | Step 3a: Create a nominal variable called "agegroup"
8 | Step 3b: Click "Create column"
9 | Step 3c: Enter Q12 < 30 in the field and press compute column
10 | Step 3d: Click the column and change labels
11 |
12 | Step 4: Click the "+" in the top and select SEM
13 |
14 | Step 5: Click lavaan
15 |
16 | Step 6: Enter the model:
17 |
18 | Prequels =~ Q2 + Q3 + Q4 + Q1
19 | Original =~ Q5 + Q6 + Q7 + Q1
20 | Sequels =~ Q8 + Q9 + Q10 + Q1
21 | Q4 ~~ Q10
22 |
23 | Step 7: Under options, select:
24 | 1. Grouping Variable: (agegroup)
25 | 2. "include mean structure"
26 |
27 | Step 8: Click the model and press command - enter
28 | - If you get an error, make sure you made all variables scale, remove the model and try again
29 |
30 | - You now estimated the configural invariance model!
31 |
32 | Step 9: Go to Advanced, select model 2 under "Model name"
33 |
34 | Step 10: Go to Options, select Loadings under "Equality Constrains"
35 |
36 | - You now estimated the weak invariance model!
37 |
38 | Unfortunately, Jasp does not currently allow for partial invariance settings. So if strong
39 | invariance is violated we cannot currently estimate a partial invariance model.
40 |
41 | Repeat steps 9 - 10 for further invariance and homogeneity tests!
--------------------------------------------------------------------------------
/Measurement_invariance_examples/StarWars_questionaire.pdf:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/SachaEpskamp/SEM-code-examples/1fc4500c21c8df785101d6ca0832efb9aef45f95/Measurement_invariance_examples/StarWars_questionaire.pdf
--------------------------------------------------------------------------------
/Measurement_invariance_examples/lavaan/StarWars.csv:
--------------------------------------------------------------------------------
1 | "Q1","Q2","Q3","Q4","Q5","Q6","Q7","Q8","Q9","Q10","Q11","Q12","Q13"
2 | 2,1,1,4,4,2,4,4,5,2,1,25,1
3 | 2,4,2,4,1,2,3,1,3,1,2,23,1
4 | 1,2,4,4,1,1,1,1,4,1,2,24,1
5 | 2,3,2,3,2,1,3,1,3,1,2,26,1
6 | 4,5,3,4,2,3,4,1,5,3,1,23,1
7 | 5,4,3,3,2,3,5,2,3,3,2,23,2
8 | 2,2,2,2,1,4,4,1,3,2,2,21,1
9 | 3,5,1,2,2,2,4,1,3,2,2,22,1
10 | 1,1,2,5,1,3,3,1,2,1,1,23,1
11 | 1,4,4,2,2,3,1,1,4,1,2,23,1
12 | 2,4,1,5,2,4,4,1,4,1,1,22,1
13 | 3,5,2,5,2,2,4,5,2,4,1,29,1
14 | 2,5,5,4,2,1,2,4,4,1,2,22,1
15 | 5,5,3,1,2,2,5,1,3,1,2,24,1
16 | 1,4,2,5,1,2,1,1,4,2,2,24,1
17 | 1,3,1,4,1,2,3,1,4,3,1,24,1
18 | 2,2,2,5,4,3,3,4,2,2,1,23,1
19 | 3,4,2,2,2,1,2,2,4,3,2,19,1
20 | 3,2,1,3,1,3,3,4,4,3,2,26,1
21 | 2,5,3,3,3,4,3,2,4,1,1,23,1
22 | 2,3,2,5,1,1,2,3,3,1,2,23,1
23 | 4,3,3,4,3,3,4,4,3,1,2,22,2
24 | 1,2,2,3,2,2,2,2,3,1,1,43,1
25 | 5,5,3,4,2,2,4,3,3,2,2,25,2
26 | 5,5,2,4,3,2,4,2,3,2,2,25,1
27 | 1,1,2,1,2,2,2,1,4,3,1,25,1
28 | 2,2,4,1,1,1,1,1,5,1,1,23,1
29 | 4,4,3,2,5,3,4,2,3,2,2,25,1
30 | 1,3,1,2,4,2,4,3,4,2,1,21,1
31 | 1,5,2,5,2,1,2,3,3,1,1,25,1
32 | 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1,31,1
203 | 4,5,3,3,2,3,2,4,3,3,1,45,1
204 | 2,4,1,4,1,2,4,3,3,1,2,23,1
205 | 2,5,4,2,2,2,2,2,3,1,2,51,1
206 | 1,5,1,3,1,1,3,3,3,3,1,46,1
207 | 5,4,3,4,2,3,4,5,3,3,2,20,2
208 | 5,5,3,2,3,3,3,3,3,2,1,56,2
209 | 5,5,5,2,3,5,4,5,5,5,2,55,2
210 | 1,5,3,2,3,2,3,2,5,2,1,21,1
211 | 1,4,2,3,4,2,4,4,2,2,2,45,1
212 | 2,4,3,5,2,4,5,2,3,1,1,21,1
213 | 2,4,4,4,3,4,4,2,5,2,2,23,1
214 | 2,5,2,1,2,2,1,1,1,1,1,27,1
215 | 1,5,3,4,1,1,2,1,2,3,2,53,1
216 | 1,5,2,4,3,1,1,1,1,2,1,37,1
217 | 3,2,3,4,3,4,2,3,4,2,2,23,1
218 | 3,5,5,3,5,3,3,5,3,3,2,59,2
219 | 1,5,3,3,5,3,3,1,3,3,1,26,1
220 | 2,5,2,3,2,3,3,1,5,3,2,18,1
221 | 1,3,1,5,2,1,1,2,3,1,1,63,1
222 | 5,3,3,4,2,5,5,5,3,3,2,23,2
223 | 4,3,3,2,2,3,3,2,2,2,2,22,1
224 | 3,5,2,4,2,2,2,1,2,1,2,27,1
225 | 3,3,3,3,4,2,4,5,3,1,2,23,2
226 | 5,5,3,3,5,3,3,5,3,5,2,29,2
227 | 2,4,2,5,2,3,2,2,3,1,2,22,1
228 | 5,3,3,2,3,3,5,3,3,2,1,32,2
229 | 5,5,5,4,3,3,4,3,3,1,2,25,2
230 | 3,3,3,3,3,3,3,3,3,3,2,24,2
231 | 4,5,3,2,5,3,4,3,3,2,1,21,2
232 | 5,5,3,2,5,5,5,5,3,2,1,27,2
233 | 4,5,3,4,3,3,3,3,3,5,2,20,2
234 | 1,5,2,2,1,1,1,3,4,2,1,49,1
235 | 1,5,1,5,2,1,1,2,3,1,1,44,1
236 | 1,5,3,2,2,1,1,1,4,2,1,19,1
237 | 1,5,5,2,1,1,2,1,3,1,2,44,1
238 | 1,1,4,5,2,2,2,2,2,1,1,43,1
239 | 1,4,2,5,2,2,1,3,2,2,1,29,1
240 | 1,4,1,2,1,3,5,2,3,1,2,20,1
241 | 1,5,4,3,1,1,1,2,3,2,1,43,1
242 | 1,5,1,4,1,1,1,3,5,1,1,22,1
243 | 1,4,2,5,2,2,1,1,2,3,1,42,1
244 | 1,5,2,5,1,1,1,1,5,1,1,37,1
245 | 1,5,3,5,5,3,1,1,4,1,1,34,1
246 | 3,5,2,3,3,2,2,3,3,2,1,24,1
247 | 5,5,5,3,5,5,5,4,5,3,2,24,2
248 | 1,5,1,2,1,1,1,1,2,2,1,38,1
249 | 5,5,5,3,5,5,5,5,5,5,1,50,2
250 | 1,5,1,5,2,2,2,2,3,1,2,46,1
251 | 5,5,2,5,2,1,1,1,3,1,1,42,1
252 | 1,5,3,2,1,1,2,1,2,1,1,39,1
253 | 1,5,2,4,1,1,1,1,3,1,1,42,1
254 | 1,4,1,5,1,1,3,1,3,1,2,45,1
255 | 1,5,4,2,1,2,2,1,2,2,2,57,1
256 | 2,5,2,5,2,2,5,1,3,1,1,19,1
257 | 3,5,5,5,2,3,4,2,4,2,1,23,1
258 | 2,5,2,3,4,4,4,2,2,2,1,23,1
259 | 5,5,3,3,1,3,3,5,3,3,2,24,2
260 | 2,5,2,4,4,2,4,1,5,3,1,41,1
261 | 2,3,2,2,1,5,4,5,3,4,2,52,1
262 | 4,4,3,4,2,3,5,2,2,1,2,24,1
263 | 3,5,3,2,2,2,3,1,1,2,2,35,1
264 | 2,4,2,4,2,2,4,1,2,1,1,24,1
265 | 2,3,3,1,1,2,2,2,4,1,1,26,1
266 | 2,5,4,3,5,2,3,1,3,2,2,25,1
267 | 2,2,4,3,4,3,3,5,3,3,1,25,1
268 | 1,5,1,5,5,1,1,1,3,1,1,22,1
269 | 2,5,4,4,4,1,3,3,5,3,1,43,1
270 | 3,5,3,2,3,3,4,2,3,3,1,42,1
271 | 4,5,1,2,1,2,2,1,4,4,1,32,1
272 | 1,4,2,4,2,2,4,1,3,2,1,34,1
273 |
--------------------------------------------------------------------------------
/Measurement_invariance_examples/lavaan/StarWars_lavaan.R:
--------------------------------------------------------------------------------
1 | #' Load packages:
2 | library("dplyr")
3 | library("lavaan")
4 |
5 | #' Read the data:
6 | Data <- read.csv("StarWars.csv", sep = ",")
7 |
8 | #' This data encodes the following variables:
9 | #'
10 | #' - Q1: I am a huge Star Wars fan! (star what?)
11 | #' - Q2: I would trust this person with my democracy.
12 | #' - Q3: I enjoyed the story of Anakin's early life.
13 | #' - Q4: The special effects in this scene are awful (Battle of Geonosis).
14 | #' - Q5: I would trust this person with my life.
15 | #' - Q6: I found Darth Vader'ss big reveal in "Empire" one of the greatest moments in movie history.
16 | #' - Q7: The special effects in this scene are amazing (Death Star Explosion).
17 | #' - Q8: If possible, I would definitely buy this droid.
18 | #' - Q9: The story in the Star Wars sequels is an improvement to the previous movies.
19 | #' - Q10: The special effects in this scene are marvellous (Starkiller Base Firing).
20 | #' - Q11: What is your gender?
21 | #' - Q12: How old are you?
22 | #' - Q13: Have you seen any of the Star Wars movies?
23 |
24 | #' Let's say we are interested in seeing if people >= 30 like the original trilogy better than people < 30.
25 | #' First we can make a grouping variable:
26 | Data$agegroup <- ifelse(Data$Q12 < 30, "young", "less young")
27 |
28 | #' Let's look at the distribution:
29 | table(Data$agegroup) #' Pretty even...
30 |
31 | #' Test for configural invariance:
32 | Model <- '
33 | Prequels =~ Q2 + Q3 + Q4 + Q1
34 | Original =~ Q5 + Q6 + Q7 + Q1
35 | Sequels =~ Q8 + Q9 + Q10 + Q1
36 | Q4 ~~ Q10
37 | '
38 | conf <- cfa(Model, Data, group = "agegroup", std.lv=TRUE)
39 |
40 | #' Look at fit:
41 | conf #' P > 0.05 so we do not reject exact fit!
42 | fitMeasures(conf) #' Looks good!
43 |
44 | #' Test for weak invariance:
45 | weak <- cfa(Model, Data, group = "agegroup",
46 | group.equal = "loadings", std.lv=TRUE)
47 |
48 | #' Compare groups:
49 | anova(conf,weak) #' Weak variance is accepted
50 |
51 | #' Test for strong invariance:
52 | strong <- cfa(Model, Data, group = "agegroup",
53 | group.equal = c("loadings","intercepts"), std.lv=TRUE)
54 |
55 | #' Compare groups:
56 | anova(conf,weak, strong) #' Questionable...
57 |
58 | #' What do the residuals tell us?
59 | residuals(strong) #' Some misfit in Q8 and Q10 in both groups
60 |
61 | #' And the lagrange multiplier tests?
62 | lavTestScore(strong)$uni %>% arrange(-X2)
63 |
64 | #' Tells me .p37 and .p39., which parameters are those:
65 |
66 | parameterEstimates(strong)[37:39,]
67 | #' Intercepts for Q8 and Q10, Q10 is the highest
68 |
69 | #' Lets' try freeing up intercept for Q10 on special effects (this is also reasonable, it could be that older
70 | #' people judge special effects on different criteria than younger people).
71 | strong_partial <- cfa(Model, Data, group = "agegroup",
72 | group.equal = c("loadings","intercepts"),
73 | group.partial = "Q10 ~ 1", std.lv=TRUE)
74 |
75 | #' Compare:
76 | anova(conf,weak, strong_partial)
77 | #' The partial strong invariance model can be accepted
78 |
79 | #' Test for strict invariance:
80 | strict <- cfa(Model, Data, group = "agegroup",
81 | group.equal = c("loadings","intercepts","residuals",
82 | "residual.covariances"),
83 | group.partial = "Q10 ~ 1", std.lv=TRUE)
84 |
85 | #' Compare:
86 | anova(conf,weak, strong_partial, strict)
87 | #' Strict invariance can be accepted!
88 |
89 | #' ### Now we can test for homogeneity!
90 | #' Are the variances and covariances equal?
91 | eqvars <- cfa(Model, Data, group = "agegroup",
92 | group.equal = c("loadings","intercepts","residuals",
93 | "residual.covariances",
94 | "lv.variances","lv.covariances"),
95 | group.partial = "Q10 ~ 1", std.lv=TRUE)
96 |
97 |
98 | #' Compare:
99 | anova(strict, eqvars)
100 |
101 | #' This is acceptable. What about the means?
102 | eqvars_and_means <- cfa(Model, Data, group = "agegroup",
103 | group.equal = c("loadings","intercepts","residuals",
104 | "residual.covariances",
105 | "lv.variances","lv.covariances",
106 | "means"),
107 | group.partial = "Q10 ~ 1", std.lv=TRUE)
108 |
109 | #' Compare:
110 | anova(strict, eqvars, eqvars_and_means)
111 |
112 | #' Homogenous means can be rejected! Let's look at the means:
113 | parameterEstimates(eqvars)[82:84, ] #' I looked before and saw rows 82-84 are the ones of interest
114 |
115 | #' Interestingly, People >= 30 seem to like the prequels more and the sequels less than people < 30!
--------------------------------------------------------------------------------
/Measurement_invariance_examples/psychonetrics/StarWars.csv:
--------------------------------------------------------------------------------
1 | "Q1","Q2","Q3","Q4","Q5","Q6","Q7","Q8","Q9","Q10","Q11","Q12","Q13"
2 | 2,1,1,4,4,2,4,4,5,2,1,25,1
3 | 2,4,2,4,1,2,3,1,3,1,2,23,1
4 | 1,2,4,4,1,1,1,1,4,1,2,24,1
5 | 2,3,2,3,2,1,3,1,3,1,2,26,1
6 | 4,5,3,4,2,3,4,1,5,3,1,23,1
7 | 5,4,3,3,2,3,5,2,3,3,2,23,2
8 | 2,2,2,2,1,4,4,1,3,2,2,21,1
9 | 3,5,1,2,2,2,4,1,3,2,2,22,1
10 | 1,1,2,5,1,3,3,1,2,1,1,23,1
11 | 1,4,4,2,2,3,1,1,4,1,2,23,1
12 | 2,4,1,5,2,4,4,1,4,1,1,22,1
13 | 3,5,2,5,2,2,4,5,2,4,1,29,1
14 | 2,5,5,4,2,1,2,4,4,1,2,22,1
15 | 5,5,3,1,2,2,5,1,3,1,2,24,1
16 | 1,4,2,5,1,2,1,1,4,2,2,24,1
17 | 1,3,1,4,1,2,3,1,4,3,1,24,1
18 | 2,2,2,5,4,3,3,4,2,2,1,23,1
19 | 3,4,2,2,2,1,2,2,4,3,2,19,1
20 | 3,2,1,3,1,3,3,4,4,3,2,26,1
21 | 2,5,3,3,3,4,3,2,4,1,1,23,1
22 | 2,3,2,5,1,1,2,3,3,1,2,23,1
23 | 4,3,3,4,3,3,4,4,3,1,2,22,2
24 | 1,2,2,3,2,2,2,2,3,1,1,43,1
25 | 5,5,3,4,2,2,4,3,3,2,2,25,2
26 | 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2,28,2
197 | 5,4,2,1,4,5,4,5,2,4,2,25,1
198 | 5,5,3,3,5,5,3,2,3,3,2,32,2
199 | 5,5,3,4,5,5,4,3,3,4,2,28,1
200 | 5,3,3,4,3,5,3,2,3,2,2,54,1
201 | 1,5,3,2,1,1,2,2,2,1,2,44,1
202 | 5,5,2,4,5,5,3,1,5,1,1,31,1
203 | 4,5,3,3,2,3,2,4,3,3,1,45,1
204 | 2,4,1,4,1,2,4,3,3,1,2,23,1
205 | 2,5,4,2,2,2,2,2,3,1,2,51,1
206 | 1,5,1,3,1,1,3,3,3,3,1,46,1
207 | 5,4,3,4,2,3,4,5,3,3,2,20,2
208 | 5,5,3,2,3,3,3,3,3,2,1,56,2
209 | 5,5,5,2,3,5,4,5,5,5,2,55,2
210 | 1,5,3,2,3,2,3,2,5,2,1,21,1
211 | 1,4,2,3,4,2,4,4,2,2,2,45,1
212 | 2,4,3,5,2,4,5,2,3,1,1,21,1
213 | 2,4,4,4,3,4,4,2,5,2,2,23,1
214 | 2,5,2,1,2,2,1,1,1,1,1,27,1
215 | 1,5,3,4,1,1,2,1,2,3,2,53,1
216 | 1,5,2,4,3,1,1,1,1,2,1,37,1
217 | 3,2,3,4,3,4,2,3,4,2,2,23,1
218 | 3,5,5,3,5,3,3,5,3,3,2,59,2
219 | 1,5,3,3,5,3,3,1,3,3,1,26,1
220 | 2,5,2,3,2,3,3,1,5,3,2,18,1
221 | 1,3,1,5,2,1,1,2,3,1,1,63,1
222 | 5,3,3,4,2,5,5,5,3,3,2,23,2
223 | 4,3,3,2,2,3,3,2,2,2,2,22,1
224 | 3,5,2,4,2,2,2,1,2,1,2,27,1
225 | 3,3,3,3,4,2,4,5,3,1,2,23,2
226 | 5,5,3,3,5,3,3,5,3,5,2,29,2
227 | 2,4,2,5,2,3,2,2,3,1,2,22,1
228 | 5,3,3,2,3,3,5,3,3,2,1,32,2
229 | 5,5,5,4,3,3,4,3,3,1,2,25,2
230 | 3,3,3,3,3,3,3,3,3,3,2,24,2
231 | 4,5,3,2,5,3,4,3,3,2,1,21,2
232 | 5,5,3,2,5,5,5,5,3,2,1,27,2
233 | 4,5,3,4,3,3,3,3,3,5,2,20,2
234 | 1,5,2,2,1,1,1,3,4,2,1,49,1
235 | 1,5,1,5,2,1,1,2,3,1,1,44,1
236 | 1,5,3,2,2,1,1,1,4,2,1,19,1
237 | 1,5,5,2,1,1,2,1,3,1,2,44,1
238 | 1,1,4,5,2,2,2,2,2,1,1,43,1
239 | 1,4,2,5,2,2,1,3,2,2,1,29,1
240 | 1,4,1,2,1,3,5,2,3,1,2,20,1
241 | 1,5,4,3,1,1,1,2,3,2,1,43,1
242 | 1,5,1,4,1,1,1,3,5,1,1,22,1
243 | 1,4,2,5,2,2,1,1,2,3,1,42,1
244 | 1,5,2,5,1,1,1,1,5,1,1,37,1
245 | 1,5,3,5,5,3,1,1,4,1,1,34,1
246 | 3,5,2,3,3,2,2,3,3,2,1,24,1
247 | 5,5,5,3,5,5,5,4,5,3,2,24,2
248 | 1,5,1,2,1,1,1,1,2,2,1,38,1
249 | 5,5,5,3,5,5,5,5,5,5,1,50,2
250 | 1,5,1,5,2,2,2,2,3,1,2,46,1
251 | 5,5,2,5,2,1,1,1,3,1,1,42,1
252 | 1,5,3,2,1,1,2,1,2,1,1,39,1
253 | 1,5,2,4,1,1,1,1,3,1,1,42,1
254 | 1,4,1,5,1,1,3,1,3,1,2,45,1
255 | 1,5,4,2,1,2,2,1,2,2,2,57,1
256 | 2,5,2,5,2,2,5,1,3,1,1,19,1
257 | 3,5,5,5,2,3,4,2,4,2,1,23,1
258 | 2,5,2,3,4,4,4,2,2,2,1,23,1
259 | 5,5,3,3,1,3,3,5,3,3,2,24,2
260 | 2,5,2,4,4,2,4,1,5,3,1,41,1
261 | 2,3,2,2,1,5,4,5,3,4,2,52,1
262 | 4,4,3,4,2,3,5,2,2,1,2,24,1
263 | 3,5,3,2,2,2,3,1,1,2,2,35,1
264 | 2,4,2,4,2,2,4,1,2,1,1,24,1
265 | 2,3,3,1,1,2,2,2,4,1,1,26,1
266 | 2,5,4,3,5,2,3,1,3,2,2,25,1
267 | 2,2,4,3,4,3,3,5,3,3,1,25,1
268 | 1,5,1,5,5,1,1,1,3,1,1,22,1
269 | 2,5,4,4,4,1,3,3,5,3,1,43,1
270 | 3,5,3,2,3,3,4,2,3,3,1,42,1
271 | 4,5,1,2,1,2,2,1,4,4,1,32,1
272 | 1,4,2,4,2,2,4,1,3,2,1,34,1
273 |
--------------------------------------------------------------------------------
/Measurement_invariance_examples/psychonetrics/StarWars_psychonetrics.R:
--------------------------------------------------------------------------------
1 | #' Load packages:
2 | library("dplyr")
3 | library("psychonetrics")
4 |
5 | #' Read the data:
6 | Data <- read.csv("StarWars.csv", sep = ",")
7 |
8 | #' This data encodes the following variables:
9 | #'
10 | #' - Q1: I am a huge Star Wars fan! (star what?)
11 | #' - Q2: I would trust this person with my democracy.
12 | #' - Q3: I enjoyed the story of Anakin's early life.
13 | #' - Q4: The special effects in this scene are awful (Battle of Geonosis).
14 | #' - Q5: I would trust this person with my life.
15 | #' - Q6: I found Darth Vader'ss big reveal in "Empire" one of the greatest moments in movie history.
16 | #' - Q7: The special effects in this scene are amazing (Death Star Explosion).
17 | #' - Q8: If possible, I would definitely buy this droid.
18 | #' - Q9: The story in the Star Wars sequels is an improvement to the previous movies.
19 | #' - Q10: The special effects in this scene are marvellous (Starkiller Base Firing).
20 | #' - Q11: What is your gender?
21 | #' - Q12: How old are you?
22 | #' - Q13: Have you seen any of the Star Wars movies?
23 | #'
24 | #' Let's say we are interested in seeing if people >= 30 like the original trilogy better than people < 30.
25 | #' First we can make a grouping variable:
26 | Data$agegroup <- ifelse(Data$Q12 < 30, "young", "less young")
27 |
28 | #' Let's look at the distribution:
29 | table(Data$agegroup) #' Pretty even...
30 |
31 | #' Observed variables:
32 | obsvars <- paste0("Q",1:10)
33 |
34 | #' Let's look at the mean scores:
35 | Data %>% group_by(agegroup) %>% summarize_each_(funs(mean),vars = obsvars)
36 | #' Less young people seem to score higher on prequel questions and lower on other questions
37 |
38 | #' Factor loadings matrix:
39 | Lambda <- matrix(0, 10, 3)
40 | Lambda[1:4,1] <- 1
41 | Lambda[c(1,5:7),2] <- 1
42 | Lambda[c(1,8:10),3] <- 1
43 |
44 | #' Residual covariances:
45 | Theta <- diag(1, 10)
46 | Theta[4,10] <- Theta[10,4] <- 1
47 |
48 | #' Latents:
49 | latents <- c("Prequels","Original","Sequels")
50 |
51 | #' Make model:
52 | mod_configural <- lvm(Data, lambda = Lambda, vars = obsvars,
53 | latents = latents, sigma_epsilon = Theta,
54 | identification = "variance",
55 | groups = "agegroup")
56 |
57 | #' Run model:
58 | mod_configural <- mod_configural %>% runmodel
59 |
60 | #' Look at fit:
61 | mod_configural
62 | mod_configural %>% fit
63 |
64 | #' Looks good, let's try weak invariance:
65 | mod_weak <- mod_configural %>% groupequal("lambda") %>% runmodel
66 |
67 | #' Compare models:
68 | compare(configural = mod_configural, weak = mod_weak)
69 |
70 | #' weak invariance can be accepted, let's try strong:
71 | mod_strong <- mod_weak %>% groupequal("nu") %>% runmodel
72 | #' Means are automatically identified
73 |
74 | #' Compare models:
75 | compare(configural = mod_configural, weak = mod_weak, strong = mod_strong)
76 |
77 | #' Questionable p-value and AIC difference, but ok BIC difference. This is quite good, but let's take a look.
78 | #' I have not yet implemented LM tests for equality constrains, but we can loko at something called "equality-free" MIs:
79 | mod_strong %>% MIs(matrices = "nu", type = "free")
80 |
81 | #' Indicates that Q10 would improve fit. We can also look at residuals:
82 | residuals(mod_strong)
83 |
84 | #' Let's try freeing intercept 10:
85 | mod_strong_partial <- mod_strong %>% groupfree("nu",10) %>% runmodel
86 |
87 | #' Compare all models:
88 | compare(configural = mod_configural,
89 | weak = mod_weak,
90 | strong = mod_strong,
91 | strong_partial = mod_strong_partial)
92 |
93 | #' This seems worth it and lead to an acceptable model! It seems that older people find the latest special effects more marvellous!
94 | mod_strong_partial %>% getmatrix("nu")
95 |
96 | #' Now let's investigate strict invariance:
97 | mod_strict <- mod_strong_partial %>% groupequal("sigma_epsilon") %>% runmodel
98 |
99 | #' Compare all models:
100 | compare(configural = mod_configural,
101 | weak = mod_weak,
102 | strong_partial = mod_strong_partial,
103 | strict = mod_strict)
104 | #' Strict invariance can be accepted!
105 |
106 | #' Now we can test for homogeneity!
107 | #' Are the latent variances equal?
108 | mod_eqvar <- mod_strict %>% groupequal("sigma_zeta") %>% runmodel
109 |
110 | #' Compare:
111 | compare(strict = mod_strict, eqvar = mod_eqvar)
112 |
113 | #' This is acceptable. What about the means? (alpha = nu_eta)
114 | mod_eqmeans <- mod_eqvar %>% groupequal("nu_eta") %>% runmodel
115 |
116 | #' Compare:
117 | compare(eqvar = mod_eqvar, eqmeans = mod_eqmeans)
118 |
119 | #' Rejected! We could look at MIs again:
120 | mod_eqmeans %>% MIs(matrices = "nu_eta", type = "free")
121 |
122 | #' Indicates the strongest effect for prequels. Let's see what happens:
123 | eqmeans2 <- mod_eqvar %>% groupequal("nu_eta",row = c("Original","Sequels")) %>% runmodel
124 |
125 | #' Compare:
126 | compare(eqvar = mod_eqvar, eqmeans = eqmeans2)
127 | #' Questionable, what about the sequels as well?
128 |
129 | eqmeans3 <- mod_eqvar %>% groupequal("nu_eta", row = "Original") %>% runmodel
130 |
131 | #' Compare:
132 | compare(eqvar = mod_eqvar, eqmeans = eqmeans3)
133 |
134 | #' Still questionable.. Let's look at the mean differences:
135 | mod_eqvar %>% getmatrix("nu_eta")
136 |
137 | #' Looks like people over 30 like the prequels better and the other two trilogies less!
--------------------------------------------------------------------------------
/README.md:
--------------------------------------------------------------------------------
1 | On this page I will collect some code to facillitate SEM analysis and visualization.
2 |
3 | The Star Wars dataset on this website was collected by Carolin Katzera, Charlotte Tanis, Esther Niehoff, Myrthe Veenman, & Jason Nak.
4 |
--------------------------------------------------------------------------------
/SEM_fit_examples/lavaan_rawdata.R:
--------------------------------------------------------------------------------
1 | library("lavaan")
2 |
3 | # In this example, I will use the default dataset used in the lavaan documentation at ?sem,
4 | # the ``industrialization and Political Democracy Example '' by Bollen (1989), page 332:
5 | # The data is available in lavaan as:
6 | PoliticalDemocracy
7 | # More information on the data is available in ?PoliticalDemocracy.
8 |
9 | # This script fits the model using the RAW data, see lavaan_sumstat.R for an example
10 | # using summary statistics (covariance matrix)
11 |
12 |
13 | # Model 1: standard SEM model without equality constraints and residual covariances:
14 | model1 <- '
15 | # latent variable definitions
16 | dem60 =~ y1 + y2 + y3 + y4
17 | dem65 =~ y5 + y6 + y7 + y8
18 | ind60 =~ x1 + x2 + x3
19 |
20 | # regressions
21 | dem60 ~ ind60
22 | dem65 ~ ind60 + dem60
23 | '
24 |
25 | # Fit in Lavaan:
26 | fit1 <- sem(model1, data=PoliticalDemocracy)
27 |
28 | # We can look at the fit in the same way we did before!
29 | fitMeasures(fit1)
30 |
31 | # Model 2: SEM model with added residual covariances (model 1 is nested in this model):
32 | model2 <- '
33 | # latent variable definitions
34 | ind60 =~ x1 + x2 + x3
35 | dem60 =~ y1 + y2 + y3 + y4
36 | dem65 =~ y5 + y6 + y7 + y8
37 |
38 | # regressions
39 | dem60 ~ ind60
40 | dem65 ~ ind60 + dem60
41 |
42 | # residual correlations
43 | y1 ~~ y5
44 | y2 ~~ y4 + y6
45 | y3 ~~ y7
46 | y4 ~~ y8
47 | y6 ~~ y8
48 | '
49 |
50 | # Fit in Lavaan:
51 | fit2 <- sem(model2, data=PoliticalDemocracy)
52 |
53 | # Compare fit:
54 | anova(fit1,fit2) # Fit is better!
55 |
56 | # Model 3: SEM model with added residual covariances AND equality constrains (nested in model 2):
57 | model3 <- '
58 | # latent variable definitions
59 | ind60 =~ x1 + x2 + x3
60 | dem60 =~ y1 + a*y2 + b*y3 + c*y4
61 | dem65 =~ y5 + a*y6 + b*y7 + c*y8
62 |
63 | # regressions
64 | dem60 ~ ind60
65 | dem65 ~ ind60 + dem60
66 |
67 | # residual correlations
68 | y1 ~~ y5
69 | y2 ~~ y4 + y6
70 | y3 ~~ y7
71 | y4 ~~ y8
72 | y6 ~~ y8
73 | '
74 |
75 | # Fit in Lavaan:
76 | fit3 <- sem(model3, data=PoliticalDemocracy)
77 |
78 | # Compare fit:
79 | anova(fit2, fit3) # Fit is better!
--------------------------------------------------------------------------------
/SEM_fit_examples/lavaan_sumstat.R:
--------------------------------------------------------------------------------
1 | library("lavaan")
2 |
3 | # In this example, I will use the default dataset used in the lavaan documentation at ?sem,
4 | # the ``industrialization and Political Democracy Example '' by Bollen (1989), page 332:
5 | # The data is available in lavaan as:
6 | PoliticalDemocracy
7 | # More information on the data is available in ?PoliticalDemocracy.
8 |
9 | # This script fits the model using summary statistics (covariance matrix) see lavaan_rawdata.R
10 | # for an example using the raw data
11 |
12 | # Cov matrix:
13 | S <- cov(PoliticalDemocracy[,c("y1","y2","y3","y4","y5","y6","y7","y8","x1","x2","x3")])
14 |
15 | # Sample size:
16 | n <- nrow(PoliticalDemocracy)
17 |
18 | # Model 1: standard SEM model without equality constraints and residual covariances:
19 | model1 <- '
20 | # latent variable definitions
21 | ind60 =~ x1 + x2 + x3
22 | dem60 =~ y1 + y2 + y3 + y4
23 | dem65 =~ y5 + y6 + y7 + y8
24 |
25 | # regressions
26 | dem60 ~ ind60
27 | dem65 ~ ind60 + dem60
28 | '
29 |
30 | # Fit in Lavaan:
31 | fit1 <- sem(model1, sample.cov = S, sample.nobs = n)
32 |
33 | # We can look at the fit in the same way we did before!
34 | fitMeasures(fit1)
35 |
36 | # Model 2: SEM model with added residual covariances (model 1 is nested in this model):
37 | model2 <- '
38 | # latent variable definitions
39 | ind60 =~ x1 + x2 + x3
40 | dem60 =~ y1 + y2 + y3 + y4
41 | dem65 =~ y5 + y6 + y7 + y8
42 |
43 | # regressions
44 | dem60 ~ ind60
45 | dem65 ~ ind60 + dem60
46 |
47 | # residual correlations
48 | y1 ~~ y5
49 | y2 ~~ y4 + y6
50 | y3 ~~ y7
51 | y4 ~~ y8
52 | y6 ~~ y8
53 | '
54 |
55 | # Fit in Lavaan:
56 | fit2 <- sem(model2, sample.cov = S, sample.nobs = n)
57 |
58 | # Compare fit:
59 | anova(fit1,fit2) # Fit is better!
60 |
61 | # Model 3: SEM model with added residual covariances AND equality constrains (nested in model 2):
62 | model3 <- '
63 | # latent variable definitions
64 | ind60 =~ x1 + x2 + x3
65 | dem60 =~ y1 + a*y2 + b*y3 + c*y4
66 | dem65 =~ y5 + a*y6 + b*y7 + c*y8
67 |
68 | # regressions
69 | dem60 ~ ind60
70 | dem65 ~ ind60 + dem60
71 |
72 | # residual correlations
73 | y1 ~~ y5
74 | y2 ~~ y4 + y6
75 | y3 ~~ y7
76 | y4 ~~ y8
77 | y6 ~~ y8
78 | '
79 |
80 | # Fit in Lavaan:
81 | fit3 <- sem(model3, sample.cov = S, sample.nobs = n)
82 |
83 | # Compare fit:
84 | anova(fit2, fit3) # Fit is better!
--------------------------------------------------------------------------------
/SEM_fit_examples/psychonetrics_rawdata.R:
--------------------------------------------------------------------------------
1 | library("psychonetrics")
2 | library("dplyr")
3 | library("lavaan")
4 |
5 | # In this example, I will use the default dataset used in the lavaan documentation at ?sem,
6 | # the ``industrialization and Political Democracy Example '' by Bollen (1989), page 332:
7 | # The data is available in lavaan as:
8 | PoliticalDemocracy
9 | # More information on the data is available in ?PoliticalDemocracy.
10 |
11 |
12 | # This script fits the model using the RAW data, see psychonetrics_sumstat.R for an example
13 | # using summary statistics (covariance matrix)
14 |
15 | # Model 1: standard SEM model without equality constraints and residual covariances:
16 | # Lambda matrix:
17 | Lambda <- matrix(c(
18 | 1,0,0, # dem60 =~ y1
19 | 1,0,0, # dem60 =~ y2
20 | 1,0,0, # dem60 =~ y3
21 | 1,0,0, # dem60 =~ y4
22 | 0,1,0, # dem65 =~ y5
23 | 0,1,0, # dem65 =~ y6
24 | 0,1,0, # dem65 =~ y7
25 | 0,1,0, # dem65 =~ y8
26 | 0,0,1, # ind60 =~ x1
27 | 0,0,1, # ind60 =~ x2
28 | 0,0,1 # ind60 =~ x3
29 | ),ncol=3,byrow=TRUE)
30 |
31 | # Beta matrix:
32 | Beta <- matrix(
33 | c(
34 | 0,0,1,
35 | 1,0,1,
36 | 0,0,0
37 | ),3,3,byrow=TRUE
38 | )
39 |
40 | # Model:
41 | mod1 <- lvm(PoliticalDemocracy, lambda = Lambda, beta = Beta)
42 |
43 | # Run model:
44 | mod1 <- mod1 %>% runmodel(verbose=TRUE)
45 |
46 | # Look at fit:
47 | mod1
48 | mod1 %>% fit
49 |
50 | # Model 2: SEM model with added residual covariances (model 1 is nested in this model):
51 | # Method A is to adjust mod1:
52 | mod2a <- mod1 %>%
53 | freepar("sigma_epsilon","y1","y5") %>%
54 | freepar("sigma_epsilon","y2","y4") %>%
55 | freepar("sigma_epsilon","y2","y6") %>%
56 | freepar("sigma_epsilon","y3","y7") %>%
57 | freepar("sigma_epsilon","y4","y8") %>%
58 | freepar("sigma_epsilon","y6","y8") %>%
59 | runmodel
60 |
61 | # Method B is to manually specify theta (called sigma_epsilon here):
62 | # 'Theta' matrix:
63 | Sigma_epsilon <- diag(11)
64 | Sigma_epsilon[1,5] <- Sigma_epsilon[5,1] <- 1
65 | Sigma_epsilon[2,4] <- Sigma_epsilon[4,2] <- 1
66 | Sigma_epsilon[2,6] <- Sigma_epsilon[6,2] <- 1
67 | Sigma_epsilon[3,7] <- Sigma_epsilon[7,3] <- 1
68 | Sigma_epsilon[4,8] <- Sigma_epsilon[8,4] <- 1
69 | Sigma_epsilon[6,8] <- Sigma_epsilon[8,6] <- 1
70 |
71 | # Construct model:
72 | mod2b <- lvm(PoliticalDemocracy, lambda = Lambda, beta = Beta, sigma_epsilon = Sigma_epsilon)
73 |
74 | # Run model:
75 | mod2b <- mod2b %>% runmodel
76 |
77 | # Both methods are identical:
78 | compare(mod2a,mod2b)
79 |
80 | # Compare to previous model:
81 | compare(mod1,mod2a)
82 |
83 | mod2a %>% fit
84 |
85 | # Model 3: SEM model with added residual covariances AND equality constrains (nested in model 2):
86 | # Again two methods, first by adjusting previous model. This uses a new function (update from Github).
87 | # The way to do this now is not very pretty yet...
88 | # First we need to know the indices of parameters, simply the rows in the parameter table.
89 | View(mod2a@parameters)
90 | # 16, 17 and 18 are factor loadings of dem60 and 31, 32 and 33 are factor loadings of dem65:
91 | mod3a <- mod2a %>%
92 | parequal(16,31) %>%
93 | parequal(17,32) %>%
94 | parequal(18,33) %>%
95 | runmodel
96 |
97 | # The second method is to use the input matrices, with integers > 1 indicating equality constrains:
98 | Lambda <- matrix(c(
99 | 1,0,0, # dem60 =~ y1
100 | 2,0,0, # dem60 =~ y2
101 | 3,0,0, # dem60 =~ y3
102 | 4,0,0, # dem60 =~ y4
103 | 0,1,0, # dem65 =~ y5
104 | 0,2,0, # dem65 =~ y6
105 | 0,3,0, # dem65 =~ y7
106 | 0,4,0, # dem65 =~ y8
107 | 0,0,1, # ind60 =~ x1
108 | 0,0,1, # ind60 =~ x2
109 | 0,0,1 # ind60 =~ x3
110 | ),ncol=3,byrow=TRUE)
111 |
112 | # Form model:
113 | mod3b <- lvm(PoliticalDemocracy, lambda = Lambda, beta = Beta, sigma_epsilon = Sigma_epsilon)
114 |
115 | # Run model:
116 | mod3b <- mod3b %>% runmodel
117 |
118 |
119 | # Both methods are identical:
120 | compare(mod3a,mod3b)
121 |
122 | # Compare to previous model:
123 | compare(mod2b,mod3b)
124 |
--------------------------------------------------------------------------------
/SEM_fit_examples/psychonetrics_sumstat.R:
--------------------------------------------------------------------------------
1 | # devtools::install_github("sachaepskamp/psychonetrics")
2 | library("psychonetrics")
3 | library("dplyr")
4 | library("lavaan")
5 |
6 | # In this example, I will use the default dataset used in the lavaan documentation at ?sem,
7 | # the ``industrialization and Political Democracy Example '' by Bollen (1989), page 332:
8 | # The data is available in lavaan as:
9 | PoliticalDemocracy
10 | # More information on the data is available in ?PoliticalDemocracy.
11 |
12 | # This script fits the model using summary statistics (covariance matrix) see psychonetrics_rawdata.R
13 | # for an example using the raw data
14 |
15 | # Cov matrix:
16 | S <- cov(PoliticalDemocracy[,c("y1","y2","y3","y4","y5","y6","y7","y8","x1","x2","x3")])
17 |
18 | # Sample size:
19 | n <- nrow(PoliticalDemocracy)
20 |
21 | # For psychonetrics, we need to rescale S to the ML Estimate!
22 | S <- (n-1)/n * S
23 |
24 | # Model 1: standard SEM model without equality constraints and residual covariances:
25 | # Lambda matrix:
26 | Lambda <- matrix(c(
27 | 1,0,0, # dem60 =~ y1
28 | 1,0,0, # dem60 =~ y2
29 | 1,0,0, # dem60 =~ y3
30 | 1,0,0, # dem60 =~ y4
31 | 0,1,0, # dem65 =~ y5
32 | 0,1,0, # dem65 =~ y6
33 | 0,1,0, # dem65 =~ y7
34 | 0,1,0, # dem65 =~ y8
35 | 0,0,1, # ind60 =~ x1
36 | 0,0,1, # ind60 =~ x2
37 | 0,0,1 # ind60 =~ x3
38 | ),ncol=3,byrow=TRUE)
39 |
40 | # Beta matrix:
41 | Beta <- matrix(
42 | c(
43 | 0,0,1,
44 | 1,0,1,
45 | 0,0,0
46 | ),3,3,byrow=TRUE
47 | )
48 |
49 | # Model:
50 | mod1 <- lvm(covs = S, nobs = n, lambda = Lambda, beta = Beta)
51 |
52 | # Run model:
53 | mod1 <- mod1 %>% runmodel
54 |
55 | # Look at fit:
56 | mod1
57 | mod1 %>% fit
58 |
59 | # Model 2: SEM model with added residual covariances (model 1 is nested in this model):
60 | # Method A is to adjust mod1:
61 | mod2a <- mod1 %>%
62 | freepar("sigma_epsilon","y1","y5") %>%
63 | freepar("sigma_epsilon","y2","y4") %>%
64 | freepar("sigma_epsilon","y2","y6") %>%
65 | freepar("sigma_epsilon","y3","y7") %>%
66 | freepar("sigma_epsilon","y4","y8") %>%
67 | freepar("sigma_epsilon","y6","y8") %>%
68 | runmodel
69 |
70 | # Method B is to manually specify theta (called sigma_epsilon here):
71 | # 'Theta' matrix:
72 | Sigma_epsilon <- diag(11)
73 | Sigma_epsilon[1,5] <- Sigma_epsilon[5,1] <- 1
74 | Sigma_epsilon[2,4] <- Sigma_epsilon[4,2] <- 1
75 | Sigma_epsilon[2,6] <- Sigma_epsilon[6,2] <- 1
76 | Sigma_epsilon[3,7] <- Sigma_epsilon[7,3] <- 1
77 | Sigma_epsilon[4,8] <- Sigma_epsilon[8,4] <- 1
78 | Sigma_epsilon[6,8] <- Sigma_epsilon[8,6] <- 1
79 |
80 | # Construct model:
81 | mod2b <- lvm(covs = S, nobs = n, lambda = Lambda, beta = Beta, sigma_epsilon = Sigma_epsilon)
82 |
83 | # Run model:
84 | mod2b <- mod2b %>% runmodel
85 |
86 | # Both methods are identical:
87 | compare(mod2a,mod2b)
88 |
89 | # Compare to previous model:
90 | compare(mod1,mod2a)
91 |
92 | # Model 3: SEM model with added residual covariances AND equality constrains (nested in model 2):
93 | # Again two methods, first by adjusting previous model. This uses a new function (update from Github).
94 | # The way to do this now is not very pretty yet...
95 | # First we need to know the indices of parameters, simply the rows in the parameter table.
96 | View(mod2a@parameters)
97 | # 16, 17 and 18 are factor loadings of dem60 and 31, 32 and 33 are factor loadings of dem65:
98 | mod3a <- mod2a %>%
99 | parequal(16,31) %>%
100 | parequal(17,32) %>%
101 | parequal(18,33) %>%
102 | runmodel
103 |
104 | # The second method is to use the input matrices, with integers > 1 indicating equality constrains:
105 | Lambda <- matrix(c(
106 | 1,0,0, # dem60 =~ y1
107 | 2,0,0, # dem60 =~ y2
108 | 3,0,0, # dem60 =~ y3
109 | 4,0,0, # dem60 =~ y4
110 | 0,1,0, # dem65 =~ y5
111 | 0,2,0, # dem65 =~ y6
112 | 0,3,0, # dem65 =~ y7
113 | 0,4,0, # dem65 =~ y8
114 | 0,0,1, # ind60 =~ x1
115 | 0,0,1, # ind60 =~ x2
116 | 0,0,1 # ind60 =~ x3
117 | ),ncol=3,byrow=TRUE)
118 |
119 | # Form model:
120 | mod3b <- lvm(covs = S, nobs = n, lambda = Lambda, beta = Beta, sigma_epsilon = Sigma_epsilon)
121 |
122 | # Run model:
123 | mod3b <- mod3b %>% runmodel
124 |
125 |
126 | # Both methods are identical:
127 | compare(mod3a,mod3b)
128 |
129 | # Compare to previous model:
130 | compare(mod2b,mod3b)
131 |
--------------------------------------------------------------------------------
/SEM_fit_examples/semplot_lavaan.R:
--------------------------------------------------------------------------------
1 | library("lavaan")
2 | library("semPlot")
3 |
4 | # Using model 2 from lavaan_rawdata.R:
5 |
6 | # Model 2: SEM model with added residual covariances (model 1 is nested in this model):
7 | model2 <- '
8 | # latent variable definitions
9 | ind60 =~ x1 + x2 + x3
10 | dem60 =~ y1 + y2 + y3 + y4
11 | dem65 =~ y5 + y6 + y7 + y8
12 |
13 | # regressions
14 | dem60 ~ ind60
15 | dem65 ~ ind60 + dem60
16 |
17 | # residual correlations
18 | y1 ~~ y5
19 | y2 ~~ y4 + y6
20 | y3 ~~ y7
21 | y4 ~~ y8
22 | y6 ~~ y8
23 | '
24 |
25 | # Fit in Lavaan:
26 | fit2 <- sem(model2, data=PoliticalDemocracy)
27 |
28 | # Some possible semPlot examples:
29 | semPaths(fit2,"mod","est", edge.color = "black", style = "lisrel", residScale = 8) # Nice already, another three based options is:
30 | semPaths(fit2,"mod","est", layout = "tree2", edge.color = "black",
31 | style = "lisrel", residScale = 8)
32 |
33 | # Also nice is the layoutSplit option for complicated models:
34 | semPaths(fit2,"mod","est", layoutSplit = TRUE, subScale = 0.3,
35 | rotation = 2, edge.color = "black", style = "lisrel",
36 | residScale = 8)
37 |
38 | # Some pretty colors:
39 | semPaths(fit2,"col","est", layout = "tree2",
40 | style = "lisrel", residScale = 8,
41 | groups = "latents",
42 | pastel = TRUE,
43 | borders = FALSE)
44 |
--------------------------------------------------------------------------------
/SEM_fit_examples/semplot_psychonetrics.R:
--------------------------------------------------------------------------------
1 | library("lavaan")
2 | library("semPlot")
3 |
4 | # Using model 2 from psychonetrics_rawdata.R:
5 |
6 | # Model 2: SEM model with added residual covariances (model 1 is nested in this model):
7 | # Lambda matrix:
8 | Lambda <- matrix(c(
9 | 1,0,0, # dem60 =~ y1
10 | 1,0,0, # dem60 =~ y2
11 | 1,0,0, # dem60 =~ y3
12 | 1,0,0, # dem60 =~ y4
13 | 0,1,0, # dem65 =~ y5
14 | 0,1,0, # dem65 =~ y6
15 | 0,1,0, # dem65 =~ y7
16 | 0,1,0, # dem65 =~ y8
17 | 0,0,1, # ind60 =~ x1
18 | 0,0,1, # ind60 =~ x2
19 | 0,0,1 # ind60 =~ x3
20 | ),ncol=3,byrow=TRUE)
21 |
22 | # Beta matrix:
23 | Beta <- matrix(
24 | c(
25 | 0,0,1,
26 | 1,0,1,
27 | 0,0,0
28 | ),3,3,byrow=TRUE
29 | )
30 | Sigma_epsilon <- diag(11)
31 | Sigma_epsilon[1,5] <- Sigma_epsilon[5,1] <- 1
32 | Sigma_epsilon[2,4] <- Sigma_epsilon[4,2] <- 1
33 | Sigma_epsilon[2,6] <- Sigma_epsilon[6,2] <- 1
34 | Sigma_epsilon[3,7] <- Sigma_epsilon[7,3] <- 1
35 | Sigma_epsilon[4,8] <- Sigma_epsilon[8,4] <- 1
36 | Sigma_epsilon[6,8] <- Sigma_epsilon[8,6] <- 1
37 |
38 | # Construct model:
39 | mod2b <- lvm(PoliticalDemocracy, lambda = Lambda, beta = Beta, sigma_epsilon = Sigma_epsilon)
40 |
41 | # Run model:
42 | mod2b <- mod2b %>% runmodel
43 |
44 | # Estravct matrices:
45 | Psi_est <- getmatrix(mod2b, "sigma_zeta")[[1]]
46 | Lambda_est <- getmatrix(mod2b, "lambda")[[1]]
47 | Beta_est <- getmatrix(mod2b, "beta")[[1]]
48 | Theta_est <- getmatrix(mod2b, "sigma_epsilon")[[1]]
49 |
50 | # Make semPlot model:
51 | plotmod <- lisrelModel(
52 | LY = Lambda_est, PS = Psi_est, TE = Theta_est, BE = Beta_est,
53 | manNamesEndo = names(PoliticalDemocracy),
54 | latNamesEndo = c("dem60","dem65","ind60")
55 | )
56 |
57 | # This is an all-y model, let;s make ind60 exogenous:
58 | plotmod@Vars$exogenous[plotmod@Vars$name %in% c("x1","x2","x3","ind60")] <- TRUE
59 |
60 | # Some possible semPlot examples:
61 | semPaths(plotmod,"mod","est", edge.color = "black", style = "lisrel", residScale = 8) # Nice already, another three based options is:
62 | semPaths(plotmod,"mod","est", layout = "tree2", edge.color = "black",
63 | style = "lisrel", residScale = 8)
64 |
65 | # Also nice is the layoutSplit option for complicated models:
66 | semPaths(plotmod,"mod","est", layoutSplit = TRUE, subScale = 0.3,
67 | rotation = 2, edge.color = "black", style = "lisrel",
68 | residScale = 8)
69 |
70 | # Some pretty colors:
71 | semPaths(plotmod,"col","est", layout = "tree2",
72 | style = "lisrel", residScale = 8,
73 | groups = "latents",
74 | pastel = TRUE,
75 | borders = FALSE)
76 |
--------------------------------------------------------------------------------
/psychonetrics/SHARE panel example/SHARE_summary_statistics.RData:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/SachaEpskamp/SEM-code-examples/1fc4500c21c8df785101d6ca0832efb9aef45f95/psychonetrics/SHARE panel example/SHARE_summary_statistics.RData
--------------------------------------------------------------------------------
/psychonetrics/SHARE panel example/SHAREresults.pdf:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/SachaEpskamp/SEM-code-examples/1fc4500c21c8df785101d6ca0832efb9aef45f95/psychonetrics/SHARE panel example/SHAREresults.pdf
--------------------------------------------------------------------------------
/psychonetrics/SHARE panel example/dataPreparationCodes.R:
--------------------------------------------------------------------------------
1 |
2 | ### NOTE: This code is *not* executable without the data, which can be requested from
3 | # http://www.share-project.org/home0.html. Here I select some variables on three of the
4 | # waves and remove all cases with at least one NA as well as cases with z-scores on any
5 | # variable > 5 (mostly because BMI has severe outliers influencing the results). I do
6 | # this to obtain a reproducible example that uses the summary statistics only,
7 | # saved in SHARE_summary_statistics.RData and available. Probably these analysis steps
8 | # would not be the most optimal for a genuine empirical study (many observations are
9 | # removed)
10 |
11 | # Load packages:
12 | library("dplyr")
13 | library("tidyr")
14 | library("reshape2")
15 |
16 | # Load dataset:
17 | load("easySHARE_rel6_1_1.rda")
18 |
19 | # Select variables and waves 3-6:
20 | SHARE_selected <- easySHARE_rel6_1_1 %>%
21 | dplyr::select(mergeid, # ID variable
22 | wave, # Wave variable
23 | sphus, # Perceived health
24 | casp, # Quality of life
25 | eurod, # Depression symptoms
26 | hc002_mod, # Doctor visists
27 | maxgrip, # Maximum grip strength measure
28 | bmi # BMI
29 | ) %>%
30 | # Select waves 4 - 6:
31 | filter(wave >= 4)
32 |
33 | # Input NAs:
34 | SHARE_selected[SHARE_selected < 0] <- NA
35 |
36 | # Rescore to more interpretable scale:
37 | SHARE_selected <- SHARE_selected %>%
38 | mutate(sphus = 6-sphus)
39 |
40 | # To long format:
41 | SHARE_selected_long <- SHARE_selected %>% gather(variable,value,sphus:bmi) %>%
42 | mutate(fullvar = paste0(wave,"_",variable))
43 |
44 | # Standardize per variable:
45 | SHARE_selected_long <- SHARE_selected_long %>%
46 | group_by(variable) %>%
47 | mutate(value = scale(value)) %>%
48 | ungroup %>%
49 | dplyr::select(fullvar,value,mergeid)
50 |
51 | # Remove any with z > 5:
52 | SHARE_selected_long <- SHARE_selected_long %>%
53 | filter(abs(value) < 5)
54 |
55 | # To wide format:
56 | SHARE_selected_wide <- SHARE_selected_long %>%
57 | spread(fullvar,value) %>%
58 | select(-mergeid)
59 |
60 | # Remove rows with any missing:
61 | SHARE_selected_wide <- na.omit(SHARE_selected_wide)
62 |
63 |
64 | # Write summary statistics:
65 | n <- nrow(SHARE_selected_wide)
66 | covMat <- (n-1)/n * cov(SHARE_selected_wide) # Note: Maximum likelihood estimate
67 | means <- colMeans(SHARE_selected_wide)
68 | save(covMat, means, file = "SHARE_summary_statistics.RData")
69 |
--------------------------------------------------------------------------------
/psychonetrics/SHARE panel example/shareAnalysis.R:
--------------------------------------------------------------------------------
1 | # Install psychonetrics:
2 |
3 | # Step 1: download the binary file from Dropbox (.zip file for Windows or .tgz file for Mac)
4 | # Note: do *not* unpack these! That is, don't unpack the zip file or open it
5 |
6 | # Step 2: Run the following code:
7 | # install.packages(file.choose(), type = "binary", repos = NULL)
8 |
9 | # Step 3: You get a popup window, select the .zip or .tgz file
10 |
11 | # If you get an error saying you miss certain packages/dependencies that are installed. install these and try again
12 |
13 | # The package should now be installed and loadable with library("...")
14 |
15 | # Alternatively, the package can be compiled from source:
16 | # library("devtools")
17 | # install_github("sachaepskamp/psychonetrics")
18 |
19 | # Load psychonetrics:
20 | library("psychonetrics")
21 |
22 | # Also load dplyr:
23 | library("dplyr")
24 |
25 | # And qgraph:
26 | library("qgraph")
27 |
28 | # Load SHARE summary statistics This is a relatively small (n = 2911) subset of
29 | # http://www.share-project.org/home0.html containing only participants with no missings
30 | # on a selected set of variables.
31 | load("SHARE_summary_statistics.RData")
32 | # Gives objects covMat and means
33 |
34 | # This dataset contains the following variables:
35 | # bmi: BMI
36 | # casp: Quality of life
37 | # eurod: # Depression symptoms
38 | # hc002_mod: # Doctor visists
39 | # maxgrip: Maximum grip strength measure
40 | # sphus: Perceived health
41 |
42 | # Form the design matrix:
43 | design <- matrix(colnames(covMat),6)
44 | # This design matrix encodes the design. Rows indicate variables and columns indicate
45 | # measurements. An NA can indicate a variable missing at a certain measurement.
46 |
47 | # Form panel-gvar model:
48 | Model <- panelgvar(covs = covMat, means = means, nobs = 2911, vars = design)
49 |
50 | # if we had raw data, we could have instead used:
51 | # Model <- panelgvar(Data, nobs = 2911, vars = design, estimator = "FIML")
52 |
53 | # Run model:
54 | Model <- Model %>% runmodel
55 |
56 | # Prune model:
57 | Model_pruned <- Model %>% prune(adjust = "fdr", recursive = FALSE, alpha = 0.05)
58 |
59 | # Stepup search to optimize BIC:
60 | Model_pruned_stepup <- Model_pruned %>% stepup(criterion = "bic")
61 |
62 | # Compare all models:
63 | compare(
64 | full = Model,
65 | pruned = Model_pruned,
66 | pruned_stepup = Model_pruned_stepup # <- best AIC and BIC
67 | )
68 |
69 | # Print some results:
70 | Model_pruned_stepup
71 |
72 | # Inspect fit:
73 | Model_pruned_stepup %>% fit # SEM fit indices
74 |
75 | # Inspect parameters:
76 | Model_pruned_stepup %>% parameters
77 |
78 | # Extract networks:
79 | temporal <- Model_pruned_stepup %>% getmatrix("PDC")
80 | contemporaneous <- Model_pruned_stepup %>% getmatrix("omega_zeta_within")
81 | betweensubjects <- Model_pruned_stepup %>% getmatrix("omega_zeta_between")
82 |
83 | # bmi: BMI
84 | # casp: Quality of life
85 | # eurod: # Depression symptoms
86 | # hc002_mod: # Doctor visists
87 | # maxgrip: Maximum grip strength measure
88 | # sphus: Perceived health
89 |
90 | # Labels:
91 | Labels <- c("BMI","Quality\nof Life","Depression","Doctor Visits","Grip Strength", "Perceived\nHealth")
92 |
93 | # Plot and save to PDF file:
94 | pdf("SHAREresults.pdf",width = 12, height = 4)
95 | layout(t(1:3))
96 | qgraph(temporal, layout = "circle", labels = Labels, theme = "colorblind",
97 | asize = 7, vsize = 28, label.cex = 1.1, mar = c(8,8,8,8), title = "Temporal",
98 | label.scale = FALSE)
99 | box("figure")
100 |
101 | qgraph(contemporaneous, layout = "circle", labels = Labels, theme = "colorblind",
102 | vsize = 28, label.cex = 1.1, mar = c(8,8,8,8),
103 | title = "Contemporaneous", label.scale = FALSE)
104 | box("figure")
105 |
106 | qgraph(betweensubjects, layout = "circle", labels = Labels, theme = "colorblind",
107 | vsize = 28, label.cex = 1.1, mar = c(8,8,8,8), title = "Between-subjects", label.scale = FALSE)
108 | box("figure")
109 |
110 | # Close pdf device and finalise image:
111 | dev.off()
112 |
--------------------------------------------------------------------------------
/qgraph_path_diagrams/1factor_1indicator/1fac1ind.pdf:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/SachaEpskamp/SEM-code-examples/1fc4500c21c8df785101d6ca0832efb9aef45f95/qgraph_path_diagrams/1factor_1indicator/1fac1ind.pdf
--------------------------------------------------------------------------------
/qgraph_path_diagrams/1factor_1indicator/1factor_1indicator.R:
--------------------------------------------------------------------------------
1 | library("qgraph")
2 |
3 |
4 | E <- matrix(c(
5 | 1,2,
6 | 3,2,
7 | 3,3,
8 | 1,1
9 | ),4,2,byrow=TRUE)
10 | img <- lapply(images[1:2],readPNG)
11 | # aspect <- c(sapply(img, function(x) nrow(x)/ncol(x)),1)
12 | size <- 1.2*c(20,15,10)
13 | shape <- c("circle","rectangle","circle")
14 | borders = c(TRUE,TRUE,TRUE)
15 | Layout <- matrix(c(
16 | 0,1,
17 | 1,1,
18 | 1.5,1
19 | ),3,2,byrow=TRUE)
20 | eCol <- c("black","black","black","black")
21 | labels <- list(expression(eta[1]),expression(y[1]),expression(epsilon[1]))
22 | eLabs <- list(expression(lambda[11]),"1",expression(theta[11]),expression(psi[11]))
23 | qgraph(E,edgelist = TRUE,
24 | vsize = size, shape = shape ,
25 | borders = borders, layout = Layout,
26 | edge.color = eCol, asize = 8, labels = labels,
27 | label.scale.equal = FALSE, bidirectional = TRUE,
28 | mar = c(2,12,2,5), esize = 6, label.cex = 1.25,
29 | edge.labels = eLabs, edge.label.cex = 2,
30 | bg = "transparent", edge.label.bg = "white",
31 | filetype = "pdf", filename = "1fac1ind",
32 | width = 8, height = 3)
--------------------------------------------------------------------------------
/qgraph_path_diagrams/1factor_2indicators/1fac2ind.pdf:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/SachaEpskamp/SEM-code-examples/1fc4500c21c8df785101d6ca0832efb9aef45f95/qgraph_path_diagrams/1factor_2indicators/1fac2ind.pdf
--------------------------------------------------------------------------------
/qgraph_path_diagrams/1factor_2indicators/1factor_2indicators.R:
--------------------------------------------------------------------------------
1 | library("qgraph")
2 | library("png")
3 | # images <- c("sun2.png", "thermometer.png", NA)
4 | E <- matrix(c(
5 | 1,2,
6 | 3,2,
7 | 3,3,
8 | 1,1,
9 | 1,4,
10 | 5,4,
11 | 5,5
12 | ),,2,byrow=TRUE)
13 | # aspect <- c(sapply(img, function(x) nrow(x)/ncol(x)),1)
14 | size <- 1.2*c(20,13,10,13,10)
15 | shape <- c("circle","rectangle","circle","rectangle","circle")
16 | borders = TRUE
17 | Layout <- matrix(c(
18 | 0,1,
19 | 1,0,
20 | 1.5,0,
21 | 1,2,
22 | 1.5,2
23 | ),,2,byrow=TRUE)
24 | eCol <- "black"
25 | labels <- list(expression(eta[1]),expression(y[1]),expression(epsilon[1]),expression(y[2]),expression(epsilon[2]))
26 | eLabs <- list("1","",expression(theta[11]),expression(psi[11]),expression(lambda[21]),"",expression(theta[22]))
27 | loopRot <- c(1.5*pi,NA,0.5*pi,NA,0.5*pi)
28 | qgraph(E,edgelist = TRUE,
29 | vsize = size, shape = shape ,
30 | borders = borders, layout = Layout,
31 | edge.color = eCol, asize = 8, labels = labels,
32 | label.scale.equal = FALSE, bidirectional = TRUE,
33 | mar = c(8,12,8,5), esize = 6, label.cex = 1.25,
34 | edge.labels = eLabs, edge.label.cex = 2,
35 | bg = "transparent", edge.label.bg = "white",
36 | loopRotation = loopRot,
37 | filetype = "pdf", filename = "1fac2ind",
38 | width = 8, height = 3)
--------------------------------------------------------------------------------
/qgraph_path_diagrams/1factor_3indicators/1fac1ind.pdf:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/SachaEpskamp/SEM-code-examples/1fc4500c21c8df785101d6ca0832efb9aef45f95/qgraph_path_diagrams/1factor_3indicators/1fac1ind.pdf
--------------------------------------------------------------------------------
/qgraph_path_diagrams/1factor_3indicators/1factor_3indicators.R:
--------------------------------------------------------------------------------
1 | library("qgraph")
2 | # images <- c("sun2.png", "thermometer.png", NA)
3 | E <- matrix(c(
4 | 1,2,
5 | 3,2,
6 | 3,3,
7 | 1,1,
8 | 1,4,
9 | 5,4,
10 | 5,5,
11 | 1,6,
12 | 7,6,
13 | 7,7
14 | ),,2,byrow=TRUE)
15 | # aspect <- c(sapply(img, function(x) nrow(x)/ncol(x)),1)
16 | size <- 1.2*c(20,10,8,10,8,10,8)
17 | shape <- c("circle","rectangle","circle","rectangle","circle","rectangle","circle")
18 | borders = TRUE
19 | Layout <- matrix(c(
20 | 0,1,
21 | 1,0,
22 | 1.5,0,
23 | 1,1,
24 | 1.5,1,
25 | 1,2,
26 | 1.5,2
27 | ),,2,byrow=TRUE)
28 | eCol <- "black"
29 | labels <- list(expression(eta[1]),expression(y[1]),expression(epsilon[1]),expression(y[2]),expression(epsilon[2]),
30 | expression(y[3]),expression(epsilon[3]))
31 | eLabs <- list("1","",expression(theta[11]),expression(psi[11]),expression(lambda[21]),"",expression(theta[22]),
32 | expression(lambda[31]),"",expression(theta[33]))
33 | loopRot <- c(1.5*pi,NA,0.5*pi,NA,0.5*pi,NA,0.5*pi)
34 | qgraph(E,edgelist = TRUE,
35 | vsize = size, shape = shape ,
36 | borders = borders, layout = Layout,
37 | edge.color = eCol, asize = 8, labels = labels,
38 | label.scale.equal = FALSE, bidirectional = TRUE,
39 | mar = c(8,12,8,5), esize = 6, label.cex = 1.25,
40 | edge.labels = eLabs, edge.label.cex = 2,
41 | bg = "transparent", edge.label.bg = "white",
42 | loopRotation = loopRot,
43 | filetype = "pdf", filename = "1fac1ind",
44 | width = 8, height = 3)
--------------------------------------------------------------------------------
/qgraph_path_diagrams/2factor_6indicator/2fac6ind.pdf:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/SachaEpskamp/SEM-code-examples/1fc4500c21c8df785101d6ca0832efb9aef45f95/qgraph_path_diagrams/2factor_6indicator/2fac6ind.pdf
--------------------------------------------------------------------------------
/qgraph_path_diagrams/2factor_6indicator/2factor_6indicators.R:
--------------------------------------------------------------------------------
1 | library("qgraph")
2 | library("png")
3 | # images <- c("sun2.png", "thermometer.png", NA)
4 | # Two factors (1-2), three indicators each:
5 | E <- matrix(c(
6 | 1,3, # Loading
7 | 1,4, # Loading
8 | 1,5, # Loading
9 | 2,6, # Loading
10 | 2,7, # Loading
11 | 2,8, # Loading
12 | 9,3,
13 | 10,4,
14 | 11,5,
15 | 12,6,
16 | 13,7,
17 | 14,8,
18 | 1,1,
19 | 2,2,
20 | 1,2,
21 | 9,9,
22 | 10,10,
23 | 11,11,
24 | 12,12,
25 | 13,13,
26 | 14,14,
27 | 2,1
28 | ),,2,byrow=TRUE)
29 |
30 |
31 |
32 | # aspect <- c(sapply(img, function(x) nrow(x)/ncol(x)),1)
33 | size <- 1*c(rep(13,2),rep(10,6),rep(8,6))
34 | shape <- c(rep("circle",2),rep("square",6),rep("circle",6))
35 | borders = TRUE
36 | Layout <- matrix(c(
37 | 2,2,
38 | 5,2,
39 | 1,1,
40 | 2,1,
41 | 3,1,
42 | 4,1,
43 | 5,1,
44 | 6,1,
45 | 1,0.5,
46 | 2,0.5,
47 | 3,0.5,
48 | 4,0.5,
49 | 5,0.5,
50 | 6,0.5
51 | ),,2,byrow=TRUE)
52 | eCol <- "black"
53 | labels <- list(expression(eta[1]),
54 | expression(eta[2]),
55 | expression(y[1]),
56 | expression(y[2]),
57 | expression(y[3]),
58 | expression(y[4]),
59 | expression(y[5]),
60 | expression(y[6]),
61 | expression(epsilon[1]),
62 | expression(epsilon[2]),
63 | expression(epsilon[3]),
64 | expression(epsilon[4]),
65 | expression(epsilon[5]),
66 | expression(epsilon[6])
67 | )
68 | eLabs <- list(
69 | "1",
70 | expression(lambda[21]),
71 | expression(lambda[31]),
72 | "1",
73 | expression(lambda[52]),
74 | expression(lambda[62]),
75 | "","","","","","",
76 | expression(psi[11]),
77 | expression(psi[22]),
78 | expression(psi[21]),
79 | expression(theta[11]),
80 | expression(theta[22]),
81 | expression(theta[33]),
82 | expression(theta[44]),
83 | expression(theta[55]),
84 | expression(theta[66]),
85 | expression(psi[21])
86 | )
87 | curve <- rep(0,22)
88 | curve[15] <- 2
89 | loopRot <- c(rep(0,2),rep(pi,2*6))
90 | qgraph(E,edgelist = TRUE,
91 | vsize = size, shape = shape ,
92 | borders = borders, layout = Layout,
93 | edge.color = eCol, asize = 5, labels = labels,
94 | label.scale.equal = FALSE, bidirectional = TRUE,
95 | mar = c(6,5,9,5), esize = 4, label.cex = 1.25,
96 | edge.labels = eLabs, edge.label.cex = 1.5,
97 | bg = "transparent", edge.label.bg = "white",
98 | loopRotation = loopRot, curve = curve, curveAll=TRUE,
99 | filetype = "pdf", filename = "2fac6ind",
100 | width = 8, height = 5)
--------------------------------------------------------------------------------
/qgraph_path_diagrams/2factor_6indicator_allcrossloadings/2fac6ind_allcross.pdf:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/SachaEpskamp/SEM-code-examples/1fc4500c21c8df785101d6ca0832efb9aef45f95/qgraph_path_diagrams/2factor_6indicator_allcrossloadings/2fac6ind_allcross.pdf
--------------------------------------------------------------------------------
/qgraph_path_diagrams/2factor_6indicator_allcrossloadings/2factor_6indicators_allcrossloadings.R:
--------------------------------------------------------------------------------
1 | library("qgraph")
2 |
3 | # images <- c("sun2.png", "thermometer.png", NA)
4 | # Two factors (1-2), three indicators each:
5 | E <- matrix(c(
6 | 1,3, # Loading
7 | 1,4, # Loading
8 | 1,5, # Loading
9 | 2,6, # Loading
10 | 2,7, # Loading
11 | 2,8, # Loading
12 | 9,3,
13 | 10,4,
14 | 11,5,
15 | 12,6,
16 | 13,7,
17 | 14,8,
18 | 1,1,
19 | 2,2,
20 | 1,2,
21 | 9,9,
22 | 10,10,
23 | 11,11,
24 | 12,12,
25 | 13,13,
26 | 14,14,
27 | 2,1
28 | ),,2,byrow=TRUE)
29 | E2 <- matrix(c(
30 | 1,6,
31 | 1,7,
32 | 1,8,
33 | 2,3,
34 | 2,4,
35 | 2,5
36 | ),,2,byrow=TRUE)
37 |
38 |
39 | # aspect <- c(sapply(img, function(x) nrow(x)/ncol(x)),1)
40 | size <- 1*c(rep(13,2),rep(10,6),rep(8,6))
41 | shape <- c(rep("circle",2),rep("square",6),rep("circle",6))
42 | borders = TRUE
43 | Layout <- matrix(c(
44 | 2,2,
45 | 5,2,
46 | 1,1,
47 | 2,1,
48 | 3,1,
49 | 4,1,
50 | 5,1,
51 | 6,1,
52 | 1,0.5,
53 | 2,0.5,
54 | 3,0.5,
55 | 4,0.5,
56 | 5,0.5,
57 | 6,0.5
58 | ),,2,byrow=TRUE)
59 | eCol <- "black"
60 | labels <- list(expression(eta[1]),
61 | expression(eta[2]),
62 | expression(y[1]),
63 | expression(y[2]),
64 | expression(y[3]),
65 | expression(y[4]),
66 | expression(y[5]),
67 | expression(y[6]),
68 | expression(epsilon[1]),
69 | expression(epsilon[2]),
70 | expression(epsilon[3]),
71 | expression(epsilon[4]),
72 | expression(epsilon[5]),
73 | expression(epsilon[6])
74 | )
75 | curve <- rep(0,nrow(E) + nrow(E2))
76 | curve[15] <- 2
77 | loopRot <- c(rep(0,2),rep(pi,2*6))
78 |
79 | Efull <- rbind(E,E2)
80 | lty <- c(rep(1,nrow(E)), rep(2,nrow(E2)))
81 | esize <- c(rep(4,nrow(E)), rep(1,nrow(E2)))
82 |
83 | qgraph(Efull,edgelist = TRUE,
84 | vsize = size, shape = shape ,
85 | borders = borders, layout = Layout,
86 | edge.color = eCol, asize = 5, labels = labels,
87 | label.scale.equal = FALSE, bidirectional = TRUE,
88 | mar = c(6,5,9,5), esize = esize, label.cex = 1.25,
89 | edge.label.cex = 1.5, lty = lty,
90 | bg = "transparent", edge.label.bg = "white",
91 | loopRotation = loopRot, curve = curve, curveAll=TRUE,
92 | filetype = "pdf", filename = "2fac6ind_allcross",
93 | width = 8, height = 5)
--------------------------------------------------------------------------------
/qgraph_path_diagrams/2factor_6indicator_crossloading/2fac6indcross.pdf:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/SachaEpskamp/SEM-code-examples/1fc4500c21c8df785101d6ca0832efb9aef45f95/qgraph_path_diagrams/2factor_6indicator_crossloading/2fac6indcross.pdf
--------------------------------------------------------------------------------
/qgraph_path_diagrams/2factor_6indicator_crossloading/2factor_6indicators_crossloading.R:
--------------------------------------------------------------------------------
1 | library("qgraph")
2 | library("png")
3 | # images <- c("sun2.png", "thermometer.png", NA)
4 | # Two factors (1-2), three indicators each:
5 | E <- matrix(c(
6 | 1,3, # Loading
7 | 1,4, # Loading
8 | 1,5, # Loading
9 | 2,6, # Loading
10 | 2,7, # Loading
11 | 2,8, # Loading
12 | 9,3,
13 | 10,4,
14 | 11,5,
15 | 12,6,
16 | 13,7,
17 | 14,8,
18 | 1,1,
19 | 2,2,
20 | 1,2,
21 | 9,9,
22 | 10,10,
23 | 11,11,
24 | 12,12,
25 | 13,13,
26 | 14,14,
27 | 2,1,
28 | 1,6
29 | ),,2,byrow=TRUE)
30 |
31 |
32 |
33 | # aspect <- c(sapply(img, function(x) nrow(x)/ncol(x)),1)
34 | size <- 1*c(rep(13,2),rep(10,6),rep(8,6))
35 | shape <- c(rep("circle",2),rep("square",6),rep("circle",6))
36 | borders = TRUE
37 | Layout <- matrix(c(
38 | 2,2,
39 | 5,2,
40 | 1,1,
41 | 2,1,
42 | 3,1,
43 | 4,1,
44 | 5,1,
45 | 6,1,
46 | 1,0.5,
47 | 2,0.5,
48 | 3,0.5,
49 | 4,0.5,
50 | 5,0.5,
51 | 6,0.5
52 | ),,2,byrow=TRUE)
53 | eCol <- "black"
54 | labels <- list(expression(eta[1]),
55 | expression(eta[2]),
56 | expression(y[1]),
57 | expression(y[2]),
58 | expression(y[3]),
59 | expression(y[4]),
60 | expression(y[5]),
61 | expression(y[6]),
62 | expression(epsilon[1]),
63 | expression(epsilon[2]),
64 | expression(epsilon[3]),
65 | expression(epsilon[4]),
66 | expression(epsilon[5]),
67 | expression(epsilon[6])
68 | )
69 | eLabs <- list(
70 | "1",
71 | expression(lambda[21]),
72 | expression(lambda[31]),
73 | "1",
74 | expression(lambda[52]),
75 | expression(lambda[62]),
76 | "","","","","","",
77 | expression(psi[11]),
78 | expression(psi[22]),
79 | expression(psi[21]),
80 | expression(theta[11]),
81 | expression(theta[22]),
82 | expression(theta[33]),
83 | expression(theta[44]),
84 | expression(theta[55]),
85 | expression(theta[66]),
86 | expression(psi[21]),
87 | expression(lambda[41])
88 | )
89 | curve <- rep(0,23)
90 | curve[15] <- 2
91 | loopRot <- c(rep(0,2),rep(pi,2*6))
92 | qgraph(E,edgelist = TRUE,
93 | vsize = size, shape = shape ,
94 | borders = borders, layout = Layout,
95 | edge.color = eCol, asize = 4, labels = labels,
96 | label.scale.equal = FALSE, bidirectional = TRUE,
97 | mar = c(6,5,9,5), esize = 4, label.cex = 1.25,
98 | edge.labels = eLabs, edge.label.cex = 1.3,
99 | bg = "transparent", edge.label.bg = "white",
100 | loopRotation = loopRot, curve = curve, curveAll=TRUE,
101 | filetype = "pdf", filename = "2fac6indcross",
102 | width = 8, height = 5)
--------------------------------------------------------------------------------
/qgraph_path_diagrams/2factor_6indicator_residual/2fac6ind_resid.pdf:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/SachaEpskamp/SEM-code-examples/1fc4500c21c8df785101d6ca0832efb9aef45f95/qgraph_path_diagrams/2factor_6indicator_residual/2fac6ind_resid.pdf
--------------------------------------------------------------------------------
/qgraph_path_diagrams/2factor_6indicator_residual/2factor_6indicators_residual.R:
--------------------------------------------------------------------------------
1 | library("qgraph")
2 |
3 | # images <- c("sun2.png", "thermometer.png", NA)
4 | # Two factors (1-2), three indicators each:
5 | E <- matrix(c(
6 | 1,3, # Loading
7 | 1,4, # Loading
8 | 1,5, # Loading
9 | 2,6, # Loading
10 | 2,7, # Loading
11 | 2,8, # Loading
12 | 9,3,
13 | 10,4,
14 | 11,5,
15 | 12,6,
16 | 13,7,
17 | 14,8,
18 | 1,1,
19 | 2,2,
20 | 1,2,
21 | 9,9,
22 | 10,10,
23 | 11,11,
24 | 12,12,
25 | 13,13,
26 | 14,14,
27 | 2,1,
28 | 11,12,
29 | 12,11
30 | ),,2,byrow=TRUE)
31 |
32 |
33 |
34 | # aspect <- c(sapply(img, function(x) nrow(x)/ncol(x)),1)
35 | size <- 1*c(rep(13,2),rep(10,6),rep(8,6))
36 | shape <- c(rep("circle",2),rep("square",6),rep("circle",6))
37 | borders = TRUE
38 | Layout <- matrix(c(
39 | 2,2,
40 | 5,2,
41 | 1,1,
42 | 2,1,
43 | 3,1,
44 | 4,1,
45 | 5,1,
46 | 6,1,
47 | 1,0.5,
48 | 2,0.5,
49 | 3,0.5,
50 | 4,0.5,
51 | 5,0.5,
52 | 6,0.5
53 | ),,2,byrow=TRUE)
54 | eCol <- "black"
55 | labels <- list(expression(eta[1]),
56 | expression(eta[2]),
57 | expression(y[1]),
58 | expression(y[2]),
59 | expression(y[3]),
60 | expression(y[4]),
61 | expression(y[5]),
62 | expression(y[6]),
63 | expression(epsilon[1]),
64 | expression(epsilon[2]),
65 | expression(epsilon[3]),
66 | expression(epsilon[4]),
67 | expression(epsilon[5]),
68 | expression(epsilon[6])
69 | )
70 | eLabs <- list(
71 | "1",
72 | expression(lambda[21]),
73 | expression(lambda[31]),
74 | "1",
75 | expression(lambda[52]),
76 | expression(lambda[62]),
77 | "","","","","","",
78 | expression(psi[11]),
79 | expression(psi[22]),
80 | expression(psi[21]),
81 | expression(theta[11]),
82 | expression(theta[22]),
83 | expression(theta[33]),
84 | expression(theta[44]),
85 | expression(theta[55]),
86 | expression(theta[66]),
87 | expression(psi[21]),
88 | expression(theta[43]),
89 | expression(theta[43])
90 | )
91 | curve <- rep(0,24)
92 | curve[15] <- 2
93 | curve[23:24] <- -3
94 | loopRot <- c(rep(0,2),rep(pi,2*6))
95 | qgraph(E,edgelist = TRUE,
96 | vsize = size, shape = shape ,
97 | borders = borders, layout = Layout,
98 | edge.color = eCol, asize = 4, labels = labels,
99 | label.scale.equal = FALSE, bidirectional = TRUE,
100 | mar = c(6,5,9,5), esize = 4, label.cex = 1.25,
101 | edge.labels = eLabs, edge.label.cex = 1.3,
102 | bg = "transparent", edge.label.bg = "white",
103 | loopRotation = loopRot, curve = curve, curveAll=TRUE,
104 | filetype = "pdf", filename = "2fac6ind_resid",
105 | width = 8, height = 5)
--------------------------------------------------------------------------------
/qgraph_path_diagrams/Path models/ThreeNodePathDiagram.R:
--------------------------------------------------------------------------------
1 | library("qgraph")
2 | E <- matrix(c(
3 | 1,2,
4 | 2,3,
5 | 2,2,
6 | 3,3
7 | ),,2,byrow=TRUE)
8 |
9 | L <- matrix(c(
10 | 0,1,
11 | 1,1,
12 | 2,1
13 | ),,2,byrow=TRUE)
14 |
15 | labels <- list("A1","A2","E1")
16 | elabs <- list(
17 | "-",
18 | "-",
19 | expression(epsilon[1]),
20 | expression(epsilon[2])
21 | )
22 | shape <- c("square","square","square")
23 |
24 | bidir <- rep(FALSE,5)
25 | bidir[3] <- TRUE
26 | residEdge <- rep(FALSE,5)
27 | residEdge[3] <- TRUE
28 |
29 | qgraph(E, edgelist = TRUE, layout = L,
30 | labels = labels, edge.color = "black",
31 | asize = 8, shape = shape,vsize = 18,
32 | mar = c(1,4,5,4), esize = 5, border.width = 2,
33 | edge.labels = elabs, edge.label.cex = 3,
34 | residuals = TRUE,
35 | loopRotation = 0, residScale = 20,
36 | height = 3, width = 7, filetype = "pdf",
37 | filename = "ThreeNodePathDiagram")
--------------------------------------------------------------------------------
/qgraph_path_diagrams/Path models/ThreeNodePathDiagram.pdf:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/SachaEpskamp/SEM-code-examples/1fc4500c21c8df785101d6ca0832efb9aef45f95/qgraph_path_diagrams/Path models/ThreeNodePathDiagram.pdf
--------------------------------------------------------------------------------
/qgraph_path_diagrams/baseline/baseline.R:
--------------------------------------------------------------------------------
1 | library("qgraph")
2 | library("png")
3 |
4 | shape <- c(rep("circle",2),rep("square",6),rep("circle",6))
5 | borders = TRUE
6 | eCol <- "black"
7 | lty <- 1
8 | esize <- 4
9 |
10 | labels <- list(
11 | expression(y[1]),
12 | expression(y[2]),
13 | expression(y[3]),
14 | expression(y[4]),
15 | expression(y[5]),
16 | expression(y[6])
17 | )
18 |
19 | qgraph(diag(1,6),
20 | vsize = 14, shape = "square" , directed = TRUE,
21 | borders = borders, layout = "circle",
22 | edge.color = eCol, asize = 8, labels = labels,
23 | label.scale.equal = FALSE, bidirectional = TRUE,
24 | mar = 1.4*c(6,6,6,6), esize = esize, label.cex = 1.25,
25 | edge.label.cex = 1.5, lty = lty,
26 | bg = "transparent", edge.label.bg = "white",
27 | curveAll=TRUE, diag = TRUE, title = "Baseline model",
28 | filetype = "pdf", filename = "baseline",
29 | width = 8, height = 5)
--------------------------------------------------------------------------------
/qgraph_path_diagrams/baseline/baseline.pdf:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/SachaEpskamp/SEM-code-examples/1fc4500c21c8df785101d6ca0832efb9aef45f95/qgraph_path_diagrams/baseline/baseline.pdf
--------------------------------------------------------------------------------
/qgraph_path_diagrams/efa_loadings/loadings.pdf:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/SachaEpskamp/SEM-code-examples/1fc4500c21c8df785101d6ca0832efb9aef45f95/qgraph_path_diagrams/efa_loadings/loadings.pdf
--------------------------------------------------------------------------------
/qgraph_path_diagrams/efa_loadings/loadingsPlot.R:
--------------------------------------------------------------------------------
1 | # Needed libraries:
2 | library("semPlot")
3 | library("qgraph")
4 | library("GA")
5 |
6 | # Unstandardized factor loadings:
7 | lambda <- structure(c(0, 0.347309557389605, 0, 0, 0.291403542576795, 0,
8 | 0.27467525445669, 0, 0, 0, 0, 0, 0, -0.135747404769135, 0, 0,
9 | 0.220977530548909, 0, 0, 0.177371401901561, 0, 0, 0, 0.602166263050583,
10 | 0.360068645572603, 0, 0, 0, 0.602797696702521, 0, 0, 0, 0, 0,
11 | 0, 0.442838991127218, 0.540960096837521, 0, 0, 0, 0, -0.230286259625514,
12 | 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0.468494433270527, 0.166956348004494,
13 | 0.119204135288922, 0, 0, 0.187579836080558, 0.16924386870456,
14 | 0, 0.173066446586688, 0, 0, 0, 0, 0, 0, 0, 0), .Dim = c(14L, 5L))
15 |
16 | # Residual variances:
17 | theta <- diag(c(0.486348735347288, 0.163964724976415, 0.050523237006943, 0.00939202259935427,
18 | 0.178308156862288, 0.0696469267486953, 0.167008251796478, 0.242443317918602,
19 | 0.137411018927023, 0.127953455640805, 0.11052415282475, 0.086872942979836,
20 | 0.267132369318934, 0.451194076239714))
21 |
22 | # Latent variance-covariance:
23 | psi <- structure(c(2.63859380152006, -0.793345350197633, -1.497683288397,
24 | -1.00124376108535, -0.669807660275558, -0.793345350197633, 1.23853495157823,
25 | 0.450308066453413, 0.30104371571473, 0.201390904693342, -1.497683288397,
26 | 0.450308066453413, 1.85009493733043, 0.568312579119007, 0.380187256809703,
27 | -1.00124376108535, 0.30104371571473, 0.568312579119007, 1.3799330797089,
28 | 0.254165965444068, -0.669807660275558, 0.201390904693342, 0.380187256809703,
29 | 0.254165965444068, 1.1700308329025), .Dim = c(5L, 5L))
30 |
31 | # Use semPlot to standardize:
32 | library("semPlot")
33 | semPlot_mod <- lisrelModel(LY = lambda, TE = theta, PS = psi)
34 | modMats <- modelMatrices(semPlot_mod, "Mplus")
35 | lambdastd <- modMats$Lambda[[1]]$std
36 |
37 | # Number of latents:
38 | nLat <- ncol(lambdastd)
39 |
40 | # Number of observed:
41 | nObs <- nrow(lambdastd)
42 |
43 | # Edgelist for graph:
44 | Edgelist <- cbind(
45 | c(col(lambdastd)),c(row(lambdastd))+ncol(lambdastd),c(lambdastd)
46 | )
47 |
48 | # shape:
49 | shape <- c(rep("ellipse",nLat),rep("rectangle",nObs))
50 |
51 | # Size1:
52 | size1 <- c(rep(13,nLat),rep(30,nObs))
53 |
54 | # Size2:
55 | size2 <- c(rep(13,nLat),rep(6,nObs))
56 |
57 | # Edge connect points:
58 | ECP <- Edgelist
59 | ECP[,1] <- 0.5*pi
60 | ECP[,2] <- 1.5*pi
61 |
62 | # Labels:
63 | latLabels <- paste0("F",1:5)
64 |
65 | # Manifest labels:
66 | manLabels <- c("irritated", "satisfied", "lonely", "anxious",
67 | "enthusiastic", "guilty", "strong", "restless", "agitated",
68 | "worry", "ashamed", "tired", "headache", "sleepy"
69 | )
70 |
71 | # Size of labels:
72 | labelCex <- c(
73 | rep(2,nLat),
74 | rep(1.25,nObs)
75 | )
76 |
77 | # Starting layout:
78 | Layout <- rbind(
79 | cbind(
80 | 0,
81 | seq(-1,1,length=nLat+2)[-c(1,nLat+2)]
82 | ),
83 | cbind(
84 | 1,
85 | seq(-1,1,length=nObs+2)[-c(1,nObs+2)]
86 | )
87 | )
88 |
89 | # Use genetic algorithm (GA) to find best placement:
90 | # fit function:
91 | fit <- function(x){
92 | # Order layout:
93 | Layout2 <- Layout[x,]
94 |
95 | # Penalty for if the latents are placed wrong:
96 | penalty <- 1 + sum(!x[1:nLat] %in% (1:nLat))
97 |
98 | # Which edges are not zero?
99 | noZero <- Edgelist[,3]!=0
100 |
101 | # Return fit:
102 | -penalty * sum(sqrt((Layout2[Edgelist[noZero,2],2] - Layout2[Edgelist[noZero,1],2])^2))
103 | }
104 |
105 | # Run GA:
106 | res <- ga(type = "permutation", fitness = fit, lower = 1, upper = nrow(Layout), parallel = FALSE,
107 | popSize = 1000, maxiter = 1000, seed = 1)
108 |
109 | # Extract best order:
110 | order <- c(res@solution[1,])
111 |
112 | # Final layout:
113 | LayoutFinal <- Layout[order,]
114 |
115 | # Plot and save to PDF:
116 | qgraph(Edgelist,
117 | shape = shape,
118 | vsize = size1,
119 | vsize2 = size2,
120 | layout = LayoutFinal,
121 | mar = c(1,3,1,6),
122 | edgeConnectPoints = ECP,
123 | labels = c(latLabels, manLabels),
124 | label.scale = FALSE,
125 | label.cex = labelCex,
126 | asize = 5,
127 | theme = "colorblind",
128 | filetype = "pdf",
129 | filename = "loadings",
130 | width = 6,
131 | height = 7)
132 |
--------------------------------------------------------------------------------
/qgraph_path_diagrams/latentgrowth/latentgrowth.R:
--------------------------------------------------------------------------------
1 | library("qgraph")
2 |
3 | # images <- c("sun2.png", "thermometer.png", NA)
4 | # Two factors (1-2), three indicators each:
5 | E <- matrix(c(
6 | 1,3, # Loading
7 | 1,4, # Loading
8 | 1,5, # Loading
9 | 1,6,
10 | 1,7,
11 | 1,8,
12 | 2,3,
13 | 2,4,
14 | 2,5,
15 | 2,6, # Loading
16 | 2,7, # Loading
17 | 2,8, # Loading
18 |
19 | 9,3,
20 | 10,4,
21 | 11,5,
22 | 12,6,
23 | 13,7,
24 | 14,8,
25 | 1,1,
26 | 2,2,
27 | 1,2,
28 | 9,9,
29 | 10,10,
30 | 11,11,
31 | 12,12,
32 | 13,13,
33 | 14,14,
34 | 2,1,
35 | 15,1,
36 | 15,2
37 | ),,2,byrow=TRUE)
38 |
39 |
40 | # aspect <- c(sapply(img, function(x) nrow(x)/ncol(x)),1)
41 | size <- 1*c(rep(13,2),rep(10,6),rep(8,6),10)
42 | shape <- c(rep("circle",2),rep("square",6),rep("circle",6),"triangle")
43 | borders = TRUE
44 | Layout <- matrix(c(
45 | 1,2,
46 | 6,2,
47 | 1,1,
48 | 2,1,
49 | 3,1,
50 | 4,1,
51 | 5,1,
52 | 6,1,
53 | 1,0.5,
54 | 2,0.5,
55 | 3,0.5,
56 | 4,0.5,
57 | 5,0.5,
58 | 6,0.5,
59 | 3.5,2
60 | ),,2,byrow=TRUE)
61 | eCol <- "black"
62 | labels <- list(expression("Intercept"),
63 | expression("Slope"),
64 | expression(y[t == 1]),
65 | expression(y[t == 2]),
66 | expression(y[t == 3]),
67 | expression(y[t == 4]),
68 | expression(y[t == 5]),
69 | expression(y[t == 6]),
70 | expression(epsilon[1]),
71 | expression(epsilon[2]),
72 | expression(epsilon[3]),
73 | expression(epsilon[4]),
74 | expression(epsilon[5]),
75 | expression(epsilon[6]),
76 | expression("1")
77 | )
78 | curve <- rep(0,nrow(E))
79 | curve[21] <- 1.2
80 | loopRot <- c(rep(0,2),rep(pi,2*6))
81 |
82 |
83 | # lty <- c(rep(1,nrow(E)), rep(2,nrow(E2)))
84 | # esize <- c(rep(4,nrow(E)), rep(1,nrow(E2)))
85 |
86 | eLabs <- list(
87 | "1","1","1","1","1","1",
88 | "1", expression(lambda[2,1]),
89 | expression(lambda[3,1]), expression(lambda[4,1]),
90 | expression(lambda[5,1]), expression(lambda[6,1]),
91 | "","","","","","",
92 | expression(psi[1,1]),
93 | expression(psi[2,2]),
94 | expression(psi[2,1]),
95 | expression(theta[1,1]),
96 | expression(theta[2,2]),
97 | expression(theta[3,3]),
98 | expression(theta[4,4]),
99 | expression(theta[5,5]),
100 | expression(theta[6,6]),
101 | expression(psi[2,1]),
102 | expression(alpha[1]),
103 | expression(alpha[2])
104 | )
105 |
106 | # ECP <- matrix(NA,nrow(E),2)
107 | # ECP[1:12,2] <- 0
108 | edgelabpos <- rep(0.5,nrow(E))
109 | edgelabpos[1:12] <- 0.3
110 | qgraph(E,edgelist = TRUE, edge.labels = eLabs,
111 | vsize = size, shape = shape ,# edgeConnectPoints = ECP,
112 | borders = borders, layout = Layout, edge.label.position = edgelabpos,
113 | edge.color = eCol, asize = 5, labels = labels,
114 | label.scale.equal = FALSE, bidirectional = TRUE,
115 | mar = c(5,2,7,2), esize = 2, label.cex = 1.25,
116 | edge.label.cex = 1.5,# lty = lty,
117 | bg = "transparent", edge.label.bg = "white",
118 | loopRotation = loopRot, curve = curve, curveAll=TRUE,
119 | filetype = "pdf", filename = "latentgrowth",
120 | width = 8, height = 6)
--------------------------------------------------------------------------------
/qgraph_path_diagrams/latentgrowth/latentgrowth.pdf:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/SachaEpskamp/SEM-code-examples/1fc4500c21c8df785101d6ca0832efb9aef45f95/qgraph_path_diagrams/latentgrowth/latentgrowth.pdf
--------------------------------------------------------------------------------
/qgraph_path_diagrams/meanstructure1/meanstructure1.R:
--------------------------------------------------------------------------------
1 | library("qgraph")
2 | # images <- c("sun2.png", "thermometer.png", NA)
3 | E <- matrix(c(
4 | 1,2,
5 | 3,2,
6 | 3,3,
7 | 1,1,
8 | 1,4,
9 | 5,4,
10 | 5,5,
11 | 1,6,
12 | 7,6,
13 | 7,7,
14 | 8,1, # Mean
15 | 8,2, # Intercepts
16 | 8,4,
17 | 8,6
18 | ),,2,byrow=TRUE)
19 | # aspect <- c(sapply(img, function(x) nrow(x)/ncol(x)),1)
20 | size <- 1.2*c(20,12,10,12,10,12,10,12)
21 | shape <- c("circle","rectangle","circle","rectangle","circle","rectangle","circle","triangle")
22 | borders = TRUE
23 | Layout <- matrix(c(
24 | 0,0,
25 | 1,0,
26 | 1.5,0,
27 | 1,1,
28 | 1.5,1,
29 | 1,2,
30 | 1.5,2,
31 | -0.25,2
32 | ),,2,byrow=TRUE)
33 | eCol <- "black"
34 | labels <- list(expression(eta[1]),expression(y[1]),expression(epsilon[1]),expression(y[2]),expression(epsilon[2]),
35 | expression(y[3]),expression(epsilon[3]),expression("1"))
36 | eLabs <- list("1","",expression(theta[11]),expression(psi[11]),expression(lambda[21]),"",expression(theta[22]),
37 | expression(lambda[31]),"",expression(theta[33]),expression("0"), expression(tau[1]), expression(tau[2]), expression(tau[3]))
38 | loopRot <- c(1.5*pi,NA,0.5*pi,NA,0.5*pi,NA,0.5*pi, NA)
39 | lty <- rep(1,nrow(E))
40 | lty[11] <- 2
41 | qgraph(E,edgelist = TRUE,
42 | vsize = size, shape = shape ,
43 | borders = borders, layout = Layout,
44 | edge.color = eCol, asize = 8, labels = labels,
45 | label.scale.equal = FALSE, bidirectional = TRUE,
46 | mar = c(6,5,5,5), esize = 4, label.cex = 1,
47 | edge.labels = eLabs, edge.label.cex = 2,
48 | bg = "transparent", edge.label.bg = "white",
49 | loopRotation = loopRot, lty = lty,
50 | filetype = "pdf", filename = "meanstructure1",
51 | width = 8, height = 5)
--------------------------------------------------------------------------------
/qgraph_path_diagrams/meanstructure1/meanstructure1.pdf:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/SachaEpskamp/SEM-code-examples/1fc4500c21c8df785101d6ca0832efb9aef45f95/qgraph_path_diagrams/meanstructure1/meanstructure1.pdf
--------------------------------------------------------------------------------
/qgraph_path_diagrams/meanstructure2/meanstructure2.R:
--------------------------------------------------------------------------------
1 | library("qgraph")
2 | # images <- c("sun2.png", "thermometer.png", NA)
3 | E <- matrix(c(
4 | 1,2,
5 | 3,2,
6 | 3,3,
7 | 1,1,
8 | 1,4,
9 | 5,4,
10 | 5,5,
11 | 1,6,
12 | 7,6,
13 | 7,7,
14 | 8,1, # Mean
15 | 8,2, # Intercepts
16 | 8,4,
17 | 8,6
18 | ),,2,byrow=TRUE)
19 | # aspect <- c(sapply(img, function(x) nrow(x)/ncol(x)),1)
20 | size <- 1.2*c(15,12,10,12,10,12,10,12)
21 | shape <- c("circle","rectangle","circle","rectangle","circle","rectangle","circle","triangle")
22 | borders = TRUE
23 | Layout <- matrix(c(
24 | 0.1,0,
25 | 1,0,
26 | 1.5,0,
27 | 1,1,
28 | 1.5,1,
29 | 1,2,
30 | 1.5,2,
31 | -0.25,2
32 | ),,2,byrow=TRUE)
33 | Layout <- Layout[,2:1]
34 | Layout[,2] <- -Layout[,2]
35 | eCol <- "black"
36 | # labels <- list(expression(eta[1]),expression(y[1]),expression(epsilon[1]),expression(y[2]),expression(epsilon[2]),
37 | # expression(y[3]),expression(epsilon[3]),expression("1"))
38 | # eLabs <- list("1","",expression(theta[11]),expression(psi[11]),expression(lambda[21]),"",expression(theta[22]),
39 | # expression(lambda[31]),"",expression(theta[33]),expression(alpha[1]), expression(tau[1]), expression(tau[2]), expression(tau[3]))
40 | loopRot <- c(1.5*pi,NA,0.5*pi,NA,0.5*pi,NA,0.5*pi, NA) + pi/2
41 | labels <- list(expression(eta[1]),expression(y[1]),expression(epsilon[1]),expression(y[2]),expression(epsilon[2]),
42 | expression(y[3]),expression(epsilon[3]),expression("1"))
43 |
44 | lty <- rep(1,nrow(E))
45 | lty[11] <- 2
46 |
47 | pdf("meanstructure2.pdf",width=8,height=4.5)
48 | layout(t(1:2))
49 | eLabs <- list("1","",expression(theta[111]),expression(psi[111]),expression(lambda[211]),"",expression(theta[221]),
50 | expression(lambda[311]),"",expression(theta[331]),expression("0"), expression(tau[11]), expression(tau[21]), expression(tau[31]))
51 |
52 | qgraph(E,edgelist = TRUE,lty=lty,
53 | vsize = size, shape = shape ,
54 | borders = borders, layout = Layout,
55 | edge.color = eCol, asize = 8, labels = labels,
56 | label.scale.equal = FALSE, bidirectional = TRUE,
57 | mar = c(6,5,5,5), esize = 4, label.cex = 1,
58 | edge.labels = eLabs, edge.label.cex = 2,
59 | bg = "transparent", edge.label.bg = "white",
60 | loopRotation = loopRot, title = "Group 1")
61 | box("figure")
62 |
63 | eLabs <- list("1","",expression(theta[112]),expression(psi[112]),expression(lambda[212]),"",expression(theta[222]),
64 | expression(lambda[312]),"",expression(theta[332]),expression("0"), expression(tau[12]), expression(tau[22]), expression(tau[32]))
65 |
66 | qgraph(E,edgelist = TRUE,lty=lty,
67 | vsize = size, shape = shape ,
68 | borders = borders, layout = Layout,
69 | edge.color = eCol, asize = 8, labels = labels,
70 | label.scale.equal = FALSE, bidirectional = TRUE,
71 | mar = c(6,5,5,5), esize = 4, label.cex = 1,
72 | edge.labels = eLabs, edge.label.cex = 2,
73 | bg = "transparent", edge.label.bg = "white",
74 | loopRotation = loopRot, title = "Group 2")
75 | box("figure")
76 | dev.off()
--------------------------------------------------------------------------------
/qgraph_path_diagrams/meanstructure2/meanstructure2.pdf:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/SachaEpskamp/SEM-code-examples/1fc4500c21c8df785101d6ca0832efb9aef45f95/qgraph_path_diagrams/meanstructure2/meanstructure2.pdf
--------------------------------------------------------------------------------
/semPlot/semPlotExample1.pdf:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/SachaEpskamp/SEM-code-examples/1fc4500c21c8df785101d6ca0832efb9aef45f95/semPlot/semPlotExample1.pdf
--------------------------------------------------------------------------------
/semPlot/semPlot_modelMatrices.R:
--------------------------------------------------------------------------------
1 | # Load packages:
2 | library("semPlot")
3 |
4 | # Factor loadings matrix:
5 | Lambda <- matrix(0, 10, 3)
6 | Lambda[1:4,1] <- 1
7 | Lambda[c(1,5:7),2] <- 1
8 | Lambda[c(1,8:10),3] <- 1 # Could also contain estimates
9 |
10 | # Dummy Psi:
11 | Psi <- matrix(1,3,3)
12 |
13 | # Theta matrix:
14 | Theta <- diag(10)
15 | Theta[4,10] <- Theta[10,4] <- 1
16 |
17 | # LISREL Model: LY = Lambda (lambda-y), TE = Theta (theta-epsilon), PS = Psi
18 | mod <- lisrelModel(LY = Lambda, PS = Psi, TE = Theta)
19 |
20 | # Plot with semPlot:
21 | semPaths(mod, "mod", "name", as.expression=c("nodes","edges"))
22 |
23 |
24 | # We can make this nicer (set whatLabels = "none" to hide labels):
25 | semPaths(mod,
26 | what = "col", # this argument controls what the color of edges represent. In this case, standardized parameters
27 | whatLabels = "name", # This argument controls what the edge labels represent. In this case, parameter estimates
28 | as.expression = c("nodes","edges"), # This argument draws the node and edge labels as mathematical exprssions
29 | style = "lisrel", # This will plot residuals as arrows, closer to what we use in class
30 | residScale = 10, # This makes the residuals larger
31 | theme = "colorblind", # qgraph colorblind friendly theme
32 | layout = "tree2", # tree layout options are "tree", "tree2", and "tree3"
33 | cardinal = "lat cov", # This makes the latent covariances connet at a cardinal center point
34 | curvePivot = TRUE, # Changes curve into rounded straight lines
35 | sizeMan = 4, # Size of manifest variables
36 | sizeLat = 10, # Size of latent varibales
37 | edge.label.cex = 1,
38 | mar = c(9,1,8,1), # Sets the margins
39 | reorder = FALSE, # Prevents re-ordering of ovbserved variables
40 | filetype = "pdf", # Store to PDF
41 | filename = "semPlotExample1", # Set the name of the file
42 | width = 8, # Width of the plot
43 | height = 5, # Height of plot
44 | groups = "latents", # Colors according to latents,
45 | pastel = TRUE, # Pastel colors
46 | borders = FALSE # Disable borders
47 | )
48 |
49 |
--------------------------------------------------------------------------------