Skip to contents

Uses stats::anova for sequential (Type-I) tests and rmorie's native Type-II / Type-III tests otherwise, which reproduce car::Anova's defaults:

  • lm / aov: F tests. Type II tests each term after all terms that do not contain it (principle of marginality), i.e. the sum of squares is RSS(model without the term and its higher-order relatives) minus RSS(model without the relatives). Type III tests each term (and the intercept) after all others in the model as fitted, by dropping its columns from the model matrix; it therefore depends on the contrasts used to fit the model (use sum-to-zero contrasts for the textbook Type III).

  • glm: likelihood-ratio chi-square tests, by refitting the reduced model matrices with the model's family, weights, offset and control (deviance differences divided by the dispersion, as car does).

Models with aliased coefficients are handled by model comparison for Type II and rejected for Type III, as in car.

Usage

anova_table(
  model,
  typ = 2L,
  digits = 3L,
  output_format = "dataframe",
  title = "ANOVA Table"
)

Arguments

model

An lm/aov/glm fit.

typ

ANOVA type (1, 2, 3).

digits

Decimal places.

output_format

Output target.

title

Title.

Value

A character string.

References

Fox, J. and Weisberg, S. (2019). An R Companion to Applied Regression, 3rd ed., Sage (sections 5.3.4 and 6.3).

Examples

set.seed(1)
df <- data.frame(x = rnorm(30), g = gl(3, 10))
df$y <- df$x + as.numeric(df$g) + rnorm(30)
m <- lm(y ~ x * g, data = df)
anova_table(m, typ = 1L)
#>           Df Sum Sq Mean Sq F value p-value    
#> x          1 21.629  21.629  33.980  <0.001 ***
#> g          2 20.398  10.199  16.023  <0.001 ***
#> x:g        2  3.017   1.508   2.370   0.115    
#> Residuals 24 15.277   0.637                    
anova_table(m, typ = 2L)
#>           Sum Sq Df F value p-value    
#> x         26.173  1  41.118  <0.001 ***
#> g         20.398  2  16.023  <0.001 ***
#> x:g        3.017  2   2.370   0.115    
#> Residuals 15.277 24