# This is an example of an R script to calculate the sample size needed # to estimate the total number of deer pellets in a 20-acre field, as done # on page 18 of the class notes. Here, for different choices of the desired # detectable difference d, the sample size n0 (without the fpc) and n (with # the fpc) are computed and plotted against the d values. N <- 5808 # Defines the population size. s2 <- 14.06 # Defines the sample variance. z <- qnorm(.975,0,1) # Defines the standard normal quantile. d <- c(1000,1500,2000,2500,3000,# The next two commands are 3500,4000,4500,5000) # alternative ways to do the same thing; d <- seq(1000,5000,500) # the "seq" command is very useful here. n0 <- N^2*s2*z^2/d^2 # Computes n0, the sample size # without the fpc. n <- 1/(1/n0 + 1/N) # Computes n, the sample size with the fpc. plot(d,n0,xlab="Difference", # Plots the sample sizes (w/o fpc) versus ylab="Sample size",pch=1, # the differences (d) with axis labels, cex=1.5) # and plotting character 1 (open circle). # points(d,n,pch=16) # Plots the sample sizes (w/ fpc) versus # the differences (d) in overlay, with # plotting character 16 (filled circle). legend(3000,1500,c("n0","n"), # Puts a legend on the plot at (2000,6000), pch=c(1,16),cex=1.5) # using the plotting characters 1 & 16. title("Sample Size Required vs. Desired Difference") # Puts a title on the plot.