#Câu 1:
# Load dataset có sẵn trong R
data("airquality")
# Xem một vài dòng đầu tiên của dữ liệu
head(airquality)
## Ozone Solar.R Wind Temp Month Day
## 1 41 190 7.4 67 5 1
## 2 36 118 8.0 72 5 2
## 3 12 149 12.6 74 5 3
## 4 18 313 11.5 62 5 4
## 5 NA NA 14.3 56 5 5
## 6 28 NA 14.9 66 5 6
# Kiểm tra dữ liệu có giá trị thiếu không
sum(is.na(airquality))
## [1] 44
# Loại bỏ các dòng có giá trị NA
airquality <- na.omit(airquality)
# Kiểm tra lại dữ liệu
summary(airquality)
## Ozone Solar.R Wind Temp
## Min. : 1.0 Min. : 7.0 Min. : 2.30 Min. :57.00
## 1st Qu.: 18.0 1st Qu.:113.5 1st Qu.: 7.40 1st Qu.:71.00
## Median : 31.0 Median :207.0 Median : 9.70 Median :79.00
## Mean : 42.1 Mean :184.8 Mean : 9.94 Mean :77.79
## 3rd Qu.: 62.0 3rd Qu.:255.5 3rd Qu.:11.50 3rd Qu.:84.50
## Max. :168.0 Max. :334.0 Max. :20.70 Max. :97.00
## Month Day
## Min. :5.000 Min. : 1.00
## 1st Qu.:6.000 1st Qu.: 9.00
## Median :7.000 Median :16.00
## Mean :7.216 Mean :15.95
## 3rd Qu.:9.000 3rd Qu.:22.50
## Max. :9.000 Max. :31.00
# Xây dựng mô hình dự đoán Ozone dựa vào các biến còn lại
model <- lm(Ozone ~ ., data = airquality)
# Xem kết quả mô hình
summary(model)
##
## Call:
## lm(formula = Ozone ~ ., data = airquality)
##
## 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
#Câu 2:
# Cài đặt thư viện cần thiết
#install.packages("Metrics")
library(Metrics)
## Warning: package 'Metrics' was built under R version 4.4.3
# Dự đoán giá trị Ozone
predictions <- predict(model, newdata = airquality)
# Tính MAE
mae_value <- mae(airquality$Ozone, predictions)
print(paste("Mean Absolute Error (MAE):", mae_value))
## [1] "Mean Absolute Error (MAE): 14.7889691435055"
# Cài đặt gói cần thiết
#install.packages("mlbench")
library(mlbench)
## Warning: package 'mlbench' was built under R version 4.4.3
# Load dataset
data("PimaIndiansDiabetes")
# Xem dữ liệu
head(PimaIndiansDiabetes)
## pregnant glucose pressure triceps insulin mass pedigree age diabetes
## 1 6 148 72 35 0 33.6 0.627 50 pos
## 2 1 85 66 29 0 26.6 0.351 31 neg
## 3 8 183 64 0 0 23.3 0.672 32 pos
## 4 1 89 66 23 94 28.1 0.167 21 neg
## 5 0 137 40 35 168 43.1 2.288 33 pos
## 6 5 116 74 0 0 25.6 0.201 30 neg
# Xây dựng mô hình hồi quy logistic
model_logit <- glm(diabetes ~ ., data = PimaIndiansDiabetes, family = binomial)
# Xem kết quả mô hình
summary(model_logit)
##
## Call:
## glm(formula = diabetes ~ ., family = binomial, data = PimaIndiansDiabetes)
##
## Coefficients:
## Estimate Std. Error z value Pr(>|z|)
## (Intercept) -8.4046964 0.7166359 -11.728 < 2e-16 ***
## pregnant 0.1231823 0.0320776 3.840 0.000123 ***
## glucose 0.0351637 0.0037087 9.481 < 2e-16 ***
## pressure -0.0132955 0.0052336 -2.540 0.011072 *
## triceps 0.0006190 0.0068994 0.090 0.928515
## insulin -0.0011917 0.0009012 -1.322 0.186065
## mass 0.0897010 0.0150876 5.945 2.76e-09 ***
## pedigree 0.9451797 0.2991475 3.160 0.001580 **
## age 0.0148690 0.0093348 1.593 0.111192
## ---
## Signif. codes: 0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
##
## (Dispersion parameter for binomial family taken to be 1)
##
## Null deviance: 993.48 on 767 degrees of freedom
## Residual deviance: 723.45 on 759 degrees of freedom
## AIC: 741.45
##
## Number of Fisher Scoring iterations: 5
# Cài đặt thư viện cần thiết
#install.packages("pROC")
library(pROC)
## Warning: package 'pROC' was built under R version 4.4.3
## Type 'citation("pROC")' for a citation.
##
## Attaching package: 'pROC'
## The following object is masked from 'package:Metrics':
##
## auc
## The following objects are masked from 'package:stats':
##
## cov, smooth, var
# Dự đoán xác suất
pred_probs <- predict(model_logit, type = "response")
# Tính ROC-AUC
roc_obj <- roc(PimaIndiansDiabetes$diabetes, pred_probs)
## Setting levels: control = neg, case = pos
## Setting direction: controls < cases
auc_value <- auc(roc_obj)
print(paste("ROC-AUC:", auc_value))
## [1] "ROC-AUC: 0.839425373134328"
# Vẽ đường cong ROC
plot(roc_obj, col = "blue", main = "ROC Curve")