Bài toán: xây dựng mô hình hồi quy logistic để dự báo bảo tồn vú qua 3 biến: Tuổi, Kích thước u và Độ mô học.

Nhâp dữ liệu

df <- read.csv('https://math.ued.udn.vn/wp-content/uploads/2024/10/So_lieu.csv')
Tuoi<-df$Tuoi
Kich_thuoc_u<-df$Kich_thuoc_u
Do_mo_hoc<-df$Do_mo_hoc
Phuong_phap_phau_thuat_tai_vu<-df$Phuong_phap_phau_thuat_tai_vu

df2<-data.frame(Tuoi,Kich_thuoc_u,Do_mo_hoc, Phuong_phap_phau_thuat_tai_vu)

df2$Do_mo_hoc<-replace(df2$Do_mo_hoc,df2$Do_mo_hoc=="Grade 1",1)
df2$Do_mo_hoc<-replace(df2$Do_mo_hoc,df2$Do_mo_hoc=="Grade 2",2)
df2$Do_mo_hoc<-replace(df2$Do_mo_hoc,df2$Do_mo_hoc=="Grade 3",3)
df2$Do_mo_hoc<-replace(df2$Do_mo_hoc,df2$Do_mo_hoc=="không xác định",0)
df2$Do_mo_hoc<-as.numeric(df2$Do_mo_hoc)

df2$Phuong_phap_phau_thuat_tai_vu<-replace(df2$Phuong_phap_phau_thuat_tai_vu,df2$Phuong_phap_phau_thuat_tai_vu=="bảo tồn",1)
df2$Phuong_phap_phau_thuat_tai_vu<-replace(df2$Phuong_phap_phau_thuat_tai_vu,df2$Phuong_phap_phau_thuat_tai_vu=="Cắt vú triệt căn",0)
df2$Phuong_phap_phau_thuat_tai_vu<-replace(df2$Phuong_phap_phau_thuat_tai_vu,df2$Phuong_phap_phau_thuat_tai_vu=="Cắt vú + tái tạo",0)
df2$Phuong_phap_phau_thuat_tai_vu<-as.numeric(df2$Phuong_phap_phau_thuat_tai_vu)

Chia dữ liệu thành 2 phần train và test

sample <- sample(c(TRUE, FALSE), nrow(df2), replace=TRUE, prob=c(0.8,0.2))
train <- df2[sample, ]
test <- df2[!sample, ] 

Chạy mô hình

model <- glm(Phuong_phap_phau_thuat_tai_vu~Tuoi+Kich_thuoc_u+Do_mo_hoc, family="binomial", data=train)
summary(model)
## 
## Call:
## glm(formula = Phuong_phap_phau_thuat_tai_vu ~ Tuoi + Kich_thuoc_u + 
##     Do_mo_hoc, family = "binomial", data = train)
## 
## Deviance Residuals: 
##     Min       1Q   Median       3Q      Max  
## -1.7900  -0.8611  -0.5723   1.0599   2.6944  
## 
## Coefficients:
##               Estimate Std. Error z value Pr(>|z|)    
## (Intercept)   2.622814   0.580063   4.522 6.14e-06 ***
## Tuoi         -0.046785   0.008845  -5.289 1.23e-07 ***
## Kich_thuoc_u -0.597833   0.080478  -7.428 1.10e-13 ***
## Do_mo_hoc     0.401208   0.146760   2.734  0.00626 ** 
## ---
## Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
## 
## (Dispersion parameter for binomial family taken to be 1)
## 
##     Null deviance: 864.06  on 702  degrees of freedom
## Residual deviance: 758.77  on 699  degrees of freedom
## AIC: 766.77
## 
## Number of Fisher Scoring iterations: 5

Đánh giá độ chính xác của mô hình

fitted.results <- predict(model,newdata=test,type='response')
fitted.results <- ifelse(fitted.results > 0.5,1,0)
misClasificError <- mean(fitted.results != test$Phuong_phap_phau_thuat_tai_vu)
print(paste('Accuracy',1-misClasificError))
## [1] "Accuracy 0.771084337349398"

Kiểm tra dụ báo 10 giá trị đầu tiên lấy trong dữ liệu “test”

test[1:10,]
##    Tuoi Kich_thuoc_u Do_mo_hoc Phuong_phap_phau_thuat_tai_vu
## 2    54          2.3         2                             1
## 8    65          5.0         2                             0
## 9    65          5.0         2                             0
## 12   54          2.2         2                             0
## 18   47          2.5         2                             1
## 26   56          3.0         2                             0
## 39   43          3.5         2                             0
## 46   53          3.0         2                             0
## 61   57          2.8         2                             0
## 66   52          3.5         2                             1
predict(model,newdata=test[1:10,],type='response')
##          2          8          9         12         18         26         39 
## 0.38315329 0.06881921 0.06881921 0.39737801 0.43333540 0.27126013 0.33649626 
##         46         61         66 
## 0.29987713 0.28588441 0.24973724
predict
## function (object, ...) 
## UseMethod("predict")
## <bytecode: 0x0000021a0c3a52a0>
## <environment: namespace:stats>