7 Confidence Interval

library(UsingR)
## Warning: package 'UsingR' was built under R version 3.1.2
## Loading required package: MASS
## Loading required package: HistData
## Warning: package 'HistData' was built under R version 3.1.2
## Loading required package: Hmisc
## Warning: package 'Hmisc' was built under R version 3.1.2
## Loading required package: grid
## Loading required package: lattice
## Loading required package: survival
## Loading required package: splines
## Loading required package: Formula
## Warning: package 'Formula' was built under R version 3.1.2
## 
## Attaching package: 'Hmisc'
## 
## The following objects are masked from 'package:base':
## 
##     format.pval, round.POSIXt, trunc.POSIXt, units
## 
## Loading required package: quantreg
## Warning: package 'quantreg' was built under R version 3.1.2
## Loading required package: SparseM
## Warning: package 'SparseM' was built under R version 3.1.2
## 
## Attaching package: 'SparseM'
## 
## The following object is masked from 'package:base':
## 
##     backsolve
## 
## 
## Attaching package: 'quantreg'
## 
## The following object is masked from 'package:Hmisc':
## 
##     latex
## 
## The following object is masked from 'package:survival':
## 
##     untangle.specials
## 
## 
## Attaching package: 'UsingR'
## 
## The following object is masked from 'package:survival':
## 
##     cancer
data(age.universe)
head(age.universe)
##     lower upper year
## 1 7.5e-05    NA 1760
## 2 2.4e-01    NA 1831
## 3 1.0e+00    10 1852
## 4 2.0e-02    NA 1897
## 5 2.0e+00    NA 1929
## 6 1.0e+01    15 1970
##                                                           source
## 1               Buffon. time of cooling from molten state. Earth
## 2                                  Lyell. age of mollusks. Earth
## 3                                         Bok. galactic clusters
## 4 Thomson. Thermodynamics of earth. Min is between 20-400million
## 5                                            E. Hubble. Redshift
## 6                                                        various
View(age.universe)
pop<- span=""> rep(0:1,c(10000-5600,5600))
phat<- span=""> mean(sample(pop,100))
phat
## [1] 0.57
phat<- span=""> mean(sample(pop,100))
phat
## [1] 0.63
res=c()
for(i in 1:1000) res[i]=mean(sample(pop,100))
quantile(res,c(.1,.9))# 80% of the time
##  10%  90% 
## 0.50 0.62
quantile(res,c(.1,.95))# 90% of the time
##  10%  95% 
## 0.50 0.64
quantile (res, c (0.025,0.975)) # 95% of the time
##  2.5% 97.5% 
##  0.47  0.66
#zstar<- -qnorm="" 1="" 560="" 570="" 7.8:="" 95="" a="" agree.="" agree="" alpha="" and="" are="" by="" comparing="" confidence="" difference="" do="" find="" first="" for="" hand="" in="" interval.="" interval="" interviewed="" is="" le="" let="" of="" people="" poll="" prop.test="" proportions.rather="" results="" same="" second="" span="" taken.="" than="" the="" time="" two="" we="" weeks="" work="" xample="">
#Example 7.4: Average height Students in a class of 30 have an average height of 66 inches, with a standard deviation of 4 inches. Assume that these heights are normally distributed, and that the class can be considered a random sample from the entire college population. What is an 80% confidence interval for the mean height of all the college students? Our assumptions allow us to apply the confidence interval for the mean
xbar<- span=""> 66; s=4; n=30
alpha<- span=""> .2
tstar<- span=""> qt(1-alpha/2,df=n-1)
tstar
## [1] 1.311434
SE<- span=""> s/sqrt(n)
c(xbar-tstar*SE,xbar+tstar*SE)
## [1] 65.04226 66.95774
##■ Example 7.5: Making coffee A barista at “t-test espresso” has been trained to set the bean grinder so that a 25-second espresso shot results in 2 ounces of espresso. Knowing that variations are the norm, he pours eight shots and measures the amounts to be 1.95,1.80, 2.10, 1.82, 1.75, 2.01, 1.83, and 1.90 ounces. Find a 90% confidence interval for the mean shot size. Does it include 2.0? As we have the data, we can use t.test() directly. We enter in the data, verify normality (with a quantile-quantile plot), and then use t.test():
ozs<- span=""> c(1.95, 1.80, 2.10, 1.82, 1.75, 2.01, 1.83,1.90)
qqnorm(ozs)
## approximately linear
t.test(ozs,conf.level=.80)
## 
##  One Sample t-test
## 
## data:  ozs
## t = 45.253, df = 7, p-value = 6.724e-10
## alternative hypothesis: true mean is not equal to 0
## 80 percent confidence interval:
##  1.835749 1.954251
## sample estimates:
## mean of x 
##     1.895
##7.3.1 One-sided confidence intervals
# Example 7.6: Serving coffee
#The barista at "t-test espresso" is told that the optimal serving temperature for coffee is 180°F. Five temperatures are taken of the served coffee:175, 185, 170, 184, and 175 degrees. Find a 90% confidence interval of the form (-∞, b] for the mean temperature.Using t.test() with alt="less" will give this type of one-sided confidence interval
x = c(175, 185, 170, 184, 175)

t.test(x,conf.level=.9,alt='less')
## 
##  One Sample t-test
## 
## data:  x
## t = 61.5671, df = 4, p-value = 1
## alternative hypothesis: true mean is less than 0
## 90 percent confidence interval:
##      -Inf 182.2278
## sample estimates:
## mean of x 
##     177.8
lstar<- span=""> qchisq(alpha/2,df=n-1)

Nikkei225

28000-28550 up in the early session, down lately.