# 1. Tải dữ liệu
data(airquality)
df1 <- na.omit(airquality) # Loại bỏ giá trị khuyết
View(df1)
View(airquality)
# 2. Xây dựng mô hình hồi quy tuyến tính
# Dự đoán Ozone dựa trên các biến còn lại (Solar.R, Wind, Temp, Month, Day)
model1 <- lm(Ozone ~ ., data = df1)
summary(model1)
## 
## Call:
## lm(formula = Ozone ~ ., data = df1)
## 
## Residuals:
##     Min      1Q  Median      3Q     Max 
## -37.014 -12.284  -3.302   8.454  95.348 
## 
## Coefficients:
##              Estimate Std. Error t value Pr(>|t|)    
## (Intercept) -64.11632   23.48249  -2.730  0.00742 ** 
## Solar.R       0.05027    0.02342   2.147  0.03411 *  
## Wind         -3.31844    0.64451  -5.149 1.23e-06 ***
## Temp          1.89579    0.27389   6.922 3.66e-10 ***
## Month        -3.03996    1.51346  -2.009  0.04714 *  
## Day           0.27388    0.22967   1.192  0.23576    
## ---
## Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
## 
## Residual standard error: 20.86 on 105 degrees of freedom
## Multiple R-squared:  0.6249, Adjusted R-squared:  0.6071 
## F-statistic: 34.99 on 5 and 105 DF,  p-value: < 2.2e-16
# 3. Dự đoán và tính MAE
predictions1 <- predict(model1, df1)
mae <- mean(abs(df1$Ozone - predictions1))

print(paste("MAE của mô hình là:", round(mae, 4)))
## [1] "MAE của mô hình là: 14.789"
library(mlbench)
library(pROC)
## Type 'citation("pROC")' for a citation.
## 
## Attaching package: 'pROC'
## The following objects are masked from 'package:stats':
## 
##     cov, smooth, var
# 1. Tải dữ liệu
data("PimaIndiansDiabetes")
df2 <- PimaIndiansDiabetes

# 2. Xây dựng mô hình Logistic Regression
# Biến mục tiêu là 'diabetes'
model2 <- glm(diabetes ~ ., data = df2, family = binomial)

# 3. Dự đoán xác suất
prob <- predict(model2, type = "response")

# 4. Tính ROC và AUC
roc_obj <- roc(df2$diabetes, prob)
## Setting levels: control = neg, case = pos
## Setting direction: controls < cases
auc_value <- auc(roc_obj)

# 5. Vẽ đồ thị ROC
plot(roc_obj, main = paste("ROC Curve (AUC =", round(auc_value, 4), ")"), col = "blue")

print(paste("AUC của mô hình là:", round(auc_value, 4)))
## [1] "AUC của mô hình là: 0.8394"