library(MASS)

# Tải dữ liệu Boston
data(Boston)

# Xây dựng mô hình hồi quy tuyến tính
model <- lm(medv ~ lstat + rm, data = Boston)

# Dự đoán giá trị
predictions <- predict(model, newdata = Boston)

# Tính MAE (Mean Absolute Error)
MAE <- mean(abs(predictions - Boston$medv))

# Tính MSE (Mean Squared Error)
MSE <- mean((predictions - Boston$medv)^2)

# In kết quả
print(paste("MAE:", MAE))
## [1] "MAE: 3.95258006711927"
print(paste("MSE:", MSE))
## [1] "MSE: 30.5124687772995"
library(ISLR)
## Warning: package 'ISLR' was built under R version 4.4.3
library(ggplot2)
library(caret)
## Warning: package 'caret' was built under R version 4.4.3
## Loading required package: lattice
library(pROC)
## Warning: package 'pROC' was built under R version 4.4.3
## Type 'citation("pROC")' for a citation.
## 
## Attaching package: 'pROC'
## The following objects are masked from 'package:stats':
## 
##     cov, smooth, var
# Load dữ liệu Default
data("Default")

# Chuyển đổi biến default thành dạng nhị phân
Default$default <- ifelse(Default$default == "Yes", 1, 0)

# Xây dựng mô hình hồi quy logistic
model <- glm(default ~ balance + income, data = Default, family = binomial)

# Dự đoán xác suất
pred_probs <- predict(model, type = "response")

# Tính ROC và AUC
roc_curve <- roc(Default$default, pred_probs)
## Setting levels: control = 0, case = 1
## Setting direction: controls < cases
auc_value <- auc(roc_curve)

# Vẽ ROC curve
plot(roc_curve, col = "blue", main = paste("ROC Curve - AUC:", round(auc_value, 2)))
abline(a = 0, b = 1, lty = 2, col = "gray")

# In giá trị AUC
print(paste("AUC:", round(auc_value, 2)))
## [1] "AUC: 0.95"

R Markdown

This is an R Markdown document. Markdown is a simple formatting syntax for authoring HTML, PDF, and MS Word documents. For more details on using R Markdown see http://rmarkdown.rstudio.com.

When you click the Knit button a document will be generated that includes both content as well as the output of any embedded R code chunks within the document. You can embed an R code chunk like this:

summary(cars)
##      speed           dist       
##  Min.   : 4.0   Min.   :  2.00  
##  1st Qu.:12.0   1st Qu.: 26.00  
##  Median :15.0   Median : 36.00  
##  Mean   :15.4   Mean   : 42.98  
##  3rd Qu.:19.0   3rd Qu.: 56.00  
##  Max.   :25.0   Max.   :120.00

Including Plots

You can also embed plots, for example:

Note that the echo = FALSE parameter was added to the code chunk to prevent printing of the R code that generated the plot.