A simple linear regression model
setwd("C:\\Users\\hed2\\Downloads\\dell\\code-storage\\code")
set.seed(100)
# Train a sample model
model <- lm(mpg ~ wt + hp, data = mtcars)
# Save the model to a file
saveRDS(model, file = "linear_model.rds")
# Load the model from the file
model_save <- readRDS("linear_model.rds")
# The model is now available in the environment
summary(model_save)
##
## Call:
## lm(formula = mpg ~ wt + hp, data = mtcars)
##
## Residuals:
## Min 1Q Median 3Q Max
## -3.941 -1.600 -0.182 1.050 5.854
##
## Coefficients:
## Estimate Std. Error t value Pr(>|t|)
## (Intercept) 37.22727 1.59879 23.285 < 2e-16 ***
## wt -3.87783 0.63273 -6.129 1.12e-06 ***
## hp -0.03177 0.00903 -3.519 0.00145 **
## ---
## Signif. codes: 0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
##
## Residual standard error: 2.593 on 29 degrees of freedom
## Multiple R-squared: 0.8268, Adjusted R-squared: 0.8148
## F-statistic: 69.21 on 2 and 29 DF, p-value: 9.109e-12
predict(model_save,mtcars)
## Mazda RX4 Mazda RX4 Wag Datsun 710 Hornet 4 Drive
## 23.572329 22.583483 25.275819 21.265020
## Hornet Sportabout Valiant Duster 360 Merc 240D
## 18.327267 20.473816 15.599042 22.887067
## Merc 230 Merc 280 Merc 280C Merc 450SE
## 21.993673 19.979460 19.979460 15.725369
## Merc 450SL Merc 450SLC Cadillac Fleetwood Lincoln Continental
## 17.043831 16.849939 10.355205 9.362733
## Chrysler Imperial Fiat 128 Honda Civic Toyota Corolla
## 9.192487 26.599028 29.312380 28.046209
## Toyota Corona Dodge Challenger AMC Javelin Camaro Z28
## 24.586441 18.811364 19.140979 14.552028
## Pontiac Firebird Fiat X1-9 Porsche 914-2 Lotus Europa
## 16.756745 27.626653 26.037374 27.769769
## Ford Pantera L Ferrari Dino Maserati Bora Volvo 142E
## 16.546489 20.925413 12.739477 22.983649
Random forest model
# Install and load caret package
library(caret)
## Loading required package: ggplot2
## Loading required package: lattice
# Train a sample model using caret
model_rf <- train(mpg ~ ., data = mtcars, method = "rf")
# Save the model to a file
saveRDS(model_rf, file = "caret_model.rds")
# Load the model from the file
model_save_cr <- readRDS("caret_model.rds")
# The model is now available in the environment
summary(model_save_cr)
## Length Class Mode
## call 4 -none- call
## type 1 -none- character
## predicted 32 -none- numeric
## mse 500 -none- numeric
## rsq 500 -none- numeric
## oob.times 32 -none- numeric
## importance 10 -none- numeric
## importanceSD 0 -none- NULL
## localImportance 0 -none- NULL
## proximity 0 -none- NULL
## ntree 1 -none- numeric
## mtry 1 -none- numeric
## forest 11 -none- list
## coefs 0 -none- NULL
## y 32 -none- numeric
## test 0 -none- NULL
## inbag 0 -none- NULL
## xNames 10 -none- character
## problemType 1 -none- character
## tuneValue 1 data.frame list
## obsLevels 1 -none- logical
## param 0 -none- list
predict(model_save_cr,mtcars)
## Mazda RX4 Mazda RX4 Wag Datsun 710 Hornet 4 Drive
## 20.85935 20.85476 24.07758 20.20637
## Hornet Sportabout Valiant Duster 360 Merc 240D
## 17.67140 18.84439 14.74875 23.55262
## Merc 230 Merc 280 Merc 280C Merc 450SE
## 22.72140 18.74571 18.67910 16.27976
## Merc 450SL Merc 450SLC Cadillac Fleetwood Lincoln Continental
## 16.43029 15.94993 11.67749 11.29731
## Chrysler Imperial Fiat 128 Honda Civic Toyota Corolla
## 13.83365 30.36074 30.54158 31.37035
## Toyota Corona Dodge Challenger AMC Javelin Camaro Z28
## 22.56693 16.17739 16.50366 14.35079
## Pontiac Firebird Fiat X1-9 Porsche 914-2 Lotus Europa
## 17.62237 29.46931 26.20460 28.28629
## Ford Pantera L Ferrari Dino Maserati Bora Volvo 142E
## 15.77033 19.95253 14.90183 22.02844