trace tracker logo

Sample size to estimate a single mean

This utility calculates the sample size required to estimate a population mean with a specified level of confidence and precision.

Inputs are the assumed population standard deviation, the desired level of confidence and the desired precision of the estimate. The desired precision of the estimate (also sometimes called the allowable or acceptable error in the estimate) is half the width of the desired confidence interval.

No results

If you would like the confidence interval width to be about 2 kg (or whatever other units) you would enter a precision of +/- 1.

The program outputs the sample size required to estimate the true value with the desired precision and confidence.


http://www.r-bloggers.com/integrating-php-and-r/
				######################################

# Program to calculate sample size for difference of two proportions

######################################



rm(list = ls())

test<- ifelse(length(commandArgs()) == 2, TRUE, FALSE)

fpath<- ifelse(test, "webRootUrl", "rtoolsPath")



# 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

    a1<- type.convert(a0[8:10])

# cat(a0)

# cat(a1)

std.dev<- a1[1]

conf<- a1[2]

error<- a1[3]

tails.conf<- 2

digits<- 0

z.conf<- qnorm(1 - (1 - conf)/tails.conf, 0, 1)



# table of inputs

inputs<- array("", dim = c(length(a1), 1))

inputs[1:length(a1), 1]<- a1[1:length(a1)]

rownames(inputs)<- c("Assumed population standard deviation", "Confidence level", "Acceptable error")



sample.size<- ceiling(((z.conf)*std.dev/error)^2)



result<- array(sample.size, dim = c(1,1))

rownames(result)<- "Sample size required: "

colnames(result)<- "Sample size"







# write to html and file

heading<- "Sample size to estimate a single mean"

subheadings<- ""

filename<- digest(Sys.time)

tmp.file<- paste(fpath, "tmp/", filename, sep = "")

output<- html.output(heading, subheadings, inputs, results = list(result), graphs = "", graph.headings = "", show.inputs = T, show.graphs = F, tmp.file) 

write.html(output, tmp.file)

cat(output)