Tái tạo độ biến thiên của dữ liệu

Xử lý dữ liệu khuyết bằng Imputation trong R

Michal Oleszak

Machine Learning Engineer

Độ biến thiên trong dữ liệu đã bù

Biểu đồ lề: biểu đồ scatter "Height" so với "Weight", các giá trị đã bù ở một trong hai biến được tô màu khác.

  • Không có độ biến thiên trong dữ liệu đã bù.
  • Ta muốn phép bù phản ánh độ biến thiên của dữ liệu quan sát.
  • Với bù dựa trên mô hình, cùng giá trị biến dự báo cho cùng giá trị bù.
  • Giải pháp: lấy mẫu từ phân phối có điều kiện.
Xử lý dữ liệu khuyết bằng Imputation trong R

Dự đoán là gì

Hầu hết mô hình thống kê ước lượng phân phối có điều kiện của biến phản hồi:

$p(y|X)$

Để tạo một dự đoán đơn, ta tóm tắt phân phối này:

  • Hồi quy tuyến tính: kỳ vọng của phân phối có điều kiện.
  • Hồi quy logistic: lớp có xác suất cao nhất.

Thay vào đó, có thể lấy mẫu từ các phân phối này để tăng độ biến thiên.

Xử lý dữ liệu khuyết bằng Imputation trong R

Lấy mẫu từ phân phối có điều kiện

Biểu đồ mật độ của phân phối chuẩn. Giá trị trung bình 25 được đánh dấu.

Xử lý dữ liệu khuyết bằng Imputation trong R

Lấy mẫu từ phân phối có điều kiện

Bảng với bốn cột: xác suất dự đoán từ hồi quy logistic (0.7 cho mọi hàng), boolean cho việc xác suất > 0.5 (TRUE cho mọi hàng), giá trị bù theo ngưỡng (1 cho mọi hàng) và giá trị bù lấy mẫu từ phân phối có điều kiện (đa số là 1, một số là 0).

Xử lý dữ liệu khuyết bằng Imputation trong R

Bù bằng hồi quy logistic

Nhiệm vụ: bù PhysActive từ dữ liệu nhanes bằng hồi quy logistic.

nhanes_imp <- hotdeck(nhanes)
missing_physactive <- is.na(nhanes$PhysActive)
Xử lý dữ liệu khuyết bằng Imputation trong R

Bù bằng hồi quy logistic

Nhiệm vụ: bù PhysActive từ dữ liệu nhanes bằng hồi quy logistic.

nhanes_imp <- hotdeck(nhanes)
missing_physactive <- is.na(nhanes$PhysActive)
logreg_model <- glm(PhysActive ~ Age + Weight + Pulse, 
                    data = nhanes_imp, family = binomial)
Xử lý dữ liệu khuyết bằng Imputation trong R

Bù bằng hồi quy logistic

Nhiệm vụ: bù PhysActive từ dữ liệu nhanes bằng hồi quy logistic.

nhanes_imp <- hotdeck(nhanes)
missing_physactive <- is.na(nhanes$PhysActive)
logreg_model <- glm(PhysActive ~ Age + Weight + Pulse, 
                    data = nhanes_imp, family = binomial)
preds <- predict(logreg_model, type = "response")
Xử lý dữ liệu khuyết bằng Imputation trong R

Bù bằng hồi quy logistic

Nhiệm vụ: bù PhysActive từ dữ liệu nhanes bằng hồi quy logistic.

nhanes_imp <- hotdeck(nhanes)
missing_physactive <- is.na(nhanes$PhysActive)
logreg_model <- glm(PhysActive ~ Age + Weight + Pulse, 
                    data = nhanes_imp, family = binomial)
preds <- predict(logreg_model, type = "response")
preds <- ifelse(preds >= 0.5, 1, 0)
Xử lý dữ liệu khuyết bằng Imputation trong R

Bù bằng hồi quy logistic

Nhiệm vụ: bù PhysActive từ dữ liệu nhanes bằng hồi quy logistic.

nhanes_imp <- hotdeck(nhanes)
missing_physactive <- is.na(nhanes$PhysActive)
logreg_model <- glm(PhysActive ~ Age + Weight + Pulse, 
                    data = nhanes_imp, family = binomial)
preds <- predict(logreg_model, type = "response")
preds <- ifelse(preds >= 0.5, 1, 0)
nhanes_imp[missing_physactive, "PhysActive"] <- preds[missing_physactive]
Xử lý dữ liệu khuyết bằng Imputation trong R

Bù bằng hồi quy logistic

Độ biến thiên của dữ liệu đã bù:

table(preds[missing_physactive])
 1 
26

Độ biến thiên của dữ liệu PhysActive quan sát:

table(nhanes$PhysActive)
  0   1 
181 610
Xử lý dữ liệu khuyết bằng Imputation trong R

Lấy mẫu từ xác suất lớp

nhanes_imp <- hotdeck(nhanes)
missing_physactive <- is.na(nhanes$PhysActive)
logreg_model <- glm(PhysActive ~ Age + Weight + Pulse, 
                    data = nhanes_imp, family = binomial)
preds <- predict(logreg_model, type = "response")
preds <- ifelse(preds >= 0.5, 1, 0)
nhanes_imp[missing_physactive, "PhysActive"] <- preds[missing_physactive]
Xử lý dữ liệu khuyết bằng Imputation trong R

Lấy mẫu từ xác suất lớp

nhanes_imp <- hotdeck(nhanes)
missing_physactive <- is.na(nhanes$PhysActive)
logreg_model <- glm(PhysActive ~ Age + Weight + Pulse, 
                    data = nhanes_imp, family = binomial)
preds <- predict(logreg_model, type = "response")

nhanes_imp[missing_physactive, "PhysActive"] <- preds[missing_physactive]
Xử lý dữ liệu khuyết bằng Imputation trong R

Lấy mẫu từ xác suất lớp

nhanes_imp <- hotdeck(nhanes)
missing_physactive <- is.na(nhanes$PhysActive)
logreg_model <- glm(PhysActive ~ Age + Weight + Pulse, 
                    data = nhanes_imp, family = binomial)
preds <- predict(logreg_model, type = "response")
preds <- rbinom(length(preds), size = 1, prob = preds)
nhanes_imp[missing_physactive, "PhysActive"] <- preds[missing_physactive]
Xử lý dữ liệu khuyết bằng Imputation trong R

Lấy mẫu từ xác suất lớp

Độ biến thiên của dữ liệu đã bù:

table(preds[missing_physactive])
0  1 
5 21

Độ biến thiên của dữ liệu PhysActive quan sát:

table(nhanes$PhysActive)
  0   1 
181 610
Xử lý dữ liệu khuyết bằng Imputation trong R

Vamos praticar!

Xử lý dữ liệu khuyết bằng Imputation trong R

Preparing Video For Download...