gen_u_mmrem: gen_u_mmrem

View source: R/gen_u_mmrem.R

gen_u_mmremR Documentation

gen_u_mmrem

Description

This function generates the residual term for the random slope in the multiple membership random effects model used in this simulation study. To construct a random effect that is correlated across clusters, a predictor, z, is generated with variance-covariance matrix built by gen_z_varcov. The cluster-correlated predictor is then used in this function to predict the random slope residual, u0j, with explained variance given by clust_cov (an argument passed to gen_z_varcov) and unexplained variance given by .resid_var. Because no other school-level predictors will be used in this simulation, total variance of the random intercept should be clust_cov[1] + .resid_var.

Usage

gen_u_mmrem(
  .n_sch,
  .u_resid_var,
  .clust_cov,
  .gamma_z,
  .override_n_sch = FALSE
)

Arguments

.n_sch

Numeric scalar. Gives the total number of schools in the dataset. The variance-covariance matrix for predictor z will have dimensions .n_sch x .n_sch.

.u_resid_var

Numeric scalar. Gives the residual variance of u0j (i.e., the variance unexplained after controlling for the school-level predictor, z).

.clust_cov

Numeric vector. The first element of the vector gives the variance of all schools' predictors, z. If present, the second element gives the covariance of z between schools k and k + 1. The values given in .clust_cov apply to all schools (that is, similar to a Toeplitz pattern). Any off-diagonal values (i.e., covariances) not specified will default to 0. The main diagonal is the variance explained by the predictor.

.gamma_z

Numeric scalar. The school-level effect of the z_predictors on the random intercept.

.override_n_sch

Logical. This function utilizes rmvn to draw from a multivariate normal distribution of size .n_sch x .n_sch. In testing, values of .n_sch > 3000 caused this function to break. It is possible that machines with more memory or faster CPUs would fare better with this step. Unless this parameter is set to .override_n_sch = TRUE, function will throw an error if .n_sch > 3000. Defaults to .override_n_sch = FALSE.

Details

NOTE: this function relies on the rmvn and gen_z_varcov, and is_off_diag functions, the first two of which (especially rmvn) get quite clunky when matrix sizes are large. Make sure that .n_sch is given a reasonable value to avoid this. In testing, it seems that 5000 works slowly, 10000 is very slow, and any more than that brings things to a complete halt. Unless .override_n_sch = TRUE (where FALSE is the default), function will throw an error if .n_sch > 3000.

Value

This function returns a dataframe with four elements: "sch_id", the ID variable (equal to seq_len(.n_sch)); "u_residual", the random intercept residual term for each school (equal to predictor_z + v_residual); "z_predictor", the value of the school-level predictor, z, for each school; and "v_residual", the residual term for "u_residual" (i.e., the residual after predicting u_residual from z_predictor) for each school. Each element is vector with length equal to .n_sch.

Examples

## Not run: 

# set the number of schools
j <- 50

# set the residual's residual variance
v <- 0.2

# set the predictor's variance (and covariance with other schools, if
desired - see \code{gen_z_varcov} for more info)
z <- c(0.8, 0.3, 0.1) # variance (0.8); covar school k/k+1 (0.3); k/k+2 (0.1)

# output school-level information
gen_u_mmrem(
  .n_sch = j,
  .u_resid_var = v,
  .clust_cov = z
)


## End(Not run)

tessaleejohnson/corclus documentation built on Oct. 11, 2022, 3:46 a.m.