# Two way anova - two categorical predictorsmod_anova2 <-lm(bill_length_mm ~ sex + species, data = penguins)# ancova - one categorical one continuous predictormod_ancova <-lm(bill_length_mm ~ sex + flipper_length_mm, data = penguins)# Multiple regression - two continuousmod_mult <-lm(bill_length_mm ~ body_mass_g + flipper_length_mm, data = penguins)
summary(mod_anova2)
Call:
lm(formula = bill_length_mm ~ sex + species, data = penguins)
Residuals:
Min 1Q Median 3Q Max
-6.087 -1.377 -0.071 1.225 11.013
Coefficients:
Estimate Std. Error t value Pr(>|t|)
(Intercept) 36.977 0.231 160.2 <2e-16 ***
sexmale 3.694 0.255 14.5 <2e-16 ***
speciesChinstrap 10.010 0.341 29.3 <2e-16 ***
speciesGentoo 8.698 0.287 30.3 <2e-16 ***
---
Signif. codes: 0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
Residual standard error: 2.32 on 329 degrees of freedom
(11 observations deleted due to missingness)
Multiple R-squared: 0.821, Adjusted R-squared: 0.819
F-statistic: 503 on 3 and 329 DF, p-value: <2e-16
summary(mod_ancova)
Call:
lm(formula = bill_length_mm ~ sex + flipper_length_mm, data = penguins)
Residuals:
Min 1Q Median 3Q Max
-9.720 -2.812 -0.812 2.021 19.764
Coefficients:
Estimate Std. Error t value Pr(>|t|)
(Intercept) -4.4669 3.2364 -1.38 0.17
sexmale 2.0727 0.4568 4.54 8e-06 ***
flipper_length_mm 0.2359 0.0163 14.46 <2e-16 ***
---
Signif. codes: 0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
Residual standard error: 4.03 on 330 degrees of freedom
(11 observations deleted due to missingness)
Multiple R-squared: 0.46, Adjusted R-squared: 0.457
F-statistic: 141 on 2 and 330 DF, p-value: <2e-16
summary(mod_mult)
Call:
lm(formula = bill_length_mm ~ body_mass_g + flipper_length_mm,
data = penguins)
Residuals:
Min 1Q Median 3Q Max
-8.806 -2.590 -0.705 1.991 18.829
Coefficients:
Estimate Std. Error t value Pr(>|t|)
(Intercept) -3.436694 4.580553 -0.75 0.45
body_mass_g 0.000662 0.000567 1.17 0.24
flipper_length_mm 0.221865 0.032348 6.86 3.3e-11 ***
---
Signif. codes: 0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
Residual standard error: 4.12 on 339 degrees of freedom
(2 observations deleted due to missingness)
Multiple R-squared: 0.433, Adjusted R-squared: 0.43
F-statistic: 129 on 2 and 339 DF, p-value: <2e-16
Different formula
What is an interaction
Effect of one predictor depends on value of another
Interaction between categorical predictors
mod <-lm(flipper_length_mm ~ species * sex, data = penguins)summary(mod)
Call:
lm(formula = flipper_length_mm ~ species * sex, data = penguins)
Residuals:
Min 1Q Median 3Q Max
-15.795 -3.411 0.088 3.459 17.589
Coefficients:
Estimate Std. Error t value Pr(>|t|)
(Intercept) 187.795 0.662 283.72 < 2e-16 ***
speciesChinstrap 3.941 1.174 3.36 0.00088 ***
speciesGentoo 24.912 0.995 25.04 < 2e-16 ***
sexmale 4.616 0.936 4.93 1.3e-06 ***
speciesChinstrap:sexmale 3.560 1.661 2.14 0.03278 *
speciesGentoo:sexmale 4.218 1.397 3.02 0.00274 **
---
Signif. codes: 0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
Residual standard error: 5.66 on 327 degrees of freedom
(11 observations deleted due to missingness)
Multiple R-squared: 0.84, Adjusted R-squared: 0.837
F-statistic: 342 on 5 and 327 DF, p-value: <2e-16
Use y ~ x + I(x^2) to get a quadratic. Or y ~ poly(x, 2)
mod1 <-lm(flipper_length_mm ~ species + sex + species:sex, data = penguins)mod2 <-lm(flipper_length_mm ~ species * sex, data = penguins)mod3 <-lm(flipper_length_mm ~ (species + sex)^2, data = penguins)coef(mod1)