Created
May 27, 2021 10:06
-
-
Save Nutzagag-Cloud/923ae6b5c1f5e06604a6ae6145029841 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
## correlation | |
cor(mtcars$mpg, mtcars$hp) | |
plot(mtcars$mpg, mtcars$hp) | |
plot(mtcars$mpg, mtcars$hp, pch = 16) | |
cor(mtcars[ , c("mpg", "wt", "hp")]) | |
## dplyr | |
library(dplyr) | |
mtcars %>% | |
select(mpg, wt, hp) %>% | |
cor() | |
cor.test(mtcars$hp, mtcars$mpg) | |
## linear Regression | |
lmfit <- lm(mpg ~ hp, data = mtcars) | |
summary(lmfit) | |
lmfit$coefficient | |
lmfit$coefficients[1] + lmfit$coefficients[2] *200 | |
lmfit$coefficients[[1]] + lmfit$coefficients[[2]] *200 | |
newcars<- data.frame(hp = c(450, 400, 250, 320)) | |
## predict | |
predict(lmfit, newdata = newcars) | |
newcars$mpg_pred <- predict(lmfit, newdata = newcars) | |
newcars$hp_pred <- NULL | |
newcars | |
summary(mtcars$hp) | |
## Root mean squared error(rmse) | |
## mutiple linear regression | |
## mpg = f(hp, wt, am) | |
lmfitv2 <- lm(mpg ~ hp + wt + am, data = mtcars) | |
coefs <- coef(lmfitv2) | |
coefs[[1]] + coefs[[2]]*200 + coefs[[3]]*3.5 + coefs[[4]]*1 | |
##build full Model | |
lmfull <- lm(mpg ~ ., data = mtcars) | |
mtcars$predicted <- predict(lmfull) | |
## RMSE | |
squared_error <- (mtcars$mpg - mtcars$predicted) ** 2 | |
(rmse <- sqrt(mean(squared_error))) | |
## split data | |
## random split | |
set.seed(42) | |
sample(1:10, 3) | |
x <- nrow(mtcars) | |
id <- sample(1:x, size = x*0.8) | |
train_data <- mtcars[id, ] | |
test_data <- mtcars[id, ] | |
## train model | |
model1 <- lm(mpg ~ hp + wt + am + disp, data = train_data) | |
p_train <- predict(model1) | |
rmse <- sqrt(mean((train_data$mpg - p_train)**2)) | |
## test model | |
p_test <- predict(model1, newdata = test_data) | |
error_test <- test_data$mpg - p_test | |
(rmse_test <- sqrt(mean((train_data$mpg - p_train)**2))) | |
##print result | |
cat("RMSE Train :", rmse_test, "\nRMSE test :", rmse_test) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment