Lecture : Prof. Dr. Suhartono, M.Kom
library(tidyverse)
## ── Attaching core tidyverse packages ──────────────────────── tidyverse 2.0.0 ──
## ✔ dplyr 1.1.2 ✔ readr 2.1.4
## ✔ forcats 1.0.0 ✔ stringr 1.5.0
## ✔ ggplot2 3.4.2 ✔ tibble 3.2.1
## ✔ lubridate 1.9.2 ✔ tidyr 1.3.0
## ✔ purrr 1.0.1
## ── Conflicts ────────────────────────────────────────── tidyverse_conflicts() ──
## ✖ dplyr::filter() masks stats::filter()
## ✖ dplyr::lag() masks stats::lag()
## ℹ Use the conflicted package (<http://conflicted.r-lib.org/>) to force all conflicts to become errors
library(caret)
## Loading required package: lattice
##
## Attaching package: 'caret'
##
## The following object is masked from 'package:purrr':
##
## lift
X1 <- c(1, 2, 3, 4, 5)
X2 <- c(2, 4, 6, 8, 10)
Y <- c(3, 5, 7, 9, 11)
data <- data.frame(X1, X2, Y)
X <- data[, -ncol(data)] # Mengambil semua kolom kecuali kolom terakhir
Y <- data[, ncol(data)] # Mengambil kolom terakhir
partisi <- createDataPartition(Y, p = 0.8, list = FALSE)
data_latih <- data[partisi, ]
data_uji <- data[-partisi, ]
model <- train(Y ~ ., data = data_latih, method = "lm")
## Warning in predict.lm(modelFit, newdata): prediction from rank-deficient fit;
## attr(*, "non-estim") has doubtful cases
## Warning in nominalTrainWorkflow(x = x, y = y, wts = weights, info = trainInfo,
## : There were missing values in resampled performance measures.
print(model)
## Linear Regression
##
## 5 samples
## 2 predictors
##
## No pre-processing
## Resampling: Bootstrapped (25 reps)
## Summary of sample sizes: 5, 5, 5, 5, 5, 5, ...
## Resampling results:
##
## RMSE Rsquared MAE
## 1.09396e-15 1 1.011537e-15
##
## Tuning parameter 'intercept' was held constant at a value of TRUE
prediksi <- predict(model, newdata = data_uji)
rmse <- RMSE(prediksi, data_uji$Y)
print(rmse)
## [1] NaN