# Unit 2 for R # 030515 prepared by C. Andy Tsao # Using R for finding CI and perform hypotheses testing # for one normal popopulation (with variance known/unknown) # We will use the SimpleR package (Joh Verzani) # It is highly recommend you have his SimpleR.pdf handy # You can refer to the chapters of confidence intervals (p59-65), # hypotheses testing (p66-68). # Install (first time only) then load "Simple" # Input data # x<-c(36,37,34,38,39,36) theta<-36.5;sigma<-1; x<-rnorm(6,theta,sigma) hist(x) xbar<-mean(x);s<-sd(x);n<-length(x); # For testing H_0: \theta \leq theta0 vs H_1: \theta > \theta0 theta0<- 36; sigma<-1; z<-(xbar-theta0)/(sigma/sqrt(n)) t<-(xbar-theta0)/(s/sqrt(n)) pvz<-1-pnorm(z) pvt<-1-pt(t,n-1) c(xbar,sigma,s,pvz,pvt) # For testing H_0: \theta \geq theta0 vs H_1: \theta < \theta0 theta0<- 36; sigma<-1; z<-(xbar-theta0)/(sigma/sqrt(n)) t<-(xbar-theta0)/(s/sqrt(n)) pvz<-pnorm(z); pvt<-pt(t,n-1); c(xbar,sigma,s,pvz,pvt) # For testing H_0: \theta = theta0 vs H_1: \theta \neq \theta0 theta0<- 36; sigma<-1; z<-(xbar-theta0)/(sigma/sqrt(n)) t<-(xbar-theta0)/(s/sqrt(n)) pvz<-2*pnorm(-abs(z)); pvt<-2*pt(-abs(t),n-1); c(xbar,sigma,s,pvz,pvt) simple.z.test(x,sigma,0.9) simple.z.test(x,sigma,0.95) t.test(x,alternative = c("two.sided"),mu = theta0,conf.level = 0.95) t.test(x,alternative = c("greater"),mu = theta0,conf.level = 0.9)