library(rio) #digunakan untuk import file data
library(glmnet) #mengaktifkan fitur pemodelan regresi dengan lasso dan ridge
## Loading required package: Matrix
## Loaded glmnet 4.1-8
data_startup <- import("D:/Kuliah/Semester 5/PSD/Tugas Ridge & Lasso/50_Startups.csv")
head(data_startup) #ringkasan data
## R&D Spend Administration Marketing Spend State Profit
## 1 165349.2 136897.80 471784.1 New York 192261.8
## 2 162597.7 151377.59 443898.5 California 191792.1
## 3 153441.5 101145.55 407934.5 Florida 191050.4
## 4 144372.4 118671.85 383199.6 New York 182902.0
## 5 142107.3 91391.77 366168.4 Florida 166187.9
## 6 131876.9 99814.71 362861.4 New York 156991.1
x1 <- data_startup$`R&D Spend`
x2 <- data_startup$Administration
x3 <- data_startup$`Marketing Spend`
y <- data_startup$Profit
data_startup <- data.frame(x1,x2,x3,y)
model_klasik <- lm(y~x1+x2+x3, data = data_startup)
summary(model_klasik)
##
## Call:
## lm(formula = y ~ x1 + x2 + x3, data = data_startup)
##
## Residuals:
## Min 1Q Median 3Q Max
## -33534 -4795 63 6606 17275
##
## Coefficients:
## Estimate Std. Error t value Pr(>|t|)
## (Intercept) 5.012e+04 6.572e+03 7.626 1.06e-09 ***
## x1 8.057e-01 4.515e-02 17.846 < 2e-16 ***
## x2 -2.682e-02 5.103e-02 -0.526 0.602
## x3 2.723e-02 1.645e-02 1.655 0.105
## ---
## Signif. codes: 0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
##
## Residual standard error: 9232 on 46 degrees of freedom
## Multiple R-squared: 0.9507, Adjusted R-squared: 0.9475
## F-statistic: 296 on 3 and 46 DF, p-value: < 2.2e-16
didapatkan model regresi \[y = 50120 + 0.8057x_1 - 0.02682x_2 + 0.02723x_3\]
eval_metrics = function(model_klasik, df, predictions, target){
resids = df[,target] - predictions
resids2 = resids**2
N = length(predictions)
r2 = as.character(summary(model_klasik)$r.squared, 2)
print(r2) #Adjusted R-squared
print(as.character(round(sqrt(sum(resids2)/N), 2))) #RMSE
}
predictions = predict(model_klasik, newdata = data_startup)
eval_metrics(model_klasik, data_startup, predictions, target = 'y')
## [1] "0.950745994068325"
## [1] "8855.34"
didapatkan nilai R-Square = 0.95074599 dan nilai RMSE = 8855.34
y <- data_startup$y #peubah respon
x <- data.matrix(data_startup[, c('x1', 'x2', 'x3')]) #Peubah penjelas
model_ridge <- glmnet(x, y, alpha = 0)
summary(model_ridge)
## Length Class Mode
## a0 100 -none- numeric
## beta 300 dgCMatrix S4
## df 100 -none- numeric
## dim 2 -none- numeric
## lambda 100 -none- numeric
## dev.ratio 100 -none- numeric
## nulldev 1 -none- numeric
## npasses 1 -none- numeric
## jerr 1 -none- numeric
## offset 1 -none- logical
## call 4 -none- call
## nobs 1 -none- numeric
cv_ridge <- cv.glmnet(x, y, alpha = 0)
optimal_lambda <- cv_ridge$lambda.min
optimal_lambda
## [1] 3881.978
model_ridge <- glmnet(x, y, alpha = 0, lambda = optimal_lambda)
coef(model_ridge)
## 4 x 1 sparse Matrix of class "dgCMatrix"
## s0
## (Intercept) 4.680600e+04
## x1 6.726292e-01
## x2 2.759133e-02
## x3 5.815077e-02
didapatkan model \[y = 46806 + 0.6726292x_1 + 0.02759133x_2 + 0.05815077x_3\]
eval_results <- function(true, predicted, df) {
SSE <- sum((predicted - true)^2)
SST <- sum((true - mean(true))^2)
R_square <- 1 - SSE / SST
RMSE = sqrt(SSE/nrow(df))
# Model performance metrics
data.frame(
RMSE = RMSE,
Rsquare = R_square
)
}
predictions_ridge <- predict(model_ridge, s = optimal_lambda, newx = x)
eval_results(y, predictions_ridge, data_startup)
## RMSE Rsquare
## 1 9680.337 0.9411412
didapatkan nilai R-Square = 0.9411412 dan nilai RMSE = 9680.337
model_lasso <- cv.glmnet(x, y, alpha = 1,standardize = TRUE)
summary(model_lasso)
## Length Class Mode
## lambda 60 -none- numeric
## cvm 60 -none- numeric
## cvsd 60 -none- numeric
## cvup 60 -none- numeric
## cvlo 60 -none- numeric
## nzero 60 -none- numeric
## call 5 -none- call
## name 1 -none- character
## glmnet.fit 12 elnet list
## lambda.min 1 -none- numeric
## lambda.1se 1 -none- numeric
## index 2 -none- numeric
lambda_best <- model_lasso$lambda.min
lambda_best
## [1] 856.0269
lasso_model <- glmnet(x, y, alpha = 1, lambda = lambda_best,standardize = TRUE)
coef(lasso_model)
## 4 x 1 sparse Matrix of class "dgCMatrix"
## s0
## (Intercept) 4.864531e+04
## x1 7.857438e-01
## x2 .
## x3 2.578377e-02
didapatkan model dengan hasil \[y = 48645.31 + 0.7857438x_1 + 0.02578377x_3\]
eval_results <- function(true, predicted, df) {
SSE <- sum((predicted - true)^2)
SST <- sum((true - mean(true))^2)
R_square <- 1 - SSE / SST
RMSE = sqrt(SSE/nrow(df))
# Model performance metrics
data.frame(
RMSE = RMSE,
Rsquare = R_square
)
}
predictions_lasso <- predict(lasso_model, s = lambda_best, newx = x)
eval_results(y, predictions_lasso, data_startup)
## RMSE Rsquare
## 1 8929.504 0.9499176
didapatkan nilai R-Square = 0.9499176 dan nilai RMSE = 8929.504
Dari ketiga model yang digunakan didapatkan hasil : Regresi Linear : R-Square = 0.95074599 RMSE = 8855.34 Regresi Ridge : R-Square = 0.9411412 dan RMSE = 9680.337 Regresi Lasso : R-Square = 0.9499176 dan RMSE = 8929.504
Dapat disimpulkan bahwa dari ketiga model yang digunakan, model Regresi Klasik merupakan model terbaik karena memiliki nilai R-Square tertinggi (0.95074599), sehingga menunjukkan bahwa model ini paling baik dalam menjelaskan variasi dalam data. Selain itu model ini juga memiliki RMSE yang paling rendah (8855.34), menunjukkan bahwa prediksinya juga akurat dalam menyesuaikan data. Oleh karena itu, berdasarkan metrik yang diberikan, model Regresi klasik merupakan yang terbaik di antara kedua model lain.
Adapun Persamaan Regresi yang didapat adalah: \[y = 50120 + 0.8057x_1 - 0.02682x_2 + 0.02723x_3\] yang berarti -50120 adalah nilai yang akan dihasilkan oleh peubah respon (y) ketika semua peubah penjelas (x₁, x₂, dan x₃) sama dengan 0. -ketika x₁ meningkat satu satuan, y diperkirakan akan meningkat sebanyak 0.8057 satuan, selama x₂ dan x₃ tetap konstan. -ketika x₂ meningkat satu satuan, y diperkirakan akan menurun sebanyak 0.02682 satuan, selama x₁ dan x₃ tetap konstan. -ketika x₃ meningkat satu satuan, y diperkirakan akan meningkat sebanyak 0.02723 satuan, selama x₁ dan x₂ tetap konstan.