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 is 0.69 and the residual standard error is 3.357. I see that at the 175 months or more line, boys tend to keep climbing heights while girls start to decrease growth rate.

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 = 38.73, slope = 0.147, color = "#F8766D") +
  geom_abline(intercept = 36.28, 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?

The three coefficients are 0.147, -2.45, and 38.73. The equation for the girls is y-hat = 0.147x + 36.28. The boys equation is y-hat = 0.147x +38.73. A girl’s predicted height at age 180 months would be 62.71 inches. A boy’s predicted height at 180 months would be 65.19 inches. For two children with the same age but one is male and the other is female, the heights would increase by a difference of 2.45 .

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 = 40.69, slope = .12, color = "#F8766D") +
  geom_abline(intercept = 32.96, slope = .18, 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?

The four coefficients are 32.96 for intercept, 0.18 for age, 7.73 for FemaleIND, and -0.06 for FemaleINT. The girl equation is y-hat = 0.18x + 32.96 and the boy equation is y-hat = 0.12x + 40.69. Yes the lines cross at about 130 months old which means they grow at about the same rate briefly before changing rates again.

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?

For model12 R squared = 0.7348 and the residual SE = 3.133. For model13 R-squared = 0.7663 and the residual SE = 2.948. Adding the interaction helped a little bit, but not significantly enough to say the model is worth using more than the original model. Boys and girls do not grow at the same rate according to this model.