Here, \(y\) is the vector of outputs, \(X\) is the , \(\beta\) is the vector of parameters and \(\varepsilon\) is the vector of the error terms. Assuming that \(\varepsilon^i\) are distributed as $\mathcal{N}(0,\sigma^2)$, another way of writing the model is

\[
y\sim \mathcal{N}(X\beta, \sigma^2I).
\]

We attempt to find an estimator \(\hat{\beta}\) of \(\beta\) by removing the error term to obtain an approximate equation:

Here, kid_score is the child’s IQ score, mom_hs is an indicator for whether the mother graduated from high school (0 or 1), and mom_iq is the mother’s IQ score.

Now we fit a linear regression model of kid_score on two predictors: mom_hs and mom_iq.

stan_glm
family: gaussian [identity]
formula: kid_score ~ mom_hs + mom_iq
observations: 434
predictors: 3
------
Median MAD_SD
(Intercept) 25.9 6.0
mom_hs 6.0 2.2
mom_iq 0.6 0.1
Auxiliary parameter(s):
Median MAD_SD
sigma 18.2 0.6
------
* For help interpreting the printed output see ?print.stanreg
* For info on the priors used see ?prior_summary.stanreg

Thus the fitted line from the linear regression above is:

Below are some suggestions for an interpretation of the mom_iq’s coefficient:

Predictive interpretation: the difference between *two* children’s IQ when their mothers’ IQs differ by 1 and the other predictors (in this case, mom_hs) are identical is 0.6 on average.

Counterfactual interpretation: changing the mother’s IQ from 100 to 101, while leaving the other predictors unchanged, would lead to an expected increase of 0.6 in child’s test score. This kind of reasoning arises in causal inference.

5.1 Interactions

The linear model with two predictors above imposes that the slope of mom_iq is the same for the subsets consisting of mom_hs = 0 and mom_hs = 1. However, if we consider the linear regression on these two subsets:

we can see that the slopes differ by a significant amount between the subsets. A remedy for this is to add an interaction term between mom_hs and mom_iq. This can be done by adding mom_hs:mom_iq to stan_glm.

stan_glm
family: gaussian [identity]
formula: kid_score ~ mom_hs + mom_iq + mom_hs:mom_iq
observations: 434
predictors: 4
------
Median MAD_SD
(Intercept) -10.0 13.5
mom_hs 49.5 14.9
mom_iq 0.9 0.1
mom_hs:mom_iq -0.5 0.2
Auxiliary parameter(s):
Median MAD_SD
sigma 18.0 0.6
------
* For help interpreting the printed output see ?print.stanreg
* For info on the priors used see ?prior_summary.stanreg

Comparing between three equations above, we can interpret the coefficients of the equation with the interaction term as follows:

The intercept represents the predicted test scores for children whose mothers did not complete high school (mom_hs = 0) and had IQs of 0 (mom_iq = 0)—not a meaningful scenario.

The intercept can be more interpretable if input variables are centered before including them as regression predictors.

The coefficient of mom_hs is the difference between the predicted test scores for children whose mothers did not complete high school (mom_hs = 0) and children whose mothers did complete high school (mom_hs = 1), both with mom_iq = 0; this is inconceivable as no mothers have IQ of 0. To make the coefficient more interpretable, one might want to center the variable (i.e. subtract the observed values of mom_hs by their mean) first.

The coefficient of mom_iq can be thought of as the comparison of mean test scores across children whose mothers did not complete high school (mom_hs = 0), but their mothers’ IQs differ by 1 point.

The coefficient on the interaction term is the difference between the slopes of the lines (regression on each mom_hs group) in the plot above.

When should we look for interactions? Interaction typically arises when the effects of a predictor are different across different groups. For example, when predicting the likelihood of cancer on smoking (0 or 1) and home radon exposure, the risk of cancer associated with the radon exposure is higher in the smoking group than non-smoking group.

5.2 Regression with multiple levels of a categorical predictor

We give an example of a regression with multiple inputs, some of which are categorical variables with multiple levels. Here, we will use the Earnings data.

earnings <-read.csv("data/earnings.csv")

summary(factor(earnings$ethnicity))

Black Hispanic Other White
180 104 38 1494

We fit a linear regression of weight on three predictors: height, male (0 or 1) and ethnicity (White, Black, Hispanic and Other).

fit_5 <-stan_glm(weight ~ height + male + ethnicity, data=earnings,refresh=0)print(fit_5)

stan_glm
family: gaussian [identity]
formula: weight ~ height + male + ethnicity
observations: 1789
predictors: 6
------
Median MAD_SD
(Intercept) -99.8 15.6
height 3.8 0.2
male 12.1 1.9
ethnicityHispanic -6.1 3.5
ethnicityOther -12.3 5.3
ethnicityWhite -5.2 2.2
Auxiliary parameter(s):
Median MAD_SD
sigma 28.6 0.5
------
* For help interpreting the printed output see ?print.stanreg
* For info on the priors used see ?prior_summary.stanreg

We can see that stan_glm has automatically created three new indicator variables (also called dummy variables), namely ethnicityHispanic, ethnicityOther and ethnicityWhite for each person. The value of each variable is 1 if the person belongs to the corresponding level, otherwise it is 0. For example, for a Hispanic person, ethnicityHispanic = 1, ethnicityOther = 0 and ethnicityWhite = 0. But if we look closely at the ethnicity column in the dataset, we would notice that the group of Blacks is missing. This is because stan_glm took Black to be the baseline category against the other groups. Consequently, a black person is represented by ethnicityHispanic = 0, ethnicityOther = 0 and ethnicityWhite = 0.

The coefficient of ethnicity, can be interpreted as follows: between two persons with the same height and same gender,

On average, a Hispanic person is \(6.1\) pounds lighter than a Black person, an Other person is \(12.3\) pounds lighter than a Black person, and a White person is \(5.2\) pounds lighter than a Black person.

On average, a Hispanic person is \(12.3-6.1=6.2\) pounds heavier than an Other person.

On average, a Hispanic person is \(6.1-5.2=0.9\) pounds lighter than a White person.

On average, a White person is \(12.3-5.2=7.1\) pounds heavier than an Other person.

Sometimes, we would like to change the baseline group; this can be done by specifying the order of the levels in the categorical variables. An example below shows how to set White as the baseline groups.

stan_glm
family: gaussian [identity]
formula: weight ~ height + male + eth
observations: 1789
predictors: 6
------
Median MAD_SD
(Intercept) -104.7 16.3
height 3.8 0.3
male 12.1 2.0
ethBlack 5.2 2.3
ethHispanic -1.0 3.0
ethOther -7.0 4.8
Auxiliary parameter(s):
Median MAD_SD
sigma 28.6 0.5
------
* For help interpreting the printed output see ?print.stanreg
* For info on the priors used see ?prior_summary.stanreg

5.2.1 Simulation-based prediction

From the fitted model, we give an example of computing \(\Pr(\text{weight} > 180)\) for a Hispanic male who is 70 inches tall. As in the previous chapter, we compute the probability using posterior simulation.

new =data.frame(height=70, male=1, eth="Hispanic")y_pred <-posterior_predict(fit_5, newdata=new)y_180_indicator <- (y_pred >180)y_180_prob <-mean(y_180_indicator)cat("Probability of being heavier than 180 pounds: ", y_180_prob)

Probability of being heavier than 180 pounds: 0.433

5.3 Paired and blocked designs as a regression problem

In the previous sections, we see that regression coefficients can be interpreted as comparisons. Conversely, comparison between two or more grounds can be treated a regressions.

Completely randomized experiment

Suppose that in an experiment, people are randomly assigned into treatment and control groups. A standard estimate for the treatment effect is \(\bar{y}_T-\bar{y}_C\). We can also obtain this estimate by assigning an indicator variable to each group: 0 for the control group and 1 for the treatment group. Then \(\bar{y}_T-\bar{y}_C\) is precisely the coefficient of the following regression:

fit <-stan_glm(y ~ treatment, data=experiment)

where treatement is 0 if the person is in the control group, and 1 if they are in the treatment group.

Paired design

When the experiment consists of control-treatment pairs and the people in each pair might not be independent, we can assign a new indicator variable that indicates each pair e.g. the control and treatment in the first pair get pair = 1, the second pair gets pair = 2, so on and so forth.

fit <-stan_glm(y ~ treatment +factor(pair), data=experiment)

Block design

When considering the treatment effect over \(J\) groups of people, we can assign a new indicator variable group to indicate each group e.g. the first group gets group = 1.

fit <-stan_glm(y ~ treatment +factor(group), data=experiment)

5.4 Weighted regression

The OLS estimate \(\hat{\beta}\) minimizes the least squares objective:

\[
\sum_i (y_i-X_i\hat{\beta})^2,
\]

where \(X_i=(1,x_{i1},\ldots,x_{ip})\). We can modify the objective with weights \(w_1,\ldots,w_n\) for each instance in the sum:

We typically require that \(\sum_i w_i = 1\). Why do we want to add these weights to the objective? Sometimes, the proportions of different groups in the sample data do not match with those in the population, and we would like to make correction when fitting the model by adding the weights. For example, suppose that our data consists of 70 white persons and 30 black persons. To balance the model between these two groups, we can put weight 1/140 on all terms that correspond to white persons, and 1/60 to all terms that correspond to black persons.

Let \(W=\text{Diag}(w_1,\ldots,w_n)\). The estimate \(\hat{\beta}_{\text{wls}}\) that minimizes the weighted objective \(\eqref{eq1}\) is

stan_glm
family: gaussian [identity]
formula: weight ~ height + male + ethnicity
observations: 1789
predictors: 6
------
Median MAD_SD
(Intercept) -100.4 16.3
height 3.9 0.3
male 12.1 2.0
ethnicityHispanic -6.2 3.5
ethnicityOther -12.3 5.3
ethnicityWhite -5.2 2.3
Auxiliary parameter(s):
Median MAD_SD
sigma 28.7 0.5
------
* For help interpreting the printed output see ?print.stanreg
* For info on the priors used see ?prior_summary.stanreg

The results of the weighted regression is not much different than those of the classical regression.