Chapter 28 Multilevel analysis

Multilevel analysis (MLA) is used for the analysis of hierarchical data. Data at the lowest level are clustered in higher levels. An important application for MLA is in ESM/EMA data. This type of data are called intensive longitudinal data. Subjects have measurements on multiple occasions. The occasions are clustered within the subjects. In these examples, the dataset/dataframe is called dat, the predictor is called predictorVariable(more than one are possible), the clustervariable is called idNumber, the dependent variable is called dependentVariable. For longitudinal data there is an indexVariable, which indicates the repeated measures. Furthermore, you may need a lagged variable to control for autocorrelation: for example, a lagged one variable: predictorVariableLag1.

28.1 jamovi

MLA can be done in jamovi with the module “GAMLj: general analysis for linear models for jamovi”. After loading this module, the Linear Models menu must be selected and then the option MIXED Model. In the input window the variables are placed as shown in Figure 28.1. Under the heading Fixed Effects the variables for the fixed effects are selected. Here, it is also possible to define interaction effects.

Select the variables for the analysis

Figure 28.1: Select the variables for the analysis

Under the heading Random Effects the variables for the random effects are selected, as shown in Figure 28.2, and when there are more than one, the correlations between these effects can also be specified by checking the box correlated.

Define the random effects for the analysis

Figure 28.2: Define the random effects for the analysis

Example 2 concerns a longitudinal example. Here we have an additional variable, called indexVariable, which is an index of time, e,g, the days or other measurement moments. Figure 28.3 shows the input in jamovi, includign a lagged variable to model the auto-correlation.

Select the variables for the longitudinal analysis

Figure 28.3: Select the variables for the longitudinal analysis

28.2 R

In R there are several possible packages for multilevel anaysis. Here the function that is illustrated, is called lmer, which is in the package lme4. The data are in data set dat. Example 1 is called as follows. So, make sure to install the lme4 package first and load it in your R session with library(lme4).

  model <- lmer(dependentVariable ~ predictorVariable + 
               (1 + predictorVariable| idNumber, data = dat)

Example 2, for longitudinal data, is called with the following code. A lagged variable can be constructed and added to the fixed part of the model to model the auto-correlation.

  model <- lmer(dependentVariable ~ predictorVariable + indexVariable + predictorVariableLag1 +
               (1 + predictorVariable| idNumber, data = dat)

The output can be put in an object and with, for example, the summary() function this output can be inspected.


If you want to see p-values for the fixed effect, you should load the package lmerTest. With this package the output of the calls to lmer automatically provide p-values.

28.3 SPSS

In SPSS the MIXED procedure can be used to do multilevel analysis. A simple model with one predictor and two random efffects, for respectively the intercept and the predictor, can be run with the following syntax.

MIXED dependentVariable  WITH predictorVariable
  /FIXED= INTERCEPT predictorVariable 
  /RANDOM= INTERCEPT predictorVariable| SUBJECT(idNUmber)  COVTYPE(UN).

Example 2 shows a longitudinal example again.

MIXED dependentVariable WITH predictorVariable indexVariable
  /FIXED=INTERCEPT predictorVariable | SSTYPE(3)
  /RANDOM=INTERCEPT  predictorVariable | SUBJECT(idNumber) COVTYPE(VC)
  /REPEATED=indexvariable  | SUBJECT(idNumber) COVTYPE(AR1).