11
$\begingroup$

I'm trying to get my head around the differences between including a categorical variable as a fixed effect or as a random intercept. Let's get some data - which I found over here.

d <- haven::read_sav(file ="https://github.com/MultiLevelAnalysis/Datasets-third-edition-Multilevel-book/blob/master/chapter%202/popularity/SPSS/popular2.sav?raw=true")
d <- select(d, pupil, class, extrav, sex, texp, popular)
d$class <- as.factor(d$class)

In that blog I linked, they construct the following model (among others):

model2 <- lme4::lmer(popular ~ 1 + sex + extrav + texp + (1 | class), data = d)

summary(model2)$coef
              Estimate  Std. Error   t value
(Intercept) 0.80976625 0.169993377  4.763516
sex         1.25379981 0.037290049 33.622905
extrav      0.45443099 0.016165300 28.111510
texp        0.08840725 0.008764057 10.087480

Now I understand each unique level of $class is given a random intercept, but I can't seem to fully grasp how this is fundamentally different from the following:

model2.2 <- lm(popular ~ 1 + sex + extrav + texp + class, data = d)
summary(model2.2)$coef[1:4, ]
              Estimate Std. Error   t value      Pr(>|t|)
(Intercept) 0.62879232 0.12653481  4.969323  7.291758e-07
sex         1.37942361 0.04233690 32.582067 4.466222e-187
extrav      0.47530311 0.01815907 26.174417 4.569729e-130
texp        0.08882755 0.00350321 25.356048 3.474237e-123

In which each level of $class is given a $\beta$ estimate in relation to some reference level. I would understand different values for each level, but I don't get why the estimates and SEs of the other (fixed) effects are affected.

$\endgroup$

3 Answers 3

7
$\begingroup$

Fixed vs Random

I know this is an old question but I don't believe the old answer here really tackles the question fully. There are three ways you can think about it: what I will informally call the theoretical perspective, the data reduction perspective, and the replication perspective.

The theoretical perspective is that the fixed effects are those that you are actually interested in modeling as a direct effect on your outcome variable. We could include someone's gender/sex as a categorical predictor of income because we know from past research that these variables have some relationship with each other and can be an important one to investigate. However, some would be less concerned with, for example, the neighborhood someone comes from with relation to income if its not important to their model (though certainly this doesn't mean other researchers wouldn't find it so). If included as a random effect, we are in some part implying that we just think it's an annoyance factor that needs to be removed to clean up how accurate our fixed estimates are (though see below for when thats not such the case). A key thing to notice here is that one person's fixed effect can be another person's random effect depending on what they're trying to say about their model.

Another perspective, one that I think is often lost when mixed models are taught, is the data reduction perspective, which is simply that we want to summarize a lot of information about variations in a DV based off selected clusters. This in my opinion is often more informative, because we often aren't interested in a random effect with few levels or one that doesn't include a lot of variance...we want something normally that varies a lot in order to 1) summarize the average distance from, say, the conditional mean 2) specifically compare across a variety of clusters (for example seeing which schools are above the mean in reading outcomes over others) and 3) remove bias from standard errors in the regression. On a separate note, it also greatly reduces down the amount of information one has to interpret from the fixed effects portion of the model. Typically when you have a standard regression that uses a categorical variable with many levels, you will get as many coefficients as there are levels. This consequently can be a bit confusing to digest.

Whereas many regressions are less concerned with what the random noise is trying to tell us, mixed models operationally add it in to inform researchers about a large depth in heterogeneity across settings, people, or other contextual factors so that we have more accurate assumptions about what the fixed effects are actually doing. This "pollution" in random variance can actually tell us a lot. A great example is the classic subject x item crossed effects design, wherein we can learn a lot about the ease/difficulty of different items and subject aptitude in general while untethering it from what fixed effects we deem important. These first two perspectives are already both important for selecting a categorical effect as either fixed or random. How one selects them is heavily weighted by theory as well as the underlying data supporting such models.

Having said that, there is another way to look at this that I believe helps solidify the distinction more, and that is the importance of the replication perspective. When we consider fixed effects, we are interpreting the effects as those we should see across studies with some semblance of stability. In other words, these are population-level effects that we are trying to infer from the model. With repeated experiments, our understanding of the fixed effects should converge into an average effect of interest. This is not the case for random effects, where by definition we consider these effects to be totally random and should not have a predictable pattern (other than average variance perhaps). These are more sample-level effects that we want to untangle because they muddy the waters with respect to the population-level effects.

As an example from Simon Wood's book on GAMs (p.72), the chapter on mixed models provides this example:

Now consider a practical example. The Machines data frame, from the nlme package, contains data from an industrial experiment comparing 3 different machine types. The aim of the experiment was to determine which machine type resulted in highest worker productivity. 6 workers were randomly selected to take part in the trial, with each worker operating each machine 3 times (presumably after an appropriate period of training designed to eliminate any ‘learning effect’)...

We are interested in the effects of these particular machine types, but are only interested in the worker effects in as much as they reflect variability between workers in the population of workers using this type of machine. Put another way, if the experiment were repeated somewhere else (with different workers) we would expect the estimates of the machine effects to be quite close to the results obtained from the current experiment, while the individual worker effects would be quite different (although with similar variability, we hope).

This should further clarify the difference between fixed and random effects, as it should be clear that with your example, classes should not have a predictable fixed effect on the outcome given how widely they would vary from experiment to experiment.

Why This Matters for Your Comparison

Recall that above I mentioned how random effects influence the standard errors. This is because typically regressions which do not account for correlated errors are downward biased...in other words they greatly underestimate how influential an actual effect is because the accuracy of these estimates is influenced by the clusters in the data.

Probably the biggest reason you aren't seeing the big picture is because you are only looking at point estimates for the fixed effects in each regression. I fit a common toy dataset in R called sleepstudy to a lmer model below, then summarize and plot the random effects.

#### Load Libraries ####
library(lmerTest)
library(lattice)

#### Fit Data ####
fit <- lmer(Reaction ~ Days + (1|Subject),
            data = sleepstudy)
summary(fit)

#### Plot ####
dotplot(ranef(fit))

The summary is quite long, but the part you probably skipped is this portion that follows, which shows how the random effects vary (here subjects vary in reaction time substantially). This means that the day of this study effected reaction time, but this varied by subject, and without modeling this we cost ourselves some accuracy in the estimated effect consecutive day has on reaction time.

Random effects:
 Groups   Name        Variance Std.Dev.
 Subject  (Intercept) 1378.2   37.12   
 Residual              960.5   30.99   
Number of obs: 180, groups:  Subject, 18

Plotting the random effects shows a great summary of the random effects, which show by-subject variance around the conditional mean, the points being the average effect and the bars around it the fluctuation around those means. For example, Subject 337's reaction time is far slower on average, whereas Subject 309 is quite quick compared to others.

enter image description here

Going back to my previous point, this is important, as we could have just as easily entered Subject in as a fixed effect. However, this would have given us a ton of coefficients (one per subject), now way to meaningfully summarize their differences, and likely wouldn't be theoretically meaningful.

$\endgroup$
5
$\begingroup$

I answer the question:

I would understand different values for each (class) level, but I don't get why the estimates and SEs of the other (fixed) effects are affected.

The question posed in the title is already answered by @ShawnHemelstrand (+1).

Let's start by reproducing the model summaries. This step is important because the second model reported by the OP is not the intended model. (class is read as a numeric variable, so it has to be cast as a factor to estimate class-specific fixed effects.)

In the first model class is a random effect:

mod1 <- lmer( popular ~ sex + extrav + texp + (1 | class), data = d)

tidy(mod1, "fixed")
#> A tibble: 4 × 7
#>   effect term        estimate std.error statistic    df   p.value
#> 1 fixed  (Intercept)   0.810    0.170        4.76  226. 3.40e-  6
#> 2 fixed  sex           1.25     0.0373      33.6  1948. 7.68e-196
#> 3 fixed  extrav        0.454    0.0162      28.1  1955. 2.56e-146
#> 4 fixed  texp          0.0884   0.00876     10.1   102. 5.41e- 17

In the second model class is a fixed effect.

mod2 <- lm( popular ~ sex + extrav + texp + factor(class), data = d)

tidy(mod2)
#> A tibble: 103 × 5
#>    term           estimate std.error statistic   p.value
#>  1 (Intercept)      0.409     0.272      1.50  1.33e-  1
#>  2 sex              1.24      0.0376    33.0   8.87e-189
#>  3 extrav           0.452     0.0163    27.7   4.62e-142
#>  4 texp             0.0773    0.0143     5.39  7.79e-  8
#>  5 factor(class)2  -0.197     0.212     -0.928 3.54e-  1
#>  6 factor(class)3   0.0955    0.221      0.431 6.66e-  1
#>  ...

I have truncated the second summary table since the question is about the fixed effects. (To learn more about the random effects, see @ShawnHemelstrand's answer.)

Right away we notice something interesting: the estimate and std. error for sex and extrav change very little while the estimate and std. error for texp changes quite a bit. Why is that?

It's helpful to know what information these three explanatory variables represent: sex and extrav are the student's sex and extraversion on a scale from 1 to 10; texp is the teacher's experience in years.

So sex and extrav are individual-level predictor: they describe the students and vary within classes; while texp is a group-level predictor: it describes the classes and thus varies between classes but not within.

And that's the reason why the estimate and std. error of sex and extrav don't change much whether we treat the classes as fixed or random effects: the classes are big (they vary in size from 16 to 26 students) and most classes are coed (88 out of 100 classes have both sexes), so the individual-level effects are mostly estimated from within-class differences between boys and girls and between students across the spectrum of extraversion who are in the same class. These within-classes differences have the same interpretation in both models: if we compare two students in the same class, the expected difference in their popularity is due to sex and extraversion; the class and teacher effects cancel out.

If the dataset were balanced, so that each class has exactly the same distribution of sex and extrav, then there would be no difference between the models as far as the estimation of the sex and extrav effects is concerned. This of course will be very unlikely to occur in an observational study but more likely to hold in a designed experiment.

$\endgroup$
3
$\begingroup$

The SE of the fixed effects are made smaller when the random effect is added because the new implied grouping of term members adds information that wasn’t previously included. This grouping is not implied or encoded in a fixed effects style. It’s additional information that you added. Sometimes folks will say that the improved term “steals” variance from other terms that they shouldn’t have otherwise had.

Gelman and Hill’s book, Data Analysis Using Regression and Multilevel/Hierarchical Models can help.

Also, see this excellent blog post on the topic.

$\endgroup$
1
  • $\begingroup$ Thanks, that helps! Will check out those resources. :) $\endgroup$ Commented Mar 1, 2021 at 13:48

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.