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. The groups have a similar pattern starting out, but as they progress into the graph the girls become lower than the boys. This starts around 175 months.

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?

Intercept coef=38.7275347 Age coef=0.1466626 FemaleIND coef=-2.4514034. Boys equation: 38.7275347 + 0.1466626 x age Girl equation: 36.2761313 + 0.1466626 x age For two children with the same age, the female is expected to measure 2.4514034 less in height. Predicted height for boy= 65.1268027 Predicted height for girl= 62.6753993. The difference between my predictins correlate with the FemaleIND of girls being approx 2 less inches than boys

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.96, slope = 0.183, color = "#F8766D") +
  geom_abline(intercept = 32.96+7.73, slope = 0.183-0.064, 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?

Intercept= 32.96091632 Age= 0.18254275 FemaleIND= 7.72784644 FemaleINT= -0.06413432. Yes, the two lines cross in the data at a little less than 125 months, which means that boys and girls are around the same height at a little under 125 months.

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?

Model2 - r-squared= 0.7348 residual standard error= 3.133 Model3 - r-squared= 0.7663 residual standard error= 2.948 Model1 - r-squared= 0.6938 residual standard error= 3.357. Adding the indicator did help. Adding the interaction helped a little bit, but barely change anything relating to the R-squared. I would use the 3rd model because it shows how the boys and girls interact rather than being straight lines. Boys and girls do not grow taller at the same rate, based off the data, boys grow taller slightly faster and girls stop growing sooner than boys.