2.1

#2_1
load("C:/exercise/ch2/exercise2_1.RData")
exercise2_1
#(1)简单频数分布表
summary(exercise2_1)
#行业
count1<-table(exercise2_1$行业)
count1
#百分比表
prop.table(count1)*100
#性别
count2<-table(exercise2_1$性别)
count2
#百分比表
prop.table(count2)*100
#满意度
count3<-table(exercise2_1$满意度)
count3
#百分比表
prop.table(count3)*100
#(1)二维列联表
#行业和性别
myexercise1<-table(exercise2_1$行业,exercise2_1$性别)
addmargins(myexercise1)
#百分比表
addmargins(prop.table(myexercise1))*100
#行业和满意度
myexercise2<-table(exercise2_1$行业,exercise2_1$满意度)
addmargins(myexercise2)
addmargins(prop.table(myexercise2))*100
#性别和满意度
myexercise3<-table(exercise2_1$性别,exercise2_1$满意度)
addmargins(myexercise3)
addmargins(prop.table(myexercise3))*100
#(1)三维列联表
#行变量行业性别,列变量满意度
mytable1<-ftable(exercise2_1)
mytable1
#行变量性别满意度,列变量为行业
mytable2<-ftable(exercise2_1,row.vars=c("性别","满意度"),col.var="行业")
mytable2
#(2)简单条形图
count11<-table(exercise2_1$行业)
count22<-table(exercise2_1$性别)
count33<-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(count11,xlab="频数",ylab="行业",horiz=TRUE,main="(a)水平条形图",col=2:5)
barplot(count22,xlab="性别",ylab="频数",main="(b)垂直条形图")
barplot(count33,xlab="满意度",ylab="频数",col=2:3,main="(c)垂直条形图")
#(2)帕累托图(不同行业人数分布的帕累托图)
count11<-table(exercise2_1$行业)
par(mai=c(0.7,0.7,0.1,0.8),cex=0.8)
x<-sort(count11,decreasing=T)
bar<-barplot(x,xlab="行业",ylab="频数",ylim=c(0,1.2*max(count11)),col=2:5)
text(bar,x,labels=x,pos=3)
y<-cumsum(x)/sum(x)
par(new=T)
plot(y,type="b",lwd=1.5,pch=15,axes=FALSE,xlab=' ',ylab=' ',main=' ')
axis(4)
mtext("累积频率",side=4,line=3)
mtext("累积分布曲线",line=-2.5,cex=0.8,adj=0.75)
#(2)复式条形图
mytable11<-table(exercise2_1$满意度,exercise2_1$行业)
par(mfrow=c(2,2),cex=0.6)
barplot(mytable11,xlab="行业",ylab="频数",ylim=c(0,16),col=c("green","blue"),legend=rownames(mytable11),args.legend=list(x=12),beside=TRUE,main="(a)行业并列条形图")
barplot(mytable11,xlab="行业",ylab="频数",ylim=c(0,44),col=c("green","blue"),legend=rownames(mytable11),args.legend=list(x=5.0),beside=FALSE,main="(b)行业堆叠条形图")
mytable22<-table(exercise2_1$满意度,exercise2_1$性别)
barplot(mytable22,xlab="性别",ylab="频数",ylim=c(0,30),col=c("green","blue"),legend=rownames(mytable22),args.legend=list(x=4.5),beside=TRUE,main="(c)性别并列条形图")
barplot(mytable22,xlab="性别",ylab="频数",ylim=c(0,75),col=c("green","blue"),legend=rownames(mytable22),args.legend=list(x=2.5),beside=FALSE,main="(d)性别堆叠条形图")
#(2)脊形图
#性别和行业的脊形图
library(vcd)
spine(行业~性别,data=exercise2_1,xlab="性别",ylab="行业",margins=c(4,3.5,1,2.5))
#行业和满意度的脊形图
spine(满意度~行业,data=exercise2_1,xlab="社区",ylab="满意度",margins=c(4,3.5,1,2.5))
#(2)马赛克图
mosaicplot(~性别+行业+满意度,data=exercise2_1,color=2:3,main="")
#(3)饼图
#二维
count11<-table(exercise2_1$行业)
name<-names(count11)
percent<-prop.table(count11)*100
label11<-paste(name," ",percent,"%",sep="")
par(pin=c(3,3),mai=c(0.1,0.4,0.1,0.4),cex=0.8)
pie(count11,labels=label11,init.angle=90)
#三维
library(plotrix)
count11<-table(exercise2_1$行业)
name<-names(count11)
percent<-prop.table(count11)*100
labs<-paste(name,"",percent,"%",sep="")
pie3D(count11,labels=labs,explode=0.1,labelcex=0.7)
#(3)扇形
count11<-table(exercise2_1$行业)
name<-names(count11)
percent<-count11/sum(count11)*100
labs<-paste(name," ",percent,"%",sep="")
library(plotrix)
fan.plot(count11,labels=labs,ticks=200,col=c("gray30","gray70","gray50","gray90"))

2.2

#2_2
load("C:/exercise/ch2/exercise2_2.RData")
exercise2_2
#nclass.Sturges(exercise2_2$灯泡寿命)#K=8
#(1)将频数分为十组,形成频数分布表#方法1
vector2_2<-as.vector(exercise2_2$灯泡寿命)
#min(vector2_2)
#max(vector2_2)
d<-table(cut(vector2_2,breaks=150*(18:28),right=FALSE))
df<-data.frame(d)
percent<-df$Freq/sum(df$Freq)*100
cumsump<-cumsum(percent)
mytable<-data.frame(d,percent,cumsump)
mytable#(1)将频数分为十组,形成频数分布表#方法2
vector2_2<-as.vector(exercise2_2$灯泡寿命)
#min(vector2_2)
#max(vector2_2)
library(plyr)
count<-table(round_any(vector2_2,150,floor))
count<-as.numeric(count)
pcount<-prop.table(count)*100
cumsump<-cumsum(pcount)
name<-paste(seq(2700,4050,by=150),"-",seq(2850,4200,by=150),sep="")
gt<-data.frame("频数"=count,"百分比"=pcount,"累积百分比"=cumsump,row.names=name)
round(gt,4)#(2)绘制直方图
dd<-exercise2_2$灯泡寿命
par(mfrow=c(2,2),mai=c(0.6,0.6,0.4,0.1),cex=0.7)
hist(dd,xlab="灯泡寿命",ylab="频数",main="(a)普通")
hist(dd,breaks=8,col="lightblue",xlab="灯泡寿命",ylab="频数",main="(b)分成8组")
hist(dd,prob=TRUE,breaks=8,xlab="灯泡寿命",ylab="密度",col="lightblue",main="(c)增加轴须线和核密度线")
rug(dd)
lines(density(dd),col="red")
hist(dd,prob=TRUE,breaks=8,xlab="灯泡寿命",ylab="密度",main="(d)增加正态密度线",col="lightblue")
curve(dnorm(x,mean(dd),sd(dd)),add=T,col="red")
rug(jitter(dd))#(2)绘制茎叶图
#使用stem函数绘制茎叶图(每个茎列出1次)
stem(exercise2_2$灯泡寿命)
#使用stem.leaf函数绘制茎叶图(每个茎列出两次)
library(aplpack)
stem.leaf(exercise2_2$灯泡寿命)

2.3

#2_3
load("C:/exercise/ch2/exercise2_3.RData")
exercise2_3
#(1)按城市绘制箱线图
boxplot(exercise2_3[,2:11],col="lightblue",xlab="城市",ylab="气温",cex.lab=0.8,cex.axis=0.6)
#(1)按城市绘制小提琴图
library(vioplot)
x1<-exercise2_3$北京
x2<-exercise2_3$沈阳
x3<-exercise2_3$上海
x4<-exercise2_3$南昌
x5<-exercise2_3$郑州
x6<-exercise2_3$武汉
x7<-exercise2_3$广州
x8<-exercise2_3$海口
x9<-exercise2_3$重庆
x10<-exercise2_3$昆明
vioplot(x1,x2,x3,x4,x5,x6,x7,x8,x9,x10,col="lightblue",names=c("北京","沈阳","上海","南昌","郑州","武汉","广州","海口","重庆","昆明"))#(2)按城市绘制点图
#将数据转换为长格式
library(reshape)
exercise2_3_1<-melt(exercise2_3,id.vars=c("月份"),variable_name="城市")
exercise2_3_1<-rename(exercise2_3_1,c(value="气温"))
save(exercise2_3_1,file="D:/R/R_text/exercise2_3_1.RData")
#exercise2_3_1
head(exercise2_3_1);tail(exercise2_3_1)
#Cleveland点图
dotchart(exercise2_3_1$气温,groups=exercise2_3_1$城市,xlab="气温",pch=12)
#lattice包绘制
library(lattice)
dotplot(气温~城市,data=exercise2_3_1,col="blue",pch=11)#(2)按城市绘制核密度估计曲线
load("D:/R/R_text/exercise2_3_1.RData")
library(lattice)
dp1<-densityplot(~气温|城市,data=exercise2_3_1,col="blue",cex=0.5,par.strip.text=list(cex=0.6),sub="(a)栅格图")
dp2<-densityplot(~气温,group=城市,data=exercise2_3_1,auto.key=list(colums=1,x=0.01,y=0.95,cex=0.6),cex=0.5,sub="(b)比较图")
plot(dp1,split=c(1,1,2,1))
plot(dp2,split=c(2,1,2,1),newpage=F)#(3)按城市绘制轮廓图
par(mai=c(0.7,0.7,0.1,0.1),cex=0.8)
matplot(t(exercise2_3[2:11]),type="b",lty=1:12,col=1:10,xlab="城市",ylab="气温",pch=1,xaxt="n")
axis(side=1,at=1:10,labels=c("北京","沈阳","上海","南昌","郑州","武汉","广州","海口","重庆","昆明"),cex.axis=0.6)
legend(x="topright",legend=exercise2_3[,1],lty=1:12,col=1:10,text.width=1,cex=0.7)#(3)按城市绘制雷达图
library(fmsb)
radarchart(exercise2_3[,2:11],axistype=0,seg=4,maxmin=FALSE,vlabels=names(exercise2_3[,2:11]),pcol=1:10,plwd=1.5)
legend(x="topleft",legend=exercise2_3[,1],lty=1:12,col=1:10,lwd=1,text.width=0.4,cex=0.6)#(4)按月份绘制星图
matrix2_3<-as.matrix(exercise2_3[,2:11])
rownames(matrix2_3)<-exercise2_3[,1]
save(matrix2_3,file="D:/R/R_text/matrix2_3.RData")
matrix2_3stars(matrix2_3,key.loc=c(9,2,3),cex=0.8)#(4)按城市绘制星图
stars(t(matrix2_3),full=FALSE,draw.segments=TRUE,key.loc=c(6,2.2,5),cex=0.8)#(4)按月份绘制脸谱图
library(aplpack)
faces(matrix2_3,nrow.plot=4,ncol.plot=3,face.type=1)#(4)按城市绘制脸谱图
faces(t(matrix2_3),nrow.plot=5,ncol.plot=2,face.type=2)

2.4

#2_4
load("C:/exercise/ch2/exercise2_4.RData")
exercise2_4
#(1)绘制散点图
attach(exercise2_4)
plot(固定资产投资,地区生产总值,xlab=" ",ylab="地区生产总值",col="red")
abline(lm(地区生产总值~固定资产投资,data=exercise2_4),col="red")
points(最终消费支出,地区生产总值,pch=2,col="blue")
abline(lm(地区生产总值~最终消费支出,data=exercise2_4),col="blue")
legend("bottomright",legend=c("固定资产投资","最终消费支出"),pch=1:3,col=c("red","blue"))#(1)绘制气泡图
attach(exercise2_4)
r<-sqrt(地区生产总值/pi)
symbols(最终消费支出,固定资产投资,circle=r,inches=0.3,fg="white",bg="lightblue",ylab="固定资产投资",xlab="最终消费支出")
text(最终消费支出,固定资产投资,rownames(exercise2_4),cex=0.6)
mtext("气泡大小 = 地区生产总值",line=-2.5,cex=0.8,adj=0.1)#(2)绘制星图比较31个地区各项数据的相似性
matrix2_4<-as.matrix(exercise2_4[,2:4])
rownames(matrix2_4)<-exercise2_4[,1]
#matrix2_4
save(matrix2_4,file="D:/R/R_text/matrix2_4.RData")stars(t(matrix2_4),full=FALSE,draw.segments=TRUE,key.loc=c(6,2.2,5),cex=0.8)#(2)绘制脸谱图比较31个地区各项数据的相似性library(aplpack)
faces(t(matrix2_4),nrow.plot=4,ncol.plot=2,face.type=0)

2.5

#2_5
load("C:/exercise/ch2/exercise2_5.RData")
exercise2_5
#绘制时间序列表,观察城镇居民和农村居民消费价格指数的变化特征
exercise2_5<-ts(exercise2_5,start=2005)
par(mai=c(0.7,0.7,0.1,0.1),cex=0.8)
plot(exercise2_5[,2],ylim=c(90,110),xlab="年份",ylab="居民消费价格指数",type="n")###表格绘制有问题###
grid(col="gray60")
points(exercise2_5[,2],type='o',ylim=c(90,110),xlab="年份",ylab="居民消费价格指数")
lines(exercise2_5[,3],type='b',lty=2,col="blue")
legend(x="topleft",legend=c("城镇居民消费价格指数","农村居民消费价格指数",lty=1:2,col=c(1,4),cex=0.8))

2.6

#2_6
load("C:/exercise/ch2/exercise2_6.RData")
exercise2_6
#绘制洛伦茨曲线分析收入分配的不平等程度#人数累积百分比做横轴,收入累积百分比做纵轴,即可绘制出洛伦茨曲线#计算绘制洛伦茨曲线所需的各百分比数值
library(DescTools)
Lc(exercise2_6$不同阶层人口数的收入额,exercise2_6$不同收入阶层的人口数)#绘制洛伦茨曲线
plot(Lc(exercise2_6$不同阶层人口数的收入额,exercise2_6$不同收入阶层的人口数),xlab="人数比例",ylab="收入比例",col=4,panel.first=grid(10,10,col="gray70"))

统计学--基于R(第3版)(基于R应用的统计学丛书)作者:贾俊平 习题答案 第二章相关推荐

  1. 统计学--基于R(第3版)(基于R应用的统计学丛书)作者:贾俊平 习题答案 第十一章

    11.1 #11.1 load("C:/exercise/ch11/exercise11_1.RData") exercise11_1 #采用指数平滑法预测2016年的PPI,并对 ...

  2. 统计学--基于R(第3版)(基于R应用的统计学丛书)作者:贾俊平 习题答案 第七章

    7.1 #7.1 #检验各月份的销量是否符合均匀分布(α=0.05) #H0:符各均匀分布,H1:不符合均匀分布 load('C:/exercise/ch7/exercise7_1.RData') e ...

  3. 《统计学》贾俊平 第一章 导论总结

    前言 在学习<统计学>一书之后,在这里按照章节进行整理总结,也是对知识的一个复习,学习统计学的意义我认为就是以后在做数据分析的时候,能用科学的.有效的方法处理数据. 1. 什么是统计学 统 ...

  4. 统计学----基于R(第三版)第六章答案(贾俊平)

    统计学----基于R(第三版)第六章答案(贾俊平) #6.1(1) load('C:/exercise/ch6/exercise6_1.RData') par(mfrow=c(1,2),cex=0.8 ...

  5. 《统计学》(贾俊平)考研初试完整学习笔记10~14章

    贾俊平<统计学第6版>学习笔记 这是我自己去年考研时整理的笔记,希望能给到432考研以及正在学习统计学的小伙伴们一点帮助吧,我是把这份笔记当作复习时的框架来用的,时不时过一遍,有不熟悉的地 ...

  6. 《统计学》(贾俊平)考研初试完整学习笔记6~9章

    贾俊平<统计学第6版>学习笔记 这是我自己去年考研时整理的笔记,希望能给到432考研以及正在学习统计学的小伙伴们一点帮助吧,我是把这份笔记当作复习时的框架来用的,时不时过一遍,有不熟悉的地 ...

  7. c语言程序设计第四版乌云高娃,C语言程序设计教学课件作者第2版乌云高娃课件源程序及习题答案第4章课件.ppt...

    C语言程序设计教学课件作者第2版乌云高娃课件源程序及习题答案第4章课件.ppt 第4章循环结构的流程及应用 学习目标 ? 使用循环处理需要反复执行的操作. ? 循环结构的流程图. ? 循环与条件的综合 ...

  8. 统计学-假设检验部分 贾俊平 #读书笔记

    贾俊平-统计学-假设检验 笔记链接: https://share.mubu.com/doc/23rRpdVegf 网课链接: 链接: https://pan.baidu.com/s/1e5yWRndc ...

  9. 《金融数据分析导论:基于R语言》习题答案(第一章)

     <金融数据分析导论:基于R语言>是芝加哥大学的教授Ruey S.Tsay所著,李洪成.尚秀芬.郝瑞丽翻译,机械工业出版社出版,是一本学习R语言和金融数据分析的很好的参考书籍. ** 注 ...

最新文章

  1. C#编程应用--线程与委托
  2. lr手工添加关联函数的步骤:
  3. Linux中读写权限
  4. 运维基础(13)日志切割工具 Logrotate
  5. POJ 2785 4 Values whose Sum is 0
  6. Visual c++6.0 如何自定义一个光标,使其变成字
  7. c语言矩阵存储,C语言实现特殊矩阵存储
  8. C++ Opengl 显示列表源码
  9. [HDU 4344]Mark the Rope(Pollard_rho+Miller_Rabin)
  10. 上元节的灯会(灭)-区间dp
  11. 深度学习中端到端的理解
  12. Ubuntu20.04安装qt详细教程
  13. vue 获取当前本机ip_Vue项目启动时自动获取本机IP地址
  14. Dropbox安装包官网下载失败的解决方法
  15. 【VMware的坑爹之路】VMware复制文件卡死死机;无法访问网络地址“*:\“
  16. vue 页面刷新404
  17. Arduino流水灯 附电路图
  18. 什么是 Proxy ?
  19. java计算机毕业设计网课系统源码+系统+数据库+lw文档+mybatis+运行部署
  20. 配对交易(一):期货品种相关性研究

热门文章

  1. 谷歌Chrome浏览器在新标签页打开书签链接的五个方法
  2. N1刷入Armbian和OpenWRT
  3. 关于react判断是否安装app,没有则去appStore(适配android和ios)
  4. 快递代领--需求分析
  5. ubuntu 错误:error :must run as root
  6. python删除excel已存在的sheet
  7. 借呗额度一直不用会被关闭吗?
  8. Labview 和信捷 XDH PLC Modbus tcp通讯
  9. 【Alios-things笔记】EMW3060 UART串口
  10. 喜讯丨星舆科技连续四年入选“全国科技型中小企业”