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:
- Why I would use the call to ANOVA in this situation, and
- 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?