Skip to content
Snippets Groups Projects
Commit 868dd35b authored by Ben Anderson's avatar Ben Anderson
Browse files

added new proportions power analysis; re-ran docs

parent ac04c11c
No related branches found
No related tags found
No related merge requests found
# Generated by roxygen2: do not edit by hand # Generated by roxygen2: do not edit by hand
export(estimateEffectSizes) export(estimateMeanEffectSizes)
export(estimateProportionEffectSizes)
import(data.table) import(data.table)
import(pwr) import(pwr)
import(reshape2) import(reshape2)
#--- Sample power related functions ---# #--- Sample power related functions ---#
#' Estimate detectable effect sizes using statistical power analysis #' Estimate detectable mean effect sizes using statistical power analysis
#' #'
#' \code{estimateEffectSizes} calculates required sample sizes for a given set of p values and samples. #' \code{estimateMeanEffectSizes} calculates required sample sizes for a given set of p values and samples. Assumes a 1-sided test.
#' #'
#' Returns a data.table of effect sizes for a given sample size. Calculates these for p = 0.01, 0.05, 0.1 & 0.2. Pick out the ones you want. #' Returns a data.table of effect sizes for a given sample size. Calculates these for p = 0.01, 0.05, 0.1 & 0.2. Pick out the ones you want.
#' #'
...@@ -15,9 +15,10 @@ ...@@ -15,9 +15,10 @@
#' @export #' @export
#' @import data.table #' @import data.table
#' @import reshape2 #' @import reshape2
#' @import pwr
#' @family Power functions #' @family Power functions
estimateEffectSizes <- function(mean,sd,samples,power){ estimateMeanEffectSizes <- function(mean,sd,samples,power){
# obtain effect sizes using supplied mean & sd # obtain effect sizes using supplied mean & sd
sigs <- c(0.01,0.05,0.1,0.2) # force these, can always remove later sigs <- c(0.01,0.05,0.1,0.2) # force these, can always remove later
nSigs <- length(sigs) nSigs <- length(sigs)
...@@ -29,12 +30,13 @@ estimateEffectSizes <- function(mean,sd,samples,power){ ...@@ -29,12 +30,13 @@ estimateEffectSizes <- function(mean,sd,samples,power){
# loop over significance values # loop over significance values
for (p in 1:nSigs){ for (p in 1:nSigs){
for (s in 1:nSamps){ # loop over the sample sizes for (s in 1:nSamps){ # loop over the sample sizes
result <- power.t.test( # pwr.t.test? result <- power.t.test(
n = samples[s], n = samples[s],
delta = NULL, delta = NULL, # what we want to calculate
sd = sd, sd = sd,
sig.level = sigs[p], sig.level = sigs[p],
power = power, power = power,
type = c("two.sample"),
alternative = c("one.sided") alternative = c("one.sided")
) )
resultsArray[s,p] <- result$delta/mean # report effect size against sample size resultsArray[s,p] <- result$delta/mean # report effect size against sample size
...@@ -57,11 +59,11 @@ estimateEffectSizes <- function(mean,sd,samples,power){ ...@@ -57,11 +59,11 @@ estimateEffectSizes <- function(mean,sd,samples,power){
return(longDT) # returned the tidied & long form dt return(longDT) # returned the tidied & long form dt
} }
#' Estimate proportion margins of error using statistical power analysis #' Estimate detectable proportion differences for given sample sizes using statistical power analysis
#' #'
#' \code{estimateProportions} calculates required sample sizes for a given set of p values and samples. #' \code{estimateProportionEffectSizes} calculates required sample sizes for a given set of p values and samples.
#' #'
#' Returns a data.table of effect sizes for a given sample size. Calculates these for p = 0.01, 0.05, 0.1 & 0.2. Pick out the ones you want. #' Returns a data.table of effect sizes (%) for a given sample size. Calculates these for p = 0.01, 0.05, 0.1 & 0.2. Pick out the ones you want.
#' #'
#' @param mean the estimated mean value to use #' @param mean the estimated mean value to use
#' @param sd the estimated stadnard deviation to use #' @param sd the estimated stadnard deviation to use
...@@ -75,7 +77,7 @@ estimateEffectSizes <- function(mean,sd,samples,power){ ...@@ -75,7 +77,7 @@ estimateEffectSizes <- function(mean,sd,samples,power){
#' @import pwr #' @import pwr
#' @family Power functions #' @family Power functions
estimateEffectSizes <- function(mean,sd,samples,power){ estimateProportionEffectSizes <- function(samples,power){
# obtain effect sizes using supplied mean & sd # obtain effect sizes using supplied mean & sd
sigs <- c(0.01,0.05,0.1,0.2) # force these, can always remove later sigs <- c(0.01,0.05,0.1,0.2) # force these, can always remove later
nSigs <- length(sigs) nSigs <- length(sigs)
...@@ -88,15 +90,13 @@ estimateEffectSizes <- function(mean,sd,samples,power){ ...@@ -88,15 +90,13 @@ estimateEffectSizes <- function(mean,sd,samples,power){
for (p in 1:nSigs){ for (p in 1:nSigs){
for (s in 1:nSamps){ # loop over the sample sizes for (s in 1:nSamps){ # loop over the sample sizes
# pwr.t.test? # pwr.t.test?
result <- power.t.test( result <- pwr::pwr.2p.test(
n = samples[s], n = samples[s],
delta = NULL, h = NULL,
sd = sd,
sig.level = sigs[p], sig.level = sigs[p],
power = power, power = power
alternative = c("one.sided")
) )
resultsArray[s,p] <- result$delta/mean # report effect size against sample size resultsArray[s,p] <- result$h # report effect size against sample size
} }
} }
dt <- data.table::as.data.table(resultsArray) # convert to dt for tidying dt <- data.table::as.data.table(resultsArray) # convert to dt for tidying
......
% Generated by roxygen2: do not edit by hand % Generated by roxygen2: do not edit by hand
% Please edit documentation in R/power.R % Please edit documentation in R/power.R
\name{estimateEffectSizes} \name{estimateMeanEffectSizes}
\alias{estimateEffectSizes} \alias{estimateMeanEffectSizes}
\title{Estimate detectable effect sizes using statistical power analysis} \title{Estimate detectable mean effect sizes using statistical power analysis}
\usage{ \usage{
estimateEffectSizes(mean, sd, samples, power) estimateMeanEffectSizes(mean, sd, samples, power)
estimateEffectSizes(mean, sd, samples, power)
} }
\arguments{ \arguments{
\item{mean}{the estimated mean value to use} \item{mean}{the estimated mean value to use}
...@@ -15,29 +13,18 @@ estimateEffectSizes(mean, sd, samples, power) ...@@ -15,29 +13,18 @@ estimateEffectSizes(mean, sd, samples, power)
\item{samples}{a list of sample sizes to iterate over} \item{samples}{a list of sample sizes to iterate over}
\item{power}{power value to use}
\item{mean}{the estimated mean value to use}
\item{sd}{the estimated stadnard deviation to use}
\item{samples}{a list of sample sizes to iterate over}
\item{power}{power value to use} \item{power}{power value to use}
} }
\description{ \description{
\code{estimateEffectSizes} calculates required sample sizes for a given set of p values and samples. \code{estimateMeanEffectSizes} calculates required sample sizes for a given set of p values and samples. Assumes a 1-sided test.
\code{estimateProportions} calculates required sample sizes for a given set of p values and samples.
} }
\details{ \details{
Returns a data.table of effect sizes for a given sample size. Calculates these for p = 0.01, 0.05, 0.1 & 0.2. Pick out the ones you want.
Returns a data.table of effect sizes for a given sample size. Calculates these for p = 0.01, 0.05, 0.1 & 0.2. Pick out the ones you want. Returns a data.table of effect sizes for a given sample size. Calculates these for p = 0.01, 0.05, 0.1 & 0.2. Pick out the ones you want.
} }
\seealso{
Other Power functions: \code{\link{estimateProportionEffectSizes}}
}
\author{ \author{
Ben Anderson, \email{b.anderson@soton.ac.uk}
Ben Anderson, \email{b.anderson@soton.ac.uk} Ben Anderson, \email{b.anderson@soton.ac.uk}
} }
\concept{Power functions} \concept{Power functions}
% Generated by roxygen2: do not edit by hand
% Please edit documentation in R/power.R
\name{estimateProportionEffectSizes}
\alias{estimateProportionEffectSizes}
\title{Estimate detectable proportion differences for given sample sizes using statistical power analysis}
\usage{
estimateProportionEffectSizes(samples, power)
}
\arguments{
\item{samples}{a list of sample sizes to iterate over}
\item{power}{power value to use}
\item{mean}{the estimated mean value to use}
\item{sd}{the estimated stadnard deviation to use}
}
\description{
\code{estimateProportionEffectSizes} calculates required sample sizes for a given set of p values and samples.
}
\details{
Returns a data.table of effect sizes (%) for a given sample size. Calculates these for p = 0.01, 0.05, 0.1 & 0.2. Pick out the ones you want.
}
\seealso{
Other Power functions: \code{\link{estimateMeanEffectSizes}}
}
\author{
Ben Anderson, \email{b.anderson@soton.ac.uk}
}
\concept{Power functions}
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment