knitr::opts_chunk$set(echo = TRUE)

---
title: "Kiem tra"
author: "Vu Thi Lan Huong"
output: html_document
---


# Thuc hanh voi cac mo hinh thong ke
# Ham co san  
data()
# 1. Chay cac thu vien can thiet 
```{r 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

{r credit_data <- read_excel("creditdata.xlsx", sheet = 1) ## 2.2. Quan sat du lieu {r View(credit_data) ## 2.3. Danh gia tong quan du lieu {r summary(credit_data) ## 2.4. Lam sach du lieu:

2.4.1. loai bo cac dong co gia tri NA xuat hien

{r 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. Khai thac du lieu ## 3.1. Loc thong tin ve diem tin dung ```{r 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
```{r ggplot(credit_data, aes(x = Home_Ownership)) +
  geom_bar()

4.2. Su phan bo

{r ggplot(credit_data, aes(x = Credit_Score)) + geom_histogram() #Mau {r ggplot(credit_data, aes(x = Credit_Score)) + geom_histogram(color= 'blue', fill = "orange") #Tieu de {r 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 {r ggplot(credit_data, aes(x = Credit_Score, y = Annual_Income)) + geom_point() #Mau {r 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

{r ggplot(credit_data, aes(x = Credit_Score, y = Home_Ownership))+ geom_boxplot() ##check lai tieu de {r 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 {r 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 {r 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)

{r 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) ```{r 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

```{r 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
```{r 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 {r credit_test <- read_csv("credit_test.csv") #B2 {r credit_test <- credit_test%>% na.omit(credit_test) #B3 {r names(credit_test) <- gsub(" ","_", names(credit_test)) ##OR: Truong hop ten truong co dau . {r names(credit_test) <- gsub("\\.","_", names(credit_test)) View(credit_test) # 2.5. Chay du bao ket qua tư mo hinh ```{r 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

```{r 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 ; {r ggplot(credit_data, aes (x=credit_data$Term))+ geom_bar ;

3.2. Xay dung mo hinh logic

{r mohinhlogic = glm(Trangthai~Credit_Score,family = "binomial",data = credit_data) summary(mohinhlogic) ## 3.3. Chay mo hinh du bao voi bo du lieu moi ```{r dubaotheologic <- predict(mohinhlogic, newdata = credit_test, type = “response”)

View(dubaotheologic)

## 3.4. mô hình hồi quy đa biến
```{r 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

4. Mô hình Arima

#Dữ liệu thực hành là Superstore {r 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 ```{r 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ố
```{r Doanhso_thang <- ts(baocaoTG$Sales, start = c(2018,1), frequency = 12)
Doanhso_thang
plot(Doanhso_thang)

##5.2. Xây dựng mô hình dự báo theo thời gian ## Cài gói dự báo forecast {r install.packages("forecast") library(forecast) ## Tạo mô hình armia {r mohinh_thoigian <- auto.arima(Doanhso_thang) summary(mohinh_thoigian) ##5.3. Dự báo dựa theo mô hình {r Dubaodoanhso_6thangtoi <-forecast(mohinh_thoigian, h =6) plot(Dubaodoanhso_6thangtoi)

6. Kmeams

Thống kê thông tin về giao dịch theo từng KH

```{r Tkgiaodich<-sieuthi%>% group_by(sieuthi$Customer_ID)%>% summarise( sodon=n_distinct(Order_ID), DS=sum(Sales), LN=sum(Profit))

Tkgiaodich

# 5.2. Biến đổi các giá trị
```{r TKgiaodich_biendoi <- scale(Tkgiaodich[-1])
TKgiaodich_biendoi

5.3. Xây dựng mô hình Kmean sau khi làm mịn

```{r mohinhKmeans <- kmeans(TKgiaodich_biendoi, centers = 3) install.packages(“factoextra”) install.packages(“ggsignif”) install.packages(“car”)

library(factoextra)

fviz_cluster(mohinhKmeans, data = TKgiaodich_biendoi) ```