library(ISLR)
data(Auto)
model <- lm(mpg ~ cylinders + displacement + horsepower + weight, data = Auto)
summary(model)
##
## Call:
## lm(formula = mpg ~ cylinders + displacement + horsepower + weight,
## data = Auto)
##
## Residuals:
## Min 1Q Median 3Q Max
## -11.5248 -2.7964 -0.3568 2.2577 16.3221
##
## Coefficients:
## Estimate Std. Error t value Pr(>|t|)
## (Intercept) 45.7567705 1.5200437 30.102 < 2e-16 ***
## cylinders -0.3932854 0.4095522 -0.960 0.337513
## displacement 0.0001389 0.0090099 0.015 0.987709
## horsepower -0.0428125 0.0128699 -3.327 0.000963 ***
## weight -0.0052772 0.0007166 -7.364 1.08e-12 ***
## ---
## Signif. codes: 0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
##
## Residual standard error: 4.242 on 387 degrees of freedom
## Multiple R-squared: 0.7077, Adjusted R-squared: 0.7046
## F-statistic: 234.2 on 4 and 387 DF, p-value: < 2.2e-16
new_observation <- data.frame(cylinders = 8, displacement = 318, horsepower = 150, weight = 3500)
predicted_mpg <- predict(model, newdata = new_observation)
print(predicted_mpg)
## 1
## 17.76268
library(caret)
## Loading required package: ggplot2
## Loading required package: lattice
ctrl <- trainControl(method = "cv", number = 10, verboseIter = TRUE)
metric <- "RMSE"
set.seed(123)
model_fit <- train(mpg ~ cylinders + displacement + horsepower + weight, data = Auto, method = "lm", trControl = ctrl, metric = metric)
## + Fold01: intercept=TRUE
## - Fold01: intercept=TRUE
## + Fold02: intercept=TRUE
## - Fold02: intercept=TRUE
## + Fold03: intercept=TRUE
## - Fold03: intercept=TRUE
## + Fold04: intercept=TRUE
## - Fold04: intercept=TRUE
## + Fold05: intercept=TRUE
## - Fold05: intercept=TRUE
## + Fold06: intercept=TRUE
## - Fold06: intercept=TRUE
## + Fold07: intercept=TRUE
## - Fold07: intercept=TRUE
## + Fold08: intercept=TRUE
## - Fold08: intercept=TRUE
## + Fold09: intercept=TRUE
## - Fold09: intercept=TRUE
## + Fold10: intercept=TRUE
## - Fold10: intercept=TRUE
## Aggregating results
## Fitting final model on full training set
print(model_fit)
## Linear Regression
##
## 392 samples
## 4 predictor
##
## No pre-processing
## Resampling: Cross-Validated (10 fold)
## Summary of sample sizes: 353, 352, 353, 353, 353, 354, ...
## Resampling results:
##
## RMSE Rsquared MAE
## 4.19486 0.7156793 3.241479
##
## Tuning parameter 'intercept' was held constant at a value of TRUE