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.
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.
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:
FemaleIND = 0): \(\text{Height} = 38.73 +
0.1466(\text{Age})\)FemaleIND = 1): \(\text{Height} = 36.28 +
0.1466(\text{Age})\) (since \(38.73 +
(-2.45) = 36.28\))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)"
)
-2.45): For two
kids with the same age, we expect females to have heights lower than
males by 2.45 inches on average.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.Age = 180 months:
-2.45 because the two lines are parallel.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:
FemaleIND = 0): \(\text{Height} = 32.96 +
0.1825(\text{Age})\)FemaleIND = 1): \(\text{Height} = 40.688 +
0.1185(\text{Age})\) (where intercept \(= 32.96 + 7.728 = 40.688\) and slope \(= 0.1825 + (-0.064) = 0.1185\))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.
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 |
model2 helped because it decreased the residual standard
error from 3.357 to 3.133 (meaning the
data points sit closer to the predicted lines) and increased R-squared
from 0.6938 to 0.7348. Adding the
interaction term in model3 improved the model even further,
lowering the residual standard error to 2.948 and
raising R-squared to 0.7663.model3 because it is the most accurate of
the three models. Based on our analysis, no, boys and girls do
not grow taller at the same rate—the interaction model shows
that boys grow faster per month than girls, causing their growth lines
to cross within the age range of the dataset.