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,
  impute = FALSE,
  se.impute = FALSE,
  keep.newdata = FALSE,
  format = "long",
  simplify = TRUE,
  seed = NULL,
  ...
)

Arguments

object

a lmm object.

newdata

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

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.

keep.newdata

[logical] Should the dataset relative to which the predictions are evaluated be output along side the predicted values? Only possible in the long format.

format

[character] Should the prediction be output in a matrix format with clusters in row and timepoints in columns ("wide"), or in a data.frame/vector with as many rows as observations ("long")

simplify

[logical] Simplify the data format (vector instead of data.frame) and column names (no mention of the time variable) when possible.

seed

[integer, >0] Random number generator (RNG) state used when starting imputation. If NULL no state is set.

...

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 Nov. 9, 2023, 1:06 a.m.