# Interpreting output from anova() when using lm() as input [duplicate]

I am learning about building linear regression models by looking over someone elses R code. Here is the example data I am using:

v1  v2  v3  response
0.417655013 -0.012026453    -0.528416414    48.55555556
-0.018445979    -0.460809371    0.054017873 47.76666667
-0.246110341    0.092230159 0.057435968 49.14444444
-0.521980295    -0.428499038    0.119640369 51.08888889
0.633310578 -0.224215856    -0.153917427    48.97777778
0.41522316  0.050609412 -0.642394965    48.5
-0.07349941 0.547128578 -0.539018121    53.95555556
-0.313950353    0.207853678 0.713903994 48.16666667
0.404643796 -0.326782199    -0.785848428    47.7
0.028246796 -0.424323318    0.289313911 49.34444444
0.720822953 -0.166712488    0.323246062 50.78888889
-0.430825851    -0.308119827    0.543823856 52.65555556
-0.964175294    0.661700584 -0.11905972 51.03333333
-0.178955757    -0.11148414 -0.151179885    48.28888889
0.488388035 0.515903257 -0.087738159    48.68888889
-0.097527627    0.188292773 0.207321867 49.86666667
0.481853599 0.21142728  -0.226700254    48.38888889
1.139561277 -0.293574756    0.574855693 54.55555556
0.104077762 0.16075114  -0.131124443    48.61111111


I read in the data and use a call to lm() to build a model:

> my_data<- read.table("data.csv", header = T, sep = ",")
> my_lm <- lm(response~v1 + v2 + v3 + v1:v2 + v1:v3 + v2:v3, data=my_data)
> summary(my_lm)

Call:
lm(formula = response ~ v1 + v2 + v3 + v1:v2 + v1:v3 + v2:v3,
data = my_data)

Residuals:
Min      1Q  Median      3Q     Max
-2.0603 -0.6615 -0.1891  1.0395  1.8280

Coefficients:
Estimate Std. Error t value Pr(>|t|)
(Intercept)  49.33944    0.42089 117.226  < 2e-16 ***
v1            0.06611    0.82320   0.080  0.93732
v2           -0.36725    1.06359  -0.345  0.73585
v3            0.72741    1.00973   0.720  0.48508
v1:v2        -2.54544    2.21663  -1.148  0.27321
v1:v3         0.80641    2.77603   0.290  0.77640
v2:v3       -12.16017    3.62473  -3.355  0.00573 **
---
Signif. codes:  0 ‘***’ 0.001 ‘**’ 0.01 ‘*’ 0.05 ‘.’ 0.1 ‘ ’ 1

Residual standard error: 1.375 on 12 degrees of freedom
Multiple R-squared:  0.697, Adjusted R-squared:  0.5455
F-statistic:   4.6 on 6 and 12 DF,  p-value: 0.01191


Following along with their code I then use a call to anova():

> my_lm_anova <- anova(my_lm)
> my_lm_anova
Analysis of Variance Table

Response: response
Df  Sum Sq Mean Sq F value   Pr(>F)
v1         1  0.0010  0.0010  0.0005 0.982400
v2         1  0.2842  0.2842  0.1503 0.705036
v3         1  9.8059  9.8059  5.1856 0.041891 *
v1:v2      1  4.3653  4.3653  2.3084 0.154573
v1:v3      1 16.4582 16.4582  8.7034 0.012141 *
v2:v3      1 21.2824 21.2824 11.2545 0.005729 **
Residuals 12 22.6921  1.8910
---
Signif. codes:  0 ‘***’ 0.001 ‘**’ 0.01 ‘*’ 0.05 ‘.’ 0.1 ‘ ’ 1


However, I am not sure:

1. Why I would use the call to ANOVA in this situation, and
2. What the ANOVA table is telling me about the predictor variables.

From the code they appear to use the ANOVA table as follows. For predictor variable v1, the result of

• Adding the 'Sum Sq' entry for v1 together with half of the 'Sum Sq' entry for v1:v2 and half of the 'Sum Sq' entry for v1:v3,
• Dividing by the sum of the entire 'Sum Sq' column, and
• Multiplying by 100

gives the percent of variance of the response variable that is explained by predictor variable v1 in the lm() model. I don't see why this is nor why half of the 'Sum Sq' entry for v1:v2 is attributed to v1 and half to v2. Is this just convenience?

The anova() function call returns an ANOVA table. You can use it to get an ANOVA table any time you want one. Thus, the question becomes, 'why might I want an ANOVA table when I can just get $t$-tests of my variables with standard output (i.e., the summary.lm() command)?'

First of all, you may be perfectly satisfied with the summary output, and that's fine. However, the ANOVA table may offer some advantages. First, if you have a categorical / factor variable with more than two levels, the summary output is hard to interpret. It will give you tests of individual levels against the reference level, but won't give you a test of the factor as a whole. Consider:

set.seed(8867)                           # this makes the example exactly reproducible
y     = c(rnorm(10, mean=0,   sd=1),
rnorm(10, mean=-.5, sd=1),
rnorm(10, mean=.5,  sd=1) )
g     = rep(c("A", "B", "C"), each=10)
model = lm(y~g)
summary(model)
# ...
# Residuals:
#      Min       1Q   Median       3Q      Max
# -2.59080 -0.54685  0.04124  0.79890  2.56064
#
# Coefficients:
#             Estimate Std. Error t value Pr(>|t|)
# (Intercept)  -0.4440     0.3855  -1.152    0.260
# gB           -0.9016     0.5452  -1.654    0.110
# gC            0.6729     0.5452   1.234    0.228
#
# Residual standard error: 1.219 on 27 degrees of freedom
# Multiple R-squared:  0.2372,  Adjusted R-squared:  0.1807
# F-statistic: 4.199 on 2 and 27 DF,  p-value: 0.02583
anova(model)
# Analysis of Variance Table
#
# Response: y
#           Df Sum Sq Mean Sq F value  Pr(>F)
# g          2 12.484  6.2418   4.199 0.02583 *
# Residuals 27 40.135  1.4865


Another reason you might prefer to look at an ANOVA table is that it allows you to use information about the possible associations between your independent variables and your dependent variable that gets thrown away by the $t$-tests in the summary output. Consider your own example, you may notice that the $p$-values from the two don't match (e.g., for v1, the $p$-value in the summary output is 0.93732, but in the ANOVA table it's 0.982400). The reason is that your variables are not perfectly uncorrelated:

cor(my_data)
#                   v1          v2         v3    response
# v1        1.00000000 -0.23760679 -0.1312995 -0.00357923
# v2       -0.23760679  1.00000000 -0.2358402  0.06069167
# v3       -0.13129952 -0.23584024  1.0000000  0.32818751
# response -0.00357923  0.06069167  0.3281875  1.00000000


The result of this is that there are sums of squares that could be attributed to more than one of the variables. The $t$-tests are equivalent to 'type III' tests of the sums of squares, but other tests are possible. The default ANOVA table uses 'type I' sums of squares, which can allow you to make more precise--and more powerful--tests of your hypotheses. (This topic is fairly advanced, though, for more you may want to read my answer here: How to interpret type I (sequential) ANOVA and MANOVA?)

• You mentioned reason for different p-values is that the variables are not perfectly uncorrelated. Aren't these p-values for different tests (F-test and t-test), so one might get different p-values? – gamma Apr 3 at 8:19
• @gamma, if the variables are uncorrelated, the p-values from a t-test & from an F-test will be identical, & $F = t^2$. – gung - Reinstate Monica Apr 3 at 12:29