Phân tích dự báo trả lời câu hỏi: “Điều gì có thể xảy
ra?”
Bằng cách sử dụng mô hình thống kê và học máy để dự đoán kết quả
tương lai dựa trên dữ liệu quá khứ.
| Mô hình | Mục tiêu | Ứng dụng |
|---|---|---|
| 1️⃣ Hồi quy tuyến tính | Dự báo biến liên tục | Dự đoán lợi nhuận theo Sales, Discount |
| 2️⃣ Hồi quy logistic | Dự đoán biến nhị phân (0/1) | Dự đoán đơn hàng có lãi không |
| 3️⃣ Chuỗi thời gian (ARIMA) | Dự báo theo thời gian | Dự báo doanh số tháng sau |
| 4️⃣ Cây quyết định (Decision Tree) | Phân loại & dự báo | Phân nhóm khách hàng mua nhiều |
| 5 K-means | Dự báo và khuyến nghị |
library(readxl)
library(dplyr)
url <- "http://giangtranvn.com/wp-content/uploads/2024/08/3.-SUPERSTORE.xlsx"
superstore <- read_excel(url)
model_lm <- lm(Profit ~ Sales + Discount, data = superstore)
summary(model_lm)
**Estimate → hệ số tác động của Sales, Discount đến Profit
p-value < 0.05 → biến có ảnh hưởng đáng kể
R-squared → phần trăm giải thích được sự biến động của Profit**
library(ggplot2)
ggplot(superstore, aes(x = Sales, y = Profit)) +
geom_point(alpha = 0.3) +
geom_smooth(method = "lm", col = "red")
superstore <- superstore %>%
mutate(HasProfit = ifelse(Profit > 0, 1, 0))
model_logit <- glm(HasProfit ~ Discount + Sales, data = superstore, family = "binomial")
summary(model_logit)
superstore$predicted_prob <- predict(model_logit, type = "response")
superstore$predicted_class <- ifelse(superstore$predicted_prob > 0.5, 1, 0)
# Confusion matrix
table(ThựcTế = superstore$HasProfit, DựĐoán = superstore$predicted_class)
library(lubridate)
sales_monthly <- superstore %>%
mutate(OrderDate = as.Date(`Order Date`)) %>%
mutate(Month = floor_date(OrderDate, "month")) %>%
group_by(Month) %>%
summarise(Sales = sum(Sales, na.rm = TRUE))
library(forecast)
sales_ts <- ts(sales_monthly$Sales, start = c(2014,1), frequency = 12)
model_arima <- auto.arima(sales_ts)
forecasted <- forecast(model_arima, h = 6)
plot(forecasted)
library(rpart)
library(rpart.plot)
model_tree <- rpart(HasProfit ~ Sales + Discount + Category + Region,
data = superstore, method = "class")
rpart.plot(model_tree, type = 2, extra = 104)
|Mô hình | Hàm R sử dụng | Mục đích
|Hồi quy tuyến tính | lm() |Dự đoán biến liên tục
|Hồi quy logistic | glm(family = “binomial”) |Dự đoán biến nhị phân
|ARIMA | auto.arima(), forecast() | Dự báo theo thời gian
|Cây quyết định | rpart() | Phân loại trực quan
|K-means|