fitted.lmm: Predicted Mean Value For Linear Mixed Model

View source: R/fitted.R

fitted.lmmR Documentation

Predicted Mean Value For Linear Mixed Model

Description

Predicted Mean Value For Linear Mixed Model

Usage

## S3 method for class 'lmm'
fitted(
  object,
  newdata = NULL,
  format = "long",
  keep.newdata = FALSE,
  impute = FALSE,
  se.impute = FALSE,
  ...
)

Arguments

object

a lmm object.

newdata

[data.frame] the covariate values for each cluster.

format

[character] Should the predicted mean be output relative as a vector ("long"), or as a matrix with in row the clusters and in columns the outcomes ("wide").

keep.newdata

[logical] Should the argument newdata be output along side the predicted values? The output will then be a data.frame.

impute

[logical] Should the missing data in the outcome be imputed based on covariates and other outcome values from the same cluster.

se.impute

[character] If FALSE the most likely value is imputed. Otherwise the imputed value is sampled from a normal distribution. The value of the argument determine which standard deviation is used: all uncertainty about the predicted value ("total"), only uncertainty related to the estimation of the model parameters ("estimate"), or only uncertainty related to the residual variance of the outcome ("residual"). Passed to predict.lmm.

...

Not used. For compatibility with the generic method.

Value

When format="wide", a data.frame with as many rows as clusters. When format="long" or keep.newdata==TRUE, a data.frame with as many rows as observations. Otherwise:

  • if impute=FALSE a vector of length the number of row of newdata containing the fitted values (i.e. based on the covariates only).

  • if impute=TRUE a vector of length the number of missing values in the outcome of newdata containing the cluster-specific conditional means (i.e. based on the covariates and outcome measurements from the same cluster).

When keep.newdata==TRUE, a dataframe with an additional column containing the fitted values (i.e. based on the covariates only). If impute=TRUE, the missing value in the outcome column are replaced by the cluster-specific conditional means (i.e. based on the covariates and outcome measurements from the same cluster).

Examples

#### simulate data in the long format ####
set.seed(10)
dL <- sampleRem(100, n.times = 3, format = "long")

#### fit Linear Mixed Model ####
eCS.lmm <- lmm(Y ~ X1 + X2 + X5, repetition = ~visit|id,
               structure = "CS", data = dL, df = FALSE)

## prediction
fitted(eCS.lmm)
fitted(eCS.lmm, newdata = data.frame(X1 = 1, X2 = 2, X5 = 3))
fitted(eCS.lmm, newdata = data.frame(X1 = 1, X2 = 2, X5 = 3), keep.newdata = TRUE)

#### fit Linear Mixed Model with missing data ####
dL2 <- dL
dL2[3,"Y"] <- NA
eCS2.lmm <- lmm(Y ~ X1 + X2 + X5, repetition = ~visit|id,
                structure = "CS", data = dL2, df = FALSE)

## most likely value to impute
fitted(eCS2.lmm, impute = TRUE)
head(fitted(eCS2.lmm, impute = TRUE, keep.newdata = TRUE))

## multiple imputation
dL2.imp1 <- data.frame(imp = "1",
    fitted(eCS2.lmm, impute = TRUE, se.impute = "total", keep.newdata = TRUE))
dL2.imp2 <- data.frame(imp = "2",
    fitted(eCS2.lmm, impute = TRUE, se.impute = "total", keep.newdata = TRUE))
head(dL2.imp1)
head(dL2.imp2)

LMMstar documentation built on Jan. 7, 2023, 1:20 a.m.