Learn R Programming

lavaSearch2 (version 1.4)

residuals2: Extract Corrected Residuals

Description

Extract correct residuals from a gaussian linear model.

Usage

residuals2(object, param, data, type)

# S3 method for lm2 residuals2(object, param = NULL, data = NULL, type = "response")

# S3 method for gls2 residuals2(object, param = NULL, data = NULL, type = "response")

# S3 method for lme2 residuals2(object, param = NULL, data = NULL, type = "response")

# S3 method for lvmfit2 residuals2(object, param = NULL, data = NULL, type = "response")

Arguments

object

a lm2, gls2, lme2, or lvmfit2 object.

param

[named numeric vector] the fitted parameters.

data

[data.frame] the data set.

type

[character] the type of residual to extract: "response" for raw residuals, "studentized" for studentized residuals, "normalized" for normalized residuals.

Value

a matrix containing the residuals relative to each sample (in rows) and each endogenous variable (in column).

Details

If argument p or data is not null, then the small sample size correction is recomputed to correct the residuals.

The raw residuals are defined by observation minus the fitted value: $$ \varepsilon = (Y_1 - \mu_1, ..., Y_m - \mu_m) $$ The studentized residuals divided the raw residuals relative to each endogenous variable by the modeled variance of the endogenous variable. $$ \varepsilon_{stud} =(\frac{Y_1 - \mu_1}{\sigma_1}, ..., \frac{Y_m - \mu_m}{\sigma_m}) $$ The normalized residuals multiply the raw residuals by the inverse of the square root of the modeled residual variance covariance matrix. $$ \varepsilon_{norm} = \varepsilon \Omega^{-1/2} $$

See Also

sCorrect to obtain lm2, gls2, lme2, or lvmfit2 objects.

Examples

Run this code
# NOT RUN {
## simulate data
set.seed(10)
m <- lvm(Y1~eta,Y2~eta,Y3~eta)
latent(m) <- ~eta
d <- lava::sim(m,20, latent = FALSE)

## standard linear model
e.lm <- lm(Y1~Y2, data = d)
sCorrect(e.lm) <- TRUE

sigma(e.lm)^2
mean(residuals(e.lm)^2)
mean(residuals2(e.lm)^2)

## latent variable model
e.lvm <- estimate(m, data = d)
sCorrect(e.lvm) <- TRUE
mean(residuals2(e.lvm)^2)

# }

Run the code above in your browser using DataLab