mixedbiastest {mixedbiastest}R Documentation

Bias Diagnostic for Linear Mixed Models

Description

Performs a permutation test to assess the bias of fixed effects in a linear mixed model fitted with 'lmer'. This function computes the test statistic and performs the permutation test, returning an object of class '"mixedbiastest"'.

Usage

mixedbiastest(model, n_permutations = 10000, k_list = NULL, verbose = FALSE)

Arguments

model

An object of class 'lmerMod' fitted using 'lmer' from the 'lme4' package.

n_permutations

Integer. Number of permutations to perform (default is 10000).

k_list

Optional list of numeric vectors. Each vector specifies a linear combination of fixed effects to test. If 'NULL', each fixed effect is tested individually.

verbose

Logical. If 'TRUE', prints detailed messages during execution.

Details

**Note:** This function currently supports only models with diagonal random effects covariance matrices (i.e., the G matrix is diagonal). The methodology for non-diagonal G matrices is described in Karl and Zimmerman (2021), but is not implemented in this version of the package.

See the list_fixed_effects function if you would like to estimate the bias of a contrast of fixed effects.

Value

An object of class "mixedbiastest" containing:

results_table

A data frame with the test results for each fixed effect or contrast, including bias estimates and p-values.

permutation_values

A list of numeric vectors containing permutation values for each fixed effect or contrast.

model

The original lmerMod model object provided as input.

Acknowledgments

Development of this package was assisted by GPT o1-preview, which helped in constructing the structure of much of the code and the roxygen documentation. The code is based on the R code provided by Karl and Zimmerman (2020).

References

Karl, A. T., & Zimmerman, D. L. (2021). A diagnostic for bias in linear mixed model estimators induced by dependence between the random effects and the corresponding model matrix. Journal of Statistical Planning and Inference, 212, 70–80. doi:10.1016/j.jspi.2020.06.004

Karl, A., & Zimmerman, D. (2020). Data and Code Supplement for 'A Diagnostic for Bias in Linear Mixed Model Estimators Induced by Dependence Between the Random Effects and the Corresponding Model Matrix'. Mendeley Data, V1. doi:10.17632/tmynggddfm.1

Examples

if (requireNamespace("plm", quietly = TRUE) && requireNamespace("lme4", quietly = TRUE)) {
library(lme4)
data("Gasoline", package = "plm")
# Fit a random effects model using lme4
mixed_model <- lmer(lgaspcar ~ lincomep + lrpmg + lcarpcap + (1 | country),
                    data = Gasoline)
result <- mixedbiastest(mixed_model)
print(result)
plot(result)
}
if (requireNamespace("lme4", quietly = TRUE)) {
library(lme4)
example_model <- lmer(y ~ x + (1| group), data = example_data)
result2 <- mixedbiastest(example_model)
print(result2)
plot(result2)


#Simulate data
set.seed(123)
n_groups <- 30
n_obs_per_group <- 10
group <- rep(1:n_groups, each = n_obs_per_group)
x <- runif(n_groups * n_obs_per_group)
beta0 <- 2
beta1 <- 5
sigma_u <- 1
sigma_e <- 0.5
u <- rnorm(n_groups, 0, sigma_u)
e <- rnorm(n_groups * n_obs_per_group, 0, sigma_e)
y <- beta0 + beta1 * x + u[group] + e
data_sim <- data.frame(y = y, x = x, group = factor(group))
model3 <- lmer(y ~ x + (1 | group), data = data_sim)
result3 <- mixedbiastest(model3, verbose = TRUE)
plot(result3)
}

[Package mixedbiastest version 0.3.0 Index]