gen_u_mmrem | R Documentation |
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
.
gen_u_mmrem( .n_sch, .u_resid_var, .clust_cov, .gamma_z, .override_n_sch = FALSE )
.n_sch |
Numeric scalar. Gives the total number of schools in the
dataset. The variance-covariance matrix for predictor z will have dimensions
|
.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
|
.gamma_z |
Numeric scalar. The school-level effect of the
|
.override_n_sch |
Logical. This function utilizes
|
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
.
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
.
## 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)
Add the following code to your website.
For more information on customizing the embed code, read Embedding Snippets.