# How to deal with subset of variables that are invariant for subsets of sample

I am thinking about a problem in which I have a sample which consists of subsamples with the same values for a subsample of independent variables. I want to apply a simple linear regression with categorical variables.

I want to determine the relationship between the reading abilities of children. Let us imagine I have collected the independent variables city, school, number of siblings, age and number of owned books.

For cities with only one school, it will be clear that all children from this city will have the same values for the independent variable school.

Do I need to cope with this special invariance of conditions for my analysis or can I neglect this observation (reference is highly appreciated)? How could I cope with the situation?

You're on the right track, but usually this problem is interpreted as a form of dependence. Children's reading abilities will most likely be affected by their school, and so measurements from a particular school are not quite independent from each other.

Commonly for this kind of problem, you assume that the schools come from some larger population of schools and estimate the variance between them. This can be done in a mixed model, using a random effect for school. A random effect is like an extra error term, except that its variance is estimated between groups of observations (not individual observations). In essence you add an offset from the intercept for every school.

A simple example in R:

require(lme4)
LMM <- lmer(reading_ability ~ siblings + age + books + (1 | school)) # random intercept


If you believe the effect of other variables is also dependent on the school (e.g., some schools have better access to books), you can estimate a random slope for that variable as well.

Continuing the example in R:

LMM <- lmer(reading_ability ~ siblings + age + (books | school)) # random intercept & slope


Lastly, schools are undoubtedly nested within cities. Cities may have different financial support for schools and so the entire reasoning used for schools can also be used for cities. You can model this hierarchically, either explicitly (using Bayesian hierarchical models) or using a nested random effect:

LMM <- lmer(reading_ability ~ siblings + age + (1 | city/school)) # nested effect
LMM <- lmer(reading_ability ~ siblings + age + (books | city/school)) # + slope for books


Actually your example is quite close to what most introductory text books on mixed models use as an example, so allow me to complete it: If you know the classes the children are in, you can make the same argument for nesting. Updating the example, your model would then look something like this:

LMM <- lmer(reading_ability ~ siblings + age + (1 | city/school/class)) # intercept only
LMM <- lmer(reading_ability ~ siblings + age + (both | city/school/class)) # intercept & slope


Have a look at other questions tagged mixed models, I've added the tag to your question!