library(Stat2Data)
library(dplyr)
library(ggplot2)
data(Kids198)
ggplot(Kids198, aes(x = Age, y = Height,
color = factor(Sex), shape = factor(Sex))) +
geom_point() +
labs(
title = "Age vs. Height for Children",
x = "Age (months)",
y = "Height (inches)",
color = "Sex (0 = boy, 1 = girl)",
shape = "Sex (0 = boy, 1 = girl)"
)
model1 <- lm(Height ~ Age, data = Kids198)
summary(model1)
##
## Call:
## lm(formula = Height ~ Age, data = Kids198)
##
## 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
Question 1: What are R-squared and the residual standard error? How do boys and girls compare in the scatterplot?
The R-squared is 0.6938, and the residual standard error is 3.357 inches. This means the model explains about 69.38% of the variation in height. From the scatterplot, both boys and girls tend to get taller as they get older. However, boys appear to have a steeper pattern and are generally taller at older ages.
Kids198$FemaleIND <- as.numeric(Kids198$Sex == 1)
Kids198 %>%
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
model2 <- lm(Height ~ Age + FemaleIND, data = Kids198)
coef(model2)
## (Intercept) Age FemaleIND
## 38.7275347 0.1466626 -2.4514034
ggplot(Kids198, aes(x = Age, y = Height,
color = factor(Sex), shape = factor(Sex))) +
geom_point() +
geom_abline(intercept = 38.7275347, slope = 0.1466626,
color = "#F8766D") +
geom_abline(intercept = 36.2761313, slope = 0.1466626,
color = "#00BFC4") +
labs(
title = "Age vs. Height, with an Indicator for Girls",
x = "Age (months)",
y = "Height (inches)",
color = "Sex (0 = boy, 1 = girl)",
shape = "Sex (0 = boy, 1 = girl)"
)
Question 2: What are the three coefficients, and what is each group’s equation? Interpret the coefficients and predict the heights at 180 months.
The three coefficients are 38.7275347 for the intercept, 0.1466626 for Age, and -2.4514034 for FemaleIND.
The equations are:
Boys: Height = 38.7275 + 0.1467(Age)
Girls: Height = 36.2761 + 0.1467(Age)
For two children with the same age, a girl is predicted to be about 2.45 inches shorter than a boy. For two children with the same sex, each additional month of age is associated with an increase of about 0.147 inches in predicted height.
At 180 months, the predicted height for a boy is about 65.13 inches, while the predicted height for a girl is about 62.68 inches. The difference is approximately 2.45 inches, which matches the FemaleIND coefficient in magnitude.
Kids198$FemaleINT <- Kids198$FemaleIND * Kids198$Age
model3 <- lm(Height ~ Age + FemaleIND + FemaleINT, data = Kids198)
coef(model3)
## (Intercept) Age FemaleIND FemaleINT
## 32.96091632 0.18254275 7.72784644 -0.06413432
ggplot(Kids198, aes(x = Age, y = Height,
color = factor(Sex), shape = factor(Sex))) +
geom_point() +
geom_abline(intercept = 32.96091632, slope = 0.18254275,
color = "#F8766D") +
geom_abline(intercept = 40.68876276, slope = 0.11840843,
color = "#00BFC4") +
labs(
title = "Age vs. Height, with an Interaction",
x = "Age (months)",
y = "Height (inches)",
color = "Sex (0 = boy, 1 = girl)",
shape = "Sex (0 = boy, 1 = girl)"
)
Question 3: What are the four coefficients, and what is each group’s equation? Do the lines cross within the data?
The four coefficients are 32.96091632 for the intercept, 0.18254275 for Age, 7.72784644 for FemaleIND, and -0.06413432 for FemaleINT.
The equations are:
Boys: Height = 32.9609 + 0.1825(Age)
Girls: Height = 40.6888 + 0.1184(Age)
The two lines cross at approximately 120.5 months, or about 10 years old. This is within the age range of the data. Before this age, girls are predicted to be slightly taller, but afterward, boys are predicted to be taller. The boys’ line is also steeper, suggesting boys have a faster rate of height increase in this dataset.
summary(model2)
##
## Call:
## lm(formula = Height ~ Age + FemaleIND, data = Kids198)
##
## 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 = Kids198)
##
## 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
Question 4: What are R-squared and the residual standard error for each model? Which model would you use, and do boys and girls grow taller at the same rate?
Model 1 has an R-squared of 0.6938 and a residual standard error of 3.357. Model 2 has an R-squared of 0.7348 and a residual standard error of 3.133. Model 3 has an R-squared of 0.7663 and a residual standard error of 2.948.
Adding the indicator variable improved the model because R-squared increased and the residual standard error decreased. Adding the interaction variable improved it further. I would choose Model 3 because it explains the most variation in height and has the lowest residual standard error.
Based on this dataset, boys and girls do not appear to grow taller at the same rate. Boys have a steeper estimated growth pattern than girls.