Answering my own question.
?sample (!)
y <- by(x, x$TrSeasYr, function(x) mean(sample(x[,1], size=999, replace =
TRUE)))
>>> Tim Howard 10/13/2011 9:42 AM >>>
All -
I have an uneven set of replicates and would like to sample from this set X
number of times to generate a mean for each grouping variable. I was thinking
the boot package would be the thing to use, but now I'm not so sure ...
given the discussion here:
http://finzi.psych.upenn.edu/Rhelp10/2010-June/243828.html
Given these data (a small subset):
> dput(x)
structure(list(numSpp = c(7, 8, 6, 4, 5, 4, 10, 12, 7, 13, 4,
11, 4, 3, 3, 10, 8, 16, 11, 6, 3, 5, 6, 13, 15, 2, 6, 11, 11,
10, 6, 9, 11, 14, 10, 7), TrSeasYr = c("1", "1",
"1", "1", "1",
"1", "1", "1", "1", "1",
"1", "1", "1", "1", "1",
"2", "2", "2",
"2", "2", "3", "3", "3",
"3", "3", "3", "3", "3",
"3", "3", "3",
"3", "3", "3", "3", "3")),
.Names = c("numSpp", "TrSeasYr"), row.names = c(NA,
-36L), class = "data.frame")
#get into R by pasting the above after "x<-" or
dget("clipboard")
# my grouping var is col 2, data are in col 1
# my attempt using boot
library(boot)
mn <- function(d,f) {
mean(d[,1] * f[d[,1]])
}
b1 <- boot(data = x, mn, R=99, stype = "f", strata=x$TrSeasYr)
> b1
STRATIFIED BOOTSTRAP
Call:
boot(data = x, statistic = mn, R = 99, stype = "f", strata =
x$TrSeasYr)
Bootstrap Statistics :
original bias std. error
t1* 8.083333 0.1352413 1.681901>
What is the most efficient way to resample, with replacement, and generate means
for each grouping variable?
Thanks in advance,
Tim
> sessionInfo()
R version 2.12.2 (2011-02-25)
Platform: i386-pc-mingw32/i386 (32-bit)
locale:
[1] LC_COLLATE=English_United States.1252 LC_CTYPE=English_United States.1252
[3] LC_MONETARY=English_United States.1252 LC_NUMERIC=C
[5] LC_TIME=English_United States.1252
attached base packages:
[1] stats graphics grDevices utils datasets methods base
other attached packages:
[1] RODBC_1.3-2 boot_1.3-2
loaded via a namespace (and not attached):
[1] tools_2.12.2
Timothy G. Howard, Ph.D.
Director of Science
New York Natural Heritage Program
625 Broadway, 5th floor
Albany, NY 12233-4757
(518) 402-8945
facsimile (518) 402-8925
[[alternative HTML version deleted]]