data() # 1. Chay cac thu vien can thiet library(tidyverse) library(tidyr) library(dplyr) library(ggplot2) library(readr) library(readxl)
## 2.1. Lay du lieu tu mot file excel
credit_data <- read_excel("creditdata.xlsx", sheet = 1)
## 2.2. Quan sat du lieu
View(credit_data)
## 2.3. Danh gia tong quan du lieu
summary(credit_data)
## 2.4. Lam sach du lieu:
## 2.4.1. loai bo cac dong co gia tri NA xuat hien
credit_data <- na.omit(credit_data)
## 2.4.2. Doi ten cho cac cot trong bang voi thay the cac gia tri " " = "_"
r names(credit_data) <-gsub(" ","_", names(credit_data))
## 3.1. Loc thong tin ve diem tin dung
creditdata_min <- credit_data%>%
filter(credit_data$Credit_Score < 1000)
view(creditdata_min)
creditdata_max <- credit_data%>%
filter(credit_data$Credit_Score >= 1000)
credit_data <- credit_data%>%
filter(credit_data$Credit_Score < 1000)
## 4.1. So sanh
##bieu do cot
ggplot(credit_data, aes(x = Home_Ownership)) +
geom_bar()
## 4.2. Su phan bo
ggplot(credit_data, aes(x = Credit_Score)) +
geom_histogram()
#Mau
ggplot(credit_data, aes(x = Credit_Score)) +
geom_histogram(color= 'blue', fill = "orange")
#Tieu de
ggplot(credit_data, aes(x = Credit_Score)) +
geom_histogram(colors= 'blue', 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")
## 4.3. So sanh tuong quan giua Thu nhap nam va diem tin dung
ggplot(credit_data, aes(x = Credit_Score, y = Annual_Income)) +
geom_point()
#Mau
ggplot(credit_data, aes(x = Credit_Score, y = Annual_Income)) +
geom_point(color = "darkblue")
## 4.4. Xac dinh cac yeu to ngoai lai dua vao cac bieu do hop
## Diem tin dung theo loai hinh so huu: de ra diem bat thuong
ggplot(credit_data, aes(x = Credit_Score, y = Home_Ownership))+
geom_boxplot()
##check lai tieu de
ggplot(credit_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")
## 4.5. Thong ke theo nhom
credit_data <- credit_data%>%
group_by(credit_data$Years_in_current_job)%>%
summarise(soluong = n(),
thunhap_namBQ = mean(Annual_Income))
credit_data
;
#------------------------------------------------------
## So sanh so luong khach hang theo so nam di lam
ggplot(credit_data, aes(x = Years_in_current_job))+
geom_bar(fill = "darkblue")
## 2.1. Mo hinh hoi quy don bien
## 2.2. Danh gia moi quan he tuong quan giua A va B (thu nhap nam - thanh toan tin dung theo thang)
ggplot(credit_data, aes(x = Annual_Income, y = Monthly_Debt))+
geom_point(color = "blue")+
geom_smooth(method = "lm", color = "red")
##-> Co ve nhu co moi quan he tuong quan, nen dua vao de phan tich
## Xet cac dieu kien xem co phu hop voi phan tich tuong quan khong: Cac bien phai dam bao phan phoi chuan
## Cach 1: Nhin xem phan phoi cua cac bien dua theo bieu do phan bo (histogram)
ggplot(credit_data, aes(x = Annual_Income))+
geom_histogram()
ggplot(credit_data, aes(x = Monthly_Debt))+
geom_histogram()
## Cach 2: Danh gia phan phoi chuan theo mot so cach mo rong
## Danh gia phan phoi chuan cua THU NHAP NAM
qqnorm(credit_data$Annual_Income)
qqline(credit_data$Annual_Income, col = "red")
qqnorm(credit_data$Monthly_Debt)
qqline(credit_data$Monthly_Debt, col = "red")
##-> Neu cac mau khong phan phoi chuan thi cac noi dung phia sau khi xay dung mo hinh se dua ra ket qua chưa chinh xac
## 2.3.1. Xay dung mo hinh hoi quy tuyen tinh don bien
#mo hinh 1
mohinh1 <- lm(credit_data$Monthly_Debt~credit_data$Annual_Income)
mohinh1
summary(mohinh1)
View(mohinh1)
#mo hinh 2
mohinh2 <- lm(Monthly_Debt~Annual_Income, credit_data)
# 2.4. Ap dung mo hinh cho mau moi
#B1
credit_test <- read_csv("credit_test.csv")
#B2
credit_test <- credit_test%>%
na.omit(credit_test)
#B3
names(credit_test) <- gsub(" ","_", names(credit_test))
##OR: Truong hop ten truong co dau .
names(credit_test) <- gsub("\\.","_", names(credit_test))
View(credit_test)
# 2.5. Chay du bao ket qua tư mo hinh
Dubaochitrahangthang_theothunhapnam <- credit_test%>%
mutate(Dubaogiatri_thang = predict(mohinh2, newdata = credit_test)) #them truong - ham du doan
View(Dubaochitrahangthang_theothunhapnam)
;
## 3.1. Truong hop khong co can tim bo sung. Trong TH minh hoa can su dung them 1 cot trang thai la phần dư phép chia hết cho 2
credit_data$Trangthai <- credit_data$Annual_Income%%2
;
credit_data$Trangthai<-factor(credit_data$Trangthai, levels = c(0,1), labels = c("Không vỡ nợ","Có vỡ nợ"))
;
## Thong ke theo nhom term
;
ggplot(credit_data, aes (x=credit_data$Term))+
geom_bar
;
## 3.2. Xay dung mo hinh logic
mohinhlogic = glm(Trangthai~Credit_Score,family = "binomial",data = credit_data)
summary(mohinhlogic)
## 3.3. Chay mo hinh du bao voi bo du lieu moi
dubaotheologic <- predict(mohinhlogic, newdata = credit_test, type = "response")
View(dubaotheologic)
## 3.4. mô hình hồi quy đa biến
mh_logicdabien <- glm(Trangthai ~ Credit_Score + Annual_Income + Current_Credit_Balance,
family = "binomial", data = creditdata)
summary(mh_logicdabien)
dubaodabien_moi <- predict ( mh_logicdabien, newsdata = dulieutest, type = "response")
dubaodabien_moi
#Dữ liệu thực hành là Superstore
sieuthi <- read_excel("3.SUPERSTORE.xlsx", sheet=1)
names(sieuthi) <- gsub(" ","_", names(sieuthi))
View(sieuthi)
#Thống kê tổng doanh số theo tháng năm
baocaoTG <- sieuthi%>%
group_by( Thang = floor_date(Order_Date, "month")) %>%
summarise(Sales = sum(Sales))
baocaoTG
#Thiết lập tạo chuỗi thời gian tuần tự theo tháng cho các giá trị doanh số
Doanhso_thang <- ts(baocaoTG$Sales, start = c(2018,1), frequency = 12)
Doanhso_thang
plot(Doanhso_thang)
## Cài gói dự báo forecast
install.packages("forecast")
library(forecast)
## Tạo mô hình armia
mohinh_thoigian <- auto.arima(Doanhso_thang)
summary(mohinh_thoigian)
##5.3. Dự báo dựa theo mô hình
Dubaodoanhso_6thangtoi <-forecast(mohinh_thoigian, h =6)
plot(Dubaodoanhso_6thangtoi)
## Thống kê thông tin về giao dịch theo từng KH
Tkgiaodich<-sieuthi%>%
group_by(sieuthi$Customer_ID)%>%
summarise(
sodon=n_distinct(Order_ID),
DS=sum(Sales),
LN=sum(Profit))
Tkgiaodich
# 6.2. Biến đổi các giá trị
TKgiaodich_biendoi <- scale(Tkgiaodich[-1])
TKgiaodich_biendoi
## 6.3. Xây dựng mô hình Kmean sau khi làm mịn
mohinhKmeans <- kmeans(TKgiaodich_biendoi, centers = 3)
install.packages("factoextra")
install.packages("ggsignif")
install.packages("car")
library(factoextra)
fviz_cluster(mohinhKmeans, data = TKgiaodich_biendoi)