-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy path第4章 探索性数据分析.R
executable file
·208 lines (135 loc) · 5.44 KB
/
第4章 探索性数据分析.R
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
# 4.1 数据 #
install.packages(MASS)
library(MASS)
data(Insurance)
head(Insurance)
nrow(Insurance);ncol(Insurance)
dim(Insurance)
# 4.2 数字化探索 #
names(Insurance)
attributes(Insurance)
str(Insurance)
summary(Insurance)
install.packages(Hmisc)
library(Hmisc)
describe(Insurance[,1:3])
describe(Insurance[,4:5])
install.packages(fBasics)
library(fBasics)
basicStats(Insurance$Holders)
install.packages(timeDate)
library(timeDate)
skewness(Insurance[,4:5])
kurtosis(Insurance[,4:5])
install.packages(Matrix)
library(Matrix)
i=sample(1:10,10,replace=TRUE)
j=sample(1:10,10,replace=TRUE)
(A=sparseMatrix(i, j, x = 1))
loca=which(A==1, arr.ind=TRUE)
plot(loca,pch = 22)
for(i in 1:10)
{ row=sample(1:64,1)
col=sample(1:5,1)
Insurance[row,col]=NA
}
install.packages(mice)
library(mice)
md.pattern(Insurance)
library(rattle)
data(weather)
numerics=c(12:21)
cor_matrix=cor(weather[numerics],use="pairwise",method="pearson")
cor_matrix
library(ellipse)
plotcorr(cor_matrix,col=rep(c("white","black"),5))
plotcorr(cor_matrix,diag=T,type="lower",col=rep(c("white","black"),5))
# 4.3 可视化探索 #
# 直方图 MASS
hist(Insurance$Claims,main="Histogram of Freq of Insurance$Claims")
hist(Insurance$Claims,freq=FALSE,density=20,
main="Histogram of Density of Insurance$Claims")
lines(density(Insurance$Claims))
str(hist(Insurance$Claims,breaks=20,labels = TRUE,
col="black",border="white",
main="Histogram of Insurance$Claims with 20 bars"))
# 累积分布图 Hmisc
Ecdf(Insurance$Claims,xlab="Claims",main="Cumulative Distribution of Claims")
data_plot=with(Insurance,
rbind(data.frame(var1=Claims[Age=="<25"],var2="<25"),
data.frame(var1=Claims[Age=="25-29"],var2="25-29"),
data.frame(var1=Claims[Age=="30-35"],var2="30-35"),
data.frame(var1=Claims[Age==">35"],var2=">35") )
)
Ecdf(data_plot$var1,lty=2,group=data_plot$var2,label.curves=1:4,
xlab="Claims", main="Cumulative Distribution of Claims by Age")
Ecdf(Insurance$Claims,add=TRUE)
# 箱型图
Claims_bp=boxplot(Insurance$Claims,main="Distribution of Claims")
Claims_bp$stats
points(x=1,y=mean(Insurance$Claims),pch=8)
Claims_points=as.matrix(Insurance$Claims[which(Insurance$Claims>102)],6,1)
Claims_text=rbind(Claims_bp$stats,mean(Insurance$Claims),Claims_points)
for(i in 1:length(Claims_text))
text(x=1.1,y=Claims_text[i,],labels=Claims_text[i,])
boxplot(var1~var2,data=data_plot,horizontal=TRUE,
main="Distribution of Claims by Age",xlab="Claims",ylab="Age")
with(Insurance,
{
boxplot(Holders ~ Age, boxwex=0.25, at=1:4+0.2,
subset = Age == ">35")
boxplot(Holders ~ Age, add = TRUE, boxwex=0.25, at=1:4+0.2,
subset = Age == "30-35")
boxplot(Holders ~ Age, add = TRUE, boxwex=0.25, at=1:4+0.2,
subset = Age == "25-29")
boxplot(Holders ~ Age, add = TRUE, boxwex=0.25, at=1:4+0.2,
subset = Age == "<25")
} )
boxplot(var1~var2, data=data_plot, add = TRUE, boxwex=0.25, at=1:4 - 0.2,
col="lightgrey", main="Distribution of Claims&Holders by Age",
xlab="Age", ylab="Claims&Holders")
legend(x="topleft", c("Claims", "Holders"), fill = c("lightgrey", "white"))
data_bp=list(data_plot$var1[which(data_plot$var2=="<25")],
data_plot$var1[which(data_plot$var2=="25-29")],
data_plot$var1[which(data_plot$var2=="30-35")],
data_plot$var1[which(data_plot$var2==">35")])
bpplot(data_bp,name=c("<25","25-29","30-35",">35"),xlab="Age", ylab="Claims")
# 条形图
Claims_Age = with(Insurance,
c( sum(Claims[which(Age=="<25")]), sum(Claims[which(Age=="25-29")]),
sum(Claims[which(Age=="30-35")]), sum(Claims[which(Age==">35")]) ) )
barplot(Claims_Age, names.arg=c("<25","25-29","30-35",">35"),density=rep(20,4),
main="Distribution of Age by Claims", xlab="Age", ylab="Claims")
Holders_Age = with(Insurance,
c( sum(Holders[which(Age=="<25")]), sum(Holders[which(Age=="25-29")]),
sum(Holders[which(Age=="30-35")]), sum(Holders[which(Age==">35")]) ) )
Holders_Age
data_bar = rbind(Claims_Age,Holders_Age)
data_bar
barplot(data_bar, names.arg=c("<25","25-29","30-35",">35"),beside=TRUE,
main="Age Distribution by Claims and Holders",
xlab="Age", ylab="Claims&Holders", col=c("black","darkgrey"))
legend(x="topleft", rownames(data_bar), fill = c("black","darkgrey"))
barplot(data_bar, names.arg=c("<25","25-29","30-35",">35"),
main="Age Distribution by Claims and Holders",
ylab="Claims&Holders", col=c("black","darkgrey"))
legend(x="topleft", rownames(data_bar), fill = c("black","darkgrey"))
# 点阵图
dotchart(data_bar,xlab="Claims&Holders", pch=1:2,
main="Age Distribution by Claims and Holders")
legend(x=14000,y=15,"<25",bty="n")
legend(x=14000,y=11,"25-29",bty="n")
legend(x=14000,y=7,"30-35",bty="n")
legend(x=14000,y=3,">35",bty="n")
# 饼图
pie(Claims_Age,labels=c("<25","25-29","30-35",">35"),
main="Pie Chart of Age by Claims",col=c("white","lightgray","darkgrey","black"))
percent = round(Claims_Age/sum(Claims_Age)*100)
label = paste(paste(c("<25","25-29","30-35",">35"),":"), percent,"%",sep="")
pie(Claims_Age,labels = label,
main="Pie Chart of Age by Claims",col=c("white","lightgray","darkgrey","black"))
install.packages(plotrix)
library(plotrix)
pie3D(Claims_Age,labels=c("<25","25-29","30-35",">35"),explode=0.05,
main="3D Pie Chart of Age by Claims",labelcex=0.8,
col=c("white","lightgray","darkgrey","black"))