[R] loop for comparing two or more groups using bootstrapping

Kristi Glover
Tue, 11 Sep 2018 06:52:06 -0000

Hi R users,

I was trying to test a null hypothesis of difference between two groups was 0. I
have many years data, such as year1, year2,, year3, year4 and I was trying to
compare between year1 and year2, year1 and year3, year2 and year3 and so on and
have used following code with an example data.


I tried to make a loop but did not work to compare between many years, and also
want to obtain the exact p value. Would you mind to help me to make a loop?

Thanks for your help.


KG


daT<-structure(list(year1 = c(0.417, 0.538, 0.69, 0.688, 0.688, 0.606,

0.667, 0.7, 0.545, 0.462, 0.711, 0.642, 0.744, 0.604, 0.612,

0.667, 0.533, 0.556, 0.444, 0.526, 0.323, 0.308, 0.195, 0.333,

0.323, 0.256, 0.345, 0.205, 0.286, 0.706, 0.7, 0.6, 0.571, 0.364,

0.429, 0.326, 0.571, 0.424, 0.341, 0.387, 0.341, 0.324, 0.696,

0.696, 0.583, 0.556, 0.645, 0.435, 0.471, 0.556), year2 = c(0.385,

0.552, 0.645, 0.516, 0.629, 0.595, 0.72, 0.638, 0.557, 0.588,

0.63, 0.744, 0.773, 0.571, 0.723, 0.769, 0.667, 0.667, 0.526,

0.476, 0.294, 0.323, 0.222, 0.556, 0.263, 0.37, 0.357, 0.25,

0.323, 0.778, 0.667, 0.636, 0.583, 0.432, 0.412, 0.333, 0.571,

0.39, 0.4, 0.452, 0.326, 0.471, 0.7, 0.75, 0.615, 0.462, 0.556,

0.4, 0.696, 0.465), year3 = c(0.435, 0.759, 0.759, 0.759, 0.714,

0.593, 0.651, 0.683, 0.513, 0.643, 0.652, 0.757, 0.791, 0.649,

0.78, 0.5, 0.5, 0.5, 0.533, 0.429, 0.333, 0.286, 0.231, 0.533,

0.303, 0.417, 0.333, 0.333, 0.357, 0.909, 1, 0.952, 0.8, 0.556,

0.529, 0.562, 0.762, 0.513, 0.733, 0.611, 0.733, 0.647, 0.909,

0.857, 0.8, 0.556, 0.588, 0.562, 0.857, 0.513), year4 = c(0.333,

0.533, 0.6, 0.483, 0.743, 0.5, 0.691, 0.619, 0.583, 0.385, 0.653,

0.762, 0.844, 0.64, 0.667, 0.571, 0.571, 0.615, 0.421, 0.5, 0.205,

0.308, 0.25, 0.6, 0.242, 0.308, 0.276, 0.235, 0.211, 0.9, 0.632,

0.72, 0.727, 0.356, 0.5, 0.368, 0.5, 0.41, 0.562, 0.514, 0.4,

0.409, 0.632, 0.72, 0.727, 0.4, 0.5, 0.421, 0.5, 0.462)), .Names =
c("year1",

"year2", "year3", "year4"), row.names = c(NA,
-50L), class = "data.frame")

head(daT)

# null hypothesis; difference is equal to zero

dif1.2<-daT$year2-daT$year1

k=10000

mysamples1.2=replicate(k, sample(dif1.2, replace=T))

mymeans1.2=apply(mysamples1.2, 2, mean)

quantile(mymeans1.2, c(0.025, 0.975))

hist(mysamples1.2)

mean(mymeans1.2)

#what is p value?


#similarly Now I want to compare between year 1 and year3,

dif1.3<-daT$year3-daT$year1

mysamples1.3=replicate(k, sample(dif1.3, replace=T))

mymeans1.3=apply(mysamples1.3, 2, mean)

quantile(mymeans1.3, c(0.025, 0.975))


	[[alternative HTML version deleted]]