2.1
data<-read.csv("D:/2020文件夹/《统计学素材—基于R》(第3版)—例题和习题数据(公开资源)/exercise/ch2/第2章 习题CSV格式数据/exercise2_1.csv")
data
summary(data)
d1<-table(data$行业)
d1
d2<-table(data$性别)
d2
d3<-table(data$满意度)
d3
data1<-table(data$行业,data$满意度)
data1
addmargins(data1)
addmargins(prop.table(data1))*100
data2<-ftable(data)
data2
data2_1<-ftable(data,row.vars=c("行业","满意度"),col.vars="性别")
data2_1
par(mfrow=c(1,3),mai=c(0.7,0.7,0.6,0.1),cex=0.7,cex.main=0.8)
barplot(d1,xlab="行业",ylab="频数",main="(a)垂直条形图")
barplot(d2,xlab="性别",ylab="频数",main="(a)垂直条形图")
barplot(d3,xlab="满意度",ylab="频数",main="(a)垂直条形图")
par(mai=c(0.7,0.7,0.1,0.8),cex=0.8)
x<-sort(d1,decreasing=T)
bar<-barplot(d1,xlab="行业",ylab="频数",ylim=c(0,1.2*max(d1)),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)
data_1<-table(data$满意度,data$行业)
par(mfrow=c(2,2),cex=0.6)
bar1<-barplot(data_1,xlab="行业",ylab="频数",ylim=c(0,30),col=c("red","green"),legend=rownames(data_1),args.legend=list(x=12),beside=TRUE,main="(a)行业并列条形图")
library(vcd)
spine(行业~满意度,data=data,xlab="满意度",ylab="行业",margins=c(4,3.5,1,2.5))
load("D:\2020文件夹\《统计学素材—基于R》(第3版)—例题和习题数据(公开资源)\exercise\ch2\第2章 习题CSV格式数据")
par(mai=c(0.4,0.4,0.2,0.1),cex=.9)
mosaicplot(~性别+行业+满意度,data=exercise2_1,color=2:3,main="")
count1<-table(data$行业)
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,radius=1)
count2<-table(data$社区)
name<-names(count2)
percent<-count2/sum(count2)*100
labs<-paste(name," ",percent,"%",sep="")
library(plotrix)
fan.plot(count2,labels=labs,ticks=200)
2.2
data<-read.csv("F:/统计学/实验作业/第二章数据/exercise2_2.csv")
data
vector2_2<-as.vector(data$灯泡寿命)
library(plyr)
count<-table(round_any(vector2_2,150,floor))
count
count<-as.numeric(count)
pcount<-prop.table(count)*100
cumsump<-cumsum(pcount)
name<-paste(seq(2700,4100,by=150),"-",seq(2850,4200,by=150),sep="")
name
gt<-data.frame("频数"=count,"频数百分比"=pcount,"累积频数百分比"=cumsump,row.names=name)
round(gt,4)
#直方图
d<-data$灯泡寿命
d
par(mfrow=c(2,2),cex=0.7,mai=c(0.6,0.6,0.2,0.1))
hist(d,xlim=c(2700,4200),ylim=c(0,30),labels=T,xlab ="灯泡寿命",ylab="频数",main="(a)普通")
rug(d)
lines(density(d),col="red",lwd=2)
hist(d,freq=FALSE,breaks=20,xlab="灯泡寿命",ylab="密度",main="增加正态密度线",col="pink")
curve(dnorm(x,mean(d),sd(d)),add=T,col="blue",lwd=2)
rug(jitter(d))
#茎叶图
data<-read.csv("F:/统计学/实验作业/第二章数据/exercise2_2.csv")
stem(data$灯泡寿命)