13  Multilevel modelling

13.1 Multilevel regression analysis

Many research topics have multilevel structured data which consist of multiple macro and micro units within each macro unit (e.g. individuals within countries, individuals within occupations, children within classes within schools, etc). Therefore, at each level there are both mean characteristics (fixed effects) and differences (random effects).

Single level regression modelling is not the appropriate method to use here. For instance, the effect of an X within clusters can be different from the effect between clusters. Indeed, single level regression does not consider the nested data structure, which may violate the uncorrelated errors assumption. It is likely that in a “rich” country all people have higher wages than in “poor” countries? If errors are correlated, this is likely to cause the following problems:

  • efficiency of estimators is low
  • standard errors are low
  • coefficients are often significant

It will also be unclear:

  • how much variation in Y is at each level?
  • how much the context impacts on individual level after controlling for other relevant factors?

The purpose of multilevel modelling (MLM) is to correct for biased estimates resulting from clustering and to provide correct standard errors, confidence intervals, as well as significance tests. It will thus decompose the total variance of Y into portions associated with each level (e.g. individual vs country).

We might want to know: How much variation is there in individual wages between and within countries? Which countries have particularly low and high wages ?

13.1.1 Fixed versus random effects

In order to understand MLM we need to understand random effects, and to understand random effects we need to understand variance. We often need to think more about where the variance in our system is showing up in our model. It allows us to decompose the variance of the dependent variable into:

  • within-context variance
  • between-context variance

So far, we are familiar with the residual variance from OLS, but might that residual variance be better attributed to within a group? Or between a group?

Recall that a factor is a categorical predictor that has two or more levels. Up to this point (e.g. in ANOVAs) we have only talked about fixed factors which assumes that the levels are separate, independent, and not similar. Fixed effects estimate separate levels with no relationship assumed between the levels. Fixed effects also assume a common variance known as homoscedasticity. Post-hoc adjustments are needed to do pairwise comparisons of the different factor levels. Random effects means that each level can be thought of as a random variable from an underlying process or distribution. Estimation of random effects provides inference about the specific levels (similar to a fixed effect), but also population level information (think about it as if each level of the effect is a draw from a random variable).

13.1.2 Example

Let’s image that we have 10 people (10 levels in our model) in a study about time spent to read the news on a daily basis over a 5-day period. Each day, we ask people to report how much time they spent reading the news (n=10*5=50) so that each person has 5 observations.

  • A fixed effect model enables us to estimate the means of the 10 individuals and assumes that each of the individuals has a common variance around their news reading time (variance is the same or similar for everyone in the study).
  • A random effect model enables us to estimate the mean and variance of the participants and to make a reasonable prediction about others that were not enrolled in the study (amount of time spent to read the news within a given individual is much likely to be similar than compared to someone else).

13.1.3 When not to use random effects

You might not want to use random effects when the number of factor levels is very low (there is not definitive recommendation here). Furthermore, it is commonly reported that you may want five or more factor levels for a random effect in order to really benefit from what the random effect can do. Note that a group with a large sample size and/or strong information (e.g. a strong relationship) will have very little influence of the grand mean and largely reflect the information contained entirely within the group.

Another case in which you may not want to random effect is when you do not assume that your factor levels come from a common distribution.

We already know about the OLS model (labelled “fixed effects” in the figure below). The next figure displays different types of random effects:

13.2 Fixed effect regression approach

One might account for the nested structured of the data by including a “grouping variable” for individuals (dummy for each country or year) or inclusion of contextual explanatory variables (e.g. gender equality index per country).

\[y_{ij}=\beta_0 + \beta_1*X_{ij} + \sum^{}_{C-1}(\beta_C*CD_j + \epsilon_{ij})\] where, \(\beta_0\) is the overall intercept (e.g. reference country or year) and \(\beta_C\) is the intercept for one country. The problem is that grouping parameters are treated as fixed effects ignoring random variability associated with macro-level characteristics. This “dummy variable approach” thus suggests that group differences are fixed effects. What if number of groups is very large? What about including group-level predictors as all degrees of freedom at group-level have been consumed by country dummy?

13.3 Random intercept model (with fixed slope)

The next model we will examine is the MLM with a random intercept and fixed slope.

13.3.1 Null model

In its simplest form, a MLM is in the form of a “null model” which contains no explanatory variables. It thus includes one regression constant (intercept) and assumes that this varies across contexts. The intercept is a random variable.

So far, we are able to answer questions like: Are there countries differences with respect to happiness? How much of this variation is due to these country differences?

13.3.2 Interclass Correlation Coefficient (ICC)

A MLM with a random intercept is a point where we can pause and run a diagnostic called the Interclass Correlation Coefficient (ICC).

\[ICC = \frac{\sigma^2_\alpha}{\sigma^2+\sigma^2_\alpha}\] The ICC tells us how much similarity is within contexts (i.e. countries). Basically, it accounts for the closeness of observations in same context relative to closeness of observations in different contexts.

For instance, it can tell us how much % of variance in Y (e.g. happiness) can be explained by context (belonging to a different country).

The ICC ranges from 0 (no clustering/single level data structure) and 1 (maximum clustering). In practice, 0 or 1 rarely occur. A general recommendation is that if the ICC is small, then use of a single level model.

13.3.3 Random intercept model (with fixed slope) including one predictor

At this stage, it is unclear which factors account for the variation. Are there more reasons why individuals and countries might differ with respect to happiness?

Random intercept model is a combination of variance component and a linear model (for a person \(i\) in country \(j\)). We thus have a fixed part (\(\beta_0 + \beta_x*X_{ij}\)), where the estimated parameters are the beta coefficients and are the same for each observation in the sample. We also have a random part (\(u_{0j} + \epsilon_{ij}\)), where estimated parameters are variances which are allowed to vary (e.g. across countries).

\[y_{ij}=\beta_0 + \beta_x*X_{ij} + u_{0j} + \epsilon_{ij}\] In this model, there are two random terms and therefore two types of residuals: \(u_j\) a the level-2 and \(e_{ij}\) at the level-1. There are also an overall (average) line \(β_0\) and group (average) lines \(β_0+u_j\).

So far, we are able to answer questions as: Do country differences in happiness remain after controlling for gender? How much of variation in happiness is due to country differences after controlling for gender? What is the relationship between an individual’s happiness and their gender?

13.3.4 Model assumptions

The model assumptions are that individual and group-level residuals are normally distributed. Furthermore, residuals at the same/different level are uncorrelated.

13.4 Random slope model (with random intercept)

The assumption of random intercept model suggests that group lines have all same slope as overall regression line in every group (effect of X on Y is the same for every country). Is this always valid? It can be argued that X is not a fixed but a random effect and that its slope can vary across groups.

We can account for the random slope by adding random term to coefficient of \(x_{1ij}\) (e.g. working hours), so it can be different for each group (e.g. country):

\[ y_{ij} = \beta_0 + (\beta_1 + u_{1j})*x_{1ij} + u_{0j} + \epsilon_{0ij} \] Note that one extra parameter \(u_{1ij}\) leads to two extra parameters \(\sigma^2_{u1}\) (variance in slopes between groups) and \(\sigma^2_{u01}\) (covariance between intercepts and slopes).

With a random slope model, we could now answer questions as: Does the effect of being employed has the same impact on happiness across countries?

Useful reference:

  • Sommet, N. & Morselli, D. (2021). Keep Calm and Learn Multilevel Linear Modeling: A Three-Step Procedure Using SPSS, Stata, R, and Mplus. (International Review of Social Psychology), 34(1). Available here.

13.4.1 Note on fixed intercept with random slope model

It might be the case that a model requires a fixed intercept but a random slope specification. This could be the case when we hypothesize that the effect of our predictor differs among groups, but when we want to fix the intercept because we know that all groups start in a similar place.

13.4.2 Testing the random part

The likelihood ratio test (LRtest) can be used to compare the random slope model to a random intercept model. The null hypothesis states that \(\sigma^2_{u1}\) and \(\sigma^2_{u0}\) should equal 0. If this is true, the random intercept model is more appropriate than a random slope model. If random intercept/slope coefficients are not significantly different from zero, this suggests that there is not much random variability in the slope/intercept. Therefore, there is no need to specify random parameter.

13.5 Cross-level interactions

Adding cross-level interactions allows us to assess whether contextual factors (Z) influence the effect of level-1 X variables. For instance, we can answer questions such as: Is the effect of gender on happiness stronger/weaker in countries with a high gender equality index?

\[ y_{ij} = \beta_0 + \beta_1*x_{ij} + \beta_2*Z_j + \beta_3x_{ij}*Z_j + u_{0j} + u_{1j}x_{ij} + \epsilon_{ij} \] The cross-level interaction should be included in the fixed part of model. Therefore, the direct main and interaction effects have to be interpreted together (similar to OLS): the \(\beta_3\) coefficient indicates the impact of each unit change in Z on the slope \(\beta_1\).

Useful links to external data sources to link at the macro-level are:

13.6 Variance decomposition approach

To assess how much variance is explained by a model, simple OLS regression entail the \(R^2\) statistic (proportion of explained variance). In MLM, there are several several (co)variances.

Hox (2010, pp.70) proposes to examine residual error variance in a sequence of models. This approach suggests examining the residual error variances in a sequence of models:

  • intercept-only model (since there are no explanatory variables in the model, it is reasonable to interpret variances as the error variances)
  • model including the level-1 predictors
  • model including the level-2 predictors
  • model with random coefficient
  • model with cross-level interaction

Other useful references:

  • Clarke, P., Crawford, C., Steele, F. & Vignoles, A. (2010). The choice between fixed and random effects models: some considerations for educational research. Institute of Education DoQSS, Working Paper No. 10. Available here.
  • Moehring, K. (2012). The fixed effect as an alternative to multilevel analysis for cross-national analyses. GK Soclife working paper. Available here

13.7 Centering and standardizing

The regression intercept equals the expected value of Y if all X are 0. However, what if 0 has no useful meaning or is not possible? (e.g. age of 0). In this case, it is useful to transform the X variables, for instance by centering them. There are two options:

  • Grand mean centering: computing variables as deviations from the overall mean (the constant in model reflects the mean of all cases)
  • Group mean centering: computing variables as deviation from the group mean (decomposes within vs. between effects)

13.8 Concluding remarks

Ultimately the application of MLM with fixed or random effects is done to capture more realism in the phenomenon we are seeking to describe with our model. While all models are to some degree incorrect, inaccurate estimation or pooling dissimilar information may extend the degree to which the model is inaccurate or misleading.

13.9 In a nutshell

Multilevel modelling (MLM) is used when data are nested (e.g., individuals within countries, students within schools). It accounts for structure, dependency, and clustering. MLM adds realism by modelling differences both within and between clusters. It avoids misleading inferences from pooled models and provides a richer, more accurate understanding of hierarchical data.

Nested data violate OLS assumptions (e.g., uncorrelated errors). MLM corrects bias, provides accurate standard errors, and decomposes variance into within- and between-group components. It answers: How much variation in Y is due to individuals vs. contexts?

Fixed vs. Random Effects:

  • Fixed Effects: Treat group differences as unique and unrelated; assume equal variances.
  • Random Effects: Treat groups as samples from a distribution; estimate group-specific variation and population-level patterns. Random effects are preferred when groups are many and assumed to come from a common distribution.

Fixed Effects Regression vs. MLM:

Using dummies for groups (fixed effects models) controls for group differences but: prevents inclusion of group-level variables and assumes no random variation across groups. MLM handles these issues more flexibly.

Types of models:

  • Random Intercept Models: Allow groups (e.g., countries) to differ in their baseline level of Y. Useful for computing the Intraclass Correlation Coefficient (ICC), which quantifies how much variance is due to grouping.
  • Random Slope Models: Allow the effect of X on Y to vary across groups. This adds variance in slopes and a covariance between slopes and intercepts. Answer questions like: Does the effect of X differ across contexts?

Cross-Level Interactions: Examine whether group-level characteristics (Z) change the strength of individual-level relationships (X on Y). Example: Does gender equality moderate the gender–happiness link?

Centering & Interpretation: Centering (grand-mean or group-mean) clarifies intercepts and decomposes within- vs. between-group effects. Particularly important when “zero” on predictors is meaningless.

Variance Decomposition: Unlike OLS and its single \(R^2\), MLM evaluates changes in multiple variance components across model stages (null, level-1 predictors, level-2 predictors, random slopes, cross-level interactions).

13.10 How it works in R?

See the lecture slides on MLM:

You can also download the PDF of the slides here: Click here

13.11 Quiz

True False Statement
If nested data are analyzed with a single-level OLS regression, the standard errors tend to be too small, so coefficients are more often significant.
An ICC close to 0 shows strong clustering in the data, which means a multilevel model is especially necessary.
In a random intercept model, the effect (slope) of X on Y is allowed to differ between groups, for example between countries.
Group mean centering makes it possible to separate within-group effects from between-group effects.
My results will appear here

13.12 Time to practice on your own

In the following example, we would like to assess cross-national differences in the gender gap in working time.

First thing you want is to download the data from the European Social Survey and select the variables: country information, working time, gender and additional explanatory variables (e.g. level of education, having children, etc). Then, you want to know how the variables are coded and to apply the necessary changes. You can also filter outliers (e.g. very high working hours).

Show the code
library(foreign)
db <- read.spss(file=paste0(getwd(),"/data/ESS10.sav"), 
                use.value.labels = T, 
                to.data.frame = T)
sel <- db |>
  dplyr::select(cntry,wkhct,gndr,eduyrs,chldhhe) |>
  stats::na.omit() 
# drop levels
sel$cntry <- droplevels(sel$cntry)
sel <- sel[complete.cases(sel),]
# recodings
sel$gndr <- ifelse(sel$gndr=="Female",1,0)
sel$wkhct <- as.numeric(sel$wkhct)
sel$eduyrs <- as.numeric(sel$eduyrs)
sel$cntry <- as.factor(sel$cntry)
# filtering
sel <- sel[sel$wkhct<60,]
hist(sel$wkhct)

Second, you start by conducting a fixed effects model.

Show the code
dummy_model <- lm(wkhct ~
                    gndr +
                    cntry, 
                  data=sel)
summary(dummy_model)
## 
## Call:
## lm(formula = wkhct ~ gndr + cntry, data = sel)
## 
## Residuals:
##     Min      1Q  Median      3Q     Max 
## -40.958  -0.958   1.287   3.350  24.145 
## 
## Coefficients:
##                      Estimate Std. Error t value Pr(>|t|)    
## (Intercept)          41.89245    0.22431 186.762  < 2e-16 ***
## gndr                 -2.31631    0.13146 -17.620  < 2e-16 ***
## cntrySwitzerland     -3.28260    0.35072  -9.360  < 2e-16 ***
## cntryCzechia         -0.56063    0.32625  -1.718 0.085746 .  
## cntryEstonia         -1.55739    0.34830  -4.471 7.83e-06 ***
## cntryFinland         -4.52794    0.32602 -13.889  < 2e-16 ***
## cntryFrance          -4.72122    0.33860 -13.943  < 2e-16 ***
## cntryGreece           1.15618    0.33480   3.453 0.000555 ***
## cntryCroatia          0.13675    0.37672   0.363 0.716614    
## cntryHungary          0.49649    0.33865   1.466 0.142649    
## cntryIceland         -5.13347    0.45526 -11.276  < 2e-16 ***
## cntryItaly           -2.60405    0.33088  -7.870 3.78e-15 ***
## cntryLithuania       -0.99921    0.34479  -2.898 0.003761 ** 
## cntryMontenegro       0.06515    0.90505   0.072 0.942617    
## cntryNorth Macedonia  0.25775    0.41787   0.617 0.537364    
## cntryNetherlands     -8.17255    0.36731 -22.250  < 2e-16 ***
## cntryNorway          -6.24267    0.37639 -16.586  < 2e-16 ***
## cntryPortugal        -1.20709    0.35048  -3.444 0.000575 ***
## cntrySlovenia        -1.14729    0.39832  -2.880 0.003978 ** 
## cntrySlovakia        -0.14967    0.37599  -0.398 0.690593    
## ---
## Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
## 
## Residual standard error: 8.059 on 15183 degrees of freedom
## Multiple R-squared:  0.1014, Adjusted R-squared:  0.1002 
## F-statistic: 90.14 on 19 and 15183 DF,  p-value: < 2.2e-16

From the output, we see that gender has a negative association with working hours: since gndr is coded 1 = female, 0 = male, the coefficient of about −2.32 means that women work on average about 2.3 fewer hours per week than men, controlling for country.

The intercept (41.89) represents the expected weekly working hours for men (gndr = 0) in the reference country.

We also note country differences: each country coefficient shows how its expected working time differs from the reference country (the country that does not appear in the list of dummies) once gender is held constant.

This model allows the intercept to vary across countries but assumes that the gender coefficient is identical in all countries.

The Multiple R-squared is 0.1014 (adjusted R-squared = 0.1002), which means that the model explains roughly 10% of the overall variance in working hours.

Third, you conduct a “null” model with only working hours. This allows you to assess how much variation in working hours can be attributed to individual differences and country differences.

Show the code
empty_model <- lme4::lmer(wkhct ~ 1 + (1 | cntry), 
                  data=sel)
stargazer::stargazer(empty_model, type="text", single.row = T)
## 
## ===============================================
##                         Dependent variable:    
##                     ---------------------------
##                                wkhct           
## -----------------------------------------------
## Constant                 38.691*** (0.599)     
## -----------------------------------------------
## Observations                  15,203           
## Log Likelihood              -53,490.590        
## Akaike Inf. Crit.           106,987.200        
## Bayesian Inf. Crit.         107,010.100        
## ===============================================
## Note:               *p<0.1; **p<0.05; ***p<0.01
# ICC
performance::icc(empty_model)
## # Intraclass Correlation Coefficient
## 
##     Adjusted ICC: 0.092
##   Unadjusted ICC: 0.092

The ICC (Intraclass Correlation Coefficient) can be interpreted as the proportion of the total variance in working hours that is attributable to differences between countries rather than between individuals within countries.

In a null random-intercept model, the ICC is calculated by dividing the between-country variance by the sum of the between-country and within-country variances.

Here, the ICC is 0.092, so about 9% of the variance in working hours is located at the country level, while about 91% is located at the individual level within countries. This indicates that observations from the same country are not entirely independent.

The intercept (about 38.7, rounded to ~39 hours) is the expected weekly working time for an “average” person in an “average” country in this random-intercept model. An “average” country refers to a hypothetical country whose country-level random effects are equal to zero

Note: At this stage, the model does not include any explanatory variables, so it does not tell us which individual- or country-level characteristics account for these differences.

Fourth, you can conduct a random intercept model with gender, and compare it with a random intercept with individual level variables (having children, years of education, etc.).

Show the code
# gender only 
random <- lme4::lmer(wkhct ~ gndr + (1 | cntry), 
                  data=sel)
stargazer::stargazer(random, type="text", single.row = T)
## 
## ===============================================
##                         Dependent variable:    
##                     ---------------------------
##                                wkhct           
## -----------------------------------------------
## gndr                     -2.315*** (0.131)     
## Constant                 39.874*** (0.606)     
## -----------------------------------------------
## Observations                  15,203           
## Log Likelihood              -53,338.130        
## Akaike Inf. Crit.           106,684.300        
## Bayesian Inf. Crit.         106,714.800        
## ===============================================
## Note:               *p<0.1; **p<0.05; ***p<0.01
# add more variables
random2 <- lme4::lmer(wkhct ~ gndr + eduyrs + chldhhe + (1 | cntry), 
                  data=sel)
stargazer::stargazer(random2, type="text", single.row = T)
## 
## ===============================================
##                         Dependent variable:    
##                     ---------------------------
##                                wkhct           
## -----------------------------------------------
## gndr                     -2.382*** (0.132)     
## eduyrs                   -0.061*** (0.018)     
## chldhheNo                -1.013*** (0.137)     
## Constant                 41.221*** (0.643)     
## -----------------------------------------------
## Observations                  15,203           
## Log Likelihood              -53,302.970        
## Akaike Inf. Crit.           106,617.900        
## Bayesian Inf. Crit.         106,663.700        
## ===============================================
## Note:               *p<0.1; **p<0.05; ***p<0.01

You can also assess the variance between and within countries using the VarCorr() function:

Show the code
as.data.frame(lme4::VarCorr(random))
##        grp        var1 var2      vcov    sdcor
## 1    cntry (Intercept) <NA>  6.778695 2.603593
## 2 Residual        <NA> <NA> 64.944980 8.058845

The output of VarCorr() contains several columns that help us interpret the variance components of a multilevel model:

  • grp: The grouping variable (here, country) or the residual component.
  • var1: The first variable associated with the random effect (e.g. the intercept).
  • var2: The second variable when a covariance or correlation between two random effects is estimated. Otherwise, it is NA.
  • vcov: Variance or covariance.
  • sdcor: Standard deviation or correlation.

Note: When var2 = NA, vcov reports the variance and sdcor reports its corresponding standard deviation (the square root of the variance). When both var1 and var2 contain a variable, vcov reports the covariance and sdcor reports the correlation between the two random effects. We will encounter this second situation when estimating a random-slope model. Standard deviations are expressed in the original measurement unit (here, working hours), whereas variances are expressed in squared units. Correlations are standardized and range between −1 and +1.

You can then visualize the country effect using sjPlot. The function produces a plot showing country-level deviations from the overall mean working hours in your random-intercept model (e.g., if the overall intercept = 39.9 hours and country A’s random effect is +1.8, then the predicted average for country A = 39.9 + 1.8 = 41.7 hours). Since the gender coefficient is fixed across countries in this model, the country-level deviation shifts the predicted working hours of both men and women by the same amount:

Show the code
sjPlot::plot_model(random, type = "re")

In the random-intercept model with gender only, the fixed intercept (about 39.9) means that the expected weekly working time for men (gndr = 0) in an “average” country is roughly 40 hours. The gender coefficient is about −2.32, so women work on average about 2.3 hours fewer per week than men, net of country differences.

The variance components show:

Between-country variance in the intercept: 6.78 (standard deviation = 2.60 hours). This means that country-specific expected working hours for men vary around the fixed intercept of 39.9 hours, with a standard deviation of 2.60 hours. Because the gender slope is fixed, the same country-level variation applies to women’s expected working hours.

Within-country (residual) variance: 64.94 (SD = 8.06 hours), reflecting unexplained individual-level variation in working hours after accounting for gender and country-level random intercepts.

When we add individual-level covariates (education and having children) in random2, the gender gap becomes slightly larger in absolute value (about −2.38), after controlling for education and the presence of children in the household.

eduyrs has a small negative association with working hours (each additional year of education is associated with approximately 0.06 fewer hours worked per week on average, holding the other variables constant).

chldhheNo is negative (−1.01), so individuals without children in the household work about 1 hour less per week than those with children, on average, controlling for other variables.

The intercept in this second model (41.22) represents the expected weekly working hours for men with zero years of education and children in the household in an “average” country Since zero years of education may not represent a typical respondent, centering eduyrs around its mean would make the intercept more substantively interpretable.

Both models allow average working hours to vary across countries but assume that the associations between the individual-level explanatory variables and working hours are identical across countries.

You can also compare the random-intercept model with the fixed-effects model. In R, anova() can be used to compare the fit of regression models using their log-likelihoods and information criteria, such as AIC and BIC. For appropriately nested models, it also provides a likelihood-ratio test assessing whether the more complex model significantly improves model fit.

Show the code
anova(random, dummy_model)
## refitting model(s) with ML (instead of REML)
## Data: sel
## Models:
## random: wkhct ~ gndr + (1 | cntry)
## dummy_model: wkhct ~ gndr + cntry
##             npar    AIC    BIC logLik -2*log(L)  Chisq Df Pr(>Chisq)    
## random         4 106683 106713 -53337    106675                         
## dummy_model   21 106617 106777 -53287    106575 100.25 17      8e-14 ***
## ---
## Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1

The anova() output compares how well the two models fit the data.

The fixed-effects model with country dummies (dummy_model) has a higher log-likelihood (−53,287 versus −53,337) and a lower AIC (106,617 versus 106,683) than the random-intercept model. Based on these two criteria, the fixed-effects model provides a better fit to the observed data.

However, the random-intercept model has a lower BIC (106,713 versus 106,777). BIC penalizes model complexity more strongly than AIC and, in this case, favors the more parsimonious random-intercept specification.

The choice between these models should therefore not depend exclusively on model fit. It should also reflect the research objectives and the assumptions underlying each specification.

Fifth, we can conduct a random slope model with gender:

Show the code
randomslope <- lme4::lmer(wkhct ~ 1 + gndr + 
                            (1 + gndr| cntry), 
                          data=sel)
stargazer::stargazer(randomslope, type="text", single.row = T)
## 
## ===============================================
##                         Dependent variable:    
##                     ---------------------------
##                                wkhct           
## -----------------------------------------------
## gndr                     -2.317*** (0.467)     
## Constant                 39.849*** (0.443)     
## -----------------------------------------------
## Observations                  15,203           
## Log Likelihood              -53,256.100        
## Akaike Inf. Crit.           106,524.200        
## Bayesian Inf. Crit.         106,570.000        
## ===============================================
## Note:               *p<0.1; **p<0.05; ***p<0.01

You can also assess the variance between countries. As explained above, vcov reports variances or covariances, while sdcor reports standard deviations or correlations. Unlike the previous random-intercept model, the random-slope model now includes two country-level random effects: the intercept and the gender slope.

Show the code
as.data.frame(lme4::VarCorr(randomslope))
##        grp        var1 var2      vcov     sdcor
## 1    cntry (Intercept) <NA>  3.532109 1.8793906
## 2    cntry        gndr <NA>  3.748612 1.9361333
## 3    cntry (Intercept) gndr  2.320979 0.6378506
## 4 Residual        <NA> <NA> 64.133868 8.0083624

You can visualize the random effects using sjPlot. This plot displays the estimated country-specific deviations from the fixed effects for each country: i) a random intercept (how men’s expected working hours in that country differ from the overall expected working hours for men); ii) a random slope for gender (how the country-specific gender coefficient differs from the overall fixed gender effect). Together, the two panels illustrate cross-national variation in both men’s expected working hours and the magnitude of the gender gap:

Show the code
sjPlot::plot_model(randomslope, type = "re")

The fixed intercept (~39.8) again represents expected working hours for men (gndr = 0) in an “average” country. The fixed slope for gndr of about −2.32 means that, on average across the country-level distribution, women work about 2.3 hours fewer per week than men.

The random effects tell us that both men’s expected working hours and the gender gap vary across countries.

Between-country variance in the intercept is 3.53, which corresponds to a standard deviation of about 1.88 hours. This means that country-specific expected working hours for men vary around the fixed intercept of 39.85 hours, with a standard deviation of 1.88 hours.

Between-country variance in the gender effect is 3.75, corresponding to a standard deviation of about 1.94 hours. This means that country-specific gender coefficients vary around the fixed gender coefficient of −2.32 hours, with a standard deviation of 1.94 hours. The gender gap can therefore be larger in some countries, smaller in others, or potentially reversed.

For example, if a country has a random slope of +1.5, its estimated gender coefficient is −2.32 + 1.5 = −0.82 hours. Conversely, if a country has a random slope of −1.5, its estimated gender coefficient is −2.32 − 1.5 = −3.82 hours.

The correlation between the country-specific intercepts and slopes is 0.64. A positive correlation means that in countries where men tend to work longer hours, the gender coefficient also tends to be less negative or more positive. Where women work fewer hours than men, this corresponds to a smaller gender gap. Conversely, countries where men work fewer hours tend to have more negative gender coefficients.

The residual variance (~64.13, SD = 8.01) reflects within-country, person-level variation that is not captured by gender and the country-level random effects.

Overall, the random-slope model relaxes the assumption that the gender effect is identical across countries. It allows us to investigate whether country-level characteristics can explain why the gender gap is larger in some countries than in others.

You can also test whether it makes sense to rely on a random-slope model instead of a random-intercept model with a fixed gender slope. Unlike the previous comparison between country fixed effects and random intercepts, these two mixed-effects models are nested: the simpler model can be obtained by removing the country-level random slope for gender and its covariance with the random intercept:

Show the code
anova(randomslope, random)
## refitting model(s) with ML (instead of REML)
## Data: sel
## Models:
## random: wkhct ~ gndr + (1 | cntry)
## randomslope: wkhct ~ 1 + gndr + (1 + gndr | cntry)
##             npar    AIC    BIC logLik -2*log(L)  Chisq Df Pr(>Chisq)    
## random         4 106683 106713 -53337    106675                         
## randomslope    6 106524 106570 -53256    106512 162.52  2  < 2.2e-16 ***
## ---
## Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1

The anova() output (here again using a likelihood-ratio test rather than a classical ANOVA) compares how well the two nested mixed-effects models fit the data.

The random-slope model, which allows the effect of gender to vary across countries, has a higher log-likelihood and lower AIC than the simpler random-intercept model. It also has a lower BIC, indicating that the improvement in model fit is sufficient to justify the additional parameters according to both information criteria.

The chi-square statistic (162.52 with 2 degrees of freedom, p-value below 2.2 × 10⁻¹⁶) provides strong evidence that allowing the gender slope to vary across countries improves model fit.

Strictly speaking, when testing random effects, the usual chi-square approximation should be interpreted with caution because a variance component cannot be negative, and the null hypothesis places it on the boundary of the parameter space. A parametric bootstrap test can provide a more rigorous alternative. Nevertheless, the substantial improvement in log-likelihood, AIC and BIC provides strong support for the random-slope specification in this example.

Thus, the random-slope specification provides a better description of the data than the random-intercept model alone.

This raises an important substantive question: which country-level characteristics explain the cross-national variation in the gender gap? In the next step, we can introduce a country-level variable, such as the Gender Equality Index (GEI), and examine its interaction with gender.

Finally, you can add a cross-level interaction using the information provided by the Gender equality Index.

Show the code
sel$cntry = droplevels(sel$cntry)
sel$cntry = as.numeric(sel$cntry)
sel$GEI = NA
sel$GEI[sel$cntry==1] = 58.8
sel$GEI[sel$cntry==2] = NA
sel$GEI[sel$cntry==3] = 55.7
sel$GEI[sel$cntry==4] = 59.8
sel$GEI[sel$cntry==5] = 73.4
sel$GEI[sel$cntry==6] = 74.6
sel$GEI[sel$cntry==7] = 51.2
sel$GEI[sel$cntry==8] = NA
sel$GEI[sel$cntry==9] = 55.6
sel$GEI[sel$cntry==10] = NA
sel$GEI[sel$cntry==11] = 63
sel$GEI[sel$cntry==12] = 55.5
sel$GEI[sel$cntry==13] = NA
sel$GEI[sel$cntry==14] = NA
sel$GEI[sel$cntry==15] = 72.1
sel$GEI[sel$cntry==16] = NA
sel$GEI[sel$cntry==17] = 59.9
sel$GEI[sel$cntry==18] = 68.3
sel$GEI[sel$cntry==19] = 54.1
# cross level interaction term
# random slope with cross-level interaction
randomslope2 <- lme4::lmer(wkhct ~ 1 + gndr + GEI +
                             gndr*GEI +
                            (1 + gndr| cntry), 
                          data=sel)
stargazer::stargazer(randomslope2, type="text", single.row = T)
## 
## ===============================================
##                         Dependent variable:    
##                     ---------------------------
##                                wkhct           
## -----------------------------------------------
## gndr                      6.325** (3.095)      
## GEI                      -0.208*** (0.035)     
## gndr:GEI                 -0.135*** (0.050)     
## Constant                 52.664*** (2.164)     
## -----------------------------------------------
## Observations                  12,020           
## Log Likelihood              -41,711.130        
## Akaike Inf. Crit.           83,438.260         
## Bayesian Inf. Crit.         83,497.420         
## ===============================================
## Note:               *p<0.1; **p<0.05; ***p<0.01

Here, we allow both gender and the Gender Equality Index (GEI) to influence working hours, and we include a cross-level interaction between GEI (country level) and gender (individual level).

The coefficient for GEI (−0.208) tells us that, for men (gndr = 0), a one-point increase in GEI is associated with approximately 0.21 fewer working hours per week. Higher gender equality is associated with slightly shorter working hours for men.

The interaction term gndr:GEI (−0.135) means that the association between GEI and working hours is more negative for women than for men.

For women (gndr = 1), the slope of GEI is −0.208 − 0.135 = −0.343. Thus, each additional GEI point is associated with approximately 0.34 fewer working hours per week for women.

This implies that, as gender equality increases, predicted working hours decrease more strongly for women than for men. Since women already have shorter predicted working hours than men across the range of GEI values displayed, the gender gap in working hours widens.

Equivalently, the difference in working hours between women and men (women − men) changes by −0.135 hours for each one-point increase in GEI. This difference becomes increasingly negative, meaning that higher GEI is associated with a larger gender gap in working time.

The intercept (52.7) represents the expected weekly working hours for men in a hypothetical country with GEI = 0. Since actual GEI values are approximately 50–75, this intercept is not substantively meaningful.

Note: Centering GEI around its mean before fitting the model would make the intercept more interpretable, as it would then represent the expected working hours for men in a country with an average GEI. Centering would also make the gender coefficient more interpretable, as it would represent the gender difference in working hours at the average level of GEI.

Optionally, you can visualize the interaction effect:

Show the code
library(dplyr)
## 
## Attaching package: 'dplyr'
## The following objects are masked from 'package:stats':
## 
##     filter, lag
## The following objects are masked from 'package:base':
## 
##     intersect, setdiff, setequal, union
# Choose representative GEI values (mean ± 1 SD is standard)
GEI_mean <- mean(sel$GEI, na.rm = TRUE)
GEI_sd   <- sd(sel$GEI, na.rm = TRUE)
# make predictions
pred_grid <- expand.grid(
  gndr = c(0, 1),
  GEI  = c(GEI_mean - GEI_sd,
           GEI_mean,
           GEI_mean + GEI_sd)
)
pred_grid$pred <- predict(randomslope2,
                          newdata = pred_grid,
                          re.form = NA)  # fixed effects only
# plot
library(ggplot2)
ggplot(pred_grid,
       aes(x = GEI, y = pred, color = factor(gndr))) +
  geom_line(size = 1.3) +
  geom_point(size = 3) +
  scale_color_manual(values = c("darkblue", "red"),
                     labels = c("Men", "Women"),
                     name = "Gender") +
  labs(x = "Gender Equality Index",
       y = "Predicted working hours",
       title = "Cross-level interaction: GEI × Gender") +
  theme_minimal()

As the GEI increases, predicted weekly working hours decrease for both men and women. Consequently, the gender gap in predicted working hours widens at higher levels of gender equality.