Kristi Glover
2014-Mar-28 18:04 UTC
starta.sampling with many (1000 times) and average them
Hi R Users, I was trying to select a sample with columns (measured data) by stratum. I was able to select rows by stratum. But, I wanted to repeat this procedures 1000 and want to take an average from the 1000 times. I think it is different than bootstraping since I wanted to select (rondomly) row with many variable at a time and repeat 1000 times then,average them. #----------------------------- I put here an example raw=structure(list(watershed = structure(c(1L, 1L, 1L, 1L, 1L, 1L, 1L, 2L, 2L, 2L, 2L, 2L, 2L), .Label = c("A", "B"), class = "factor"), year = c(2001, 2001, 2002, 2002, 2002, 2002, 2002, 2001, 2001, 2001, 2002, 2002, 2002), sp1 = c(18.38, 29.1, 90.72, 16.12, 49.12, 20.81, 65.1, 1.87, 72.99, 93.45, 38.44, 67.13, 45.71), sp2 = c(46.46, 94, 86.87, 46.91, 21.41, 92.82, 87.75, 16.18, 18.16, 18.76, 19.26, 52.73, 49.09), sp3 = c(86.9, 62.82, 74.32, 75.49, 20.17, 58.84, 16.51, 44.14, 44.39, 32.36, 53.28, 67.42, 33.37)), .Names = c("watershed", "year", "sp1", "sp2", "sp3"), class = "data.frame", row.names = c(NA, -13L)) raw$group<-paste(as.character(raw$watershed),as.character(raw$year), sep="|") # strata.sampling ----- strata.sampling <- function(data, group,size, method = NULL) { require(sampling) if (is.null(method)) method <- "srswor" if (!method %in% c("srswor", "srswr")) stop('method must be "srswor" or "srswr"') temp <- data[order(data[[group]]), ] ifelse(length(size) > 1, size <- size, ifelse(size < 1, size <- round(table(temp[group]) * size), size <- rep(size, times=length(table(temp[group]))))) strat = strata(temp, stratanames = names(temp[group]), size = size, method = method) getdata(temp, strat) } #--------------- attach(raw) test1<-strata.sampling(raw, 'group', 1)# select 1 row by watershed and year test2<-strata.sampling(raw, 'group', 1)# select 1 row by watershed and year test3<-strata.sampling(raw, 'group', 1)# select 1 row by watershed and year test4<-strata.sampling(raw, 'group', 1)# select 1 row by watershed and year ... ... ... # here I wanted to repeat this procedure 1000 times test1000<-strata.sampling(raw, 'group', 2)# select 1 row by watershed and year # and then average of this 1000 tables testA<-average(test1[,3:5],test2[,3:5],test3[,3:5],.....test1000[,3:5]) Inform<-subset(test1, select=c(1,2,6,9) FinalData<-data.frame(Inform, testA) Would you give me some suggestion on how I can make a loop to repeat this procedures and average them. Thanks [[alternative HTML version deleted]]