当前位置:文档之家› 统计学-基于R第3版习题答案(第二章)

统计学-基于R第3版习题答案(第二章)

习题2.1(1)简单频数分布表:> load("D:\\工作总结\\人大\\R语言\\《统计学—基于R》(第3版)—例题和习题数据(公开资源)\\exercis e\\ch2\\exercise2_1.RData")> summary(exercise2_1)行业性别满意度电信业:38 男:58 不满意:75航空业:19 女:62 满意 :45金融业:26旅游业:37二维列联表:> mytable1<-table(exercise2_1$行业,exercise2_1$满意度)> addmargins(mytable1) # 增加边界和不满意满意 Sum电信业 25 13 38航空业 12 7 19金融业 11 15 26旅游业 27 10 37Sum 75 45 120三维列联表:> mytable1<-ftable(exercise2_1, row.vars = c("性别","满意度"), col.var="行业");mytable1 行业电信业航空业金融业旅游业性别满意度男不满意 11 7 7 11满意 6 3 7 6女不满意 14 5 4 16满意 7 4 8 4(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版)—例题和习题数据(公开资源)\\exercis e\\ch2\\exercise2_2.RData")> library(actuar)> v<-as.vector(exercise2_2$灯泡寿命)> gd1<-grouped.data(v, breaks = 10, right = FALSE)> table1<-data.frame(gd1);table1Var.1 v1 [2600, 2800) 12 [2800, 3000) 43 [3000, 3200) 124 [3200, 3400) 135 [3400, 3600) 276 [3600, 3800) 207 [3800, 4000) 198 [4000, 4200) 4(2)直方图> d<-exercise2_2$灯泡寿命> hist(d,breaks=10,xlab="寿命",ylab="频数")茎叶图:> stem(exercise2_2$灯泡寿命)The decimal point is 2 digit(s) to the right of the |26 | 028 | 96830 | 055788235679932 | 788899013456934 | 1133566779911222334445555936 | 03356660002444556778838 | 2245556667005566940 | 0010172.3(1)箱线图:> load("D:\\工作总结\\人大\\R语言\\《统计学—基于R》(第3版)—例题和习题数据(公开资源)\\exercis e\\ch2\\exercise2_3.RData")> boxplot(exercise2_3[,-1],xlab="城市",ylab="气温",b=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)effect of variables:modified item Var"height of face " "1月""width of face " "2月""structure of face" "3月""height of mouth " "4月""width of mouth " "5月""smiling " "6月""height of eyes " "7月""width of eyes " "8月""height of hair " "9月""width of hair " "10月""style of hair " "11月""height of nose " "12月""width of nose " "1月""width of ear " "2月""height of ear " "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版)—例题和习题数据(公开资源)\\exercis e\\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版)—例题和习题数据(公开资源)\\exercis e\\ch2\\exercise2_6.RData")> library(DescTools)> Lc(exercise2_6$不同阶层人口数的收入额*10000/exercise2_6$不同收入阶层的人口数,exercise2_6$不同收入阶层的人口数) # 标红为组中值,收入/人数$p[1] 0.0000000 0.3478261 0.6086957 0.8260870 0.9565217 1.0000000$L[1] 0.00000000 0.06060606 0.15151515 0.33333333 0.63636364 1.00000000$L.general[1] 0 20000 50000 110000 210000 330000$Gini[1] 0.6232632$x[1] 1250.00 2500.00 6000.00 16666.67 60000.00$n[1] 80 60 50 30 10attr(,"class")[1] "Lc"> plot(Lc(exercise2_6$不同阶层人口数的收入额*10000/exercise2_6$不同收入阶层的人口数,exercise2_ 6$不同收入阶层的人口数),xlab="人数比例",ylab="收入比例",col=4,panel.first=grid(10,10,col="gr ay70"))结论:>0.4,收入差距巨大。

相关主题