统计学基于R语言第二版CH2例题代码Word格式文档下载.docx
《统计学基于R语言第二版CH2例题代码Word格式文档下载.docx》由会员分享,可在线阅读,更多相关《统计学基于R语言第二版CH2例题代码Word格式文档下载.docx(14页珍藏版)》请在冰点文库上搜索。
-table(example2_1$性别,example2_1$态度)
mytable3
addmargins(mytable3)
addmargins(prop.table(mytable3))*100
文本框2--3
#install.packages('
gmodels'
library(gmodels)
CrossTable(example2_1$性别,example2_1$态度)
文本框2--4
-ftable(example2_1)
-ftable(example2_1,row.vars=c('
性别'
'
态度'
),col.vars=c('
社区'
))
addma<
-addmargins(table(example2_1))
ftable(addma)
tab1<
-table(example2_1)
prop1<
-prop.table(tab1)*100
addma2<
-addmargins(prop1)
ftable(addma2,row.vars=c('
文本框2--5
example2_2<
example2_2.csv'
save(example2_2,file='
example2_2.RData'
example2_2
vector2_2<
-as.vector(example2_2$销售额)
vector2_2
library(plyr)
count<
-table(round_any(vector2_2,10,floor))
-as.numeric(count)
pcount<
-prop.table(count)*100
coumsum<
-cumsum(pcount)
name<
-paste(seq(160,270,by=10),'
-'
seq(170,280,by=10),seq='
'
gt<
-data.frame(count,pcount,coumsum)
colnames(gt)<
-c('
频数'
百分比'
累计百分比'
rownames(gt)<
-name
-round(gt,4)
gt
d<
-table(cut(vector2_2,breaks=10*(16:
28),right=F))
percent<
-prop.table(d)
cpercent<
-cumsum(percent)*100
mytable<
-cbind(d,percent,cpercent)
mytable
文本框2--6
par(mfrow=c(1,3),mai=c(0.7,0.7,0.6,0.1),cex=0.7,cex.main=0.8)
barplot(count1,xlab='
ylab='
horiz=T,main='
水平条形图'
col=2:
5)
barplot(count2,xlab='
main='
垂直条形图'
barplot(count3,xlab='
3)
文本框2--7
par(mai=c(0.7,0.7,0.1,0.8),cex=0.8)
x<
-sort(count1,decreasing=T)
barplot(x,xlab='
y<
-cumsum(x)/sum(x)
par(new=T)
plot(y,type='
b'
lwd=1.5,pch=15,axes=F,xlab=,main='
axis(4)
par(las=0)
mtext('
累计频率'
side=4,line=3)
累计分布曲线'
line=-2.5,cex=0.8,adj=0.75)
文本框2--8
-table(example2_1$态度,example2_1$社区)
par(mfrow=c(2,2),cex=0.6)
barplot(mytable,xlab='
ylim=c(0,16),col=c('
green'
blue'
),legend=rownames(mytable),args.legend=list(x=12),beside=T,main='
社区并列条形图'
ylim=c(0,30),col=c('
),legend=rownames(mytable),args.legend=list(x=4.8),beside=F,main='
-table(example2_1$态度,example2_1$性别)
),legend=rownames(mytable),args.legend=list(x=4.5),beside=T,main='
性别并列条形图'
ylim=c(0,60),col=c('
),legend=rownames(mytable),args.legend=list(x=1),beside=F,main='
文本框2--9
library(vcd)
spine(社区~性别,data=example2_1,xlab='
ylab='
margins=c(4,3.5,1,2.5))
spine(态度~社区,data=example2_1,xlab='
文本框2--10
mosaicplot(~性别+社区+态度,data=example2_1,col=2:
3,main='
文本框2--11
-names(count1)
-prop.table(count1)*100
label<
-paste(name,'
percent,'
%'
sep='
par(pin=c(3,3),mai=c(0.1,0.4,0.1,0.4),cex=0.8)
pie(count1,labels=label,init.angle=90)
library(plotrix)
pie3D(count1,labels=label,explode=0.1,labelcex=0.7)
文本框2--12
fan.plot(count1,labels=label,ticks=200,col=c('
gray30'
gray70'
gray50'
gray90'
文本框2--13
par(mfrow=c(2,2),mai=c(0.6,0.6,0.4,0.1),cex=0.7)
hist(example2_2$销售额,xlab='
销售额'
main='
普通'
breaks=20,col='
分20组'
密度'
lightblue'
prob=T,main='
增加轴须线和核密度线'
rug(example2_2$销售额)
lines(density(example2_2$销售额),col='
red'
增加正态密度线'
curve(dnorm(x,mean(example2_2$销售额),sd(example2_2$销售额)),add=T,col='
rug(jitter(example2_2$销售额))
文本框2--14
hist(faithful$eruptions,breaks=20,prob=T,xlab='
喷发持续时间'
col='
rug(faithful$eruptions)
lines(density(faithful$eruptions,bw=0.1),type='
l'
lwd=2,col='
curve(dnorm(x,mean=mean(faithful$eruptions),sd=sd(faithful$eruptions)),add=T,col='
lwd=2,lty=6)
文本框2--15
stem(example2_2$销售额)
library(aplpack)
stem.leaf(example2_2$销售额,1,2)
stem.leaf.backback(example2_2$销售额[1:
60],example2_2$销售额[61:
120])
文本框2--16
par(mfrow=c(3,1),mai=c(0.4,0.2,0.3,0.2))
-rnorm(1000,50,5)
boxplot(x,range=1.5,col='
horizontal=T,main='
相邻值与箱子连线的箱线图(range=1.5)'
boxplot(x,range=3,col='
相邻值与箱子连线的箱线图(range=3)'
boxplot(x,range=0,varwidth=T,col='
pink'
极值与箱子连线的箱线图'
文本框2--17
-example2_2$销售额
layout(matrix(c(1,2),ncol=1),heights=c(2,1))
par(mai=c(0.15,0.4,0.2,0.2),cex=0.8)
hist(x,freq=F,col='
breaks=15,xlab='
rug(x,col='
blue4'
abline(v=quantile(x),col='
points(quantile(x),c(0,0,0,0,0),lwd=5,col='
red2'
lines(density(x),col='
lwd=2)
par(mai=c(0.35,0.42,0.2,0.43),cex=0.8)
boxplot(x,col='
lwd=2,horizontal=T)
rug(x,ticksize=0.1,col='
文本框2--18
example2_3<
example2_3.csv'
save(example2_3,file='
example2_3.RData'
example2_3
class(example2_3)
boxplot(example2_3,col='
xlab='
运动员'
射击环数'
cex.lab=0.8,cex.axis=0.6)
文本框2--19
library(vioplot)
vioplot(example2_3,col='
文本框2--20
-cbind(example2_3,id=factor(1:
20))
library(reshape)
example2_3_1<
-melt(example2_3,id.vars=c('
id'
),variable_name='
-rename(example2_3_1,c(value='
save(example2_3_1,file='
example2_3_1.RData'
head(example2_3_1)
tail(example2_3_1)
example2_3_1
dotchart(example2_3_1$射击环数,example2_3_1$运动员,xlab='
pch=20)
library(lattice)
dotplot(射击环数~运动员,data=example2_3_1,col='
pch=19)
文本框2--21
par(cex=0.7,mai=c(0.6,0.6,0.1,0.1))
densityplot(~射击环数|运动员,data=example2_3_1,col='
cex=0.5)
attach(example2_3_1)
library(sm)
pare(射击环数,运动员,lty=1:
6,col=1:
6,lwd=1.5)
legend('
topleft'
legend=levels(运动员),lty=1:
6)
文本框2--22
-seq(0,25,length=100)
-4+0.5*x+rnorm(100,0,2)
-data.frame(x,y)
plot(x,y)
polygon(d[chull(d),],col='
lty=3,lwd=2)
points(d)
abline(lm(y~x,lwd=2,col=4))
abline(v=mean(x),h=mean(y),lty=2,col='
文本框2--23
example2_4<
example2_4.csv'
save(example2_4,file='
example2_4.RData'
par(mfcol=c(1,2),mai=c(0.7,0.7,0.3,0.1),cex=0.8,cex.main=0.8)
plot(广告费用,销售收入,main='
普通带网格线'
type='
n'
grid()
points(广告费用,销售收入,main='
rug(广告费用,side=1,col=4)
rug(销售收入,side=2,col=4)
带有拟合直线'
abline(lm(销售收入~广告费用,data=example2_4),col='
rug(广告费用,side=1,col=4)
文本框2--24
par(fig=c(0,0.8,0,0.8),mai=c(0.9,0.9,0.1,0.1))
plot(广告费用,销售收入,xlab='
广告费用'
销售收入'
cex.lab=0.7,cex.axis=0.7)
par(fig=c(0,0.8,0.52,1),new=T)
boxplot(广告费用,horizontal=T,axes=F)
par(fig=c(0.52,1,0,0.9),new=T)
boxplot(销售收入,axes=F)
文本框2--25
abline(lm(销售收入~广告费用,data=example2_4))
points(销售网点数,销售收入,pch=2,col='
abline(lm(销售收入~销售网点数,data=example2_4),col='
points(销售人员数,销售收入,pch=3,col='
abline(lm(销售收入~销售人员数,data=example2_4),col='
bottomright'
legend=c('
销售网点数'
销售人员数'
),pch=1:
3,col=c('
black'
文本框2--26
library(scatterplot3d)
attach(example2_4)
s3d<
-scatterplot3d(销售人员数,销售网点数,销售收入,pch=16,highlight.3d=T,type='
h'
cex.lab=0.7)
fig<
-lm(销售收入~销售人员数+销售网点数)
s3d$plane3d(fig,col='
文本框2--27
plot(example2_4,cex=0.8,gap=0.5)
library(car)
scatterplotMatrix(~销售收入+销售网点数+销售人员数+广告费用,diagonal='
histogram'
gap=0.5)
文本框2--28
n<
-30
-rnorm(n)
z<
-abs(rnorm(n))+5:
1
plot(x,y,cex=z,col='
points(x,y,cex=z,lwd=1.5)
文本框2--29
r<
-sqrt(销售收入/pi)
symbols(广告费用,销售网点数,circle=r,inches=0.3,fg='
white'
bg='
text(广告费用,销售网点数,rownames(example2_4),cex=0.6)
文本框2--30
example2_5<
example2_5.csv'
save(example2_5,file='
example2_5.RData'
par(mai=c(0.7,0.7,0.1,0.1))
matplot(t(example2_5[2:
9]),type='
lty=1:
7,xlab='
消费项目'
支出金额'
pch=1,xaxt='
cn<
-as.character(colnames(example2_5)[2:
9])
axis(side=1,at=1:
8,labels=cn,cex.axis=0.6)
文本框2--31
fmsb'
library(fmsb)
example2_5
radarchart(example2_5[,2:
9],axistype=0,seg=4,maxmin=F,vlabels=names(example2_5[,2:
9]),pcol=1:
7,plwd=1.5)
rn<
-as.character(example2_5[,1])
legend(x='
legend=rn,col=1:
7,lwd=1,text.width=0.5,cex=0.7)
文本框2--32
matrix2_5<
-as.matrix(example2_5[,2:
rownames(matrix2_5)<
-example2_5[,1]
save(matrix2_5,file='
matrix2_5.RData'
stars(matrix2_5,key.loc=c