#install.packages(“mlbench”, repos = “https://cran.rstudio.com/”) #install.packages(“pROC”, repos = “https://cran.rstudio.com/”)
# Câu 1
# Load data
data("airquality")
df <- na.omit(airquality) # Loại bỏ các dòng NA
# Xây dựng mô hình hồi quy tuyến tính
model_lm <- lm(Ozone ~ ., data = df)
# Dự đoán
pred_lm <- predict(model_lm, newdata = df)
# Tính MAE (Mean Absolute Error)
mae <- mean(abs(pred_lm - df$Ozone))
# Kết quả
print(paste("MAE:", round(mae, 2)))
## [1] "MAE: 14.79"
# Câu 2
# Cài đặt và load gói cần thiết
#install.packages("mlbench")
#install.packages("pROC")
library(mlbench)
## Warning: package 'mlbench' was built under R version 4.4.3
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
data("PimaIndiansDiabetes")
df <- PimaIndiansDiabetes
# Tách tập train/test (dùng 70% train, 30% test)
set.seed(123)
train_index <- sample(1:nrow(df), 0.7 * nrow(df))
train_data <- df[train_index, ]
test_data <- df[-train_index, ]
# Xây dựng mô hình hồi quy logistic
model_logit <- glm(diabetes ~ ., data = train_data, family = "binomial")
# Dự đoán xác suất trên tập test
pred_probs <- predict(model_logit, newdata = test_data, type = "response")
# Tính ROC và AUC
roc_obj <- roc(test_data$diabetes, pred_probs)
## Setting levels: control = neg, case = pos
## Setting direction: controls < cases
auc_val <- auc(roc_obj)
# Vẽ đường ROC
plot(roc_obj, main = paste("ROC Curve - AUC:", round(auc_val, 3)))
# In giá trị AUC
print(paste("AUC:", round(auc_val, 3)))
## [1] "AUC: 0.845"