-1

I can use the anova function to get the ANOVA table for linear model. However this gives the individual effects of each explanatory variable on the response variable. I wonder if there is any function in R to get the overall ANOVA where we can get the overall effect of all explanatory variables on response variable. Thanks in advance for your help.

MWE

set.seed(12345)
X1 <- 10*abs(rnorm(n=10, mean = 5, sd = 1))
X2 <- 20*abs(rnorm(n=10, mean = 4, sd = 2))
Y <- 50*abs(rnorm(n=10, mean = 10, sd = 4))

df <- data.frame(X1, X2, Y)

fm1 <- lm(formula=Y~X1+X2, data=df)
anova(fm1)

Analysis of Variance Table

Response: Y
          Df Sum Sq Mean Sq F value Pr(>F)
X1         1  37842   37842  0.7815  0.406
X2         1 115774  115774  2.3910  0.166
Residuals  7 338941   48420  

Edited (2017-10-23)

The required output is

Analysis of Variance Table

Response: Y
           Df Sum Sq      Mean Sq      F value  Pr(>F)
Regression 2  153616.587  76808.293    1.586    0.2703
Residuals  7  338940.570  48420.081  
MYaseen208
  • 22,666
  • 37
  • 165
  • 309

2 Answers2

6

OK let's try this: The F-statistic for the fit compares the fit to a fit with no predictors, e.g. the fit to Y~1. So

anova(lm(Y~1,df),lm(Y~X1+X2,df))
# Analysis of Variance Table
#
# Model 1: Y ~ 1
# Model 2: Y ~ X1 + X2
#   Res.Df    RSS Df Sum of Sq      F Pr(>F)
# 1      9 492557                           
# 2      7 338941  2    153617 1.5863 0.2703

gives you the components of the F-statistic of the fit:

summary(lm(Y~X1+X2,df))$fstatistic
#   value   numdf   dendf 
# 1.58629 2.00000 7.00000 
jlhoward
  • 58,004
  • 7
  • 97
  • 140
0

supernova function from package with option type = 1 gives the required output.

library(supernova)
supernova(fm1, type = 1, verbose = FALSE)
Analysis of Variance Table (Type I SS)
 Model: Y ~ X1 + X2

                 SS df         MS     F    PRE     p
 ----- | ----------  - ---------- ----- ------ -----
 Model | 153616.587  2  76808.293 1.586 0.3119 .2703
 X1    |  37842.327  1  37842.327 0.782 0.1004 .4060
 X2    | 115774.260  1 115774.260 2.391 0.2546 .1660
 Error | 338940.570  7  48420.081                   
 ----- | ----------  - ---------- ----- ------ -----
 Total | 492557.157  9  54728.573 
MYaseen208
  • 22,666
  • 37
  • 165
  • 309