R 基础 课后 非参数统计.docx
- 文档编号:28276897
- 上传时间:2023-07-10
- 格式:DOCX
- 页数:10
- 大小:16.45KB
R 基础 课后 非参数统计.docx
《R 基础 课后 非参数统计.docx》由会员分享,可在线阅读,更多相关《R 基础 课后 非参数统计.docx(10页珍藏版)》请在冰豆网上搜索。
R基础课后非参数统计
#A.1
AGE=c(18,23,22,21,20,19,20,20,20)
first3=sort(AGE)[(length(AGE)-2):
length(AGE)]
delete.1=order(AGE)[(length(AGE)-2):
length(AGE)]
except=AGE[-delete.1]
c(except[1:
2],19,except[3:
length(except)])
except[2]=22
#A.2
a1=rep(1:
3,rep(2,3))
a2=c(1,8,10,11)
a3=seq(1,30,length(a2))
a4=seq(1,5,2)
a4=c(a1,a4,rep(0,2))
a5=2:
10
a6=c(a2,a3[-(1:
3)],a4)
a7=c(rep(1,10),rep(0,8))
#1.3
library(MASS)
data(geyser)
a=geyser
a1=subset(a,waiting<70)
a1=geyser[geyser$waiting<70,]#两个a1等价
a2=subset(a,waiting<70&waiting!
=57)
a2=geyser[geyser$waiting<70&geyser$waiting!
=57,]#两个a2等价
a3=subset(a,waiting<70,c(duration))
a4=subset(a,duration>70,c(waiting))
#A.3
library(MASS)
a=geyser
attach(a)
b1=waiting[waiting<70]
b2=waiting[waiting<70&waiting!
=57]
b3=duration[waiting<70]
b4=waiting[duration>70]
#1.4
x=c(0,1,1,2,3,4)
num=function(x){
r=0
p=c(rep(0,length(x)-1))
q=c(rep(0,length(x)-1))
for(iin1:
(length(x)-1))
{
for(jin(i+1):
length(x)){
p[i]=p[i]+I(x[i] q[i]=q[i]+I(x[i]>x[j]) } r=r+p[i]-q[i] } r } h=rep(0,1000) for(iin1: length(h)){ x=runif(5,-5,5) h[i]=num(x) } y=as.factor(h) yy=levels(y) a=rep(0,11)#记数记录-10: 10: 2各种结果出现的次数 p=rep(0,11) for(iin1: 11){ a[i]=sum(h==(-12+2*i)) p[i]=a[i]/length(h) } a p hist(h) h=rep(0,10000) for(iin1: length(h)){ x=sample(1: 5,5,replace=T)#-5到5中间的整数随机抽样 h[i]=num(x) } y=as.factor(h) yy=levels(y) a=rep(0,length(yy))#记数记录-10: 10各种结果出现的次数 p=rep(0,length(yy)) for(iin1: length(yy)){ a[i]=sum(h==(-11+i)) p[i]=a[i]/length(h) } a p hist(h) #当随机取10000次的一个结果a=[713217741255163718251684125674333896] #p=[0.00710.03210.07740.12550.16370.18250.16840.12560.07430.03380.0096] #当随机取十万次数据的一个结果 #a=[79534217553125211677118180165381255374183400850] #p=[0.007950.034210.075530.125210.167710.181800.165380.125530.074180.034000.00850] #1.5 uniroot(f=function(x)2*x^3-4*x^2+3*x-6,interval=c(-10,10)) f=function(x){2*x^3-4*x^2+3*x-6} f(0) a=-10 b=10 root=function(a,b) { c=(a+b)/2; while(abs(f(c))>0.00001){ if(f(c)*f(a)<0){b=c;c=(a+b)/2;} else{a=c;c=(a+b)/2;}} c #1.6 x=seq(0,2*pi,0.2) y=sin(x)/(cos(x)+x) #1.7 chartonum=function(x){ a=c("abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ"); b=strsplit(a,""); for(iin1: 52){ if(b[[1]][i]==x){t=i;i=i+1} else{i=i+1} } t }#将字符转化为数字,小写为前26位,大写为后26位,输入为单个字符 f7=function(x){ y=strsplit(x,"");#将输入分为单个字符 for(iin1: length(y[[1]])){ t=chartonum(y[[1]][i]) if(t<14){t=t+13;y[[1]][i]=LETTERS[t];} elseif(t>=14&t<=26){t=t-13;y[[1]][i]=LETTERS[t];} elseif(t>=27&t<=39){t=t+13-26;y[[1]][i]=letters[t];} else{t=t-13-26;y[[1]][i]=letters[t];} } mima=y; mima } #1.8 f1.81=function(a){ for(iin1: length(a)) {c[i]=a[length(a)-i+1]} c } f1.82=function(a,b){ for(iin1: (length(a)+length(b))){ if(i%%2==1){c[i]=a[(i+1)/2];} else{c[i]=b[i/2];} } c }#%%为求余符号 #1.9 f1.9=function(n,m){ a=rep(1,n); i=1; k=0; t=1; while(sum(a)>1|a[t]==0){ t=i%%n; if(t==0){t=n;} else{t=i%%n} if(a[t]! =0){ k=k+1;i=i+1;} else{i=i+1} if(k%%m==0){k=0;a[t]=0;} } t } #1.10 student<-read.table("C: \\DocumentsandSettings\\Administrator\\桌面\\非参数统计配套数据\\各章数据\\第一章\\student.txt",header =T) student1=as.data.frame(student) means=apply(student1[,2: 6],1,mean) b1=data.frame(student1,means) b2=student1[which(b==max(b)),] b3=I(student1[,2: 6]<60)#判断每个学生每门课程是否及格 b4=apply(b3,1,sum)#找出每个学生有几门课程不及格 b5=b1[which(b4>1),] b6=b1[-which(b4>1),] t.test(b5$means,b6$means) #不说明情况下认为两总体方差不同,特殊说明var.equal=TRUE置信区间为均值差的置信区间 wilcox.test(b5$means,b6$means) wilcox.test(b5$means,b6$means,paires=FALSE) #1.11 basket<-read.table("C: \\DocumentsandSettings\\Administrator\\桌面\\非参数统计配套数据\\各章数据\\第一章\\basket.txt",header =T) A=I(basket[,2: 6]=='A') A=basket[,2: 6]=='A' Asum=apply(A,1,sum) B=basket[,2: 6]=='B' Bsum=apply(B,1,sum) a1=basket[which(Asum>0&Bsum>0),1] length(a1)#求个数 a2=basket[which(Asum>0&Bsum>=3),] length(a2$ID)#求个数 #1.12 x=seq(-10,10,0.05) y1=sin(x) y2=cos(x) y3=y1+y2 plot(x,y1,col=1,lty=1,axes=T,main="习题1.12",sub="sin(),cos(),sin()+cos()对比图") points(x,y2,col=2,lty=3) points(x,y3,col=3,lty=4) plot(x,y1,col=1,lty=1,main="习题1.12",sub="sin(),cos(),sin()+cos()对比图") lines(x,y1,col=1,lty=1) lines(x,y2,col=2,lty=3) lines(x,y3,col=3,lty=4) curve(sin(x),-10,10,col=1,lty=1,main="习题1.12",sub="sin(),cos(),sin()+cos()对比图") curve(cos(x),add=TRUE,col=2,lty=3) curve(sin(x)+cos(x),add=TRUE,col=3,lty=4) #1.13 x=seq(-5,5,length=50) a=runif(500,-5,5) y=0.1*a*sin(2*a) f1=function(x,y){1-exp(-1/x^2+y^2)} z1=outer(x/2,x/2,f1) persp(z1) f2=function(x,y){0.1*x*sin(2*y)} z2=outer(x,x,f2) persp(z2) f3=function(x,y){sin(x)+cos(x)} z3=outer(x,x,f3) persp(z3) plot(sin(3*x),sin(6*x),type="l") #1.14 a=rnorm(100,3,sqrt(5)) b=rnorm(20,5,sqrt(3)) c=c(a,b) hist(c) boxplot(c) qqnorm(c) #1.15 x=rnorm(100,0,1) y1=log(abs(x))#lamda=0 hist(y1) qqnorm(y1) qqline(y1,col=2) y2=(x-1)#lamda=1 hist(y2) qqnorm(y2) qqline(y2,col=3) y3=1-1/x#lamda=-1 hist(y3) qqnorm(y3) qqline(y2,col=4)
- 配套讲稿:
如PPT文件的首页显示word图标,表示该PPT已包含配套word讲稿。双击word图标可打开word文档。
- 特殊限制:
部分文档作品中含有的国旗、国徽等图片,仅作为作品整体效果示例展示,禁止商用。设计者仅对作品中独创性部分享有著作权。
- 关 键 词:
- 基础 课后 非参数统计 参数 统计