residuals2: Residuals With Small Sample Correction.

residuals2R Documentation

Residuals With Small Sample Correction.

Description

Extract residuals from a latent variable model. Similar to stats::residuals but with small sample correction.

Usage

residuals2(object, type, format, ssc, ...)

## S3 method for class 'lvmfit'
residuals2(
  object,
  type = "response",
  format = "wide",
  ssc = lava.options()$ssc,
  ...
)

Arguments

object

a lvmfit or lvmfit2 object (i.e. output of lava::estimate or lavaSearch2::estimate2).

type

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

format

[character] Use "wide" to return the residuals in the wide format (one row relative to each sample). Otherwise use "long" to return the residuals in the long format.

ssc

[character] method used to correct the small sample bias of the variance coefficients: no correction ("none"/FALSE/NA), correct the first order bias in the residual variance ("residual"), or correct the first order bias in the estimated coefficients "cox"). Only relevant when using a lvmfit object.

...

additional argument passed to estimate2 when using a lvmfit object.

Details

When argument object is a lvmfit object, the method first calls estimate2 and then extract 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}

Value

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

See Also

estimate2 to obtain lvmfit2 objects.

Examples

#### simulate data ####
set.seed(10)
n <- 101

Y1 <- rnorm(n, mean = 0)
Y2 <- rnorm(n, mean = 0.3)
Id <- findInterval(runif(n), seq(0.1,1,0.1))
data.df <- rbind(data.frame(Y=Y1,G="1",Id = Id),
           data.frame(Y=Y2,G="2",Id = Id)
           )

#### latent variable models ####
library(lava)
e.lvm <- estimate(lvm(Y ~ G), data = data.df)
residuals(e.lvm)
residuals2(e.lvm)
residuals(e.lvm) - residuals2(e.lvm)


bozenne/lavaSearch2 documentation built on Feb. 13, 2024, 10:18 p.m.