In: Statistics and Probability
Answer the following bootstrap question by showing the R code :
A set of data X contains the following numbers:
119.7 104.1 92.8 85.4 108.6 93.4 67.1 88.4 101.0 97.2
95.4 77.2 100.0 114.2 150.3 102.3 105.8 107.5 0.9 94.1
We generated n = 20 observations Xi = 10 Wi+100, where Wi has a contaminated normal distribution with proportion of contamination 20% and σc = 4.
Suppose we are interested in testing:
H0 : μ = 90 versus H1 : μ > or <90 .
(a) Determine the bootstrap p-value for this situation.
(b) Using R Program to Compute the p-value based on 3000 bootstraps and show the R code.
My R codes always give me the P value =0. What's wrong with it?
m <- 90
x <- c(119.7, 104.1, 92.8, 85.4, 108.6, 93.4, 67.1, 88.4, 101,
97.2, 95.4, 77.2, 100, 114.2, 150.3, 102.3, 105.8, 107.5, 0.9, 94.1
)
boottestonemean <- function(x,m,b){
n<-length(x)
m=90
b=3000
v <- mean(x)
z <- x - mean(x) + 90
counter <- 0
teststatall <- rep(0,b)
for(i in 1:b){xstar <- sample(z, n, replace=TRUE)
vstar <- mean(xstar)
if(vstar > v)
{counter <- counter+1}
teststatall[i]<-vstar}
pvalue <- counter/b
list(origtest=v,pvalue=pvalue,teststatall=teststatall)
}
Use R program.
Run the below code
set.seed(2)
m <- 90
x <- c(119.7, 104.1, 92.8, 85.4, 108.6, 93.4, 67.1, 88.4, 101,
97.2, 95.4, 77.2, 100, 114.2, 150.3, 102.3, 105.8, 107.5, 0.9, 94.1
)
boottestonemean <- function(x,m,b){
n<-length(x)
m=90
b=3000
v <- mean(x)
z <- x - mean(x) + 90
counter <- 0
teststatall <- rep(0,b)
for(i in 1:b){xstar <- sample(z, n, replace=TRUE)
vstar <- mean(xstar)
if(vstar > v)
{counter <- counter+1}
teststatall[i]<-vstar}
pvalue <- counter/b
list(origtest=v,pvalue=pvalue,teststatall=teststatall)
}
R=boottestonemean(x,90,3000)
R$pvalue ## boostrap p-value
T<-(mean(x)-90)/(sd(x)/20^0.5) #one sided t test
T
Run the above and get answer below
> R$pvalue ## boostrap p-value [1] 0.2056667 > > T<-(mean(x)-90)/(sd(x)/20^0.5) #one sided t test > T [1] 0.8447507 > R=boottestonemean(x,90,3000) > R$pvalue ## boostrap p-value [1] 0.1896667 > > T<-(mean(x)-90)/(sd(x)/20^0.5) #one sided t test > T [1] 0.8447507 |
|
|