Random-effect of a discrete categorical variable

Institutional descrimination: Random-effect of Girl Model

\[ y_{i}^{1} = \Lambda_{i,j}^{1,2} \times \eta_j^{2}+ e_{i}, \]

\[ e_i^{1} \sim N(0, \Theta^{1,1}) \] \[ \eta_{j}^2 \sim N(\alpha^{2}, \Psi^{2,2}) \]

xxM Model Matrices

Level-1 (Student): Within matrices

Residual Covariance Matrix

We have a single dependent variable and hence a single parameter level-1 residual variance \( (\Theta_{1,1}) \) at level-1. Hence, the residual covariance or theta matrix is a (1×1) matrix:

\Theta^{1,1} =

Level-2 (School): Within Matrices

At level-2, we have two latent variables: intercept and slope of Girl. Hence, we have two latent means and a covariance matrix.

Latent Means

With two latent variables, the latent variable mean matrix is a (2×1) matrix:

\alpha^2 =
\alpha^2_1 & \\

\( (\alpha^2_1) \) is the mean of the intercept and \( (\alpha^2_2) \) is the mean of the slope parameter or the average effect of \( (G_{ij}) \) on \( (y_{ij}) \).

Latent Factor Covariance Matrix

The latent covariance matrix is a (2×2) matrix with two variances and single covariance:

\psi_{1,1}^{2,2} & \\
\psi_{2,1}^{2,2} & \psi_{2,2}^{2,2}

\( (\psi_{1,1}^{2,2}) \) is the variance of the intercept factor representing variability in the intercept of \( (y_{ij}) \) across schools and \( (\psi_{2,2}^{2,2}) \) is the variance of the slope parameter representing between-persons variability in the effect of \( (G_{ij}) \) on \( (y_{ij}) \). Finally, \( (\psi_{2,1}^{2,2}) \) is the covariance between intercept and slope .

Across level matrices: School to Student

Across-level factor-loading matrix

We need to capture the effect of level-2 intercept and slope factors on the level-1 dependent variable using a factor-loading matrix with fixed parameters. The factor-loading matrix \( (\Lambda^{1,2}) \) has a single row and two columns (1×2):

\Lambda^{1,2} =
1.0_{i,j}^{1,2} & G_{i,j}^{1,2}

The first column is fixed to 1.0, whereas the second column is fixed to student-specific values of girl or \( (G_{ij}) \).

Path Diagram

(insert diagram)

Code Listing



There is a single student level dependent variable course and a single independent variable girl :

  1. We do not wish to estimate any factor-loadings. Factor-loadings are fixed to 1.0 and \( (Girl_{ij}) \). Hence, both elements of pattern matrix are zero: \[ \Lambda_{pattern} = \begin{bmatrix} 0 & 0 \end{bmatrix} \]
  2. We want to fix the first-factor loading to 1.0 (intercept). We use the value matrix to provide the fixed-value of 1.0 for the first factor-loading. The second factor-loading does not have a single fixed value for every observation. Instead each observation \( (i) \) would have its own value for that factor-loading \( (Girl_{ij}) \). Clearly, the value matrix cannot be used for providing individual specific fixed values. Hence, the second element in the value matrix is left as 0.0. xxM ignores it internally. \[ \Lambda_{value} = \begin{bmatrix} 1.0 & 0.0 \end{bmatrix} \]
  3. The job of fixing the factor-loading is left to the label matrix. A label matrix is used to assign labels to each parameter within the matrix. Label matrices can be used to impose equality constraints across matrices. Any two parameters with the same label are constrained to be equal. Label matrices are also used for specifying that a specific parameter is to be fixed to data-values. In this case, the first-label is irrelevant as that parameter has already been fixed to 1.0. We use a descriptive label \( (One) \) as the first label indicating that the loading is fixed to \( 1.0 \). The second factor-loading is the one we are interested in. We want to fix the second factor-loading to the observation specific values of the predictor \( (Girl_{ij}) \). This is accomplished by using a two-part label: levelName.predictorName. In this case, the predictor is a student level variable. Hence, the first part of the label is student. The second part is the actual predictor name, in this case girl. \[ \Lambda_{label} = \begin{bmatrix} One & student.girl \end{bmatrix} \]

Load xxM

xxM library needs to be loaded first.

Prepare R datasets

For this analysis, we use import data from the text file downloaded from XXx. Separate student and school datasets are created as follows:

student dataset includes the following columns:

  • student Unique student IDs
  • school School IDs for
  • course dependent varaible
  • girl student level predictor

school dataset includes the following columns:

  • school Unique school IDs

Construct R-matrices

For each parameter matrix, construct three related matrices:

  1. pattern matrix: A matrix indicating free or fixed parameters.
  2. value matrix: with start or fixed values for corresponding parameters.
  3. label matrix: with user friendly label for each parameter. label matrix is optional.

Construct model

xxmModel() is used to declare level names. The function returns a model object that is passed as a parameter to subsequent statements. Variable name for the return value can be abything. A

Add submodels

For each declared level xxmSubmodel() is invoked to add corresponding submodel to the model object. The function adds three types of information to the model object:

  • parents declares a list of all parents of the current level.
    • Level with the independent variable is the parent level.
    • Level with the dependent variable is the child level.
  • variables declares names of observed dependent (ys), observed independent (xs) and latent variables (etas) for the level.
  • data R data object for the current level.

Add within-level matrices

For each declared level xxmWithinMatrix() is used to add within-level parameter matrices. For each parameter matrix, the function adds the three matrices constructed earlier:

  • pattern
  • value
  • label (optional)

Add across-level matrices

Pairs of levels that share parent-child relationship have regression relationships. xxmBetweenMatrix() is used to add corresponding rergession matrices connecting the two levels.

  • Level with the independent variable is the parent level.
  • Level with the dependent variable is the child level.

For each parameter matrix, the function adds the three matrices constructed earlier:

  • pattern
  • value
  • label (optional)

Estimate model parameters

Estimation process is initiated by xxmRun(). If all goes well, a q&d summary of the results is printed.

Estimate profile-likelihood confidence intervals

Once parameters are estimated, confidence intervals are estimated by invoking xxmCI(). Depending on the the number of observations and the complexity of the model, xxmCI() may take a long time to compute. xxmCI() also prints a summary of parameter estimates and CIS.

View results

A summary of results may be retrived as an R list by a call to xxmSummary(). The returned list has two elements:

  1. fit is a list with five elements:
    • deviance is \( -2 Log Likelihood \) for the maximum likelihood fit function.
    • nParameters is the total number of unique parameters.
    • nObservations is the total number of observations across all levels.
    • aic is Akaike’s Information Criterion or AIC computed as \( -2ll + 2*p \).
    • bic is Bayesian Information Criterion or BIC computed as \( -2ll + p*\log(n) \).
  2. estimates is a single table of free parameter estimates
    All xxM parameters have superscripts {child, parent} and subscripts {to, from}. xxM adds a descriptive parameter label if one is not already provided by the user.

Free model object

xxM model object may hog a significant amount of RAM outside of R’s workspace. This memory will automatically be released, when the workspace is cleared by a call to rm(list=ls()) or at the end of the R session. Alternatively, it is recommended that xxmFree() may be called to release the memory.

Proc Mixed

Import data

For small models such as the current one Proc Mixed is sufficient.

SAS code for a random-slopes model uses a CLASS statement to identify the level-2 units, in this case school. The MODEL statement estimates the fixed-effects \( (\alpha) \). The RANDOM statement specifies that the level-1 intercepts and the effect of level-1 predictor \( (girl_{ij}) \) is allowed to vary across “subject” (i.e., schools). The covariance among the random-effects \( (G) \) is freely estimated (specified by “type = UN”). The \( G \) matrix corresponds to the xxM \( (\Psi) \) matrix. Finally, like all regression models, Proc Mixed estimates the residual variance of the level-1 dependent variable \( (\Theta_{1,1}) \) by default. The important thing to note is that there is one-to-one correspondence between the parameters estimated in Proc Mixed and SEM.

Proc Mixed

For large models PROC HPMIXED is required.

Proc HPMixed