X=scan()
159 280 101 212 224 379 179 264
222 362 168 250 149 260 485 170
#H
0
:
µ
≤
µ
0
= 225
, H
1
:
µ > µ
0
= 225
.
X<-c(159, 280, 101, 212, 224, 379, 179, 264,
222, 362, 168, 250, 149, 260, 485, 170)
mu=225,side=1 #右侧检验
#
mean.test1<-function(x, mu=0, sigma=-1, side=0)
n<-length(X); xb<-mean(X)
#方差未知 t检验
t<-(xb-mu)/(sd(X)/sqrt(n))
#
P_value<-function(cdf, x, paramet=numeric(0), side=0)
#cdf即pt #t分布函数
#向量为t #t value
paramet=n-1 #=length(X)-1
side=side #=1
#{
#n<-length(paramet)
#n<-length(paramet)
#P<-switch( n+1, cdf(x), cdf(x, paramet),cdf(x, paramet[1], paramet[2]),cdf(x, paramet[1], paramet[2], paramet[3]) )
#if (side<0) P
#else if (side>0) 1-P
#else
#if (P<1/2) 2*P
#else 2*(1-P)
#}
#n=length(paramet) #=1
#switch(2,list)
#P=pt(t,length(X)-1) #cdf=pt, cdf(x,paramet)即pt(t,n-1=length(X)-1)
#side=1>0 取1-P
#最终取P=1-P
P=1-pt(t,n-1)
data.frame(mean=xb, df=n-1, T=t, P_value=P)
#所得结果等同于函数运算结果:
> mean.test1(X, mu=225, side=1)
mean df T P_value
1 241.5 15 0.6685177 0.2569801