好文档 - 专业文书写作范文服务资料分享网站

统计学基于R语言第二版CH2例题代码 

天下 分享 时间: 加入收藏 我要投稿 点赞

统计学基于R语言第二版

Ch2例题代码

目 录

第2章 .....................................................................................................................................................................1

文本框2--1 .................................................................................................................................................1 文本框2--2 .................................................................................................................................................1 文本框2--3 .................................................................................................................................................2 文本框2--4 .................................................................................................................................................2 文本框2--5 .................................................................................................................................................2 文本框2--6 .................................................................................................................................................3 文本框2--7 .................................................................................................................................................3 文本框2--8 .................................................................................................................................................3 文本框2--9 .................................................................................................................................................4 文本框2--10 ...............................................................................................................................................4 文本框2--11 ...............................................................................................................................................4 文本框2--12 ...............................................................................................................................................4 文本框2--13 ...............................................................................................................................................5 文本框2--14 ...............................................................................................................................................5 文本框2--15 ...............................................................................................................................................5 文本框2--16 ...............................................................................................................................................5 文本框2--17 ...............................................................................................................................................6 文本框2--18 ...............................................................................................................................................6 文本框2--19 ...............................................................................................................................................6 文本框2--20 ...............................................................................................................................................6 文本框2--21 ...............................................................................................................................................7 文本框2--22 ...............................................................................................................................................7 文本框2--23 ...............................................................................................................................................7 文本框2--24 ...............................................................................................................................................8 文本框2--25 ...............................................................................................................................................8 文本框2--26 ...............................................................................................................................................8 文本框2--27 ...............................................................................................................................................8 文本框2--28 ...............................................................................................................................................9 文本框2--29 ...............................................................................................................................................9 文本框2--30 ...............................................................................................................................................9 文本框2--31 ...............................................................................................................................................9 文本框2--32 ............................................................................................................................................ 10 文本框2--33 ............................................................................................................................................ 10 文本框2--34 ............................................................................................................................................ 10 文本框2--35 ............................................................................................................................................ 10

i

第2章

设置路径

setwd(\第2章 例题CSV格式数据\

文本框2--1

example2_1<-read.csv('example2_1.csv') save(example2_1,file = 'example2_1.RData') load('example2_1.RData') example2_1

summary(example2_1)

count1<-table(example2_1$社区) count1

prop.table(count1)*100

count2<-table(example2_1$性别) count2

prop.table(count2)*100

count3<-table(example2_1$态度) prop.table(count3)*100

文本框2--2

mytable1<-table(example2_1$社区,example2_1$性别) mytable1

addmargins(mytable1)

addmargins(prop.table(mytable1))*100

mytable2<-table(example2_1$社区,example2_1$态度) mytable2

addmargins(mytable2)

addmargins(prop.table(mytable2))*100

mytable3<-table(example2_1$性别,example2_1$态度) mytable3

addmargins(mytable3)

addmargins(prop.table(mytable3))*100

1

文本框2--3

#install.packages('gmodels') library(gmodels)

CrossTable(example2_1$性别,example2_1$态度)

文本框2--4

mytable1<-ftable(example2_1) mytable1

mytable2<-ftable(example2_1,row.vars = c('性别','态度'),col.vars = c('社区')) mytable2

addma<-addmargins(table(example2_1)) ftable(addma)

tab1<-table(example2_1) prop1<-prop.table(tab1)*100 addma2<-addmargins(prop1)

ftable(addma2,row.vars = c('性别','态度'),col.vars = c('社区'))

文本框2--5

example2_2<-read.csv('example2_2.csv') save(example2_2,file = 'example2_2.RData') load('example2_2.RData') example2_2

vector2_2<-as.vector(example2_2$销售额) vector2_2 library(plyr)

count<-table(round_any(vector2_2,10,floor)) count<-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 gt<-round(gt,4) gt

2

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

load('example2_1.RData')

count1<-table(example2_1$社区) count2<-table(example2_1$性别) count3<-table(example2_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 = '社区',horiz = T,main = '水平条形图',col=2:5) barplot(count2,xlab = '性别',ylab = '频数',main = '垂直条形图')

barplot(count3,xlab = '态度',ylab = '频数',main = '垂直条形图',col=2:3)

文本框2--7

load('example2_1.RData')

count1<-table(example2_1$社区) par(mai=c(0.7,0.7,0.1,0.8),cex=0.8) x<-sort(count1,decreasing = T)

barplot(x,xlab = '社区',ylab = '频数',col=2:5) 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)

mtext('累计分布曲线',line=-2.5,cex=0.8,adj=0.75)

文本框2--8

load('example2_1.RData')

mytable<-table(example2_1$态度,example2_1$社区) par(mfrow=c(2,2),cex=0.6)

barplot(mytable,xlab = '社区',ylab = '频数',ylim =

c(0,16),col=c('green','blue'),legend=rownames(mytable),args.legend = list(x=12),beside = T,main = '社区并列条形图')

barplot(mytable,xlab = '社区',ylab = '频数',ylim =

c(0,30),col=c('green','blue'),legend=rownames(mytable),args.legend = list(x=4.8),beside =

3

统计学基于R语言第二版CH2例题代码 

统计学基于R语言第二版Ch2例题代码目录第2章.....................................................................................................
推荐度:
点击下载文档文档为doc格式
7uilv37rh54yj364q360565jb3urou010yq
领取福利

微信扫码领取福利

微信扫码分享