[R] Bootstraping for groups and subgroups and joing with other table

Chris Stubben stubben at lanl.gov
Wed Aug 30 19:17:56 CEST 2006


>   I have a table with following collumns: State, SamplePlot, Species and
BodySize. I sampled bird species at
> 34 SamplePlots and 5 States (regions) monthly during two years. On each bird
record I measured bodysize
> and identified the species. So I have many records of each species (about 150
species) at each SamplePlot
> and each Region (State). 
> 
>   Now I would like bootstrap these data, selecting 50 records for each
State/SamplePlot combinations and
> count how many species (richness) were sampled at bootstrap. I need to do this
1.000 times. 
> 
>   After that and need join the number of species [obtained at each bootstrap
and for each State/SamplePlot
> combination] with a dataframe that have other attributes for SamplePlot (like
Area, Perimeter etc). 



I asked a similar question earlier... IF you have data frame birds and
bird.plots, maybe something like this.

#initialize empty variable 
boot<-NULL

for(i in 1:100)
{
   ## split on state and site and create list a
   a <-split(birds, paste(birds$State,birds$SampleSite), drop=T)

   # sample 50 rows each OR by number of observations (better?)
   # b<-lapply( a, function(x) x[sample(nrow(x), 50, replace=T),])
     b<-lapply( a, function(x) x[sample(nrow(x), replace=T),])

   ## count number of unique species or other statistic? 
   ###  and add row to boot matrix
   boot<-rbind(boot, unlist( lapply(b, function (x) length(unique(x$Species)) ) ))
}

## mean

y<-apply(boot, 2, mean)


## convert to data frame for merge

y<-data.frame(y)

names(y)<-"boot.count"


## add row names to bird.plots for easy join
rownames(bird.plots)<-paste(bird.plots$State,bird.plots$SampleSite)

merge(bird.plots,y, by=0)

          Row.names      State SampleSite Area boot.count
1       Bahia Site1      Bahia      Site1   10       1.00
2       Bahia Site2      Bahia      Site2   25       1.96
3       Bahia Site3      Bahia      Site3   70       1.72
4       Bahia Site4      Bahia      Site4   15       1.73
5       Bahia Site5      Bahia      Site5    5       1.42
6  RioJaneiro Site1 RioJaneiro      Site1   32       2.49
7  RioJaneiro Site2 RioJaneiro      Site2   45       1.63
8  RioJaneiro Site3 RioJaneiro      Site3   10       2.37
9    SaoPaulo Site1   SaoPaulo      Site1   23       2.41
10   SaoPaulo Site2   SaoPaulo      Site2   45       2.57


Chris Stubben



More information about the R-help mailing list