library(tidyverse)
## -- Attaching packages --------------------------------------- tidyverse 1.3.1 --
## v ggplot2 3.3.6 v purrr 0.3.4
## v tibble 3.1.6 v dplyr 1.0.8
## v tidyr 1.2.0 v stringr 1.4.0
## v readr 2.1.2 v forcats 0.5.1
## -- Conflicts ------------------------------------------ tidyverse_conflicts() --
## x dplyr::filter() masks stats::filter()
## x dplyr::lag() masks stats::lag()
library(datasets)
library(tidyr)
data <- read.csv("D:/College/Datasets_csv_files/a412dfbd88b3db70b74b-5f23f993cd87c283ce766e7ac6b329ee7cc2e1d1/mtcars.csv")
View(data)
names(data)
## [1] "model" "mpg" "cyl" "disp" "hp" "drat" "wt" "qsec" "vs"
## [10] "am" "gear" "carb"
attach(data)
## The following object is masked from package:ggplot2:
##
## mpg
#fit full model
full.model <- lm(mpg ~ disp + carb + hp + cyl, data = mtcars)
#fit reduced model
reduced.model <- lm(mpg ~ disp + carb, data = mtcars)
summary(full.model)
##
## Call:
## lm(formula = mpg ~ disp + carb + hp + cyl, data = mtcars)
##
## Residuals:
## Min 1Q Median 3Q Max
## -5.0761 -1.5752 -0.2051 1.0745 6.3047
##
## Coefficients:
## Estimate Std. Error t value Pr(>|t|)
## (Intercept) 34.021595 2.523397 13.482 1.65e-13 ***
## disp -0.026906 0.011309 -2.379 0.0247 *
## carb -0.926863 0.578882 -1.601 0.1210
## hp 0.009349 0.020701 0.452 0.6551
## cyl -1.048523 0.783910 -1.338 0.1922
## ---
## Signif. codes: 0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
##
## Residual standard error: 2.973 on 27 degrees of freedom
## Multiple R-squared: 0.788, Adjusted R-squared: 0.7566
## F-statistic: 25.09 on 4 and 27 DF, p-value: 9.354e-09
summary(reduced.model)
##
## Call:
## lm(formula = mpg ~ disp + carb, data = mtcars)
##
## Residuals:
## Min 1Q Median 3Q Max
## -4.3379 -2.0849 -0.3448 1.5118 6.2836
##
## Coefficients:
## Estimate Std. Error t value Pr(>|t|)
## (Intercept) 31.152710 1.263620 24.654 < 2e-16 ***
## disp -0.036296 0.004676 -7.762 1.47e-08 ***
## carb -0.955677 0.358789 -2.664 0.0125 *
## ---
## Signif. codes: 0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
##
## Residual standard error: 2.964 on 29 degrees of freedom
## Multiple R-squared: 0.7737, Adjusted R-squared: 0.7581
## F-statistic: 49.58 on 2 and 29 DF, p-value: 4.393e-10
#perform ANOVA to test for differences in models
anova(reduced.model , full.model)
## Analysis of Variance Table
##
## Model 1: mpg ~ disp + carb
## Model 2: mpg ~ disp + carb + hp + cyl
## Res.Df RSS Df Sum of Sq F Pr(>F)
## 1 29 254.82
## 2 27 238.71 2 16.113 0.9113 0.414
modelA1 <- lm(mpg ~ hp + wt, data = mtcars)
modelB1 <- lm(mpg ~ hp + wt + am, data = mtcars)
anova(modelB1, modelA1)
## Analysis of Variance Table
##
## Model 1: mpg ~ hp + wt + am
## Model 2: mpg ~ hp + wt
## Res.Df RSS Df Sum of Sq F Pr(>F)
## 1 28 180.29
## 2 29 195.05 -1 -14.757 2.2918 0.1413