Random sampling units in a clustered population
Select a random sample of units from a sampling frame of a clustered population (for example, animals with different owners in a village, pigs in pens in a piggery, cattle in pens in a feedlot, etc). A full sampling frame of all units is generated with units in each cluster numbered sequentially. Simple random sampling is then done to generate a sample with units identified by cluster id and then by sequential number for each cluster.
Sampling can be based on either a specified number of units to be sampled or a specified proportion of the total number of units in the sampling frame. Sampling can also be with or without (default) replacement.
A list of all clusters and the number of units in each cluster is required for generation of the sampling frame. This can be copied and pasted from excel or other software and should be formatted as two columns: the first column is the unique cluster id and the second is the number of units in each cluster.
Sample size should be entered as an integer number of units to be sampled or a proportion (>0 and <1) of the total population to be sampled.
Download and check out the example data if you are unsure what is required.
No results
###################################### # Program to do random animal sampling ###################################### # uses rand.clustered() function rm(list = ls()) # cat("
Test:",length(commandArgs())) test<- ifelse(length(commandArgs()) == 2, TRUE, FALSE) fpath<- ifelse(test, "webRootUrl", "rtoolsPath") # cat("
", test) # load header scripts source(paste(fpath, "R/epi_head.R", sep = "")) source(paste(fpath, "R/HTMLStream.R", sep = "")) source(paste(fpath, "R/epitools_functions.r", sep = "")) # extract command arguments # 1 = sample size, 2 = method (1 = fixed %, 0 = fixed number) # cat("
", a0[8:9]) a1<- type.convert(a0[8:9]) # cat("
", a1) n<- a1[1] # sample size replace<- a1[2] # sampling percentage or number heading<- "Random sampling results" filename<- digest(Sys.time()) data.file<- ifelse(test, paste(fpath, "docs/RandomAnimalDemo.txt", sep = ""), a0[10]) #test name tmp.path<- paste(fpath, "tmp/", sep = "") tmp.file<- paste(fpath, "tmp/", filename, sep = "") sinkfile<- paste(fpath, "tmp/", filename, ".txt", sep="") # fpath, # cat("
", data.file) size<- file.info(data.file)$size if (size == 0) cat("
No Data entered
") data.list<- read.csv(data.file) inputs<- array(0, dim = c(ifelse(n<1,4,3), 1)) rownames(inputs)<- c("Sample size", "Sampling type", "Number of units in sampling frame", "Actual sample size")[1:nrow(inputs)] inputs[1]<- ifelse(n<1, paste(n*100, "%", sep = ""), paste(n, "Units")) inputs[2]<- ifelse(replace, "With replacement", "Without replacement") inputs[3]<- sum(data.list[,2]) if(n < 1) inputs[4]<- paste(ceiling(sum(data.list[,2])*n), "units") # calculate new n if doing fixed percentage # do random sampling # build sampling frame tmp<- rand.clustered(data.list, n, replace) ran.list<- tmp[[1]] owner.list<- tmp[[2]] # cat("
", nrow(ran.list)) file.name<- file(paste(tmp.file, "_result.xls", sep = ""), open = "wt") writeLines(c(heading, date(), ""), con = file.name) writeLines("Inputs", con = file.name) write.table(inputs, file = file.name, sep = "\t", append = T, col.names = F) writeLines(c("", "List of randomly sampled units"), con = file.name) write.table(ran.list, file = file.name, sep = "\t", append = T, row.names = F) writeLines(c("", "List of units selected for each owner"), con = file.name) write.table(owner.list, file = file.name, sep = "\t", append = T, row.names = F) close(file.name) # output results d1<- paste(substr(date(), 1, 10), substr(date(), 20,24), " @", substr(date(), 11, 16)) # reformat headers output<- paste("", heading, "
\n") output<- paste(output, "", "Analysed: ", d1, "
\n", sep="") output<- paste(output, "Inputs
\n") output<- paste(output, HTMLStream(inputs, cellborder = 0, classfirstline = "mbg", classfirstcolumn = "mbg", classcellinside = "left_mar", cellalign = "center", align = "left")) output<- paste(output, "Results
\n") output<- paste(output, "Selected animals by owner\n") output<- paste(output, HTMLStream(owner.list, cellborder = 0, classfirstline = "mbg", classfirstcolumn = "mbg", classcellinside = "left_mar", cellalign = "center", align = "left")) output<- paste(output, "
List of animals selected\n") output<- paste(output, HTMLStream(ran.list, cellborder = 0, classfirstline = "mbg", classfirstcolumn = "mbg", classcellinside = "left_mar", cellalign = "center", align = "left")) output<- paste(output, "
Download excel file of results
\n") output<- paste(output, "Detailed results
\n", sep = "") # sink() # write output to file file.name<- file(paste(tmp.file, "_result.html", sep = ""), open = "wt") cat(output, file=file.name) close(file.name) #) cat(output)