BÀI 2: THỰC HÀNH CÁC MÔ HÌNH THỐNG KÊ TRONG R
#Các gói đã cài đặt không cần cài lại, chạy các thư viện cần thiết cho dự án
library(tidyverse)
library(tidyr)
library(dplyr)
library(ggplot2)
library(readxl)
credita_data <- read_excel("creditdata.xlsx", sheet = 1)
View(credita_data)
credita_data <- na.omit(credita_data)
names(credita_data) <- gsub(" ", "_", names(credita_data))
ggplot(data = credita_data, aes (x=Credit_Score)) +
geom_histogram()
credita_data_nho <- credita_data %>%
filter (Credit_Score < 1000)
View(credita_data_nho)
credita_data_lon <- credita_data %>%
filter (Credit_Score > 1000)
View(credita_data_lon)
credita_data <- credita_data %>%
filter (Credit_Score < 1000)
View(credita_data)
summary(credita_data)
Loai_sh_nha <- credita_data %>%
group_by(Home_Ownership) %>%
summarise(So_luong = n(), Thu_nhap_TB = mean(Annual_Income))
View (Loai_sh_nha)
ggplot(credita_data, aes(x = Home_Ownership, fill = Term)) +
geom_bar() + labs(title = "Số lượng KH theo loại hình sở hữu nhà", x = "Sở hữu nhà", y = "Số lượng")
ggplot(credita_data, aes(x = Annual_Income)) +
geom_histogram()
ggplot(credita_data, aes(x = Credit_Score)) +
geom_histogram( color = "cyan", fill = "blue")
ggplot(credita_data, aes(x = Credit_Score)) +
geom_histogram(colors= 'white', fill = "orange")
labs(title = "Biểu đồ phân bổ điểm tín dụng",
x = "Điểm tín dụng",
y = "Số lượng khách hàng")
ggplot(credita_data, aes(x = Credit_Score, y = Annual_Income)) +
geom_point()
ggplot(credita_data, aes(x = Credit_Score, y = Annual_Income)) +
geom_point(color = "darkblue")
ggplot(credita_data, aes(x = Credit_Score, y = Home_Ownership))+
geom_boxplot()
ggplot(credita_data, aes(x = Home_Ownership, y = Credit_Score))+
geom_boxplot(color = "red", fill = "lightgreen")
labs(title = "Phân bổ giữa các loại hình sở hữu")
ggplot(credit_data, aes(x = Years_in_current_job))+
geom_bar(fill = "darkblue")
—–PHẦN 2——–
ggplot(credita_data, aes(x = Annual_Income, y = Monthly_Debt))+
geom_point(color = "blue")+
geom_smooth(method = "lm", color = "red")
mohinh2 <- lm(Monthly_Debt ~ Annual_Income, credita_data)
library(readr)
dulieutest <- read.csv("credit_test.csv")
dulieutest <- dulieutest %>%
na.omit()
summary(dulieutest)
View(dulieutest)
names(dulieutest) <- gsub("\\.", "_", names(dulieutest))
mohinh2 <- lm(Monthly_Debt ~ Annual_Income, credita_data)
Dubaochitrahangthang_theothunhapnam <- dulieutest %>%
`mutate(Dubaogiatri_Tha = predict(mohinh2, newdata = dulieutest))
View(Dubaochitrahangthang_theothunhapnam)
—PHẦN 3 MÔ HÌNH HỒI QUY LOGIC———-
credita_data$trang_thai <- credita_data$Annual_Income %%2
credita_data$trang_thai <- factor(credita_data$trang_thai, level = c(0,1), labels = c ("Không vỡ nợ", "Có"))
tk_term <- credita_data %>%
group_by(Term) %>%
summarise(so_luong = n())
tk_term
Mohinhlogic <- glm(trang_thai ~ Credit_Score, family = "binomial", data = credita_data)
summary(Mohinhlogic)
Dubaotheologic <- predict(Mohinhlogic, newdata = dulieutest, type = "response")
summary(Dubaotheologic)
View(Dubaotheologic)
Mohinhlogicdabien <- glm(trang_thai ~ Credit_Score + Annual_Income + Current_Loan_Amount, family = "binomial", data = credita_data)
summary(Mohinhlogicdabien)
Dubaotheologicdabiendabien <- predict(Mohinhlogicdabien, newdata = dulieutest, type = "response")
summary(Dubaotheologicdabiendabien)
View(Dubaotheologicdabiendabien)
—PHẦN 4 MÔ HÌNH ARIMA———-
sieuthi <- read_excel("3.SUPERSTORE.xlsx", sheet = 1)
View(sieuthi)
baocaoTG <- sieuthi %>%
group_by( Thang = floor_date(`Order Date`, "month")) %>%
summarise(Sales = sum(Sales))
View(baocaoTG)
Doanhso_thang <- ts(baocaoTG$Sales, start = c(2018,1), frequency = 12)
Doanhso_thang
plot(Doanhso_thang)
install.packages("forecast")
library(forecast)
mohinh_thoigian <- auto.arima(Doanhso_thang)
summary(mohinh_thoigian)
Dubaodoanhso_6thangtoi <- forecast(mohinh_thoigian, h = 6)
plot(Dubaodoanhso_6thangtoi)
—–PHẦN 5 MÔ HÌNH K-MEANS———–
View(sieuthi)
Tkgiaodich <- sieuthi %>%
group_by(`Customer ID`) %>%
summarise(
so_don = n_distinct(`Order ID`),
DS = sum(Sales),
LN = sum(Profit)
)
View(Tkgiaodich)
Tkgiaodich_biendoi <- scale(Tkgiaodich[-1])
View(Tkgiaodich_biendoi)
mohinhKmean <- kmeans(Tkgiaodich_biendoi, centers = 3)
install.packages("factoextra")
library(factoextra)
fviz_cluster (mohinhKmean, data = Tkgiaodich_biendoi)
View(mohinhKmean)