Skip to content

Commit

Permalink
add nratio, update Rd, test data
Browse files Browse the repository at this point in the history
  • Loading branch information
vikjam committed May 5, 2017
1 parent bbaf795 commit 54b164b
Show file tree
Hide file tree
Showing 8 changed files with 50 additions and 22 deletions.
2 changes: 1 addition & 1 deletion DESCRIPTION
Original file line number Diff line number Diff line change
Expand Up @@ -16,5 +16,5 @@ Remotes: hadley/testthat
License: MIT + file LICENSE
Encoding: UTF-8
LazyData: true
RoxygenNote: 5.0.1
RoxygenNote: 6.0.1
VignetteBuilder: knitr
22 changes: 10 additions & 12 deletions R/twomeans.r
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,13 @@
#' @param m2 Mean of a group 2 (e.g., the experimental-group)
#' @param n1 Number of obs. in group 1 (e.g., the control-group)
#' @param n2 Number of obs. in group 2 (e.g., the control-group)
#' @param nratio Specify the ratio of group 1 to group 2.
#' @param nratio Ratio of sample sizes, group 2 to group 1; default is nratio = 1, meaning equal group sizes
#' @param sd Standard deviation of each group, i.e., sd = sd1 = sd2
#' @param sd1 Standard deviation of a group 1 (e.g., the control-group)
#' @param sd2 Standard deviation of a group 2 (e.g., the experimental-group)
#' @param sd1 Standard deviation of a group 1 (e.g., the control group)
#' @param sd2 Standard deviation of a group 2 (e.g., the experimental group)
#' @param sig.level significance level; default is sig.level = 0.05
#' @param power one minus the probability of type II error; default is power = 0.8
#' @return returns an object with all the study parameters
#' @return Returns an object with all the study parameters
#' @export
#' @importFrom stats qnorm
#' @examples
Expand Down Expand Up @@ -50,8 +50,10 @@ twomeans <- function(m1 = NULL,
}

beta = 1 - power
n1 <- (1 + 1 / nratio) * (sd * (qnorm(1 - sig.level / 2) + qnorm(1 - beta)) /
(m1 - m2))^2

n1 <- (sd1^2 + (sd2^2) / nratio) *
(qnorm(1 - sig.level / 2) + qnorm(1 - beta))^2 / ((m1 - m2)^2)

n2 <- nratio * n1

# Round the n's
Expand All @@ -78,19 +80,15 @@ twomeans <- function(m1 = NULL,
}

.check.twomeans <- function(m1, m2, n1, n2, nratio, sd, sd1, sd2, sig.level, power) {
# # Must specify effect size or sample size
# if(all(is.null(m1), is.null(m1))) {
# stop("Must specify means (m1 and m2) or sample sizes of the groups (n1 and n2)")
# }

# Non-negative standard deviation
if(any(sd <= 0, sd1 <= 0, sd2 <= 0)) {
stop("sd must be positive")
}

# Non-negative observations
if(any(n1 <= 0, n2 <= 0)) {
stop("n1 and n2 must be positive")
if(any(n1 <= 0, n2 <= 0, nratio <= 0)) {
stop("n1, n2, and nratio must be positive")
}

# alpha and power must fall within the interval [0, 1]
Expand Down
1 change: 0 additions & 1 deletion man/balsakhi.Rd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 0 additions & 1 deletion man/clustered.Rd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

9 changes: 4 additions & 5 deletions man/twomeans.Rd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

27 changes: 27 additions & 0 deletions tests/testthat/test_data.r
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
context("Check balsakhi data")

test_that("Number of rows", {
data(balsakhi)
expect_equal(nrow(balsakhi), 10198)
})

test_that("Number of columns", {
data(balsakhi)
expect_equal(ncol(balsakhi), 76)
})

test_that("Mean of post_totnorm in control group", {
data(balsakhi)
expect_equal(mean(balsakhi[which(balsakhi$bal == 0), "post_totnorm"],
na.rm = TRUE),
0.4288781)
})

test_that("Standard deviation of post_totnorm in control group", {
data(balsakhi)
expect_equal(sd(balsakhi[which(balsakhi$bal == 0), "post_totnorm"],
na.rm = TRUE),
1.15142,
tolerance = 1e-6)
})

4 changes: 2 additions & 2 deletions tests/testthat/test_twomeans_params.r
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,9 @@ context("twomeans parameters")

test_that("non-negative values", {
expect_error(twomeans(n1 = 0, n2 = 2, sd = 5),
"n1 and n2 must be positive")
"n1, n2, and nratio must be positive")
expect_error(twomeans(n1 = 0, n2 = 2, sd = 5),
"n1 and n2 must be positive")
"n1, n2, and nratio must be positive")
expect_error(twomeans(m1 = 12, m2 = 16, sd = 0),
"sd must be positive")
})
Expand Down
6 changes: 6 additions & 0 deletions tests/testthat/test_twomeans_results.r
Original file line number Diff line number Diff line change
Expand Up @@ -6,3 +6,9 @@ test_that("sampsi 12 16, sd(5) power(0.8)", {
expect_equal(calc$n2, 25)
})

test_that("sampsi 12 15, sd1(5) sd2(7) r(0.5) power(0.8)", {
calc <- twomeans(m1 = 12, m2 = 15, nratio = 0.5, sd1 = 5, sd2 = 7)
expect_equal(calc$n1, 108)
expect_equal(calc$n2, 54)
})

0 comments on commit 54b164b

Please sign in to comment.