THỰC HÀNH

#0.chạy các thư viện cần thiết cho dự án

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

Thao tác với dữ liệu

2.1 lấy dữ liệu từ file excel

creditdata <- read_excel(“creditdata.xlxs”, sheet = 1)

##2.1.1Quan sát dữ liệu

View(creditdata)

##2.1.2 Đánh giá tổng quan dữ liệu

summary(creditdata)

##3. Làm sạch dữ liệu

##3.1 Loại bỏ các dòng có giá trị NA xuất hiện creditdata <- na.omit(creditdata)

##3.2 Đổi tên cho các cột trong bảng với thay thế các giá trị “” bằng “_”

names(creditdata) <- gsub(““,”_“, names(creditdata))

##4. Khai thác dữ liệu

##4.1 Lọc các thông tin về điểm tín dụng

creditdata_max <- creditdata%>% filter(Credit_Score > 1000)

creditdata <- creditdata%>% filter(Credit_Score <1000)

5. Thống kê

5.1 So sánh

ggplot( creditdata, aes(x= Home_Ownship)) + geom_bar()

##5.2 Sự phân bổ

ggplot( creditdata, aes(x= Home_Ownship)) + geom_histogram()

ggplot( creditdata, aes(x= Home_Ownship)) + geom_histogram(color = “blue”, fill= “orange”)

ggplot( creditdata, aes(x= Home_Ownship)) + geom_histogram(color = “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”)

##5.3 So sánh tương quan giữ thu nhập năm và điểm tín dụng

ggplot(creditdata, aes(x=Credit_Score, y = Annual_Income)) + geom_point()

ggplot(creditdata, aes(x=Credit_Score, y = Annual_Income)) + geom_point(color = “darkblue”)

##5.4 Xác định ngoại lai dựa vào biểu đồ hộp

Điểm tín dụng theo loại hình sở hữu

ggplot(creditdata, aes(x= Credit_Score, y = Home_Ownership)) + geom_boxplot()

ggplot(creditdata, aes(x= Credit_Score, y = Home_Ownership)) + geom_boxplot(color = “red”, fill = “lightgreen”) labs(title = “Phân bổ giữa các loại hình sở hữu”)

##5.5 Thống kê theo nhóm

creditdata <- creditdata%>% group_by(Years_in_current_job)%>% summarise(soluong = n(), thunhap_BT = mean(Annual_Income)) creditdata

#————————-

#PHÂN TÍCH CHẨN ĐOÁN VÀ DỰ THEO CÁC MÔ HÌNH

#MÔ HÌNH HỒI QUY TUYẾN TÍNH #MÔ HÌNH HỒI QUY ĐƠN BIẾN

ĐÁNH GIÁ MỐI QUAN HỆ TƯƠNG QUAN GIỮ A - B (THU NHẬP NĂM VÀ - THANH TOÁN)

ggplot(credit, aes(x= Annual_Income, y = Monthly_Debt)) + geom_point(color = “blue”) + geom_smooth(method = lm, color = “red”)

##—> có vẻ như có mối tương quan, nên đưa vào để phân tích ## Xét điều kiện xem có phù hợp với phân tích tương quan không ### Cách 1: Nhìn xem phân phối của các biển dựa theo biểu đồ phân bổ histogram

ggplot(creditdata, aes(x= Annual_Income)) + geom_histogram() ### Cách 2: Đánh giá phân phối chuẩn theo 1 số cách mở rộng

đánh giá phân phối chuẩn của thu nhập nhằm

qqnorm(creditdata\(Annual_Income) qqline(creditdata\)Annual_Income, 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 mô hình hồi quy logic đa biến

dubaotheologicdabien <- glm(Trangthai ~ creditdata\(Credit_Score + creditdata\)Annual_Income + creditdata$Current_Credit_Balance, family = “binomial”, data = creditdata)

summary(mh_logicdabien)

dubaodabien_moi <- predict( mh_logicdabien, newsdata = dulieutest, type = “response”)

dubaodabien_moi

#4.Mô hình arima #dữ liệu thực hành là superstore

sieuthi<-read_excel(“C:\Users\Shincuti\Downloads\3.SUPERSTORE.xlsx”,sheet=1)

View(sieuthi)

#thống kê tổng doanh số theo tháng năm names(sieuthi) <-gsub(” “,”_“, names(sieuthi))

baocaoTG <- sieuthi%>% group_by( Thang = floor_date(Order_Date, “month”)) %>% summarise(Sales = sum(Sales)) #thiết lập tạo chuối thowif gian tuần tự theo tháng cho các daonh số Doanhso_thang<-ts(baocaoTG$Sales,start=c(2018,1),frequency =12 )

plot(Doanhso_thang)

##5.2Xd mô hình dự báo theo thời gian #cài gói dự báo forecast

install.packages(“forecast”) library(forecast)

##tạo mô hình arima mohinh_thoigian<-auto.arima(Doanhso_thang) summary(mohinh_thoigian)

#5.3.Dự báo dựa theo mh Dubaodoanhso_6thangtoi<-forecast(mohinh_thoigian,h=12)

plot(Dubaodoanhso_6thangtoi)

Doanhso_thang

#6.Kmeasms

View(sieuthi) ##Thống kê tt về gd 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ị cần (tham chiếu,cần chọn lọc) phân cụm giá trị 0-1

Tkgiaodich_biendoi<-scale(Tkgiaodich[-1])

Tkgiaodich_biendoi

#5.3.Xd mh Kmean sau khi làm mịn

mohinhKmeans<-kmeans(Tkgiaodich_biendoi, centers = 6)

install.packages(“factoextra”) library(factoextra)

fviz_cluster(mohinhKmeans, data= Tkgiaodich_biendoi)