/
lavaan2lm_list.R
480 lines (448 loc) · 12 KB
/
lavaan2lm_list.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
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
#' @title 'lavaan'-class to
#' 'lm_from_lavaan_list'-Class
#'
#' @description Converts the regression
#' models in a `lavaan`-class model to
#' an `lm_from_lavaan_list`-class
#' object.
#'
#' @details It identifies all dependent
#' variables in a `lavaan` model and
#' creates an `lm_from_lavaan`-class
#' object for each of them.
#'
#' This is an advanced helper used by
#' [plot.cond_indirect_effects()].
#' Exported for advanced users and
#' developers.
#'
#' @return An
#' `lm_from_lavaan_list`-class object,
#' which is a list of `lm_from_lavaan`
#' objects. It has a `predict`-method
#' ([predict.lm_from_lavaan_list()]) for
#' computing the predicted values from
#' one variable to another.
#'
#' @param fit A `lavaan`-class object,
#' usually the output of
#' [lavaan::lavaan()] or its wrappers.
#'
#'
#' @seealso [predict.lm_from_lavaan_list]
#'
#' @examples
#' library(lavaan)
#' data(data_med)
#' mod <-
#' "
#' m ~ a * x + c1 + c2
#' y ~ b * m + x + c1 + c2
#' "
#' fit <- sem(mod, data_med, fixed.x = FALSE)
#' fit_list <- lm_from_lavaan_list(fit)
#' tmp <- data.frame(x = 1, c1 = 2, c2 = 3, m = 4)
#' predict(fit_list, x = "x", y = "y", m = "m", newdata = tmp)
#'
#' @export
lm_from_lavaan_list <- function(fit) {
ngroups <- lavaan::lavTech(fit, "ngroups")
if (ngroups > 1) {
group_numbers <- seq_len(ngroups)
group_labels <- lavaan::lavTech(fit, "group.label")
out <- lapply(group_numbers,
function(x) {
lm_from_lavaan_list_i(fit = fit,
group_number = x)
})
names(out) <- group_labels
class(out) <- c("lm_from_lavaan_list", class(out))
} else {
out <- lm_from_lavaan_list_i(fit = fit)
}
out
}
#' @noRd
lm_from_lavaan_list_i <- function(fit,
group_number = NULL) {
ptable <- lav_ptable(fit)
if ("group" %in% colnames(ptable)) {
tmp <- unique(ptable$group)
if (length(tmp) > 1) {
if (is.null(group_number)) {
stop("The model has more than one groups but group_number not set.")
}
if (!(group_number %in% tmp)) {
stop("group_number is", group_number,
"but group numbers in the model are",
paste0(tmp, collapse = ", "))
}
ptable <- ptable[ptable$group == group_number, ]
}
}
# Get all dvs (ov.nox, lv.ox)
dvs <- lavaan_get_dvs(ptable)
dat <- lav_data_used(fit)
# Get all ivs
ivs_list <- sapply(dvs, lavaan_get_ivs,
ptable = ptable,
simplify = FALSE)
# Get all betas
bs_list <- sapply(dvs, lavaan_get_betas,
ivs_list = ivs_list,
ptable = ptable,
simplify = FALSE)
# Get all intercepts
int_list <- sapply(dvs, lavaan_get_intercepts,
ptable = ptable,
simplify = FALSE)
# Get all product terms
prods <- find_all_products(dat, expand = TRUE)
# Update product terms
ivs_list1 <- sapply(ivs_list, to_product,
prods = prods,
simplify = FALSE)
# Update beta names
bs_list1 <- mapply(function(x, y) {
names(y) <- x
y
},
x = ivs_list1, y = bs_list,
SIMPLIFY = FALSE,
USE.NAMES = TRUE)
# Generate lm_like_object
mods <- mapply(to_formula, dv = dvs, ivs = ivs_list1)
# Return an lm_list object
out <- mapply(lm_from_lavaan,
model = mods,
betas = bs_list1,
intercept = int_list,
SIMPLIFY = FALSE,
USE.NAMES = TRUE)
class(out) <- c("lm_from_lavaan_list", class(out))
out
}
#' @title Model Terms of an
#' 'lm_from_lavaan'-Class Object
#'
#' @description It extracts the terms
#' object from an `lm_from_lavaan`-class
#' object.
#'
#' @details A method for
#' `lm_from_lavaan`-class that converts
#' a regression model for a variable in
#' a `lavaan` model to a `formula`
#' object. This function simply calls
#' [stats::terms()] on the `formula`
#' object to extract the predictors of a
#' variable.
#'
#' @return A `terms`-class object. See
#' [terms.object] for details.
#'
#' @param x An 'lm_from_lavaan'-class
#' object.
#'
#' @param ... Additional arguments.
#' Ignored.
#'
#'
#' @seealso [terms.object],
#' [lm_from_lavaan_list()]
#'
#' @examples
#' library(lavaan)
#' data(data_med)
#' mod <-
#' "
#' m ~ a * x + c1 + c2
#' y ~ b * m + x + c1 + c2
#' "
#' fit <- sem(mod, data_med, fixed.x = FALSE)
#' fit_list <- lm_from_lavaan_list(fit)
#' terms(fit_list$m)
#' terms(fit_list$y)
#'
#' @export
terms.lm_from_lavaan <- function(x, ...) {
stats::terms(x$model)
}
#' @title Coefficients of an
#' 'lm_from_lavaan'-Class Object
#'
#' @description Returns the path
#' coefficients of the terms in an
#' `lm_from_lavaan`-class object.
#'
#' @details An `lm_from_lavaan`-class
#' object converts a regression model
#' for a variable in a `lavaan`-class
#' object to a `formula`-class object.
#' This function simply extracts the
#' path coefficients estimates.
#' Intercept is always included, and set
#' to zero if mean structure is not in
#' the source `lavaan`-class object.
#'
#' This is an advanced helper used by
#' [plot.cond_indirect_effects()].
#' Exported for advanced users and
#' developers.
#'
#' @return A numeric vector of the path
#' coefficients.
#'
#' @param object A
#' 'lm_from_lavaan'-class object.
#'
#' @param ... Additional arguments.
#' Ignored.
#'
#'
#' @seealso [lm_from_lavaan_list()]
#'
#' @examples
#' library(lavaan)
#' data(data_med)
#' mod <-
#' "
#' m ~ a * x + c1 + c2
#' y ~ b * m + x + c1 + c2
#' "
#' fit <- sem(mod, data_med, fixed.x = FALSE)
#' fit_list <- lm_from_lavaan_list(fit)
#' coef(fit_list$m)
#' coef(fit_list$y)
#'
#' @export
coef.lm_from_lavaan <- function(object, ...) {
c(object$intercept, object$coefficients)
}
#' @title Predicted Values of a
#' 'lm_from_lavaan'-Class Object
#'
#' @description Compute the predicted
#' values based on the model stored in a
#' 'lm_from_lavaan`-class object.
#'
#' @details An `lm_from_lavaan`-class
#' method that converts a regression
#' model for a variable in a `lavaan`
#' model to a `formula` object. This
#' function uses the stored model to
#' compute predicted values using
#' user-supplied data.
#'
#' This is an advanced helper used by
#' [plot.cond_indirect_effects()].
#' Exported for advanced users and
#' developers.
#'
#' @return A numeric vector of the
#' predicted values, with length equal
#' to the number of rows of
#' user-supplied data.
#'
#' @param object A
#' 'lm_from_lavaan'-class object.
#'
#' @param newdata Required. A data frame
#' of the new data. It must be a data
#' frame.
#'
#' @param ... Additional arguments.
#' Ignored.
#'
#'
#' @seealso [lm_from_lavaan_list()]
#'
#' @examples
#' library(lavaan)
#' data(data_med)
#' mod <-
#' "
#' m ~ a * x + c1 + c2
#' y ~ b * m + x + c1 + c2
#' "
#' fit <- sem(mod, data_med, fixed.x = FALSE)
#' fit_list <- lm_from_lavaan_list(fit)
#' tmp <- data.frame(x = 1, c1 = 2, c2 = 3, m = 4)
#' predict(fit_list$m, newdata = tmp)
#' predict(fit_list$y, newdata = tmp)
#'
#' @export
predict.lm_from_lavaan <- function(object, newdata, ...) {
if (missing(newdata)) {
stop("This method for lm_from_lavaan requires new data.")
}
tivs <- stats::delete.response(stats::terms(object))
bs <- stats::coef(object)
dat0 <- stats::model.frame(tivs, newdata)
mm <- stats::model.matrix(tivs, dat0) # No need for contrasts.arg
p <- length(bs)
out <- mm[, names(bs)] %*% matrix(bs, nrow = p, ncol = 1)
unname(out[, 1])
}
#' @title Predicted Values of an
#' 'lm_from_lavaan_list'-Class Object
#'
#' @description It computes the
#' predicted values based on the models
#' stored in an
#' 'lm_from_lavaan_list`-class object.
#'
#' @details An
#' `lm_from_lavaan_list`-class object
#' is a list of `lm_from_lavaan`-class
#' objects.
#'
#' This is an advanced helper used by
#' [plot.cond_indirect_effects()].
#' Exported for advanced users and
#' developers.
#'
#' @return A numeric vector of the
#' predicted values, with length equal
#' to the number of rows of
#' user-supplied data.
#'
#' @param object A
#' 'lm_from_lavaan'-class object.
#'
#' @param x The variable name at the
#' start of a path.
#'
#' @param y The variable name at the end
#' of a path.
#'
#' @param m Optional. The mediator(s)
#' from `x` to `y`. A numeric vector of
#' the names of the mediators. The path
#' goes from the first element to the
#' last element. For example, if `m =
#' c("m1", "m2")`, then the path is `x
#' -> m1 -> m2 -> y`.
#'
#' @param newdata Required. A data frame
#' of the new data. It must be a data
#' frame.
#'
#' @param ... Additional arguments.
#' Ignored.
#'
#'
#' @seealso [lm_from_lavaan_list()]
#'
#' @examples
#' library(lavaan)
#' data(data_med)
#' mod <-
#' "
#' m ~ a * x + c1 + c2
#' y ~ b * m + x + c1 + c2
#' "
#' fit <- sem(mod, data_med, fixed.x = FALSE)
#' fit_list <- lm_from_lavaan_list(fit)
#' tmp <- data.frame(x = 1, c1 = 2, c2 = 3, m = 4)
#' predict(fit_list, x = "x", y = "y", m = "m", newdata = tmp)
#'
#' @export
predict.lm_from_lavaan_list <- function(object,
x = NULL,
y = NULL,
m = NULL,
newdata, ...) {
ptable <- lm_from_lavaan_list2ptable(object)
if (!check_path(x = x, y = y, m = m, est = ptable)) {
stop(paste0("The path from ",
dQuote(x),
" to ",
dQuote(y),
" through ",
paste0(dQuote(m), collapse = ", "),
" is invalid."))
}
ys <- c(m, y)
newdata_i <- newdata
for (yi in ys) {
yi_hat <- stats::predict(object[[yi]], newdata = newdata_i)
newdata_i[, yi] <- yi_hat
}
newdata_i[, y]
}
#' @noRd
lm_from_lavaan_list2ptable <- function(x) {
vars <- sapply(x, get_variables,
simplify = FALSE)
dvs <- names(x)
ptable <- mapply(cbind, dvs, "~", vars,
SIMPLIFY = FALSE)
ptable <- do.call(rbind, ptable)
ptable <- as.data.frame(ptable)
colnames(ptable) <- c("lhs", "op", "rhs")
ptable
}
#' @noRd
get_variables <- function(x) {
tmp <- attr(stats::terms(x), "factors")
id <- (colSums(tmp) == 1)
colnames(tmp)[id]
}
#' @noRd
lavaan_get_ivs <- function(dv, ptable) {
ptable_dv <- ptable[(ptable$lhs == dv) & (ptable$op == "~"), ]
ptable_iv0 <- ptable_dv$rhs
ptable_iv0
}
#' @noRd
lavaan_get_betas <- function(dv, ivs_list, ptable) {
ptable_dv <- ptable[(ptable$lhs == dv) & (ptable$op == "~"), ]
ivs <- ivs_list[[dv]]
betas <- ptable_dv$est[which(ptable_dv$rhs %in% ivs)]
names(betas) <- ivs
betas
}
#' @noRd
lavaan_get_intercepts <- function(dv, ptable) {
ptable_dv <- ptable[(ptable$lhs == dv) & (ptable$op == "~1"), ]
if (nrow(ptable_dv) == 0) {
out <- 0
} else {
out <- ptable_dv[, "est"]
}
names(out) <- "(Intercept)"
out
}
#' @noRd
to_product <- function(ivs, prods) {
prod_names <- names(prods)
out <- ivs
for (i in seq_along(ivs)) {
if (ivs[i] %in% prod_names) {
prod_comp <- prods[[which(ivs[i] == prod_names)]]
out[i] <- paste0(prod_comp, collapse = ":")
}
}
out
}
#' @noRd
to_formula <- function(dv, ivs) {
# TO-FIX: Handle intercept
out <- paste(dv, "~", paste(ivs, collapse = " + "))
stats::as.formula(out)
}
#' @noRd
lm_from_lavaan <- function(model, betas, intercept) {
out <- list(model = model,
coefficients = betas,
intercept = intercept)
class(out) <- c("lm_from_lavaan", class(out))
out
}
#' @noRd
lavaan_get_dvs <- function(ptable) {
i <- ptable$op == "~"
out <- unique(ptable$lhs[i])
out
}