library(MASS)
data<-read.csv("C:/Users/amohm/OneDrive/Desktop/data-table-B9.csv", header = TRUE)
colnames(data) <- c('x1', 'x2', 'x3', 'x4', 'y')
First Model
``` r
model1 <- lm(y~x1+x2+x3+x4+x1:x2+x1:x3+x1:x4+x2:x3+x2:x4+x3:x4, data)
summary(model1)
```
```
##
## Call:
## lm(formula = y ~ x1 + x2 + x3 + x4 + x1:x2 + x1:x3 + x1:x4 +
## x2:x3 + x2:x4 + x3:x4, data = data)
##
## Residuals:
## Min 1Q Median 3Q Max
## -9.4804 -3.0766 -0.6635 2.9625 12.2221
##
## Coefficients: (2 not defined because of singularities)
## Estimate Std. Error t value Pr(>|t|)
## (Intercept) 15.88376 23.17863 0.685 0.49616
## x1 0.18696 0.78447 0.238 0.81255
## x2 0.37921 0.06332 5.989 1.89e-07 ***
## x3 -11.99940 67.31148 -0.178 0.85919
## x4 -8.86442 35.62553 -0.249 0.80446
## x1:x2 0.01155 0.00869 1.329 0.18955
## x1:x3 NA NA NA NA
## x1:x4 -1.11525 1.14847 -0.971 0.33592
## x2:x3 NA NA NA NA
## x2:x4 -0.38547 0.11962 -3.222 0.00218 **
## x3:x4 72.85976 103.15353 0.706 0.48308
## ---
## Signif. codes: 0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
##
## Residual standard error: 4.683 on 53 degrees of freedom
## Multiple R-squared: 0.7496, Adjusted R-squared: 0.7118
## F-statistic: 19.83 on 8 and 53 DF, p-value: 1.947e-13
```
Second Model
``` r
model2 <- lm(y~x1+x2+x3+x4+x1:x2+x1:x3+x1:x4+x2:x3+x2:x4, data)
summary(model2)
```
```
##
## Call:
## lm(formula = y ~ x1 + x2 + x3 + x4 + x1:x2 + x1:x3 + x1:x4 +
## x2:x3 + x2:x4, data = data)
##
## Residuals:
## Min 1Q Median 3Q Max
## -9.697 -3.366 -0.619 2.935 12.248
##
## Coefficients: (2 not defined because of singularities)
## Estimate Std. Error t value Pr(>|t|)
## (Intercept) -0.082307 5.103231 -0.016 0.98719
## x1 0.168736 0.780398 0.216 0.82963
## x2 0.379500 0.063023 6.022 1.57e-07 ***
## x3 34.976261 10.325554 3.387 0.00132 **
## x4 15.927554 6.068027 2.625 0.01125 *
## x1:x2 0.011544 0.008649 1.335 0.18757
## x1:x3 NA NA NA NA
## x1:x4 -1.091776 1.142653 -0.955 0.34360
## x2:x3 NA NA NA NA
## x2:x4 -0.386007 0.119062 -3.242 0.00204 **
## ---
## Signif. codes: 0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
##
## Residual standard error: 4.661 on 54 degrees of freedom
## Multiple R-squared: 0.7473, Adjusted R-squared: 0.7145
## F-statistic: 22.81 on 7 and 54 DF, p-value: 4.942e-14
```
anova(model1,model2)
## Analysis of Variance Table
##
## Model 1: y ~ x1 + x2 + x3 + x4 + x1:x2 + x1:x3 + x1:x4 + x2:x3 + x2:x4 +
## x3:x4
## Model 2: y ~ x1 + x2 + x3 + x4 + x1:x2 + x1:x3 + x1:x4 + x2:x3 + x2:x4
## Res.Df RSS Df Sum of Sq F Pr(>F)
## 1 53 1162.4
## 2 54 1173.4 -1 -10.942 0.4989 0.4831
model1 <- lm(y~x1+x2+x3+x4+x1:x2+x1:x3+x1:x4+x2:x3+x2:x4+x3:x4, data)
model2 <- lm(y~x1+x3+x4+x1:x4, data)
summary(model2)
##
## Call:
## lm(formula = y ~ x1 + x3 + x4 + x1:x4, data = data)
##
## Residuals:
## Min 1Q Median 3Q Max
## -12.244 -6.507 -2.370 4.203 25.774
##
## Coefficients:
## Estimate Std. Error t value Pr(>|t|)
## (Intercept) 12.3647 9.1290 1.354 0.181
## x1 -0.8596 1.4200 -0.605 0.547
## x3 30.2631 19.1490 1.580 0.120
## x4 5.1275 10.6120 0.483 0.631
## x1:x4 0.5423 2.0891 0.260 0.796
##
## Residual standard error: 8.655 on 57 degrees of freedom
## Multiple R-squared: 0.08031, Adjusted R-squared: 0.01577
## F-statistic: 1.244 on 4 and 57 DF, p-value: 0.3026
anova(model1, model2)
## Analysis of Variance Table
##
## Model 1: y ~ x1 + x2 + x3 + x4 + x1:x2 + x1:x3 + x1:x4 + x2:x3 + x2:x4 +
## x3:x4
## Model 2: y ~ x1 + x3 + x4 + x1:x4
## Res.Df RSS Df Sum of Sq F Pr(>F)
## 1 53 1162.4
## 2 57 4269.7 -4 -3107.3 35.419 2.157e-14 ***
## ---
## Signif. codes: 0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
plot(model2)
``` r
model1 <- lm(y~x1+x2+x3+x4+x1:x2+x1:x3+x1:x4+x2:x3+x2:x4+x3:x4, data)
model2 <- lm(y~x1+x3+x4+x4:x1, data)
anova(model1, model2)
```
```
## Analysis of Variance Table
##
## Model 1: y ~ x1 + x2 + x3 + x4 + x1:x2 + x1:x3 + x1:x4 + x2:x3 + x2:x4 +
## x3:x4
## Model 2: y ~ x1 + x3 + x4 + x4:x1
## Res.Df RSS Df Sum of Sq F Pr(>F)
## 1 53 1162.4
## 2 57 4269.7 -4 -3107.3 35.419 2.157e-14 ***
## ---
## Signif. codes: 0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
```
model1 <- lm(y~x1+x2+x3+x4+x1:x2+x1:x3+x1:x4+x2:x3+x2:x4+x3:x4, data)
model2 <- lm(y~x1+x3+x4+x4:x1, data)
anova(model1, model2)
## Analysis of Variance Table
##
## Model 1: y ~ x1 + x2 + x3 + x4 + x1:x2 + x1:x3 + x1:x4 + x2:x3 + x2:x4 +
## x3:x4
## Model 2: y ~ x1 + x3 + x4 + x4:x1
## Res.Df RSS Df Sum of Sq F Pr(>F)
## 1 53 1162.4
## 2 57 4269.7 -4 -3107.3 35.419 2.157e-14 ***
## ---
## Signif. codes: 0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
newx1 <- c(.5,10)
newx2 <- c(10,3)
newx3 <- c(.5,.25)
newx4 <- c(.75,.85)
Confidence interval
predict(model2, data.frame(x1=newx1,x3=newx3,x4=newx4), interval = "confidence")
## fit lwr upr
## 1 31.11546 22.13526 40.09566
## 2 20.30224 11.50942 29.09506
Prediction interval
predict(model2, data.frame(x1=newx1,x3=newx3,x4=newx4), interval = "prediction")
## fit lwr upr
## 1 31.11546 11.5959996 50.63492
## 2 20.30224 0.8682749 39.73620