统计学基于R第3版习题答案第二章.docx
《统计学基于R第3版习题答案第二章.docx》由会员分享,可在线阅读,更多相关《统计学基于R第3版习题答案第二章.docx(26页珍藏版)》请在冰豆网上搜索。
统计学基于R第3版习题答案第二章
习题
2.1
(1)简单频数分布表:
>load("D:
\\工作总结\\人大\\R语言\\《统计学—基于R》(第3版)—例题和习题数据(公开资源)\\exercise\\ch2\\exercise2_1.RData")
>summary(exercise2_1)
行业性别满意度
电信业:
38男:
58不满意:
75
航空业:
19女:
62满意:
45
金融业:
26
旅游业:
37
二维列联表:
>mytable1<-table(exercise2_1$行业,exercise2_1$满意度)
>addmargins(mytable1)#增加边界和
不满意满意Sum
电信业251338
航空业12719
金融业111526
旅游业271037
Sum7545120
三维列联表:
>mytable1<-ftable(exercise2_1,row.vars=c("性别","满意度"),col.var="行业");mytable1
行业电信业航空业金融业旅游业
性别满意度
男不满意117711
满意6376
女不满意145416
满意7484
(2)
条形图:
>count1<-table(exercise2_1$行业)
>count2<-table(exercise2_1$性别)
>count3<-table(exercise2_1$满意度)
>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="频数")
>barplot(count2,xlab="性别",ylab="频数")
>barplot(count3,xlab="满意度",ylab="频数")
帕累托图:
>count1<-table(exercise2_1$行业)
>par(mai=c(0.7,0.7,0.1,0.8),cex=0.8)
>x<-sort(count1,decreasing=T)
>bar<-barplot(x,xlab="行业",ylab="频数",ylim=c(0,1.2*max(count1)),col=2:
5)
>text(bar,x,labels=x,pos=3)#条形图增加数值
>y<-cumsum(x)/sum(x)#cumsum累计求和
>par(new=T)
>plot(y,type="b",lwd=1.5,pch=15,axes=F)
>axis(4)#右Y轴
>mtext("累积频率",side=4,line=3)
>mtext("累积分布曲线",line=-2.5,cex=0.8,adj=0.75)
复式条形图:
>mytable1<-table(exercise2_1$满意度,exercise2_1$行业)
>barplot(mytable1,xlab="行业",ylab="频数",legend=rownames(mytable1),args.legend=list(x=13),beside=T)
脊形图:
>library(vcd)
>spine(行业~满意度,data=exercise2_1,xlab="满意度",ylab="行业",margins=c(4,3.5,1,2.5))
马赛克图:
>mosaicplot(~性别+行业+满意度,data=exercise2_1,col=2:
3)
(3)
饼图:
>count1<-table(exercise2_1$行业)
>name<-names(count1)
>percent<-prop.table(count1)*100
>label1<-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=label1,init.angle=90)
扇形图:
>count1<-table(exercise2_1$行业)
>name<-names(count1)
>percent<-count1/sum(count1)*100
>labs<-paste(name,"",percent,"%",sep="")
>library(plotrix)
>fan.plot(count1,labels=labs,ticks=200)
2.2
(1)分10组,绘制频数分布表
>load("D:
\\工作总结\\人大\\R语言\\《统计学—基于R》(第3版)—例题和习题数据(公开资源)\\exercise\\ch2\\exercise2_2.RData")
>library(actuar)
>v<-as.vector(exercise2_2$灯泡寿命)
>gd1<-grouped.data(v,breaks=10,right=FALSE)
>table1<-data.frame(gd1);table1
Var.1v
1[2600,2800)1
2[2800,3000)4
3[3000,3200)12
4[3200,3400)13
5[3400,3600)27
6[3600,3800)20
7[3800,4000)19
8[4000,4200)4
(2)直方图
>d<-exercise2_2$灯泡寿命
>hist(d,breaks=10,xlab="寿命",ylab="频数")
茎叶图:
>stem(exercise2_2$灯泡寿命)
Thedecimalpointis2digit(s)totherightofthe|
26|0
28|968
0557********
32|7888990134569
34|11335667799112223344455559
0335********
38|22455566670055669
40|001017
2.3
(1)箱线图:
>load("D:
\\工作总结\\人大\\R语言\\《统计学—基于R》(第3版)—例题和习题数据(公开资源)\\exercise\\ch2\\exercise2_3.RData")
>boxplot(exercise2_3[,-1],xlab="城市",ylab="气温",cex.lab=0.8,cex.axis=0.6)#从第二列开始,到最后
小提琴图:
>library(vioplot)
>x1<-exercise2_3$北京
>x2<-exercise2_3$沈阳
>x3<-exercise2_3$上海
>x4<-exercise2_3$南昌
>vioplot(x1,x2,x3,x4,names=c("北京","沈阳","上海","南昌"))
(2)点图:
>library(reshape)
>table1_1<-melt(exercise2_3,id.vars=c("月份"),variable_name="城市")
>table1_1<-rename(table1_1,c(value="温度"))
>dotchart(table1_1$温度,groups=table1_1$城市,xlab="温度",pch=20)
>library(lattice)
>dotplot(温度~城市,data=table1_1,pch=19)
核密度图:
>library(lattice)
>dp1<-densityplot(~温度,group=城市,data=table1_1,auto.key=list(columns=1,x=0.01,y=0.95,cex=0.6),cex=0.5)
>plot(dp1)
>library(sm)
>pare(table1_1$温度,table1_1$城市,lty=1:
6,col=1:
6)
>legend("topleft",legend=levels(table1_1$城市),lty=1:
6,col=1:
6)
(3)轮廓图
>matplot(t(exercise2_3[,-1]),type="b",xlab="城市",ylab="温度",pch=1,xaxt="n")
>axis(side=1,at=1:
10,labels=c("北京","沈阳","上海","南昌","郑州","武汉","广州","海口","重庆","昆明"))
>legend("bottomright",legend=names(exercise2_3[,-1]))#取列名
雷达图:
>library(fmsb)
>table1<-data.frame(t(exercise2_3[,2:
11]))#行列进行转换,并数据框
>radarchart(table1,axistype=0,seg=4,maxmin=F,vlabels=exercise2_3[,1])
>legend(x="topleft",legend=names(exercise2_3[,2:
11]),col=1:
10,lty=1:
10)#lty图例
(4)星图:
>matrix1<-as.matrix(exercise2_3[,2:
11])
>rownames(matrix1)<-exercise2_3[,1]
>stars(matrix1,key.loc=c(7,2,5),cex=0.8)
脸谱图:
>library(aplpack)
>faces(t(matrix1),nrow.plot=5,ncol.plot=2,face.type=0)
effectofvariables:
modifieditemVar
"heightofface""1月"
"widthofface""2月"
"structureofface""3月"
"heightofmouth""4月"
"widthofmouth""5月"
"smiling""6月"
"heightofeyes""7月"
"widthofeyes""8月"
"heightofhair""9月"
"widthofhair""10月"
"styleofhair""11月"
"heightofnose""12月"
"widthofnose""1月"
"widthofear""2月"
"heightofear""3月"
2.4
(1)散点图:
>plot(地区生产总值,最终消费支出,xlab="",ylab='最终消费支出')
>abline(lm(最终消费支出~地区生产总值,data=exercise2_4))
>points(固定资产投资,最终消费支出,ylab='最终消费支出',pch=2,col="blue")
>abline(lm(最终消费支出~固定资产投资,data=exercise2_4),col="blue")
气泡图:
>r<-sqrt(最终消费支出/pi)
>symbols(最终消费支出,地区生产总值,circles=r,inches=0.3,fg="white",bg="lightblue",ylab="最终消费支出",xlab="地区生产总值")
>text(最终消费支出,地区生产总值,rownames(exercise2_4))
>mtext("气泡大小=最终消费支出",line=-2.5,adj=0.1)
(2)星图:
>matrix1<-as.matrix(exercise2_4[,2:
4])
>rownames(matrix1)<-exercise2_4[,1]
>stars(matrix1,key.loc=c(7,2,5),cex=0.8)
脸谱图:
>library(aplpack)
>faces(matrix1,nrow.plot=6,ncol.plot=6,face.type=0)
2.5
时序图:
>load("D:
\\工作总结\\人大\\R语言\\《统计学—基于R》(第3版)—例题和习题数据(公开资源)\\exercise\\ch2\\exercise2_5.RData")
>table1<-ts(exercise2_5,start=2004)
>plot(table1[,2],xlab="年份",ylab="价格指数",type="n")
>points(table1[,2],type="o",xlab="年份",ylab="城镇价格指数")
>lines(table1[,3],type="b")
2.6
洛伦茨曲线:
>load("D:
\\工作总结\\人大\\R语言\\《统计学—基于R》(第3版)—例题和习题数据(公开资源)\\exercise\\ch2\\exercise2_6.RData")
>library(DescTools)
>Lc(exercise2_6$不同阶层人口数的收入额*10000/exercise2_6$不同收入阶层的人口数,exercise2_6$不同收入阶层的人口数)#标红为组中值,收入/人数
$p
[1]0.00000000.34782610.60869570.82608700.95652171.0000000
$L
[1]0.000000000.060606060.151515150.333333330.636363641.00000000
$L.general
[1]02000050000110000210000330000
$Gini
[1]0.6232632
$x
[1]1250.002500.006000.0016666.6760000.00
$n
[1]8060503010
attr(,"class")
[1]"Lc"
>plot(Lc(exercise2_6$不同阶层人口数的收入额*10000/exercise2_6$不同收入阶层的人口数,exercise2_6$不同收入阶层的人口数),xlab="人数比例",ylab="收入比例",col=4,panel.first=grid(10,10,col="gray70"))
结论:
>0.4,收入差距巨大