# STAT3530 -- comparing nested models with F tests
# Follows Handout 7. Work through the three group tests, then the single
# predictor, then the contrast, checking each p-value against the handout.
# Run a line at a time.

site <- 'https://tdruiz-stat3530.share.connect.posit.cloud/_data/'

load(url(paste0(site, 'seatpos.RData')))

# 38 drivers, eight body measurements, and hipcenter: the horizontal distance
# of the hips from a fixed point in the car.

seat <- lm(hipcenter ~ ., data = seatpos)

summary(seat)

# The omnibus test is highly significant and not one t test is. The extra sum
# of squares test asks about predictors in groups instead.

# ===========================================================================
# 1. TESTING A SUBSET -- handout question 5
# ===========================================================================
# Fit the reduced model each H0 describes, then compare it to the full model.
# anova() reports Df (the number of constraints), Sum of Sq (the extra sum of
# squares), F and p.
#
# You can drop terms with a minus sign: lm(hipcenter ~ . - Age, data = seatpos)

# (a) do the body measurements matter, given Age and Weight?
#     target: F = 3.2402, p = 0.0146



# (b) do we need both HtShoes and Ht, given everything else?
#     target: F = 0.3433, p = 0.7123



# (c) does Age matter, given everything else?
#     target: F = 1.8499, p = 0.1843



# Compare (a) to the individual t tests. How can a group of predictors matter
# when no single one does?



# ===========================================================================
# 2. ONE PREDICTOR, TWO WAYS -- handout question 6b
# ===========================================================================
# Test (c) imposes a single constraint, so it is the partial t test on Age in
# disguise. Pull out the t statistic and square it.

summary(seat)$coefficients['Age', 't value']^2

# Check this against your F statistic from (c). The t column in summary() is a
# whole set of F tests, each dropping one predictor with the rest left in.

# ===========================================================================
# 3. TESTING A CONTRAST -- handout question 10
# ===========================================================================
# A contrast c'beta is still a linear constraint, so it still gives an F test
# with one numerator degree of freedom.
#
# HtShoes and Ht measure the same person, differing only by the shoes:
#
#     H0: beta_HtShoes = beta_Ht

car::linearHypothesis(seat, 'HtShoes = Ht')   # target: p = 0.8684

# You can get there by hand too. The constraint forces the two predictors to
# share a coefficient, so they enter as a sum. Build that reduced model and
# confirm you get the same F.



# What does this test tell you about whether height matters for seat position?



# ===========================================================================
# YOUR TURN: what drives a country's savings rate?
# ===========================================================================
# Savings rates averaged over 1960-1970 for 50 countries. Two predictors
# describe the age structure of the population (pop15, pop75) and two describe
# the economy (dpi, ddpi).
#
# GOAL: run all three kinds of test on a model you have not seen fitted.

load(url(paste0(site, 'savings.RData')))

m1 <- lm(sr ~ ., data = savings)

# --- a subset ---------------------------------------------------------------
# Does age structure matter? Write H0 first, then fit the reduced model.



# --- one predictor ----------------------------------------------------------
# Test whether ddpi belongs, and check that F = t^2.



# --- a contrast -------------------------------------------------------------
# Are the two demographic effects equal and opposite, i.e. pop15 + pop75 = 0?
# Then test whether they are equal instead. Do these ask the same thing?


