library(AER)
## Loading required package: car
## Loading required package: carData
## Loading required package: lmtest
## Loading required package: zoo
##
## Attaching package: 'zoo'
## The following objects are masked from 'package:base':
##
## as.Date, as.Date.numeric
## Loading required package: sandwich
## Loading required package: survival
data("CreditCard")
mean(CreditCard$income)
## [1] 3.365376
median(CreditCard$income, na.rm = FALSE)
## [1] 2.9
var(CreditCard$income)
## [1] 2.869303
sd(CreditCard$income)
## [1] 1.693902
mean(CreditCard$expenditure)
## [1] 185.0571
median(CreditCard$expenditure, na.rm = FALSE)
## [1] 101.2983
var(CreditCard$expenditure)
## [1] 74103.14
sd(CreditCard$expenditure)
## [1] 272.2189
TN <- CreditCard$income
BangTN <- cut(TN, breaks = c(0.2100, 2.868, 5.526, 8.184, 10.842, 13.5000 ), labels = c("thấp", "trung bình" , "trung bình cao", "cao", "rất cao"), right = TRUE)
tabin= table(BangTN)
tabin
## BangTN
## thấp trung bình trung bình cao cao rất cao
## 647 550 92 24 5
barplot(tabin, xlab = "Thu Nhập", ylab = "Tần số", main = "Biểu đồ biểu thị mức độ thu nhập hằng năm(USD)", col = c("green", "red", "blue", "white", "pink"))
Sng <- CreditCard$dependents
BangSng <- cut(Sng, breaks = c(0,2,4,6), labels = c("0-2", "2-4", "4-6"), right = TRUE)
tabde=table(BangSng)
tabde
## BangSng
## 0-2 2-4 4-6
## 485 159 16
barplot(tabde, xlab = "Sng", ylab = "Tần số", main = "Biểu đồ thể hiện số người phụ thuộc vào người được khảo sát", col = c("green", "red", "yellow"))
CV <- CreditCard$selfemp
table(CV)
## CV
## no yes
## 1228 91
tabse=table(CV)
tabse
## CV
## no yes
## 1228 91
barplot(tabse, xlab = "CV", ylab = "Tần số", main = "Biểu đồ cho biết cá nhân có làm chủ hay không?", col = c("red","green"))
### Giải thích kết quả từ bảng tần số của biến selfemp: cá nhân có tự
làm chủ hay không
tabearnde = table(BangTN, BangSng)
tabearnde
## BangSng
## BangTN 0-2 2-4 4-6
## thấp 205 41 1
## trung bình 234 81 12
## trung bình cao 33 27 2
## cao 9 10 1
## rất cao 3 0 0
chisq.test(TN, Sng, correct=FALSE)
## Warning in chisq.test(TN, Sng, correct = FALSE): Chi-squared approximation may
## be incorrect
##
## Pearson's Chi-squared test
##
## data: TN and Sng
## X-squared = 3310, df = 2580, p-value < 2.2e-16
Sng0den2 <- Sng[Sng>=0 & Sng<2]
vearnandde0den2 <- c(205,234,33,9,3)
Sng2den4 <- Sng[Sng>=2 & Sng<4]
vearnandde2den4 <- c(41,81,27,10,0)
Sng4den6 <- Sng[Sng>=4 & Sng<6]
vearnandde4den6 <- c(1,12,2,1,0)
plot(vearnandde0den2, xlab = "Mức Thu Nhập: Thấp - Trung bình - Trung bình cao - Cao - Rất Cao", ylab= "Tần số", main = "Biểu đồ của biến thu nhập khi có từ 0-2 người phụ thuộc vào", col = c ("red", "green","yellow", "blue", "black"))
plot(vearnandde2den4, xlab = "Mức Thu Nhập: Thấp - Trung bình - Trung bình cao - Cao - Rất Cao", ylab= "Tần số", main = "Biểu đồ của biến thu nhập khi có từ 2-4 người phụ thuộc vào", col = c ("red", "green","yellow", "blue", "black"))
plot(vearnandde4den6, xlab = "Mức Thu Nhập: Thấp - Trung bình- Trung bình cao - Cao - Rất Cao", ylab= "Tần số", main = "Biểu đồ của biến thu nhập khi có từ 4-6 người phụ thuộc vào", col = c ("red", "green","yellow", "blue", "black"))
tabearnse = table(BangTN, CV)
tabearnse
## CV
## BangTN no yes
## thấp 615 32
## trung bình 506 44
## trung bình cao 80 12
## cao 23 1
## rất cao 3 2
chisq.test(TN,CV,correct = FALSE)
## Warning in chisq.test(TN, CV, correct = FALSE): Chi-squared approximation may
## be incorrect
##
## Pearson's Chi-squared test
##
## data: TN and CV
## X-squared = 487.92, df = 430, p-value = 0.02773
vearnandno <- c(615,506,80,23,3)
vearnandyes <- c(32,44,12,1,2)
plot(vearnandno, xlab= "Mức Thu Nhập: Thấp - Trung Bình - Trung bình cao - Cao - Rất Cao", ylab = "Tần Số", main = "Biểu đồ thể hiện mức thu nhập khi biết người được khảo sát làm chủ", col = c("red", "green","yellow", "blue", "black"))
plot(vearnandyes, xlab= "Mức Thu Nhập: Thấp - Trung Bình - Trung bình cao - Cao - Rất Cao", ylab = "Tần Số", main = "Biểu đồ thể hiện mức thu nhập khi biết người được khảo sát không làm chủ", col = c("red", "green","yellow", "blue", "black"))
library(AER)
data("CreditCard")
head(CreditCard)
## card reports age income share expenditure owner selfemp dependents
## 1 yes 0 37.66667 4.5200 0.033269910 124.983300 yes no 3
## 2 yes 0 33.25000 2.4200 0.005216942 9.854167 no no 3
## 3 yes 0 33.66667 4.5000 0.004155556 15.000000 yes no 4
## 4 yes 0 30.50000 2.5400 0.065213780 137.869200 no no 0
## 5 yes 0 32.16667 9.7867 0.067050590 546.503300 yes no 2
## 6 yes 0 23.25000 2.5000 0.044438400 91.996670 no no 0
## months majorcards active
## 1 54 1 12
## 2 34 1 13
## 3 58 1 5
## 4 25 1 7
## 5 64 1 5
## 6 54 1 1
cr <- CreditCard
str(CreditCard)
## 'data.frame': 1319 obs. of 12 variables:
## $ card : Factor w/ 2 levels "no","yes": 2 2 2 2 2 2 2 2 2 2 ...
## $ reports : num 0 0 0 0 0 0 0 0 0 0 ...
## $ age : num 37.7 33.2 33.7 30.5 32.2 ...
## $ income : num 4.52 2.42 4.5 2.54 9.79 ...
## $ share : num 0.03327 0.00522 0.00416 0.06521 0.06705 ...
## $ expenditure: num 124.98 9.85 15 137.87 546.5 ...
## $ owner : Factor w/ 2 levels "no","yes": 2 1 2 1 2 1 1 2 2 1 ...
## $ selfemp : Factor w/ 2 levels "no","yes": 1 1 1 1 1 1 1 1 1 1 ...
## $ dependents : num 3 3 4 0 2 0 2 0 0 0 ...
## $ months : num 54 34 58 25 64 54 7 77 97 65 ...
## $ majorcards : num 1 1 1 1 1 1 1 1 1 1 ...
## $ active : num 12 13 5 7 5 1 5 3 6 18 ...
names(cr) <- c("ca","re","ag","in","sh","ex","ow","se","de","mo","ma","ac")
list = ls(cr)
head(cr)
## ca re ag in sh ex ow se de mo ma ac
## 1 yes 0 37.66667 4.5200 0.033269910 124.983300 yes no 3 54 1 12
## 2 yes 0 33.25000 2.4200 0.005216942 9.854167 no no 3 34 1 13
## 3 yes 0 33.66667 4.5000 0.004155556 15.000000 yes no 4 58 1 5
## 4 yes 0 30.50000 2.5400 0.065213780 137.869200 no no 0 25 1 7
## 5 yes 0 32.16667 9.7867 0.067050590 546.503300 yes no 2 64 1 5
## 6 yes 0 23.25000 2.5000 0.044438400 91.996670 no no 0 54 1 1
head(cr,9)
## ca re ag in sh ex ow se de mo ma ac
## 1 yes 0 37.66667 4.5200 0.033269910 124.983300 yes no 3 54 1 12
## 2 yes 0 33.25000 2.4200 0.005216942 9.854167 no no 3 34 1 13
## 3 yes 0 33.66667 4.5000 0.004155556 15.000000 yes no 4 58 1 5
## 4 yes 0 30.50000 2.5400 0.065213780 137.869200 no no 0 25 1 7
## 5 yes 0 32.16667 9.7867 0.067050590 546.503300 yes no 2 64 1 5
## 6 yes 0 23.25000 2.5000 0.044438400 91.996670 no no 0 54 1 1
## 7 yes 0 27.91667 3.9600 0.012575760 40.833330 no no 2 7 1 5
## 8 yes 0 29.16667 2.3700 0.076433760 150.790000 yes no 0 77 1 3
## 9 yes 0 37.00000 3.8000 0.245627900 777.821700 yes no 0 97 1 6
ag <- CreditCard$age
inc <- CreditCard$income
ow <- CreditCard$owner
x <- CreditCard[,c("age","income","owner")]
library(ggplot2)
barplot( ag, xlab = " ", ylab = "ag", main = "Biểu đồ thể hiện dữ liệu của biến Tuổi age ", col = c("red", "green", "blue", "pink", "white"))
barplot(inc, xlab = " ", ylab ="inc", main = "Biểu đồ thể hiện dữ liệu của biến thu nhập ", col = c("green","black","red","yellow","pink" ))
ThuNhaptang = cr[order(cr$`in`), ]
head(ThuNhaptang)
## ca re ag in sh ex ow se de mo ma ac
## 1201 yes 0 49.83333 0.210 0.2718571000 47.24166 yes no 2 42 1 1
## 1226 no 0 39.58333 0.490 0.0024489800 0.00000 yes no 2 100 1 0
## 320 no 0 23.91667 1.200 0.0010000000 0.00000 no no 0 15 0 0
## 1207 no 0 20.58333 1.200 0.0010000000 0.00000 no yes 1 24 1 0
## 226 yes 0 32.83333 1.320 0.0387553100 42.63083 no no 1 6 1 2
## 660 no 0 23.25000 1.434 0.0008368201 0.00000 yes no 0 12 1 6
own <- cr$ow
table(own)
## own
## no yes
## 738 581
de <- cr$de
table(cut(de,6))
##
## (-0.006,1] (1,2] (2,3] (3,4] (4,5] (5,6.01]
## 926 218 115 44 9 7