library(openxlsx)
library(lmtest) # Dùng cho kiểm định Durbin-Watson (dwtest)
library(car) # Dùng cho kiểm tra đa cộng tuyến (vif)
clean_data <- openxlsx::read.xlsx("04_Ket_qua.xlsx")
# Đảm bảo các biến phân loại/logic ở đúng định dạng factor
clean_data$nearest_warehouse <- as.factor(clean_data$nearest_warehouse)
clean_data$season <- as.factor(clean_data$season)
clean_data$is_expedited_delivery <- as.factor(clean_data$is_expedited_delivery)
clean_data$is_happy_customer <- as.factor(clean_data$is_happy_customer)
options(scipen = 999)
model_full <- lm(calculated_total ~ coupon_discount + delivery_charges +
distance_to_nearest_warehouse + nearest_warehouse + season +
is_expedited_delivery + is_happy_customer,
data = clean_data)
summary(model_full)
##
## Call:
## lm(formula = calculated_total ~ coupon_discount + delivery_charges +
## distance_to_nearest_warehouse + nearest_warehouse + season +
## is_expedited_delivery + is_happy_customer, data = clean_data)
##
## Residuals:
## Min 1Q Median 3Q Max
## -47378 -20070 -9731 -212 800195
##
## Coefficients:
## Estimate Std. Error t value Pr(>|t|)
## (Intercept) 42099.54 42779.31 0.984 0.3256
## coupon_discount 115.50 410.45 0.281 0.7785
## delivery_charges -595.74 599.07 -0.994 0.3205
## distance_to_nearest_warehouse -99.03 393.35 -0.252 0.8013
## nearest_warehouse2 -6941.89 9318.75 -0.745 0.4567
## nearest_warehouse3 8355.71 9152.12 0.913 0.3617
## season2 16348.72 10737.48 1.523 0.1285
## season3 -9496.73 14292.64 -0.664 0.5067
## season4 -3786.65 15498.09 -0.244 0.8071
## is_expedited_delivery1 18175.02 13093.92 1.388 0.1658
## is_happy_customer1 20830.37 11507.22 1.810 0.0709 .
## ---
## Signif. codes: 0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
##
## Residual standard error: 76650 on 471 degrees of freedom
## Multiple R-squared: 0.02611, Adjusted R-squared: 0.005435
## F-statistic: 1.263 on 10 and 471 DF, p-value: 0.2489
(GVIF < 5 là không có đa cộng tuyến)
vif(model_full)
## GVIF Df GVIF^(1/(2*Df))
## coupon_discount 1.020100 1 1.010000
## delivery_charges 6.231331 1 2.496264
## distance_to_nearest_warehouse 1.019877 1 1.009889
## nearest_warehouse 1.037935 2 1.009352
## season 2.999992 3 1.200936
## is_expedited_delivery 3.516321 1 1.875186
## is_happy_customer 2.200282 1 1.483335
Chỉ giữ lại biến is_happy_customer (p-value = 0.0709
< 0.1).
model_opt <- lm(calculated_total ~ is_happy_customer,
data = clean_data)
summary(model_opt)
##
## Call:
## lm(formula = calculated_total ~ is_happy_customer, data = clean_data)
##
## Residuals:
## Min 1Q Median 3Q Max
## -25153 -18030 -10869 -4296 827094
##
## Coefficients:
## Estimate Std. Error t value Pr(>|t|)
## (Intercept) 14840 6584 2.254 0.0246 *
## is_happy_customer1 11079 7770 1.426 0.1546
## ---
## Signif. codes: 0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
##
## Residual standard error: 76780 on 480 degrees of freedom
## Multiple R-squared: 0.004217, Adjusted R-squared: 0.002142
## F-statistic: 2.033 on 1 and 480 DF, p-value: 0.1546
par(mfrow = c(2, 2))
plot(model_opt)
par(mfrow = c(1, 1))
dwtest(model_opt)
##
## Durbin-Watson test
##
## data: model_opt
## DW = 2.0493, p-value = 0.7066
## alternative hypothesis: true autocorrelation is greater than 0