Reputation: 1526
Suppose you have fit a model with an intercept term in R
. When you call the anova
function on the model it does not show the null model prior to adding the intercept term, or even the model that only includes the intercept. Below is an example using a simple linear regression.
#Generate random dataset
set.seed(1)
n <- 100
b0 <- 10
b1 <- 3
sig <- 4
xx <- 10*runif(n)
ee <- sig*rnorm(n)
DATA <- data.frame(x = xx, y = b0 + b1*xx + ee)
#Fit a linear model
MODEL <- lm(y ~ 1 + x, data = DATA)
#Show the ANOVA
anova(MODEL)
Analysis of Variance Table
Response: y
Df Sum Sq Mean Sq F value Pr(>F)
x 1 6922.1 6922.1 488.52 < 2.2e-16 ***
Residuals 98 1388.6 14.2
---
Signif. codes: 0 ‘***’ 0.001 ‘**’ 0.01 ‘*’ 0.05 ‘.’ 0.1 ‘ ’ 1
I would like to generate the ANOVA table for the model with two additional preliminary rows, starting with a first row that uses a null model with no intercept term and then showing a second row using the model with only the intercept term. The remaining output shown above would then start at the third row of the ANOVA table.
What is the simplest way to do this?
Upvotes: 1
Views: 480
Reputation: 141
MODEL1 <- lm(y ~ -1, data = DATA)
MODEL2 <- lm(y ~ 1, data = DATA)
MODEL3 <- lm(y ~ x, data = DATA)
anova(MODEL1, MODEL2, MODEL3)
Upvotes: 2