This is an R Markdown document. Markdown is a simple formatting syntax for authoring HTML, PDF, and MS Word documents. For more details on using R Markdown see http://rmarkdown.rstudio.com.
When you click the Knit button a document will be generated that includes both content as well as the output of any embedded R code chunks within the document. You can embed an R code chunk like this:
summary(cars)
## speed dist
## Min. : 4.0 Min. : 2.00
## 1st Qu.:12.0 1st Qu.: 26.00
## Median :15.0 Median : 36.00
## Mean :15.4 Mean : 42.98
## 3rd Qu.:19.0 3rd Qu.: 56.00
## Max. :25.0 Max. :120.00
data()
install.packages(“tidyverse”) # chạy các thư viện cần thiết cho dự án
library(tidyverse) library(tidyr) library(dplyr) library(ggplot2) library(readxl)
creditdata <- read_excel(“creditdata.xlsx”,sheet =1)
View(creditdata)
summary(creditdata)
creditdata <- na.omit()
names(creditdata) <-gsub(” “,”_“, names(credit_data))
##4.1 Lọc các thông tin về điểm tín dụng
creditdata <- creditdata%>% filter(Credit_Score <1000)
creditdatamax <- creditdata%>% filter(Credit_Score>1000)
View (creditdata)
#————————PHẦN 1: PHÂN TÍCH MÔ TẢ———
ggplot(creditdata, aes(x=Home_Ownership)) + geom_bar()
ggplot(creditdata, aes(x= Credit_Score)) + geom_histogram()
ggplot(creditdata, aes(x= Credit_Score)) + 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ữa 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”)
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ổ các loại hình sở hữu”)
creditdata.theotuoidilam <- creditdata%>% group_by(Years_in_current_job)%>% summarise(soluong = n(), thunhap_BT = mean(Annual_Income))
ggplot(creditdata, aes(x=Years_in_current_job)) + geom_bar(color=“darkblue”)
View(creditdata)
#—-PHẦN 2: PHÂN TÍCH CHUẨN ĐOÁN VÀ DỰ BÁO DỰA THEO CÁC MÔ HÌNH———
creditdata %>% filter(!is.na(Credit_Score), !is.na(Annual_Income)) %>% ggplot(aes(x = Credit_Score, y = Annual_Income)) + geom_point(color = “darkblue”) + geom_smooth(method = “lm”)
ggplot(creditdata, aes(x= Annual_Income, y= Monthly_Debt))+ geom_point(color=“blue”) geom_smooth(method=“lm”)
##–> có vẻ như có mối quan hệ tương quan, nên đưa vào phân tích ## xét các điều kiện xem có phù hợp với phân tích tương quan không: Các biến phải phân phối chuẩn ## cần xác định có phân phối chuẩn hay không ### cách 1: nhìn xem phân phối của các biến dựa theo phân bổ histogram
ggplot(creditdata, aes(x=Annual_Income))+ geom_histogram() ###cách 2: đánh giá phân phối chuẩn theo một số cách mở rộng
qqnorm(creditdata\(Annual_Income) qqline(creditdata\)Annual_Income, col=‘red’)
mohinh1 <- lm(creditdata\(Monthly_Debt ~ creditdata\)Annual_Income)
mohinh2 <- lm(Monthly_Debt ~ Annual_Income, data =creditdata)
mohinh1
View(mohinh1)
summary(mohinh1)
#2.4 Áp dụng mô hình cho mẫu mới
dulieutest <- read.csv(“credit_test.csv”)
dulieutest <- dulieutest%>% na.omit(dulieutest)
names(dulieutest) <- gsub(“\.”,“_“,names(dulieutest))
View(dulieutest)
#2.5 Chạy dự báo kết quả từ mô hình
Dubaochitrahangthang_theothunhapnam <- dulieutest%>% mutate(Dubaogiatri_Tha = predict(mohinh2,newdata = dulieutest ))
Dubaochitrahangthang_theothunhapnam
View(Dubaochitrahangthang_theothunhapnam)
#3 MÔ HÌNH HỒI QUY LOGIC ## lưu ý trong dữ liệu phải có cột ở định dạng 0 1 ## 3.1 Th không có thì thêm 1 cột trạng thái là phần dư phép chia hết cho 2
creditdata\(Trangthai <- creditdata\)Annual_Income %% 2 creditdata\(Trangthai <- factor(creditdata\)Trangthai, level = c(0,1),labels = c(“Không vỡ nợ”,“Có”))
View(creditdata)
#xây dụng mô hình logic
tk_term <- creditdata%>% group_by(Term)%>% summarise(soluong= n())
ggplot(creditdata, aes(x=Term,y=n()))
Mohinhlogic <- glm(Trangthai ~ Credit_Score, family= “binomial”,data = creditdata)
summary( Mohinhlogic)
Dubaotheologic <- predict(Mohinhlogic, newdata = dulieutest, type=“response”)
summary(Dubaotheologic)
View(Dubaotheologic)
mh_logicdabien <- glm(Trangthai~Credit_Score + Annual_Income + Current_Credit_Balance, family =“binomial”,data = creditdata)
summary(mh_logicdabien)
dubaodabien_moi <- predict(mh_logicdabien, newdata = dulieutest, type=“response”)
dubaodabien_moi
#4. MÔ HÌNH ARIMA # Dữ liệu thực hành là Superstore
sieuthi <- read_excel(“3.SUPERSTORE.xlsx”, sheet = 1)
View(sieuthi)
sieuthi
baocaoTG <- sieuthi%>% group_by( Thang =
floor_date(Order Date, “month”) ) %>% summarise(Sales =
sum(Sales))
baocaoTG
Doanhso_thang <- ts(baocaoTG$Sales, start =c(2018,1),frequency = 12)
plot (Doanhso_thang)
install.packages(“forecast”) library(forecast) names(sieuthi) <-gsub(” “,”_“, names(sieuthi))
mohinh_thoigian <- auto.arima(Doanhso_thang)
summary(mohinh_thoigian)
Dubaodoanhso_6thangtoi <- forecast(mohinh_thoigian, h =6)
plot(Dubaodoanhso_6thangtoi)
Tkgiaodich <- sieuthi%>% group_by(Customer_ID) %>% summarise( sodon = n_distinct(Order_ID), DS = sum(Sales), LN = sum(Profit) )
Tkgiaodich
Tkgiaodich_biendoi <- scale(Tkgiaodich[-1])
Tkgiaodich_biendoi
mohinhKmeans <- kmeans(Tkgiaodich_biendoi, centers = 3)
install.packages(“factoextra”) library(factoextra)
fviz_cluster(mohinhKmeans, data = Tkgiaodich_biendoi)