Skip to main content

Simple Linear Regression: Frequentist Inference

Lecture 11A handout

Author

Name: _________________

Baby weights

The babies data in the {openintro} package has 1,236 babies born at Kaiser Foundation hospitals in the East Bay (San Francisco area) between 1960 and 1967. Today we use bwt (birth weight, in ounces), gestation (length of the pregnancy, in days), and smoke (1 if the mother smoked during pregnancy, 0 if not).

ggplot(babies, aes(x = gestation, y = bwt)) +
  geom_point(alpha = 0.4) +
  geom_smooth(method = "lm", se = FALSE)

Scatterplot of birth weight in ounces against gestation in days, with the least squares line. Most points are between 250 and 300 days and between 90 and 150 ounces. Birth weight tends to increase with gestation, but the points are widely scattered around the upward-sloping line.

Response variable (\(y\)): blank to fill in \(\qquad\) Explanatory variable (\(x\)): blank to fill in

Math class vs. statistics class. Real data do not fall exactly on a line, so the statistics equation has one more term. Name each term.

Equation of a line in math class and in statistics class
Math class Name Statistics class Name
\(y = b + mx\) \(y_i = \beta_0 + \beta_1 x_i + \epsilon_i\)
\(y\) blank to fill in \(y_i\) blank to fill in
\(b\) blank to fill in \(\beta_0\) blank to fill in
\(m\) blank to fill in \(\beta_1\) blank to fill in
\(x\) blank to fill in \(x_i\) blank to fill in
\(\epsilon_i\) blank to fill in

Fitting the model

lm() (linear model) comes with R in the {stats} package, which loads automatically. tidy() is from the {broom} package: it shows the estimates as a table.

model_g <- lm(bwt ~ gestation, data = babies)
tidy(model_g)
# A tibble: 2 × 5
  term        estimate std.error statistic  p.value
  <chr>          <dbl>     <dbl>     <dbl>    <dbl>
1 (Intercept)  -10.1      8.32       -1.21 2.27e- 1
2 gestation      0.464    0.0297     15.6  3.22e-50

Fitted equation: \(\widehat{\text{bwt}} =\) blank to fill in \(+\) blank to fill in \(\times\) gestation

  1. Expected birth weight of a baby born after 300 days of gestation: blank to fill in

  2. Interpret the slope in context.

  1. Interpret the intercept. Does it make sense? (We will come back to this at the end of class.)
  1. Baby number 148 had a gestation of 300 days and weighed 160 ounces (the pink point below). Find the residual, \(e_{148} = y_{148} - \hat{y}_{148}\). Is the baby above or below the line?

Scatterplot of birth weight against gestation with the regression line. Baby number 148, at 300 days and 160 ounces, is a large pink point above the line. A dashed vertical segment connects the point to the line at 129.1 ounces.

lm() finds the least squares line: the line with the smallest blank to fill in, \(\sum e_i^2\).

Inference for the slope

If there were no relationship between gestation and birth weight, the population slope would be blank to fill in.

\(H_0\): blank to fill in \(\qquad H_A\): blank to fill in

The p.value in the gestation row of tidy(model_g) is blank to fill in.

confint() is also from the {stats} package. It gives 95% confidence intervals.

confint(model_g)
                  2.5 %    97.5 %
(Intercept) -26.3915884 6.2632199
gestation     0.4059083 0.5226169

What do you conclude at \(\alpha = 0.05\)? Interpret the 95% confidence interval for the slope.

A categorical (binary) explanatory variable: smoking

model_s <- lm(bwt ~ smoke, data = babies)
tidy(model_s)
# A tibble: 2 × 5
  term        estimate std.error statistic  p.value
  <chr>          <dbl>     <dbl>     <dbl>    <dbl>
1 (Intercept)   123.       0.649    190.   0       
2 smoke          -8.94     1.03      -8.65 1.55e-17
confint(model_s)
                2.5 %     97.5 %
(Intercept) 121.77391 124.320430
smoke       -10.96413  -6.911199

\(\widehat{\text{bwt}} =\) blank to fill in \(+\) blank to fill in \(\times\) smoke. smoke can only be 0 or 1:

  • Expected birth weight, mother did not smoke (smoke = 0): blank to fill in
  • Expected birth weight, mother smoked (smoke = 1): blank to fill in

So the intercept is blank to fill in and the slope is blank to fill in.

Interpret the slope and its 95% confidence interval. Is there a difference?

Can we conclude that smoking causes lower birth weight? Can we generalize to babies born today?

Centering

The intercept of model_g, -10.1 ounces, is the expected birth weight at 0 days of gestation. That does not make sense. A common fix is to center the explanatory variable: subtract its mean from every value.

\[\text{gestation\_c} = \text{gestation} - \overline{\text{gestation}}\]

mean(babies$gestation, na.rm = TRUE)
[1] 279.3385
babies <- babies |>
  mutate(gestation_c = gestation - mean(gestation, na.rm = TRUE))

model_c <- lm(bwt ~ gestation_c, data = babies)
tidy(model_c)
# A tibble: 2 × 5
  term        estimate std.error statistic  p.value
  <chr>          <dbl>     <dbl>     <dbl>    <dbl>
1 (Intercept)  120.       0.477      251.  0       
2 gestation_c    0.464    0.0297      15.6 3.22e-50
  1. What is gestation_c for a baby born after 279 days (about average)? After 300 days? After 260 days?

Two scatterplots of birth weight with the same regression line. On the left, the x-axis is gestation in days from 0 to 360. The data are all on the right, and a pink point marks where the line crosses x = 0, far from the data, at -10.1 ounces. On the right, the x-axis is centered gestation, from about -130 to 75 days, and a dashed vertical line at 0 is in the middle of the data, where the line is at about 120 ounces, marked with a pink point.

  1. Interpret the new intercept, 120, in context. Does it make sense now?
  1. Compare the slope of model_c with the slope of model_g. Why did it not change?

People often center so that the intercept describes a typical value of \(x\). We will see this again on Wednesday!

Summary

  • Model: \(y_i = \beta_0 + \beta_1 x_i + \epsilon_i\); in R, blank to fill in ({stats}) with tidy() ({broom}) and confint() ({stats}).
  • Slope (numeric \(x\)): expected change in \(y\) for blank to fill in. Binary \(x\): the slope is blank to fill in.
  • Residual: blank to fill in. Do not blank to fill in.
  • Centering \(x\) (subtracting its mean) changes the intercept to blank to fill in; the slope blank to fill in.
  • \(H_0: \beta_1 = 0\). A small p-value, or a confidence interval that does not include blank to fill in, is evidence of a relationship, but not of blank to fill in.