Compute residuals for the y-variables given the values of the x-variables
lavResidualsY.RdThis function can be used to compute the (case-wise) residuals for the
(observed) y-variables in a structural equation model, defined as the
difference between the observed values and the (model-based) predicted
values that are returned by lavPredictY.
Usage
lavResidualsY(object, newdata = NULL,
ynames = lav_object_vnames(object, "ov.y"),
xnames = lav_object_vnames(object, "ov.x"),
method = "conditional.mean",
label = TRUE, assemble = TRUE,
force_zero_mean = FALSE,
lambda = 0,
...)Arguments
- object
An object of class
lavaan.- newdata
An optional data.frame, containing the same variables as the data.frame that was used when fitting the model in
object. This data.frame should also include the y-variables (their observed values are used to compute the residuals). Note that if no meanstructure was used in the original fit, we will use the saturated sample means of the original fit as substitutes for the model-implied means. Alternatively, refit the model usingmeanstructure = TRUE. For a multiple-group model,newdatacan also be a list with one data.frame per group (in the same order as the groups inobject). An element may beNULLto skip that group. This is useful when the groups have a different set of variables, so thatynamesandxnamescan be specified per group (as a list). In that case, the result contains residuals only for the groups for which data were provided (a single matrix if only one group was requested, otherwise a named list).- ynames
The names of the observed variables that should be treated as the y-variables. It is for these variables that the function will compute the (model-based) residuals for each observation. Can also be a list to allow for a separate set of variable names per group (or block).
- xnames
The names of the observed variables that should be treated as the x-variables. Can also be a list to allow for a separate set of variable names per group (or block).
- method
A character string. The only available option for now is
"conditional.mean". See Details.- label
Logical. If TRUE, the columns of the output are labeled.
- assemble
Logical. If TRUE, the residuals for the separate groups in the output are reassembled into a single data.frame with a group column, having the same dimensions as the original (or newdata) dataset.
- force_zero_mean
Logical. Only relevant if there is no mean structure. If
TRUE, the (model-implied) mean vector is set to the zero vector. IfFALSE, the (model-implied) mean vector is set to the (unrestricted) sample mean vector.- lambda
Numeric. A lambda regularization penalty term.
- ...
To support old argument names.
Details
The residuals are computed as observed - predicted, where the
predicted values are obtained by lavPredictY using the same
arguments. See the help page of lavPredictY for more details
about how the predictions are computed.
These residuals can be used, for example, to diagnose the (SEM-based) out-of-sample prediction of outcome (y) variables, or to check the distributional assumptions (for example, multivariate normality) for the y-variables given the x-variables.
See also
lavPredictY to compute the (model-based) predicted values for
the y-variables given the x-variables.
References
de Rooij, M., Karch, J.D., Fokkema, M., Bakk, Z., Pratiwi, B.C, and Kelderman, H. (2022) SEM-Based Out-of-Sample Predictions, Structural Equation Modeling: A Multidisciplinary Journal. doi:10.1080/10705511.2022.2061494
Examples
model <- '
# latent variable definitions
ind60 =~ x1 + x2 + x3
dem60 =~ y1 + a*y2 + b*y3 + c*y4
dem65 =~ y5 + a*y6 + b*y7 + c*y8
# regressions
dem60 ~ ind60
dem65 ~ ind60 + dem60
# residual correlations
y1 ~~ y5
y2 ~~ y4 + y6
y3 ~~ y7
y4 ~~ y8
y6 ~~ y8
'
fit <- sem(model, data = PoliticalDemocracy)
lavResidualsY(fit, ynames = c("y5", "y6", "y7", "y8"),
xnames = c("x1", "x2", "x3", "y1", "y2", "y3", "y4"))
#> y5 y6 y7 y8
#> [1,] -1.097 0.436 0.734 2.805
#> [2,] 4.037 1.273 3.422 -0.045
#> [3,] 1.087 1.646 0.567 0.628
#> [4,] 0.169 0.819 -0.298 -3.921
#> [5,] -1.015 -1.831 0.488 -0.818
#> [6,] -0.305 -2.409 -0.694 -5.255
#> [7,] -1.482 -1.185 1.001 -4.042
#> [8,] 0.146 0.104 2.832 2.405
#> [9,] 2.828 1.718 -1.140 0.838
#> [10,] 0.187 0.611 0.318 2.108
#> [11,] 1.597 -0.993 1.339 0.135
#> [12,] 1.947 -0.485 -1.001 0.711
#> [13,] -0.119 -1.573 -2.571 2.846
#> [14,] 0.533 -5.524 1.529 -6.233
#> [15,] 0.353 0.703 2.285 3.198
#> [16,] 0.225 -5.409 -2.246 -0.142
#> [17,] 1.063 -0.863 0.878 -3.489
#> [18,] -1.250 3.514 -0.385 1.600
#> [19,] -0.417 2.235 -0.500 1.149
#> [20,] 0.557 -1.690 -0.616 -3.088
#> [21,] 0.787 2.178 -0.557 1.088
#> [22,] 0.008 -0.105 -3.032 -1.049
#> [23,] -1.056 -0.707 -1.275 0.622
#> [24,] -0.139 1.025 0.588 0.056
#> [25,] 0.981 3.169 -1.241 -0.160
#> [26,] -4.443 -0.620 -0.155 0.617
#> [27,] 2.549 -0.684 1.586 0.598
#> [28,] -0.928 -1.413 -1.587 -1.203
#> [29,] 1.196 -2.107 1.107 -1.087
#> [30,] -0.456 -0.669 -5.092 -2.798
#> [31,] 1.909 0.011 -0.565 1.383
#> [32,] -1.367 1.556 -1.387 0.716
#> [33,] 0.710 -0.764 -1.349 0.507
#> [34,] -4.889 -1.651 -3.209 -3.097
#> [35,] -0.745 -4.219 2.151 2.754
#> [36,] 0.413 -3.015 -0.827 -2.159
#> [37,] -1.393 -2.195 -2.808 -0.457
#> [38,] -1.881 -2.082 -1.456 -2.552
#> [39,] 0.664 -1.364 2.088 1.202
#> [40,] 0.531 -4.594 -0.919 -1.142
#> [41,] 0.328 -4.720 0.918 -2.682
#> [42,] 1.198 0.644 3.564 -0.588
#> [43,] -0.344 -1.138 -0.594 0.130
#> [44,] 3.170 2.749 3.936 3.141
#> [45,] 3.137 2.776 -2.736 -0.691
#> [46,] -2.337 4.896 0.582 1.701
#> [47,] -0.069 0.454 -3.944 -1.499
#> [48,] -2.129 4.040 1.382 0.736
#> [49,] -0.871 -1.247 -0.105 -2.585
#> [50,] -1.860 -2.605 -1.033 0.812
#> [51,] 1.342 -0.904 1.373 0.379
#> [52,] 0.546 -0.224 2.750 0.205
#> [53,] 0.330 0.900 1.522 2.457
#> [54,] 0.461 2.106 2.742 2.157
#> [55,] 0.002 -0.183 -3.598 -1.164
#> [56,] 0.036 -0.089 -0.478 2.046
#> [57,] -0.075 -0.349 0.976 -1.373
#> [58,] 0.175 -2.638 1.085 -0.046
#> [59,] 1.286 -0.854 1.264 -1.732
#> [60,] 0.603 2.649 -0.071 1.647
#> [61,] -0.733 0.530 -2.401 -0.368
#> [62,] -0.999 1.757 -1.182 0.947
#> [63,] -1.842 1.136 4.859 3.202
#> [64,] -1.629 0.810 0.486 -0.536
#> [65,] 0.296 3.825 1.097 2.908
#> [66,] 0.998 2.505 5.328 1.370
#> [67,] -3.489 3.780 1.053 2.860
#> [68,] 1.416 -1.144 -1.240 1.794
#> [69,] 0.913 -0.978 -5.129 -2.144
#> [70,] -0.801 1.936 1.918 1.031
#> [71,] -0.887 2.311 1.037 -1.663
#> [72,] 0.516 0.721 1.519 0.523
#> [73,] 0.127 0.586 -2.307 0.692
#> [74,] 1.535 0.854 0.401 2.052
#> [75,] -1.845 2.260 -2.974 -0.951