#Câu 11
# Load dataset airquality có sẵn trong R
data("airquality")

# Kiểm tra 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
# Xử lý dữ liệu: loại bỏ hàng có giá trị thiếu (NA)
airquality <- na.omit(airquality)

# Hồi quy tuyến tính dự đoán Ozone theo các biến còn lại
model <- lm(Ozone ~ Solar.R + Wind + Temp + Month + Day, data = airquality)

# Hiển thị kết quả hồi quy
summary(model)
## 
## Call:
## lm(formula = Ozone ~ Solar.R + Wind + Temp + Month + Day, 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
adj_r_squared <- summary(model)$adj.r.squared
print(paste("Hệ số R bình phương hiệu chỉnh:", adj_r_squared))
## [1] "Hệ số R bình phương hiệu chỉnh: 0.607080837365279"
#Câu 2
# Cài đặt gói nếu chưa có
#install.packages("mlbench")

# Load gói
library(mlbench)
## Warning: package 'mlbench' was built under R version 4.4.3
# Load dataset
data("PimaIndiansDiabetes")

# Kiểm tra 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
# Hồi quy logistic với biến phụ thuộc là diabetes
logistic_model <- glm(diabetes ~ ., data = PimaIndiansDiabetes, family = binomial)

# Hiển thị kết quả
summary(logistic_model)
## 
## 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
# Chia dữ liệu thành tập train và test (70%-30%)
set.seed(123)  # Để kết quả tái lập
train_idx <- sample(1:nrow(PimaIndiansDiabetes), 0.7 * nrow(PimaIndiansDiabetes))
train_data <- PimaIndiansDiabetes[train_idx, ]
test_data <- PimaIndiansDiabetes[-train_idx, ]

# Xây dựng lại mô hình trên tập train
logistic_model <- glm(diabetes ~ ., data = train_data, family = binomial)

# Dự đoán trên tập test
pred_prob <- predict(logistic_model, newdata = test_data, type = "response")

# Chuyển đổi thành nhãn 0/1 với ngưỡng 0.5
pred_label <- ifelse(pred_prob > 0.5, "pos", "neg")

# Kiểm tra ma trận nhầm lẫn
table(Predicted = pred_label, Actual = test_data$diabetes)
##          Actual
## Predicted neg pos
##       neg 138  36
##       pos  12  45