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

data(Kids198)

Step 1: Look at the groups, and fit one line

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: does one group sit higher, is one group’s pattern steeper, or both?

R-squared = 0.6938

Residual Standard Error = 3.357 on 196 degrees of freedom

In the beginning, both boys and girls sit relatively in the same spot, but at around the 183 month mark (roughly 15 years old) boys peak.

Step 2: An indicator variable

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

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 = 32.728, slope = 0.147, color = "#F8766D") +
  geom_abline(intercept = 30.277, slope = 0.147, 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 FemaleIND coefficient and the Age coefficient in context. Predict the height of a boy and a girl who are each 180 months old. How does the difference between your predictions compare to the FemaleIND coefficient?

Boys Intercept: 32.728 Girls Intercept: 30.277 Age/Slope: 0.147 FemaleIND: -2.451

For every additional year of age (age + 1), while only considering individuals of the same gender, we would expect the height to increase by 0.147 inches

My predictions were not far off.

Step 3: An interaction variable

Kids198 <- mutate(Kids198, FemaleINT = FemaleIND * 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.961, slope = 0.183, color = "#F8766D") +
  geom_abline(intercept = 40.691, slope = 0.120, 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 two lines cross within the data? If so, at about what age, and what does that mean for boys and girls?

BInt: 32.961 Age/Slope: 0.183 FemaleIND: 7.730 FemaleINT: -0.064 GInt: 40.691 GSlope: 0.120

The lines do cross at roughly around 10 years old. This means that both girls and boys were about the same height at that age.

Step 4: Which model is best?

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? Did adding the indicator help? Did adding the interaction help? Which of the three models would you use, and why? Then answer our question: do boys and girls grow taller at the same rate?

R squared(Model 12): 0.735

R squared(Model 13): 0.766

Boys and girls do grow at around the same rate in early childhood, but as they get older, boys grow at a faster rate.