Skip to content

Commit d63225e

Browse files
wanjau_merckwanjau_merck
authored andcommitted
to_integer.R tests
1 parent ff9a1d5 commit d63225e

File tree

2 files changed

+273
-0
lines changed

2 files changed

+273
-0
lines changed

tests/testthat/helper-fns-ind-test.R

Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
# Helper function to calculate sample size ratio
2+
calculate_ratio <- function(ratio=1) {
3+
if (!is.numeric(ratio) ) {
4+
stop("The Ratio value provided must be numeric")
5+
}
6+
return(ratio)
7+
}
8+
9+
# Helper function to calculate significance level (alpha)
10+
calculate_alpha <- function(alpha=0.025){
11+
if (!is.numeric(alpha) ) {
12+
stop("Alpha value provided must be numeric")
13+
}
14+
return(alpha) # Return the desired significance level
15+
}
16+
17+
# Helper function to calculate beta (type II error rate)
18+
calculate_beta <- function(beta=0.1) {
19+
if (!is.numeric(beta) ) {
20+
stop("Beta value provided must be numeric")
21+
}
22+
return(beta) # Return the desired beta
23+
}
24+
25+
# Helper function to calculate study duration
26+
calculate_study_duration <- function(duration=36) {
27+
if (!is.numeric(duration) ) {
28+
stop("Duration must be numeric")
29+
}
30+
return(duration) # Return the desired study duration
31+
}
32+
33+
34+
# Helper function to calculate the enrollment rate
35+
enroll_rate <- function(duration = 18,rate = 20) {
36+
37+
if (!is.numeric(duration) || !is.numeric(rate)) {
38+
stop("Duration and rate must be numeric")
39+
}
40+
return(gsDesign2::define_enroll_rate(duration, rate))
41+
}
42+
43+
#Helper function to calculate the fail rate
44+
fail_rate <- function(duration = c(4, 100),fail_rate = log(2) / 12, dropout_rate = .001,hr = c(1, .6)) {
45+
# Check if duration, fail_rate, dropout_rate, and hr are provided and are numeric, if not, throw an error
46+
if (!is.numeric(duration) || !is.numeric(fail_rate) || !is.numeric(dropout_rate) || !is.numeric(hr)) {
47+
stop("All parameters must be numeric values.")
48+
}
49+
50+
# If the check passes, proceed with defining fail_rate
51+
return(gsDesign2::define_fail_rate(duration, fail_rate, dropout_rate, hr))
52+
}
Lines changed: 221 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,221 @@
1+
test_that("fixed_design_ahr function works correctly", {
2+
# Creating the fixed design object
3+
x <- fixed_design_ahr(
4+
alpha = calculate_alpha(), power = calculate_beta(),
5+
enroll_rate = enroll_rate(),
6+
fail_rate = fail_rate(),
7+
study_duration = calculate_study_duration()
8+
) %>% to_integer()
9+
10+
# Test that to_integer converts sample size to integer for fixed_design_ahr
11+
expect_equal(x$analysis$n, round(x$analysis$n))
12+
13+
# Testing enroll_rate components
14+
expect_s3_class(x$input$enroll_rate, "tbl_df")
15+
16+
# Testing fail_rate components
17+
expect_s3_class(x$input$fail_rate, "tbl_df")
18+
19+
# Testing design attribute
20+
expect_equal(attributes(x)$class, c("fixed_design", "list"))
21+
expect_equal(x$design, "ahr")
22+
# Check that the sample size (N) is non-negative integer
23+
expect_true(x$analysis$n >= 0)
24+
25+
# Verify that alpha and power are within the correct range
26+
expect_true(x$input$alpha > 0 & x$input$alpha < 1)
27+
expect_true(x$input$power > 0 & x$input$power <= 1)
28+
29+
# Ensure enroll_rate and fail_rate tables have non-negative values
30+
expect_true(all(x$input$enroll_rate$rate >= 0))
31+
expect_true(all(x$input$fail_rate$fail_rate >= 0))
32+
33+
# Check that dropout_rate is between 0 and 1
34+
expect_true(all(x$input$fail_rate$dropout_rate >= 0 & x$input$fail_rate$dropout_rate <= 1))
35+
36+
# Check if summary output is in the correct format and has expected names
37+
summary_x <- summary(x)
38+
expect_s3_class(summary_x, "tbl_df")
39+
expect_equal(ncol(summary_x), 7)
40+
expect_named(summary_x, c("Design", "N", "Events", "Time", "Bound", "alpha", "Power"))
41+
})
42+
43+
test_that("fixed_design_fh function works correctly", {
44+
# Creating the fixed design object
45+
x <- fixed_design_fh(
46+
alpha = calculate_alpha(), power = calculate_beta(),
47+
enroll_rate = enroll_rate(),
48+
fail_rate = fail_rate(),
49+
rho = 0.5, gamma = 0.5,
50+
study_duration = calculate_study_duration(), ratio = calculate_ratio()
51+
) %>% to_integer()
52+
53+
# Test that to_integer converts sample size to integer for fixed_design_fh
54+
expect_equal(x$analysis$n, round(x$analysis$n))
55+
56+
# Testing enroll_rate components
57+
expect_s3_class(x$input$enroll_rate, "tbl_df")
58+
59+
# Testing fail_rate components
60+
expect_s3_class(x$input$fail_rate, "tbl_df")
61+
62+
# Testing design attribute
63+
expect_equal(attributes(x)$class, c("fixed_design", "list"))
64+
expect_equal(x$design, "fh")
65+
})
66+
67+
test_that("fixed_design_mb function works correctly", {
68+
# Creating the fixed design object
69+
x <- fixed_design_mb(
70+
alpha = calculate_alpha(), power = calculate_beta(),
71+
enroll_rate = enroll_rate(),
72+
fail_rate = fail_rate(),
73+
tau = 4,
74+
study_duration = calculate_study_duration(), ratio = calculate_ratio()
75+
) %>% to_integer()
76+
77+
# Test that to_integer converts sample size to integer for fixed_design_mb
78+
expect_equal(x$analysis$n, round(x$analysis$n))
79+
80+
# Testing enroll_rate components
81+
expect_s3_class(x$input$enroll_rate, "tbl_df")
82+
83+
# Testing fail_rate components
84+
expect_s3_class(x$input$fail_rate, "tbl_df")
85+
86+
# Testing design attribute
87+
expect_equal(attributes(x)$class, c("fixed_design", "list"))
88+
expect_equal(x$design, "mb")
89+
90+
# Check that the sample size (N) and events are non-negative integers
91+
expect_true(x$analysis$n >= 0)
92+
expect_equal(x$analysis$event, round(x$analysis$event), tolerance = 1e-6)
93+
94+
# Validate that alpha and power values are within the correct range
95+
expect_true(x$input$alpha > 0 & x$input$alpha < 1)
96+
expect_true(x$input$power > 0 & x$input$power <= 1)
97+
98+
# Ensure that enroll_rate and fail_rate have non-negative values
99+
expect_true(all(x$input$enroll_rate$rate >= 0))
100+
expect_true(all(x$input$fail_rate$fail_rate >= 0))
101+
102+
# Check that dropout_rate is between 0 and 1
103+
expect_true(all(x$input$fail_rate$dropout_rate >= 0 & x$input$fail_rate$dropout_rate <= 1))
104+
105+
# Check if the design attribute matches the correct format
106+
expect_match(x$design, "mb")
107+
108+
# Test that the summary output has the correct format and values
109+
summary_x <- summary(x)
110+
expect_s3_class(summary_x, "tbl_df")
111+
expect_equal(ncol(summary_x), 7)
112+
expect_named(summary_x, c("Design", "N", "Events", "Time", "Bound", "alpha", "Power"))
113+
114+
# Ensure that summary values are within expected ranges
115+
expect_true(summary_x$N > 0)
116+
expect_true(summary_x$Events > 0)
117+
expect_true(summary_x$Time > 0)
118+
expect_true(summary_x$alpha > 0 & summary_x$alpha < 1)
119+
expect_true(summary_x$Power > 0 & summary_x$Power <= 1)
120+
121+
# Ensure tau is correctly reflected in the Design column of the summary
122+
expect_match(summary_x$Design, "Modestly weighted LR: tau = 4")
123+
})
124+
125+
126+
127+
test_that("fixed_design_ahr handles invalid inputs", {
128+
# Pass an invalid alpha value
129+
expect_error(fixed_design_ahr(
130+
alpha = -0.01, power = 0.9,
131+
enroll_rate = define_enroll_rate(duration = 18, rate = 1),
132+
fail_rate = define_fail_rate(
133+
duration = c(4, 100),
134+
fail_rate = log(2) / 12, hr = c(1, .6),
135+
dropout_rate = .001
136+
),
137+
study_duration = 36
138+
), "must have 0 < alpha < 1 - beta < 1!")
139+
140+
})
141+
142+
test_that("to_integer with fixed_design_ahr returns correct results", {
143+
x <- fixed_design_ahr(
144+
alpha = 0.025, power = 0.9,
145+
enroll_rate = define_enroll_rate(duration = 18, rate = 1),
146+
fail_rate = define_fail_rate(
147+
duration = c(4, 100),
148+
fail_rate = log(2) / 12, hr = c(1, .6),
149+
dropout_rate = .001
150+
),
151+
study_duration = 36
152+
)
153+
154+
result <- to_integer(x)
155+
156+
# Check the class of the result
157+
expect_s3_class(result, "fixed_design")
158+
159+
# Check if sample size and event are integers
160+
expect_equal(result$analysis$n, round(result$analysis$n))
161+
expect_equal(result$analysis$event, round(result$analysis$event), tolerance = 1e-6)
162+
})
163+
164+
test_that("to_integer with fixed_design_fh returns correct results", {
165+
x <- fixed_design_fh(
166+
alpha = 0.025, power = 0.9,
167+
enroll_rate = define_enroll_rate(duration = 18, rate = 20),
168+
fail_rate = define_fail_rate(
169+
duration = c(4, 100),
170+
fail_rate = log(2) / 12,
171+
hr = c(1, .6),
172+
dropout_rate = .001
173+
),
174+
rho = 0.5, gamma = 0.5,
175+
study_duration = 36, ratio = 1
176+
)
177+
178+
result <- to_integer(x)
179+
180+
# Check the class of the result
181+
expect_s3_class(result, "fixed_design")
182+
183+
# Check if sample size and event are integers
184+
expect_equal(result$analysis$n, round(result$analysis$n))
185+
expect_equal(result$analysis$event, round(result$analysis$event),tolerance = 1e-6)
186+
# Validate if the design attribute is correctly set
187+
expect_equal(attributes(result)$class, c("fixed_design", "list"))
188+
expect_equal(result$design, "fh")
189+
190+
# Ensure the study duration is positive and within an acceptable range
191+
expect_true(result$input$study_duration > 0)
192+
193+
# Check that the alpha and power values are within expected limits
194+
expect_true(result$input$alpha > 0 & result$input$alpha < 1)
195+
expect_true(result$input$power > 0 & result$input$power <= 1)
196+
197+
# Verify that rho and gamma parameters are within the correct range (0, 1)
198+
expect_true(result$input$rho >= 0 & result$input$rho <= 1)
199+
expect_true(result$input$gamma >= 0 & result$input$gamma <= 1)
200+
201+
# Ensure enroll_rate and fail_rate tables have valid non-negative values
202+
expect_true(all(result$input$enroll_rate$rate >= 0))
203+
expect_true(all(result$input$fail_rate$fail_rate >= 0))
204+
205+
# Validate that dropout_rate is between 0 and 1
206+
expect_true(all(result$input$fail_rate$dropout_rate >= 0 & result$input$fail_rate$dropout_rate <= 1))
207+
208+
# Test if the summary output is in the correct format and has expected names
209+
summary_result <- summary(result)
210+
expect_s3_class(summary_result, "tbl_df")
211+
expect_equal(ncol(summary_result), 7)
212+
expect_named(summary_result, c("Design", "N", "Events", "Time", "Bound", "alpha", "Power"))
213+
214+
# Ensure that the output summary values are within acceptable ranges
215+
expect_true(summary_result$N > 0)
216+
expect_true(summary_result$Events > 0)
217+
expect_true(summary_result$Time > 0)
218+
expect_true(summary_result$alpha > 0 & summary_result$alpha < 1)
219+
expect_true(summary_result$Power > 0 & summary_result$Power <= 1)
220+
})
221+

0 commit comments

Comments
 (0)