CallCenter<-read.csv("calls80 copy.csv",
header=TRUE)
head(CallCenter)
## length
## 1 77
## 2 289
## 3 128
## 4 59
## 5 19
## 6 148
hist(CallCenter$length)

mean(CallCenter$length)
## [1] 196.575
nsim=1000
bootStrapCI1<-function(data, nsim){
n<-length(data)
bootCI<-c()
for(i in 1:nsim){
bootSamp<-sample(1:n, n, replace=TRUE)
thisXbar<-mean(data[bootSamp])
bootCI<-c(bootCI, thisXbar)
}
return(bootCI)
}
# By hand
xbar<-mean(CallCenter$length) #196.575
sd<-sd(CallCenter$length) #342.0215
sd
## [1] 342.0215
n<-length(CallCenter$length) #80
n
## [1] 80
se<-sd/sqrt(n) #19.32248
se
## [1] 38.23917
mu_0<-130 # One-sided upper alternative I do not know why it is 130
test_stat<-(xbar-mu_0)/se
test_stat #1.74016
## [1] 1.741016
# Bootstrap confidence interval for the mean
lengthBootCI<-bootStrapCI1(CallCenter$length, nsim=1000)
# Approximated sampling distribution
hist(lengthBootCI)

qqnorm(lengthBootCI)
qqline(lengthBootCI)

#10 object sample new bootstrap
###104 102 35 211 56 325 67 9 179 59
SRS<-c(104, 102, 35, 211, 56, 325, 67, 179, 59)
# Function for bootstrap confidence interval
# for one sample mean
bootStrapCI1<-function(data, nsim){
n<-length(data)
bootCI<-c()
for(i in 1:nsim){
bootSamp<-sample(1:n, n, replace=TRUE)
thisXbar<-mean(data[bootSamp])
bootCI<-c(bootCI, thisXbar)
}
return(bootCI)
}
# By hand
xbarr<-mean(SRS) #126.44
sdd<-sd(SRS) #94.76565
nn<-length(SRS) #9
see<-sdd/sqrt(nn) #31.59955
mu_00<-130 # One-sided upper alternative
test_statss<-(xbarr-mu_00)/see
test_statss #-0.11255
## [1] -0.1125584
SRSBootCI<-bootStrapCI1(SRS, nsim=1000)
# Approximated sampling distribution
hist(SRSBootCI)

qqnorm(SRSBootCI)
qqline(SRSBootCI)

spines<-read.csv("nspines.csv",
header=TRUE)
head(spines)
## ns dbh
## 1 n 27.8
## 2 n 14.5
## 3 n 39.1
## 4 n 3.2
## 5 n 58.8
## 6 n 55.5
hist(spines$dbh)

boxplot(spines)

North<-c(27.8, 14.5, 39.1, 3.2, 58.8, 55.5, 25, 5.4, 19, 30.6, 15.1, 3.6, 28.4, 15, 2.2, 14.2, 44.2, 25.7, 11.2, 46.8, 36.9, 54.1, 10.2, 2.5, 13.8, 43.5, 13.8, 39.7, 6.4, 4.8)
South<-c(44.4, 26.1, 50.4, 23.3, 39.5, 51, 48.1, 47.2, 40.3, 37.4, 36.8, 21.7, 35.7, 32, 40.4, 12.8, 5.6, 44.3, 52.9, 38, 2.6, 44.6, 45.5, 29.1, 18.7, 7, 43.8, 28.3, 36.9, 51.6)
qqnorm(North)
qqline(South)

bootStrapCI2<-function(data1, data2, nsim){
n1<-length(data1)
n2<-length(data2)
bootCI2<-c()
for(i in 1:nsim){
bootSamp1<-sample(1:n1, n1, replace=TRUE)
bootSamp2<-sample(1:n2, n2, replace=TRUE)
thisXbar<-mean(data1[bootSamp1])-mean(data2[bootSamp2])
bootCI2<-c(bootCI2, thisXbar)
}
return(bootCI2)
}
spineBootCI<-bootStrapCI2(North, South, nsim=1000)
hist(spineBootCI)

# Quantile Method
quantile(spineBootCI, c(0.025, 0.975))
## 2.5% 97.5%
## -19.00375 -3.05625
# Hybrid Method: Parametric critical value and bootstrap SE
bootSE<-sd(spineBootCI)
(mean(North)-mean(South))+c(-1,1)*qt(0.975, df=14)*bootSE
## [1] -19.683923 -1.982744