## A Note on Fixed vs. Random Effects

There are a staggering number of different names for these models, with different disciplines using different terminology. In the language used in this course, fixed effects are varying coefficients (which can be slopes or intercepts) that are implemented by creating group dummies, random effects are varying coefficients (again, slopes or intercepts) which are drawn from a common distribution, and multilevel models are models which include any of these effects.  Fixed Effects; Random Effects1

Unfortunately, one of the most widely used packages in R for multilevel models uses some confusing terminology. The `lme4` package uses the following terms:

• fixed effect: non-random parameter
• random effect: random parameter, drawn from a distribution

## Fixed Effect Models

Multilevel models allow us to do two very powerful things: control for unobserved variation across groups in grouped data, and control for the fact that group level variables don’t actually introduce as many independent observations as we think they do. Load the radon data from https://www.stat.columbia.edu/~gelman/arm/examples/radon/srrs2.dat, and log transform the radon `activity` variable (replace any values of 0 with .1 to avoid problems in the transformed variable). Drop any observations with a value of 9 for `floor`, as this is a missing data code and treating it as a value will bias our estimates. Use `state2` as the state variable for all subsequent examples.

``````sr <- read.delim('https://www.stat.columbia.edu/~gelman/arm/examples/radon/srrs2.dat', sep = ',')
sr\$radon <- log(ifelse (sr\$activity == 0, .1, sr\$activity))
sr <- sr[which(sr\$floor < 9), ]``````

Run a standard linear model (the complete pooling model) explaining the level of `radon` as a function of `floor`, and a model with fixed effects, using states as the group. Remember to generate group dummies for the fixed effects model.

``````pooled <- lm(radon ~ floor, data = sr)
fix_int <- lm(radon ~ floor + as.factor(state2), data = sr)
fix_slope <- lm(radon ~ floor + as.factor(state2), data = sr)``````

We can also estimate a fixed effects model with varying slopes instead of intercepts, although again, interpreting and presenting these results is not the easiest task. Create group dummies like before, except this time interact them with `floor`. This produces a model with varying slope and intercept fixed effects; this is closest to the completely separate regression approach we saw last week with `plyr`. We could also omit the group dummies as a separate predictor, yielding a model with varying slope fixed effects. The figure below shows the regression lines for each model. In the next section we’ll see how to create these plots for random effects models.