1 回答

TA貢獻1790條經(jīng)驗 獲得超9個贊
1、 方差已知時的均值估計
z.test<-function(x,n,sigma,a,u0,alt){
result<-list()
mean<-mean(x)
result$interval<-c(mean-sigma*qnorm(1-a/2,0,1)/sqrt(n),mean+sigma*qnorm(1-a/2,0,1)/sqrt(n))
z<-(mean-u0)/(sigma/sqrt(n))
p<-pnorm(z,lower.tail=F) #函數(shù)筆記:lower.tail是真的話,得出的就是X<=x的分位數(shù),為假的話就是用P(X>x)的辦法尋找這個值。一般我們用默認的真就可以了
result$z<-z
result$p.value<-p #通過P值判定參數(shù)估計效果
if(alt==2)
reslut$p.value<-2*pnorm(abs(z),lower.tail=F)
else
reslut$p.value<-pnorm(z)
reslut#函數(shù)筆記:如果函數(shù)的結果需要有多個返回值,可以創(chuàng)建一個list(),并返回該對象。也可以用return()函數(shù),設定返回值。但是一個函數(shù)的返回的對象只有一個。
}
2、 方差未知時的均值估計
在小樣本中,我們通常使用R的內置函數(shù)t.test()調用格式:
t.test(x, y = NULL,
alternative = c("two.sided", "less","greater"),
mu = 0, paired = FALSE, var.equal = FALSE,
conf.level = 0.95, )
對于大樣本,我們可以使用樣本方差代替總體方差,使用z.test()處理
3、 方差的區(qū)間估計
chisq.var.test<-function(x,n,a,alt=2,sigma0=1)
{
result<-list()
v<-var(x)
result$interval<-c((n-1)*v/qchisq(1-a/2,n-1,lower.tail=T),(n-1)*v/qchisq(a/2,n-1,lower.tail=T))
chi2<-(n-1)*v/sigma0
result$chi2<-chi2
p<-pchisq(chi2,n-1)
if(alt==2)
result$p.value<-2*min(pchisq(chi2,n-1),pchisq(chi2,n-1,lower.tail=F))
else
result$p.value<-pchisq(chi2,n-1,lower.tail=F)
result
}
- 1 回答
- 0 關注
- 6418 瀏覽
添加回答
舉報