From 868dd35b2e3239dc95827bf0b7d1e29d1638a6f7 Mon Sep 17 00:00:00 2001
From: Ben Anderson <dataknut@icloud.com>
Date: Mon, 29 Oct 2018 17:32:03 +1300
Subject: [PATCH] added new proportions power analysis; re-ran docs

---
 NAMESPACE                            |  3 +-
 R/power.R                            | 30 +++++++++----------
 man/estimateEffectSizes.Rd           | 43 ----------------------------
 man/estimateMeanEffectSizes.Rd       | 30 +++++++++++++++++++
 man/estimateProportionEffectSizes.Rd | 30 +++++++++++++++++++
 5 files changed, 77 insertions(+), 59 deletions(-)
 delete mode 100644 man/estimateEffectSizes.Rd
 create mode 100644 man/estimateMeanEffectSizes.Rd
 create mode 100644 man/estimateProportionEffectSizes.Rd

diff --git a/NAMESPACE b/NAMESPACE
index 4d36a73..2c11f07 100644
--- a/NAMESPACE
+++ b/NAMESPACE
@@ -1,6 +1,7 @@
 # Generated by roxygen2: do not edit by hand
 
-export(estimateEffectSizes)
+export(estimateMeanEffectSizes)
+export(estimateProportionEffectSizes)
 import(data.table)
 import(pwr)
 import(reshape2)
diff --git a/R/power.R b/R/power.R
index 126933c..2b2b161 100644
--- a/R/power.R
+++ b/R/power.R
@@ -1,8 +1,8 @@
 #--- 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.
 #'
@@ -15,9 +15,10 @@
 #' @export
 #' @import data.table
 #' @import reshape2
+#' @import pwr
 #' @family Power functions
 
-estimateEffectSizes <- function(mean,sd,samples,power){
+estimateMeanEffectSizes <- function(mean,sd,samples,power){
   # obtain effect sizes using supplied mean & sd
   sigs <- c(0.01,0.05,0.1,0.2) # force these, can always remove later
   nSigs <- length(sigs)
@@ -29,12 +30,13 @@ estimateEffectSizes <- function(mean,sd,samples,power){
   # loop over significance values
   for (p in 1:nSigs){
     for (s in 1:nSamps){ # loop over the sample sizes
-      result <- power.t.test( # pwr.t.test?
+      result <- power.t.test(
         n = samples[s],
-        delta = NULL,
+        delta = NULL, # what we want to calculate
         sd = sd,
         sig.level = sigs[p],
         power = power,
+        type = c("two.sample"),
         alternative = c("one.sided")
       )
       resultsArray[s,p] <- result$delta/mean # report effect size against sample size
@@ -57,11 +59,11 @@ estimateEffectSizes <- function(mean,sd,samples,power){
   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 sd the estimated stadnard deviation to use
@@ -75,7 +77,7 @@ estimateEffectSizes <- function(mean,sd,samples,power){
 #' @import pwr
 #' @family Power functions
 
-estimateEffectSizes <- function(mean,sd,samples,power){
+estimateProportionEffectSizes <- function(samples,power){
   # obtain effect sizes using supplied mean & sd
   sigs <- c(0.01,0.05,0.1,0.2) # force these, can always remove later
   nSigs <- length(sigs)
@@ -88,15 +90,13 @@ estimateEffectSizes <- function(mean,sd,samples,power){
   for (p in 1:nSigs){
     for (s in 1:nSamps){ # loop over the sample sizes
       # pwr.t.test?
-      result <- power.t.test(
+      result <- pwr::pwr.2p.test(
         n = samples[s],
-        delta = NULL,
-        sd = sd,
+        h = NULL,
         sig.level = sigs[p],
-        power = power,
-        alternative = c("one.sided")
+        power = power
       )
-      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
diff --git a/man/estimateEffectSizes.Rd b/man/estimateEffectSizes.Rd
deleted file mode 100644
index 4f31c0f..0000000
--- a/man/estimateEffectSizes.Rd
+++ /dev/null
@@ -1,43 +0,0 @@
-% Generated by roxygen2: do not edit by hand
-% Please edit documentation in R/power.R
-\name{estimateEffectSizes}
-\alias{estimateEffectSizes}
-\title{Estimate detectable effect sizes using statistical power analysis}
-\usage{
-estimateEffectSizes(mean, sd, samples, power)
-
-estimateEffectSizes(mean, sd, samples, power)
-}
-\arguments{
-\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{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}
-}
-\description{
-\code{estimateEffectSizes} calculates required sample sizes for a given set of p values and samples.
-
-\code{estimateProportions} 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.
-
-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.
-}
-\author{
-Ben Anderson, \email{b.anderson@soton.ac.uk}
-
-Ben Anderson, \email{b.anderson@soton.ac.uk}
-}
-\concept{Power functions}
diff --git a/man/estimateMeanEffectSizes.Rd b/man/estimateMeanEffectSizes.Rd
new file mode 100644
index 0000000..6391f2b
--- /dev/null
+++ b/man/estimateMeanEffectSizes.Rd
@@ -0,0 +1,30 @@
+% Generated by roxygen2: do not edit by hand
+% Please edit documentation in R/power.R
+\name{estimateMeanEffectSizes}
+\alias{estimateMeanEffectSizes}
+\title{Estimate detectable mean effect sizes using statistical power analysis}
+\usage{
+estimateMeanEffectSizes(mean, sd, samples, power)
+}
+\arguments{
+\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}
+}
+\description{
+\code{estimateMeanEffectSizes} calculates required sample sizes for a given set of p values and samples. Assumes a 1-sided test.
+}
+\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{estimateProportionEffectSizes}}
+}
+\author{
+Ben Anderson, \email{b.anderson@soton.ac.uk}
+}
+\concept{Power functions}
diff --git a/man/estimateProportionEffectSizes.Rd b/man/estimateProportionEffectSizes.Rd
new file mode 100644
index 0000000..c30cf1d
--- /dev/null
+++ b/man/estimateProportionEffectSizes.Rd
@@ -0,0 +1,30 @@
+% 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}
-- 
GitLab