Thuc hanh voi cac mo hinh thong ke

Ham co san

data() # 1. Chay cac thu vien can thiet

library(tidyverse)
library(tidyr)
library(dplyr)
library(ggplot2)
library(readr)
library(readxl)

2. Thao tac voi du lieu

## 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 " " = "_"
names(credit_data) <-gsub(" ","_", names(credit_data))

3. Khai thac du lieu

## 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. Thong ke

## 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")

PHAN 2: PHAN TICH VA CHUAN DOAN DUA THEO CAC MO HINH

# MO HINH HOI QUY TUYEN TINH
## 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. MO HINH HOI QUY LOGIC

## Luu y la trong du lieu phai co cot o dinh dang 0,1
## 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. Mo hinh hoi quy logic da bien
mohinhlogicdabien <- glm(Trangthai~Credit_Score + Annual_Income + Current_Credit_Balance,family = "binomial",data = credit_data)

summary(mohinhlogicdabien)

dubaodabiendlmoi <- predict(mohinhlogicdabien, newdata = credit_data, type = "response")

View(dubaodabiendlmoi)
;
dubaodabiendlmoi

4. MO HINH ARIMA

sieuthi <- read_xlsx("3.SUPERSTORE.xlsx", sheet = 1)

View(sieuthi)

## 2.4.2. Doi ten cho cac cot trong bang voi thay the cac gia tri " " = "_"
names(sieuthi) <-gsub(" ","_", names(sieuthi))

# Thong ke tong doanh so theo Thang nam

baocaoTG <- sieuthi%>%
  group_by(Thang = floor_date(Order_Date,"month"))%>%
  summarise(Sales = sum(Sales))

baocaoTG

# Thiet lap tao chuoi thoi gian tuan tu theo thang cho cac gia tri doanh so 
Doanhso_Thang <- ts(baocaoTG$Sales, start = c(2018,1), frequency = 12)


View(Doanhso_Thang)

plot(Doanhso_Thang)

# Xay dung mo hinh du bao theo thoi gian
# Cai goi du bao forecast

install.packages("forecast")
library(forecast)

# Tao mo hinh Arima

mohinh_thoigian <- auto.arima(Doanhso_Thang)  #tu dong thong ke chuoi thoi gian 

summary(mohinh_thoigian)

# 5.3. Du bao dua theo mo hinh
dubaodso_6thangtoi <- forecast(mohinh_thoigian, h = 6)

plot(dubaodso_6thangtoi)

5. Kmeans

#View(sieuthi)

# 5.1. Thong ke thong tin

tkgiaodich <- sieuthi%>%
  group_by (sieuthi$Customer_ID)%>%
  summarise(
    sodon = n_distinct(Order_ID),
    doanhso = sum(Sales),
    loainhuan = sum(Profit)
  )

tkgiaodich

# 5.2. Bien doi cac gia tri can phan cum ve gia tri nhi phan 0,1

tkgiaodich_biendoi <- scale(tkgiaodich[-1])
;
tkgiaodich_biendoi

# 5.3. Xay dung mo hinh Kmeans sau khi lam min

mohinhkmeans <- kmeans(tkgiaodich_biendoi, center = 6)   #chia lam 6 nhom

mohinhkmeans

install.packages("factoextra")
library(factoextra)

fviz_cluster(mohinhkmeans, data = tkgiaodich_biendoi)