5.2apply函数应用
- 1、下载文档前请自行甄别文档内容的完整性,平台不提供额外的编辑、内容补充、找答案等附加服务。
- 2、"仅部分预览"的文档,不可在线预览部分如存在完整性等问题,可反馈申请退款(可完整预览的文档不适用该条件!)。
- 3、如文档侵犯您的权益,请联系客服反馈,我们会尽快为您处理(人工客服工作时间:9:00-18:30)。
UG$birth=as.numeric(UG$birth)
UG1=UG[,6:9]
apply(UG1,2,mean)#对阵列行或者列使用函数
lapply(UG1,mean) ##对列表或者向量使用函数
x=list(a = 1:10, beta = exp(-3:3), logic = c(TRUE,FALSE,FALSE,TRUE))
lapply(x,mean)
lapply(UG1,function(x) list(mean=mean(x),sd=sd(x)))
##sapply(x, f, simplify=FALSE, S=FALSE)返回的值与lapply(x,f)是一致的。
sapply(UG1,function(x) list(mean=mean(x),sd=sd(x)))
k=c("a","b")
sapply(k, paste,S=FALSE,1:5,sep="...") #返回向量或矩阵
#对不规则阵列使用向量,即对一组非空值按照一组确定因子进行相应计算
#tapply(X, INDEX, FUN, ..., simplify = TRUE)
height <- c(174, 165, 180, 171, 160)
sex<-c("F","F","M","F","M")
tapply(height, sex, mean)
tapply(UG$height,INDEX=UG$sex,FUN=mean)
aggregate(UG1,by=list(UG$sex),FUN=mean)
aggregate(UG1,by=list(sex=UG$sex,region=UG$region),FUN=mean)
######################第五章 随机变量及其分布#######################################################
par(mar=c(4,4,2,1)+0.2,cex=0.8)
n=100;p=0.8
x=1:50
P=choose(n,x)*p^x*(1-p)^(n-x)
plot(x,P,'h')
n=10;p=0.5
x=0:10
dbinom(x,n,p)
round(dbinom(x,n,p),4)
round(pbinom(x,n,p),4)
N=100;M=5;n=10
x=1:5
P=choose(M,x)*choose(N-M,n-x)/choose(N,n)
plot(x,P,'h')
lamda=6
x=0:5
P=lamda^x*exp(-lamda)/factorial(x)
plot(x,P,'h')
x=0:1;y=c(1,1)
plot(x,y,ylim=c(0,1.5),type='l')
x=seq(-4,4,0.1)
y=1/sqrt(2*pi)*exp(-x^2/2)
plot(x,y,type='l')
a=qnorm(0.95);a
x0=4
z=seq(-2,2,0.1)
x=seq(-x0,x0,0.1)
plot(x,dnorm(x),xlim=c(-x0,x0),ylim=c(0,0.5),type="l",xlab="x",ylab="p(x)")
abline(h=0,lty=3)
a=160
Za=(a-mean(UG$height))/sd(UG$height);Za
Pa=pnorm(Za);Pa
b=180
Zb=(mean(b-UG$height))/sd(UG$height);Zb
Pb=1-pnorm(Zb);Pb
P=1-Pa-Pb;P
x=0:50
y=0.3*exp(-0.3*x)
plot(x,y,type='l')
rnorm(10)
y0=dnorm(0);y0 #y=1/sqrt(2*pi)*exp(0)
dnorm(1) == exp(-1/2)/sqrt(2*pi)
y1=dnorm(1);y1 #y1=1/sqrt(2*pi*exp(1))
p1=pnorm(-1.96);p1
p2=pnorm(1.96);p2
q1=qnorm(0.05);q1
q2=qnorm(0.95);q2
rbinom(10,1,0.5)
rbinom(10,5,0.35)
par(mfrow=c(1,3))
p=0.25
for(n in c(10,20,50)){
x=rbinom(100,n,p)
hist(x,prob=T,main=paste("n =",n))
xn=0:n
points(xn,dbinom(xn,n,p),type="h",lwd=3)
}
par(mfrow=c(1,1))
rhyper(15,10,5,3)
m=10; n=7; k=8
rx=rhyper(15,m,n,k);rx
x=0:(k+1)
px=phyper(x,m,n,k)
dx=dhyper(x,m,n,k)
cbind(px,dx)
par(mfrow=c(1,3))
m=10;n=5;k=3
for(n in c(10,20,50)){
x=rhyper(100,m,n,k);x
hist(x,prob=T,main=paste("n =",n))
xn=0:n
points(xn,dhyper(xn,m,n,k),type="h",lwd=3)
}
par(mfrow=c(1,1))
rpois(10,lambda=4)
runif(5,0,1) # 生成5个[0,1]的均匀分布的随机数
runif(3,1,3) # 生成3个[1,3] 的均匀分布的随机数
runif(5) # 默认生成[0,1]上的均匀分布随机数
x=runif(10000)
hist(x,prob=T,ylim=c(0,1.5),main='uniform(0,1)')
curve(dunif(x,0,1),add=T) #添加均匀分布的密度函数线
x=rnorm(1000)
hist(x,prob=T,ylim=c(0,0.5),,main='N(0,1)')
curve(dno
rm(x),add=T)
x=rexp(1000,1/10) # 生成100个均值为10的指数分布随机数
hist(x,prob=T,ylim=c(0,0.1),main="exp(1/10)")
curve(dexp(x,1/10),add=T)
sample(c("H","T"),10,rep=TRUE)
sample(100,10)
sample(100,10,rep=TRUE)
i=sample(48,10);i #抽取学生序号
UG$id[i] #学生编号
UG$name[i] #学生姓名
UG[i,1:2]
x=seq(0,20,0.1)
curve(dchisq(x,2),0,20,ylab="p(x)")
curve(dchisq(x,4),add=T,lty=2)
curve(dchisq(x,6),add=T,lty=3)
curve(dchisq(x,8),add=T,lty=4)
curve(dchisq(x,10),add=T,lty=5)
legend(13,0.4,c("n=2","n=4","n=6","n=8","n=10"),lty=1:5,bty="n")
x=seq(-4,4,.01)
plot(x,dnorm(x),type="l",lty=1)
for(i in c(1,5,10))
points(x,dt(x,df=i),type="l",lty=i+1)
legend(2,0.3,c("N(0,1)","t(10)","t(5)","t(1)"),lty=1:4,bty="n")
x=seq(0,6,0.1)
plot(x,df(x,3,3),type="l",ylim=c(0,1.2),ylab="p(x)")
curve(df(x,5,5),0,6,add=T)
curve(df(x,10,10),0,6,add=T)
curve(df(x,20,20),0,6,add=T)
curve(df(x,30,30),0,6,add=T)
n=10; p=0.25
z=rbinom(1,n,p)
x=(z-n*p)/sqrt(n*p*(1-p)); x
m =100 # m 模拟次数
n = 10; p = 0.25
z = rbinom(m,n,p) # 产生100个二项随机数
x = (z-n*p)/sqrt(n*p*(1-p)) # 对100个二项随机数标准化
hist(x,prob=T,main=paste("n =",n))
curve(dnorm(x),add=T) # 增加正态曲线
sim.clt<-function(m=100,n=10,p=0.25){
z = rbinom(m,n,p)
x = (z-n*p)/sqrt(n*p*(1-p))
hist(x,prob=T,breaks=20,main=paste("n =",n,"p =",p))
curve(dnorm(x),add=T)
}
par(mar=c(4,4,2,1),mfrow=c(2,2),cex=0.8)
sim.clt() # 默认 m=100,n=10,p=0.25
sim.clt(1000) # 取 m=1000,n=10,p=0.25
sim.clt(1000,30) # 取 m=1000,n=30,p=0.25#图中是否加入m取值结果?
sim.clt(1000,30,0.5) # 取 m=1000,n=30,p=0.5
par(mfrow=c(1,1))
u0=seq(0,3,by=0.1);u0 #临界表的行值
u.0=seq(0,0.1,by=0.01);u.0 #临界表的列值
u=u0+matrix(u.0,31,11,byrow=T);u #临界表分位数
p=pnorm(u);p #临界表概率值
colnames(p)<-u.0 #临界表列标记
rownames(p)<-u0 #临界表行标记
p #形成正态分布临界表
a=c(0.01,0.025,0.05,0.1,0.5,0.95,0.975,0.99);a #尾部概率
n=2:30 #样本数
cbind(n,'0.01'=qt(0.01,n),'0.025'=qt(0.025,n),'0.05'=qt(0.05,n),'0.1'=qt(0.1,n),
'0.5'=qt(0.5,n),'0.95'=qt(0.95,n),'0.975'=qt(0.975,n),'0.99'=qt(0.99,n))
m=100;n=20;
x=rnorm(n);x
se=sqrt(p*(1-p)/n)
a=0.05; z=qnorm(1-a/2)
matplot(rbind(1:m,1:m),rbind(p-z*se,p+z*se),type="l",xlab='',ylab='')
abline(h=p0) # 画直线p0=0.5