Day 8 Applied examples

June 18th, 2025

8.1 Announcements

url <- "https://raw.githubusercontent.com/stat720/summer2025/refs/heads/main/data/blood_study_pigs.csv"
df_pigs <- read.csv(url)

## Type I 
m_intercept.only <- lm(Serum_haptoglobin_mg.dL ~ 1, data = df_pigs)
m_Trt.only <- lm(Serum_haptoglobin_mg.dL ~ Trt, data = df_pigs)
m_Trt.Day <- lm(Serum_haptoglobin_mg.dL ~ Trt+factor(Day), data = df_pigs)
m_Trt.Day.TxD <- lm(Serum_haptoglobin_mg.dL ~ Trt*factor(Day), data = df_pigs)

sum(m_intercept.only$residuals^2)
## [1] 212318.2
sum(m_intercept.only$residuals^2) - sum(m_Trt.only$residuals^2)
## [1] 81720.2
sum(m_Trt.only$residuals^2) - sum(m_Trt.Day$residuals^2)
## [1] 69494.52
sum(m_Trt.Day$residuals^2) - sum(m_Trt.Day.TxD$residuals^2)
## [1] 24352.12
sum(m_Trt.Day.TxD$residuals^2)
## [1] 36751.37
anova(m_Trt.Day.TxD)
## Analysis of Variance Table
## 
## Response: Serum_haptoglobin_mg.dL
##                  Df Sum Sq Mean Sq F value    Pr(>F)    
## Trt               5  81720   16344  80.049 < 2.2e-16 ***
## factor(Day)       1  69495   69495 340.369 < 2.2e-16 ***
## Trt:factor(Day)   5  24352    4870  23.854 < 2.2e-16 ***
## Residuals       180  36751     204                      
## ---
## Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
## Type II 

m_intercept.only <- lm(Serum_haptoglobin_mg.dL ~ 1, data = df_pigs)
m_Day.only <- lm(Serum_haptoglobin_mg.dL ~ factor(Day), data = df_pigs)
m_Trt.only <- lm(Serum_haptoglobin_mg.dL ~ Trt , data = df_pigs)
m_Trt.Day <- lm(Serum_haptoglobin_mg.dL ~ Trt + factor(Day), data = df_pigs)
m_Trt.Day.TxD <- lm(Serum_haptoglobin_mg.dL ~ Trt*factor(Day), data = df_pigs)

sum(m_intercept.only$residuals^2)
## [1] 212318.2
sum(m_Day.only$residuals^2) - sum(m_Trt.Day$residuals^2)
## [1] 81720.2
sum(m_Trt.only$residuals^2) - sum(m_Trt.Day$residuals^2)
## [1] 69494.52
sum(m_Trt.Day$residuals^2) - sum(m_Trt.Day.TxD$residuals^2)
## [1] 24352.12
sum(m_Trt.Day.TxD$residuals^2)
## [1] 36751.37
car::Anova(m_Trt.Day.TxD, type = 2)
## Anova Table (Type II tests)
## 
## Response: Serum_haptoglobin_mg.dL
##                 Sum Sq  Df F value    Pr(>F)    
## Trt              81720   5  80.049 < 2.2e-16 ***
## factor(Day)      69495   1 340.369 < 2.2e-16 ***
## Trt:factor(Day)  24352   5  23.854 < 2.2e-16 ***
## Residuals        36751 180                      
## ---
## Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1

8.2 Applied example for today

R script

8.3 Tomorrow

  • Holiday.
  • Kahoot! on Friday.