# Dyestuff: Random-Intercepts model

Dyestuff example included with lme4 involves a random-intercepts model of yield in different batches of production of chemicals. Level-1 is response and level-2 is batch

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

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

# xxM Model Matrices

## Level-1 (Response): 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} = \begin{bmatrix} \theta^{1,1}_{1,1} \end{bmatrix}$

## Level-2 (Batch): Within Matrices

At level-2, we have a single latent variable: Random-intercept of yield.

### Latent Means

With a single latent variable, the latent variable mean matrix is a (1×1) matrix:

$\alpha^2 = \begin{bmatrix} \alpha^2_1 \end{bmatrix}$

$$(\alpha^2_1)$$ is the mean of the intercept for yield.

### Latent Factor Covariance Matrix

The latent covariance matrix is a (1×1) matrix with single variance of the intercept:

$\Psi^{2,2}= \begin{bmatrix} \psi_{1,1}^{2,2} \end{bmatrix}$

## Across level matrices: Batch to Response

The factor-loading matrix $$(\Lambda^{1,2})$$ has a single element fixed to 1.0.

$\Lambda^{1,2} = \begin{bmatrix} 1.0_{i,j}^{1,2} \end{bmatrix}$

(insert diagram)

# Code Listing

xxMSAS: Proc MixedR: lmer

## xxM

xxM library needs to be loaded first.

### Prepare R datasets

For this analysis, we use data packaged with lme4.
For analysis with xxM separate response and batch datasets are created as follows:

response dataset includes the following columns:

• response Unique response IDs (integer)
• batch IDs for the parent level (integer)
• yield the dependent varaible (numeric)

batch dataset includes the following columns:

• batch Unique batch IDs (integer)

### 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 anything.

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.

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)

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.