【R】均值假设检验

时间:2022-01-19 17:41:02
p_value<-function(cdf,x,parament=numeric(0),side=0)
{
n<-length(parament) p<-switch(n+1,
cdf(x),
cdf(x,parament),
cdf(x,parament[1],parament[2]),
cdf(x,parament[1],parament[2],parament[3])
) if(side<0) p
else if (side>0) 1-p
else
if(p<0.5) 2*p
else 2*(1-p)
} mean.test1<-function(x,mu=0,sigma=-1,side=0)
{
n<-length(x)
xb<-mean(x)
if(sigma>0)
{
z<-(xb-mu)/(sigma/sqrt(n))
p<-p_value(pnorm,z,side=side)
data.frame(mean=xb,df=n,Z=z,p_value=p)
}
else
{
t<-(xb-mu)/(sd(x)/sqrt(n))
p<-p_value(pt,t,parament=n-1,side=side)
data.frame(mean=xb,df=n-1,T=t,p_value=p)
}
}