/
plot_lcsm.R
358 lines (327 loc) · 12.3 KB
/
plot_lcsm.R
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
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
#' Plot simplified path diagram of univariate and bivariate latent change score models
#'
#' @description Note that the following three arguments are needed to create a plot (see below for more details):
#' - `lavaan_object`: the lavaan fit object needs to be specified together with a
#' - `lcsm`: a string indicating whether the latent change score model is "univariate" or "bivariate", and
#' - `lavaan_syntax`: a separate object with the lavaan syntax as a string
#'
#' @param lavaan_object lavaan object of a univariate or bivariate latent change score model.
#' @param lavaan_syntax String, lavaan syntax of the lavaan object specified in \code{lavaan_object}.
#' If \code{lavaan_syntax} is provided a layout matrix will be generated automatically.
#' @param layout Matrix, specifying number and location of manifest and latent variables of LCS model specified in \code{lavaan_object}.
#' @param return_layout_from_lavaan_syntax Logical, if TRUE and \code{lavaan_syntax} is provided, the layout matrix generated for \link[semPlot]{semPaths} will be returned for inspection of further customisation.
#' @param lcsm String, specifying whether lavaan_object represent a "univariate" or "bivariate" LCS model.
#' @param what See \code{semPlot}. "path" to show unweighted grey edges, "par" to show parameter estimates as weighted (green/red) edges
#' @param whatLabels See \link[semPlot]{semPaths}. "label" to show edge names as label, "est" for parameter estimates, "hide" to hide edge labels.
#' @param lcsm_colours Logical, if TRUE the following colours will be used to highlight different parts of the model: Observed variables (White); Latent true scores (Green); Latent change scores (Blue) ; Change factors (Yellow).
#' @param edge.width See \link[semPlot]{semPaths}.
#' @param node.width See \link[semPlot]{semPaths}.
#' @param border.width See \link[semPlot]{semPaths}.
#' @param fixedStyle See \link[semPlot]{semPaths}.
#' @param freeStyle See \link[semPlot]{semPaths}.
#' @param residuals See \link[semPlot]{semPaths}.
#' @param label.scale See \link[semPlot]{semPaths}.
#' @param sizeMan See \link[semPlot]{semPaths}.
#' @param sizeLat See \link[semPlot]{semPaths}.
#' @param intercepts See \link[semPlot]{semPaths}.
#' @param fade See \link[semPlot]{semPaths}.
#' @param nCharNodes See \link[semPlot]{semPaths}.
#' @param curve_covar See \link[semPlot]{semPaths}.
#' @param edge.label.cex See \link[semPlot]{semPaths}.
#' @param nCharEdges See \link[semPlot]{semPaths}.
#' @param ... Other arguments passed on to \link[semPlot]{semPaths}.
#' @importFrom lavaan lavInspect
#' @references Sacha Epskamp (2019). semPlot: Path Diagrams and Visual Analysis of Various SEM Packages' Output. R package version 1.1.1.
#' \url{https://CRAN.R-project.org/package=semPlot/}
#' @examples
#' # Simplified plot of univariate lcsm
#' lavaan_syntax_uni <- fit_uni_lcsm(
#' data = data_bi_lcsm,
#' var = c("x1", "x2", "x3", "x4", "x5"),
#' model = list(
#' alpha_constant = TRUE,
#' beta = TRUE,
#' phi = TRUE
#' ),
#' return_lavaan_syntax = TRUE,
#' return_lavaan_syntax_string = TRUE
#' )
#'
#' lavaan_object_uni <- fit_uni_lcsm(
#' data = data_bi_lcsm,
#' var = c("x1", "x2", "x3", "x4", "x5"),
#' model = list(
#' alpha_constant = TRUE,
#' beta = TRUE,
#' phi = TRUE
#' )
#' )
#'
#' plot_lcsm(
#' lavaan_object = lavaan_object_uni,
#' what = "cons", whatLabels = "invisible",
#' lavaan_syntax = lavaan_syntax_uni,
#' lcsm = "univariate"
#' )
#' \dontrun{
#' # Simplified plot of bivariate lcsm
#' lavaan_syntax_bi <- fit_bi_lcsm(
#' data = data_bi_lcsm,
#' var_x = c("x1", "x2", "x3", "x4", "x5"),
#' var_y = c("y1", "y2", "y3", "y4", "y5"),
#' model_x = list(
#' alpha_constant = TRUE,
#' beta = TRUE,
#' phi = TRUE
#' ),
#' model_y = list(
#' alpha_constant = TRUE,
#' beta = TRUE,
#' phi = TRUE
#' ),
#' coupling = list(
#' delta_lag_xy = TRUE,
#' delta_lag_yx = TRUE
#' ),
#' return_lavaan_syntax = TRUE,
#' return_lavaan_syntax_string = TRUE
#' )
#'
#' lavaan_object_bi <- fit_bi_lcsm(
#' data = data_bi_lcsm,
#' var_x = c("x1", "x2", "x3", "x4", "x5"),
#' var_y = c("y1", "y2", "y3", "y4", "y5"),
#' model_x = list(
#' alpha_constant = TRUE,
#' beta = TRUE,
#' phi = TRUE
#' ),
#' model_y = list(
#' alpha_constant = TRUE,
#' beta = TRUE,
#' phi = TRUE
#' ),
#' coupling = list(
#' delta_lag_xy = TRUE,
#' delta_lag_yx = TRUE
#' )
#' )
#'
#' plot_lcsm(
#' lavaan_object = lavaan_object_bi,
#' what = "cons", whatLabels = "invisible",
#' lavaan_syntax = lavaan_syntax_bi,
#' lcsm = "bivariate"
#' )
#' }
#'
#' @return Plot
#' @export
#'
plot_lcsm <- function(lavaan_object,
layout = NULL,
lavaan_syntax = NULL,
return_layout_from_lavaan_syntax = FALSE,
lcsm = c("univariate", "bivariate"),
lcsm_colours = FALSE,
curve_covar = .5,
what = "path",
whatLabels = "est",
edge.width = 1,
node.width = 1,
border.width = 1,
fixedStyle = 1,
freeStyle = 1,
residuals = FALSE,
label.scale = FALSE,
sizeMan = 3,
sizeLat = 5,
intercepts = FALSE,
fade = FALSE,
nCharNodes = 0,
nCharEdges = 0,
edge.label.cex = 0.5,
# DoNotPlot = TRUE,
...) {
if (is.null(layout) == TRUE) {
# Extract info for layout matrix from lavaan
# Construct x
row_x_mani_vars_lavaanify <- lavaan::lavaanify(lavaan_syntax) %>%
dplyr::as_tibble() %>%
dplyr::filter(stringr::str_detect(rhs, "^x\\d")) %>%
dplyr::select(rhs) %>%
dplyr::distinct() %>%
dplyr::pull()
timepoints_x <- base::length(row_x_mani_vars_lavaanify)
row_x_latent_vars_lavaanify <- lavaan::lavaanify(lavaan_syntax) %>%
dplyr::as_tibble() %>%
dplyr::filter(stringr::str_detect(rhs, "^lx\\d")) %>%
dplyr::select(rhs) %>%
dplyr::distinct() %>%
dplyr::pull()
row_x_change_scores_lavaanify <- lavaan::lavaanify(lavaan_syntax) %>%
dplyr::as_tibble() %>%
dplyr::filter(stringr::str_detect(rhs, "^dx\\d")) %>%
dplyr::select(rhs) %>%
dplyr::distinct() %>%
dplyr::pull()
row_x_change_factors_lavaanify <- lavaan::lavaanify(lavaan_syntax) %>%
dplyr::as_tibble() %>%
dplyr::filter(stringr::str_detect(rhs, "^g\\d")) %>%
dplyr::select(rhs) %>%
dplyr::distinct() %>%
dplyr::pull()
change_factors_x_num <- length(row_x_change_factors_lavaanify)
# Construct Y
row_y_mani_vars_lavaanify <- lavaan::lavaanify(lavaan_syntax) %>%
dplyr::as_tibble() %>%
dplyr::filter(stringr::str_detect(rhs, "^y\\d")) %>%
dplyr::select(rhs) %>%
dplyr::distinct() %>%
dplyr::pull()
timepoints_y <- base::length(row_y_mani_vars_lavaanify)
row_y_latent_vars_lavaanify <- lavaan::lavaanify(lavaan_syntax) %>%
dplyr::as_tibble() %>%
dplyr::filter(stringr::str_detect(rhs, "^ly\\d")) %>%
dplyr::select(rhs) %>%
dplyr::distinct() %>%
dplyr::pull()
row_y_change_scores_lavaanify <- lavaan::lavaanify(lavaan_syntax) %>%
dplyr::as_tibble() %>%
dplyr::filter(stringr::str_detect(rhs, "^dy\\d")) %>%
dplyr::select(rhs) %>%
dplyr::distinct() %>%
dplyr::pull()
row_y_change_factors_lavaanify <- lavaan::lavaanify(lavaan_syntax) %>%
dplyr::as_tibble() %>%
dplyr::filter(stringr::str_detect(rhs, "^j\\d")) %>%
dplyr::select(rhs) %>%
dplyr::distinct() %>%
dplyr::pull()
change_factors_y_num <- length(row_y_change_factors_lavaanify)
# Create layout matrix
if (lcsm == "univariate") {
# Create univariate layoiut matrix
row_x_mani_vars_layout <- row_x_mani_vars_lavaanify
row_x_latent_vars_layout <- row_x_latent_vars_lavaanify
row_x_change_scores_layout <- c(NA, row_x_change_scores_lavaanify)
row_x_change_factors_layout <- c(NA, row_x_change_factors_lavaanify, rep(NA, timepoints_x - 1 - change_factors_x_num))
layout_from_lavaan_syntax <- base::matrix(
c(
row_x_change_factors_layout,
row_x_change_scores_layout,
row_x_latent_vars_layout,
row_x_mani_vars_layout
),
ncol = timepoints_x,
nrow = 4,
byrow = TRUE
)
if (lcsm_colours == TRUE) {
lcsm_colour_matrix <- c(
rep("white", timepoints_x), # bottom 1
rep("#56C667FF", timepoints_x), # bottom 2
rep("#2D718EFF", timepoints_x - 1), # bottom 3
rep("#FDE725FF", 1) # bottom 4
)
} else if (lcsm_colours == FALSE) {
lcsm_colour_matrix <- "white"
}
} else if (lcsm == "bivariate") {
# Create bivariate layoiut matrix
# Create bivariate layout matrix
row_x_mani_vars_layout <- c(NA, NA, row_x_mani_vars_lavaanify)
row_x_latent_vars_layout <- c(NA, NA, row_x_latent_vars_lavaanify)
row_x_change_scores_layout <- c(NA, NA, NA, row_x_change_scores_lavaanify)
row_x_change_factors_layout <- c(NA, row_x_change_factors_lavaanify, rep(NA, timepoints_x + 1 - change_factors_x_num))
row_y_mani_vars_layout <- c(NA, NA, row_y_mani_vars_lavaanify)
row_y_latent_vars_layout <- c(NA, NA, row_y_latent_vars_lavaanify)
row_y_change_scores_layout <- c(NA, NA, NA, row_y_change_scores_lavaanify)
row_y_change_factors_layout <- c(NA, row_y_change_factors_lavaanify, rep(NA, timepoints_y + 1 - change_factors_y_num))
layout_from_lavaan_syntax <- base::matrix(
c(
row_y_mani_vars_layout,
row_y_latent_vars_layout,
row_y_change_scores_layout,
row_y_change_factors_layout,
row_x_change_factors_layout,
row_x_change_scores_layout,
row_x_latent_vars_layout,
row_x_mani_vars_layout
),
ncol = timepoints_x + 2,
nrow = 8,
byrow = TRUE
)
if (lcsm_colours == TRUE) {
lcsm_colour_matrix <- c(
rep("white", timepoints_x), # bottom 1
rep("white", timepoints_x), # top 1
rep("#56C667FF", timepoints_x), # bottom 2
rep("#2D718EFF", timepoints_y - 1), # bottom 3
rep("#FDE725FF", 1), # bottom 4
rep("#56C667FF", timepoints_x), # top 2
rep("#2D718EFF", timepoints_x - 1), # top 3
rep("#FDE725FF", 1) # top 4
)
} else if (lcsm_colours == FALSE) {
lcsm_colour_matrix <- "white"
}
}
if (return_layout_from_lavaan_syntax == TRUE) {
return(layout_from_lavaan_syntax)
} else if (return_layout_from_lavaan_syntax == FALSE) {
graph <- semPlot::semPaths(
object = lavaan_object,
layout = layout_from_lavaan_syntax,
what = what,
color = lcsm_colour_matrix,
whatLabels = whatLabels,
edge.width = edge.width,
node.width = node.width,
border.width = border.width,
fixedStyle = fixedStyle,
freeStyle = freeStyle,
residuals = residuals,
label.scale = label.scale,
sizeMan = sizeMan,
sizeLat = sizeLat,
intercepts = intercepts,
fade = fade,
nCharNodes = 0,
nCharEdges = 0,
edge.label.cex = edge.label.cex,
DoNotPlot = TRUE,
...
)
}
} else if (is.null(layout) == FALSE) {
graph <- semPlot::semPaths(
object = lavaan_object,
layout = layout,
what = what,
color = lcsm_colour_matrix,
whatLabels = whatLabels,
edge.width = edge.width,
node.width = node.width,
border.width = border.width,
fixedStyle = fixedStyle,
freeStyle = freeStyle,
residuals = residuals,
label.scale = label.scale,
sizeMan = sizeMan,
sizeLat = sizeLat,
intercepts = intercepts,
fade = fade,
nCharNodes = 0,
nCharEdges = 0,
edge.label.cex = edge.label.cex,
DoNotPlot = TRUE,
...
)
}
# aww where did I get this from, must have googled it
graph$graphAttributes$Edges$curve <- ifelse(graph$Edgelist$bidir, curve_covar, 0)
# Create plot
plot(graph)
}