Introduction

In this analysis, we explore the Kids198 dataset from the Stat2Data package to answer our central question: Do boys and girls grow taller at the same rate? We also investigate whether a multiple regression equation with an indicator variable—and an interaction variable—predicts Height (in inches) from Age (in months) better than a single regression line.

Step 1: Look at the Groups, and Fit One Line

First, we load the required packages, load the Kids198 dataset, and create a scatterplot of Height versus Age colored and shaped by Sex (0 = male, 1 = female).

library(Stat2Data)
library(dplyr)
library(ggplot2)

data(Kids198)
my_data <- Kids198

ggplot(my_data, aes(x = Age, y = Height, color = factor(Sex), shape = factor(Sex))) +
  geom_point() +
  labs(
    title = "Height vs. Age by Sex",
    x = "Age (months)",
    y = "Height (inches)",
    color = "Sex (0 = Male, 1 = Female)",
    shape = "Sex (0 = Male, 1 = Female)"
  )

Looking at the scatterplot, the males seem to sit a bit higher toward the higher ages while the females sit slightly lower than the males, making the males’ pattern appear steeper than the females’.

Next, we fit a single simple linear regression line (model1) with Height as the response variable and Age as the explanatory variable.

model1 <- lm(Height ~ Age, data = my_data)
summary(model1)
## 
## Call:
## lm(formula = Height ~ Age, data = my_data)
## 
## Residuals:
##     Min      1Q  Median      3Q     Max 
## -10.434  -2.181   0.073   2.247   9.380 
## 
## Coefficients:
##              Estimate Std. Error t value Pr(>|t|)    
## (Intercept) 37.077279   1.145824   32.36   <2e-16 ***
## Age          0.149109   0.007076   21.07   <2e-16 ***
## ---
## Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
## 
## Residual standard error: 3.357 on 196 degrees of freedom
## Multiple R-squared:  0.6938, Adjusted R-squared:  0.6922 
## F-statistic:   444 on 1 and 196 DF,  p-value: < 2.2e-16

For model1, the R-squared is 0.6938 and the residual standard error is 3.357 inches.

Step 2: Adding an Indicator Variable

Because lm() requires numeric predictors, we create an indicator variable FemaleIND that equals 1 when Sex == 1 (female) and 0 when Sex == 0 (male).

my_data <- mutate(my_data, FemaleIND = ifelse(Sex == 1, 1, 0))

my_data %>%
  select(Age, Height, Sex, FemaleIND) %>%
  head()
##   Age Height Sex FemaleIND
## 1 210   67.8   0         0
## 2 144   63.0   1         1
## 3 119   50.1   0         0
## 4 130   55.7   1         1
## 5 157   63.2   0         0
## 6 102   48.8   0         0

Now we fit model2 using both Age and FemaleIND as explanatory variables.

model2 <- lm(Height ~ Age + FemaleIND, data = my_data)
coef(model2)
## (Intercept)         Age   FemaleIND 
##  38.7275347   0.1466626  -2.4514034

From coef(model2), the intercept is 38.73, the Age coefficient is 0.1466, and the FemaleIND coefficient is -2.45. Using these coefficients, we get two parallel lines with the same slope (0.1466) but different intercepts:

ggplot(my_data, aes(x = Age, y = Height, color = factor(Sex))) +
  geom_point() +
  geom_abline(intercept = 38.73, slope = 0.1466, color = "#F8766D") +
  geom_abline(intercept = 36.28, slope = 0.1466, color = "#00BFC4") +
  labs(
    title = "Height vs. Age by Sex (Parallel Slopes Model)",
    x = "Age (months)",
    y = "Height (inches)",
    color = "Sex (0 = Male, 1 = Female)"
  )

Interpreting Coefficients and Predictions

  • Indicator coefficient (-2.45): For two kids with the same age, we expect females to have heights lower than males by 2.45 inches on average.
  • Age coefficient (0.1466): Holding sex constant, for two kids of the same sex, we expect height to increase by 0.1466 inches for every additional month of age.
  • Predictions at Age = 180 months:
    • Males: \(38.73 + 0.1466(180) = \mathbf{65.12}\) inches
    • Females: \(36.28 + 0.1466(180) = \mathbf{62.67}\) inches
    • The difference between these two predictions is 2.45 inches (males are taller by 2.45 inches, or females are shorter by 2.45 inches), which matches the indicator coefficient -2.45 because the two lines are parallel.

Step 3: Adding an Interaction Variable

To allow boys and girls to have different slopes (growth rates) as well as different intercepts, we create an interaction variable FemaleINT by multiplying FemaleIND by Age, and fit model3.

my_data <- mutate(my_data, FemaleINT = FemaleIND * Age)

model3 <- lm(Height ~ Age + FemaleIND + FemaleINT, data = my_data)
coef(model3)
## (Intercept)         Age   FemaleIND   FemaleINT 
## 32.96091632  0.18254275  7.72784644 -0.06413432

From coef(model3), the intercept is 32.96, the Age coefficient is 0.1825, the FemaleIND coefficient is 7.728, and the FemaleINT coefficient is -0.064. This gives each group its own intercept and slope:

ggplot(my_data, aes(x = Age, y = Height, color = factor(Sex))) +
  geom_point() +
  geom_abline(intercept = 32.96, slope = 0.1825, color = "#F8766D") +
  geom_abline(intercept = 40.688, slope = 0.1185, color = "#00BFC4") +
  labs(
    title = "Height vs. Age by Sex (Interaction Model)",
    x = "Age (months)",
    y = "Height (inches)",
    color = "Sex (0 = Male, 1 = Female)"
  )

In this model, the FemaleIND coefficient (7.728) represents the vertical gap between the lines at Age = 0, rather than everywhere. The interaction coefficient (-0.064) shows the difference between the two groups’ slopes: males’ heights increase by 0.064 inches per month faster than females’ heights do. In the graph, the two lines both increase with age, have noticeably different slopes, and cross within the data.

Step 4: Comparing Models and Conclusion

Finally, we inspect the summaries of model2 and model3 to compare their fit against model1.

summary(model2)
## 
## Call:
## lm(formula = Height ~ Age + FemaleIND, data = my_data)
## 
## Residuals:
##     Min      1Q  Median      3Q     Max 
## -9.1019 -1.9756 -0.0169  2.0227  8.1599 
## 
## Coefficients:
##              Estimate Std. Error t value Pr(>|t|)    
## (Intercept) 38.727535   1.110526  34.873  < 2e-16 ***
## Age          0.146663   0.006617  22.164  < 2e-16 ***
## FemaleIND   -2.451403   0.446458  -5.491 1.24e-07 ***
## ---
## Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
## 
## Residual standard error: 3.133 on 195 degrees of freedom
## Multiple R-squared:  0.7348, Adjusted R-squared:  0.7321 
## F-statistic: 270.1 on 2 and 195 DF,  p-value: < 2.2e-16
summary(model3)
## 
## Call:
## lm(formula = Height ~ Age + FemaleIND + FemaleINT, data = my_data)
## 
## Residuals:
##     Min      1Q  Median      3Q     Max 
## -7.3834 -2.0663  0.0506  1.7862  8.4161 
## 
## Coefficients:
##              Estimate Std. Error t value Pr(>|t|)    
## (Intercept) 32.960916   1.537781  21.434  < 2e-16 ***
## Age          0.182543   0.009383  19.454  < 2e-16 ***
## FemaleIND    7.727846   2.034957   3.798 0.000195 ***
## FemaleINT   -0.064134   0.012545  -5.112  7.6e-07 ***
## ---
## Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
## 
## Residual standard error: 2.948 on 194 degrees of freedom
## Multiple R-squared:  0.7663, Adjusted R-squared:  0.7626 
## F-statistic:   212 on 3 and 194 DF,  p-value: < 2.2e-16
Model Explanatory Variables R-squared Residual Standard Error (inches)
model1 Age 0.6938 3.357
model2 Age, FemaleIND 0.7348 3.133
model3 Age, FemaleIND, FemaleINT 0.7663 2.948