您当前的位置: 首页 >  r语言

R语言基础题及答案(四)——R语言与统计分析第四章课后习题(汤银才)

发布时间:2021-09-15 18:28:44 ,浏览量:9

R语言与统计分析第四章课后习题(汤银才) 题-1

模拟得到1000个参数为0.3的贝努里分布随机数, 并用图示表示出来

# 为了更清晰显示密度,通过cex把点画小点 plot(rbinom(1000,1,0.3),cex=0.5) 

在这里插入图片描述

题-2

用命令rnorm( )命令产生1000个均值为10, 方差为4的正态分布随机数,用直方图呈现数据的分布并添加核密度曲线.

rn<-rnorm(1000,mean=10,sd=2) hist(rn,probability=T) lines(density(rn),col="red",lwd=3) 

在这里插入图片描述

题-3

模拟得到三个t分布混合而成的样本, 用直方图呈现数据的分布并添加核密度曲线.

x=c(rt(100,1),rt(100,2),rt(100,10)) hist(x,xlim=c(min(x),max(x)),probability=T,col='lightblue') lines(density(x),col="#E54222",lwd=3) 

在这里插入图片描述

题-4

由程序包DAAG中的数据集possum,

  1. 利用函数hist(possum$age)作出负鼠年龄的直方图. 试选用两种不同的 断点并作比较, 说明两图的不同之处;

  2. 求出负鼠年龄变量的均值、标准差、中位数以及上下四分位数.

# 数据导入和框划分 library('DAAG') data(possum) par(mfrow=c(2,2)) # 不同断点直方图 hist(possum$age,breaks=1+(0:8)*1) hist(possum$age,breaks=0+(1:9)*1) hist(possum$age,breaks=1+(0:5)*2) hist(possum$age,breaks=seq(from=0,to=10,by=0.9)) # 均值、标准差、中位数以及上下四分位数 mean(possum$age,na.rm=TRUE) sd(possum$age,na.rm=TRUE) median(possum$age,na.rm=TRUE) quantile(possum$age,na.rm=TRUE) 

在这里插入图片描述

[1] 3.833 [1] 1.909 [1] 3 0% 25% 50% 75% 100% 1.00 2.25 3.00 5.00 9.00

题-5

考虑程序包DAAG中的数据集tinting,

  1. 获得变量tint和sex的列联表;

  2. 在同一图上作出变量sex与tint的联合柱状图;

  3. 作出age和it的散点图, 并进一步完成下面的操作:

    i. 用函数lowness()作出拟合线;

    ii. 在图的两边加上更细小的刻度;

    iii. 在图的两边加上箱型图.

  4. 作出age和it关于因子变量tint的条件散点图;

  5. 作出age和it关于因子变量tint和sex的条件散点图;

  6. 做出it与csoa的等高线图;

  7. 使用matplot( )描述变量age, it和csoa

# 数据导入 library('DAAG') data(tinting) # 1-获得变量tint和sex的列联表 ts<-table(tinting$tint,tinting$sex) # 2-在同一图上作出变量sex与tint的联合柱状图 barplot(ts) op <- par( ) layout(matrix(c(2,1,0,3), 2, 2, byrow=T ), c(1,6), c(2,1)) par(mar=c(1,1,5,2)) plot(tinting$age,tinting$it) lines(lowess(tinting$age,tinting$it),lwd=3) # 拟合线 rug(side=1,jitter(tinting$age,3),lwd=0.5) # 细小刻度-X轴 rug(side=2,jitter(tinting$it,3),lwd=0.5) # 细小刻度-y轴 par(mar=c(1,2,5,1)) boxplot(tinting$it,axes=FALSE) # 箱型图-y轴 par(mar=c(5,1,1,2)) boxplot(tinting$age,horizontal=T,axes=FALSE) # 箱型图-X轴 # 4-作出age和it关于因子变量tint的条件散点图 coplot(tinting$age~tinting$it|tinting$tint) # 5-作出age和it关于因子变量tint和sex的条件散点图 coplot(tinting$age~tinting$it|tinting$tint*tinting$sex) # 6-做出it与csoa的等高线图 library(MASS) z<-kde2d(tinting$it,tinting$csoa) contour(z,col="red",drawlabels=FALSE) # 7-使用matplot( )描述变量age, it和csoa d<-data.frame(y1=tinting$age,y2=tinting$it,y3=tinting$csoa) matplot(d,type='l',main="matplot") 

联合柱状图: 在这里插入图片描述 散点图: 在这里插入图片描述 条件散点图: 在这里插入图片描述 在这里插入图片描述 高线图图及matplot( ) 在这里插入图片描述

题-6

> data(InsectSprays)

> InsectSprays

得到数据集InsectSprays, 根据数据作出有意义的图, 并对数据作出描述性统计.

data(InsectSprays) # 列联表 cs<-table(InsectSprays$count,InsectSprays$spray) barplot(cs) # 分类图 mys<-c(1,2,3,4,5,6)[InsectSprays$spray] plot(InsectSprays$count,col=mys,pch=mys) # 分类归纳 legend(x=40,y=26,legend=c("A","B","C","D","E","F"),col=c(1,2,3,4,5,6),pch=c(1,2,3,4,5,6)) c.s<-data.frame(A=InsectSprays$count[1:12], B=InsectSprays$count[13:24], C=InsectSprays$count[25:36], D=InsectSprays$count[37:48], E=InsectSprays$count[49:60], F=InsectSprays$count[61:72]) summary(c.s) 

在这里插入图片描述 在这里插入图片描述 在这里插入图片描述

题-7

假定某校100名女生的血清总蛋白含量(g/L)服从均值为75, 标准差为3,并假定数据由下面的命令产生

> options(digits=4)

> rnorm(100,75,9)

根据产生的数据

  1. 计算样本均值、方差、标准差、极差、四分位极差、变异系数、偏度、峰度和五数概括;

  2. 画出直方图、核密度估计曲线、经验分布图和QQ图;

  3. 画出茎叶图、框须图.

options(digits=4) db<- rnorm(100,75,sd=3) mean(db) # 均值 var(db) # 方差 sd(db) # 标准差 max(db)-min(db) # 极差 mad(db) # 四分位极值 sd(db)/mean(db) # 变异系数 library(fBasics) skewness(db) # 偏度 kurtosis(db) # 峰度 fivenum(db) # 五数概括 # 画出直方图、核密度估计曲线、经验分布图和QQ图 # 直方图、核密度估计曲线 hist(db,probability=T,breaks = 40:110) lines(density(db),col='red',lwd=3) # QQ图 qqnorm(db,main="QQ图") qqline(db,col='#95B3D7',lwd=3) # 经验分布图 x<-sort(db) n<-length(x) y<-(1:n)/n
m<-mean(db) s<-sd(db) plot(x,y,type='s',main="经验分布图") curve(pnorm(x,m,s),col='red',lwd=2,add=T) # 画出茎叶图、框须图 stem(db) boxplot(db,main="框须图",horizontal=T) 

[1] 74.85 [1] 8.883 [1] 2.98 [1] 15.13 [1] 2.722 [1] 0.03982 [1] 0.1153 attr(,“method”) [1] “moment” [1] -0.2226 attr(,“method”) [1] “excess” [1] 68.24 73.05 74.94 76.68 83.37 . The decimal point is at the | . 68 | 22578 70 | 01145560357788 72 | 146800123356677889 74 | 00244556677790001111234556779 76 | 02244566679902336999 78 | 255801137 80 | 2271 82 | 4

在这里插入图片描述 在这里插入图片描述 在这里插入图片描述

在这里插入图片描述

题-8

某校测得20名学生的四项指标: 性别、年龄、身高(cm)和体重(kg), 具体数据如表4.1所示. 表 4.1: 学生身高与体重数据

学号 性别 年龄 身高 体重 01 F 18 166 54 02 F 18 155 58 03 F 19 154 50 04 F 18 160 47 05 F 20 162 46 06 F 19 153 48 07 F 21 156 50 08 F 20 152 49 09 F 21 170 57 10 F 20 156 52 11 M 18 168 61 12 M 18 166 55 13 M 19 172 63 14 M 18 178 68 15 M 20 169 59 16 M 19 180 65 17 M 21 177 59 18 M 20 168 56 19 M 21 182 69 20 M 20 170 61
  1. 绘制体重对身高的散点图;

  2. 绘制不同性别下, 体重对身高的散点图;

  3. 绘制不同年龄阶段, 体重对身高的散点图;

  4. 绘制不同性别和不同年龄阶段, 体重对身高的散点图.

library(RODBC) info<-data.frame("序号"=1:20,"性别"=c(rep("F",10),rep("M",10)),"年龄"=c(18,18,19,18,20,19,21,20,21,20,18,18,19,18,20,19,21,20,21,20),"身高"=c(166,165,154,160,162,153,156,152,170,156,168,166,172,178,169,180,177,168,182,170),"体重"=c(54,58,50,47,46,48,50,49,57,52,61,55,63,68,59,65,59,56,69,61)) print(info) # 体重对身高散点图 plot(info$体重~info$身高,main="体重对身高散点图") # 绘制不同性别下, 体重对身高的散点图 coplot(info$体重~info$身高|info$性别) # 绘制不同年龄阶段, 体重对身高的散点图 coplot(info$体重~info$身高|info$年龄) # 绘制不同性别和不同年龄阶段, 体重对身高的散点图 coplot(info$体重~info$身高|info$性别*info$年龄) 

在这里插入图片描述 在这里插入图片描述 在这里插入图片描述 在这里插入图片描述

关注
打赏
1688896170
查看更多评论

暂无认证

  • 9浏览

    0关注

    115984博文

    0收益

  • 0浏览

    0点赞

    0打赏

    0留言

私信
关注
热门博文
立即登录/注册

微信扫码登录

0.0562s