代码是选修课《数据分析与统计》上附加题,可直接应用。欢迎纠错。
#*****************
#本程序利用R分布别做出单个、双个正态分布总体
#区间估计的均值和双侧、单侧置信区间
#包含4个函数
#code by Vac 2017.12.8
#*****************
#单个正态分布
#u的区间估计,sigma已知
onenorm_u_sigma<-function(X,sigma,alpha=0.99){
n<-length(X);
tmpx<-sigma*qnorm(1-alpha/2)/sqrt(n);
a<-mean(X)-tmpx;b<-mean(X)+tmpx;#双侧
cat("其",alpha*100,"%双侧置信区间是:[",a,",",b,"]","\n")
bu<-mean(X)+sigma*qnorm(1-alpha)/sqrt(n)#单侧上限
ad<-mean(X)-sigma*qnorm(1-alpha)/sqrt(n)#单侧下限
cat("其",alpha*100,"%单侧置信区间上限是:",bu,"\n下限是",ad,"\n")
}
#u的区间估计,sigma未知
onenorm_u_sd<-function(X,alpha=0.99){
n<-length(X);
tmpx<-sd(X)*qt(1-alpha/2,n-1)/sqrt(n);
a<-mean(X)-tmpx;b<-mean(X)+tmpx;#双侧
cat("其",alpha*100,"%双侧置信区间是:[",a,",",b,"]","\n")
bu<-mean(X)+sd(X)*qt(1-alpha,n-1)/