您好,欢迎访问三七文档
实验六重要的参数检验与功效检验【实验类型】验证性【实验学时】2学时【实验目的】1、掌握假设检验的基本思想;2、掌握重要的参数检验及功效检验的求解方法;3、了解非参数假设检验的基本思想及求解方法。【实验内容】1、参数检验(t检验、F检验、二项分布检验和泊松检验等)的计算;2、功效检验的计算;3、非参数检验(符号与秩检验、分布的检验、相关性检验等)的求解。【实验方法或步骤】第一部分、课件例题:#1例6.2X-c(159,280,101,212,224,379,179,264,222,362,168,250,149,260,485,170)t.test(X,alternative=greater,mu=225)#单侧检验由于p值(=0.257)0.05,不能拒绝原假设,接受H0,即认为平均寿命不大于225h#2例6.3X-c(78.1,72.4,76.2,74.3,77.4,78.4,76.0,75.5,76.7,77.3)Y-c(79.1,81.0,77.3,79.1,80.0,79.1,79.1,77.3,80.2,82.1)t.test(X,Y,al=l,var.equal=T)#H1:μ1-μ20t.test(X,Y,al=l)#使用总体方差不同模型t.test(X,Y,al=l,paired=TRUE)#配对数据检验##公式形式obtain-data.frame(value=c(78.1,72.4,76.2,74.3,77.4,78.4,76.0,75.5,76.7,77.3,79.1,81.0,77.3,79.1,80.0,79.1,79.1,77.3,80.2,82.1),group=gl(2,10))#生成2水平、各有10个元素的因子向量t.test(value~group,data=obtain,alternative=less,var.equal=TRUE)由于p值(=0.000218)0.05,拒绝原假设,即接受H1,再利用μ1-μ2的置信区间,可以说明新操作方法能够提高得率。#3例6.4X-c(78.1,72.4,76.2,74.3,77.4,78.4,76.0,75.5,76.7,77.3)Y-c(79.1,81.0,77.3,79.1,80.0,79.1,79.1,77.3,80.2,82.1)var.test(X,Y)#方差相等的F检验##使用公式形式obtain-data.frame(value=c(78.1,72.4,76.2,74.3,77.4,78.4,76.0,75.5,76.7,77.3,79.1,81.0,77.3,79.1,80.0,79.1,79.1,77.3,80.2,82.1),group=gl(2,10))#生成2水平、各有10个元素的因子向量var.test(value~group,data=obtain)由于p值(=0.559)0.05,无法拒绝原假设,所以认为两总体的方差是相同的。从方差比的置信区间[0.37,6.02]来看,它包含1,因此,在例6.3中,假设两总体方差相同是合理的#4例6.5prop.test(400,10000,p=0.02)#比率(成功的概率)检验#5例6.6n-c(2000,1500);x-c(652,576)prop.test(x,n)#双样本的比率检验#6例6.7X-matrix(c(15,32,10,42,42,59),nrow=2,byrow=T)colnames(X)-c(30s,24s,20s)rownames(X)-c(Yes,No)XX.yes-X[Yes,]X.total-margin.table(X,2)#计算表格的列和(参数1为行和)prop.test(X.yes,X.total)#三样本的比率检验pairwise.prop.test(X.yes,X.total)#比率的多重比较#7例6.8binom.test(3,100,p=0.01,al=g)#二项分布检验(单边)#8例6.9poisson.test(x=12,T=1.2,r=5,al=g)#Poisson分布检验(单边)#9例6.10##t检验X-c(1050,960,1120,1250,1280);mu0-1000t.test(X,mu=mu0,al=g)#原假设H0:μ1≤μ0=1000##计算功效power.t.test(n=length(X),delta=mean(X)-mu0,sd=sd(X),type=one.sample,alternative=one.side)##计算给定功效下的试验次数power.t.test(power=0.90,delta=mean(X)-mu0,sd=sd(X),type=one.sample,alternative=one.side)#10例6.12power.prop.test(power=0.9,p1=652/2000,p2=576/1500)#11例6.13binom.test(3,12,al=l,conf.level=0.9)#12例6.14X-scan(F:/文档/大学课程/R语言/ch06/city.data)#读取数据prop.test(sum(X99),length(X))#13例6.15X-scan(F:/文档/大学课程/R语言/ch06/battery.data)wilcox.test(X,mu=140,exact=F,conf.int=T)binom.test(sum(X140),n=19)#n=19是去掉了M=140的项#14例6.16x-rep(1:4,c(62,41,14,11));y-rep(1:4,c(20,37,16,15))wilcox.test(x,y,conf.int=TRUE)#15例6.17#计算样本均值与样本标准差,并作为总体参数的估计Z-scan(F:/文档/大学课程/R语言/ch06/exam.data);mu-mean(Z);S-sd(Z)#将整个实轴划分成8个子区间p-seq(from=0.125,to=0.875,by=0.125)q-qnorm(p,mean=mu,sd=S);q#计算落入每个子区间的实际频数Y-table(cut(Z,breaks=c(-Inf,q,Inf)));Y#作拟合优度检验F-pnorm(q,mean=mu,sd=S);m-length(Y)p-F[1];p[m]-1-F[m-1]for(iin2:(m-1))p[i]-F[i]-F[i-1](chi-chisq.test(Y,p=p))#因用样本值代替总体参数,自由度减少2,需重新计算p值Pval-1-pchisq(chi$statistic,df=m-3)names(Pval)-P_val;Pval#16例6.18X-scan(F:/文档/大学课程/R语言/ch06/exam.data)shapiro.test(X)#17例6.19x-matrix(c(60,3,32,11),nc=2);chisq.test(x)#18例6.20x-matrix(c(4,5,18,6),nc=2);fisher.test(x)#19例6.21X-array(c(19,0,132,9,11,6,52,97),dim=c(2,2,2))mantelhaen.test(X)#20例6.22rt-read.table(F:/文档/大学课程/R语言/ch06/weight.data);rtwith(rt,cor.test(X,Y))cor.test(~X+Y,data=rt)#公式形式#21例6.23X-c(86,77,68,91,70,71,85,87,63)Y-c(88,76,64,96,65,80,81,72,60)cor.test(X,Y,method=kendall)#22例6.24library(tseries)X-scan(what=)#注意符号(共40)输入完成后回车一下++-+---+-+++--+-++---+--+----++++--+-++-runs.test(as.factor(X))第二部分、教材例题:#1例6.2.1#z.test()函数定义z.test-function(x,n,sigma,alpha,u0=0,alternative=two.sided){options(digits=4)result-list()mean-mean(x)z-(mean-u0)/(sigma/sqrt(n))p-pnorm(z,lower.tail=FALSE)result$mean-meanresult$z-zresult$p.value-pif(alternative==two.sided){p-2*presult$p.value-p}elseif(alternative==greater|alternative==less){result$p.value-p}elsereturn(yourinputiswrong)result$conf.int-c(mean-sigma*qnorm(1-alpha/2,mean=0,sd=1,lower.tail=TRUE)/sqrt(n),mean+sigma*qnorm(1-alpha/2,mean=0,sd=1,lower.tail=TRUE)/sqrt(n))result}z.test(0.13,25,0.1,0.05,u0=0.12,alternative=less)#2例6.2.2salt-c(490,506,508,502,498,511,510,515,512)t.test(salt,mu=500)#3例6.2.3CaCo3-c(20.9,20.41,20.10,20.00,20.19,22.60,20.99,20.41,20,23,22)t.test(CaCo3,mu=20.7)#4例6.2.4#函数chisq.var.test()可以用来求σ2置信区间chisq.var.test-function(x,var,alpha,alternative=two.sided){options(digits=4)result-list()n-length(x)v-var(x)result$var-vchi2-(n-1)*v/varresult$chi2-chi2p-pchisq(chi2,n-1)if(alternative==less|alternative==greater){result$p.value-p}elseif(alternative==two.sided){if(p.5)p-1-pp-2*presult$p.value-p}elsereturn(yourinputiswrong)result$conf.int-c((n-1)*v/qchisq(alpha/2,df=n-1,lower.tail=F),(n-1)*v/qchisq(alpha/2,df=n-1,lower.tail=T))result}time-c(42,65,75,78,59,71,57,68,54,55)chisq.var.test(time,80,0.05,alternative=less)#5例6.3.1x-c(20.5,19.8,19.7,20.4,20.1,20.0,19.0,19.9)y-c(20.7,19.8,19.5,20.8,20.4,19.6,20.2)t.test(x,y,var.equal=TRUE)#6例6.3.2x-c(20.5,19.8,19.7,20.4,20.1,20.0,19.0,19.9)y-c(20.7,19.8,19.5,20.8,20.4,19.6,20.2)var.test(x,y)#7例6.4.1x-c(20.5,18.8,19.8,20.9,2
本文标题:R语言实验六
链接地址:https://www.777doc.com/doc-4476058 .html