Skip to content

Commit f19dac1

Browse files
committed
starting to fix the unit tests after changes
1 parent f4ff59d commit f19dac1

File tree

4 files changed

+61
-51
lines changed

4 files changed

+61
-51
lines changed

R/prep_data.R

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -76,6 +76,12 @@ reshape_ald_to_wide <- function(df) {
7676
}
7777

7878
##TODO: check this
79+
#' Convert from wide to long format
80+
#'
81+
#' @import tidyr
82+
#' @import stringr
83+
#'
84+
#' @examples
7985
reshape_ald_to_long <- function(df) {
8086

8187
# Separate the 'colname' into 'stat', 'variable', and 'study' columns

tests/testthat/test-ALD_stats.R

Lines changed: 14 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -11,9 +11,9 @@ ald <- list(
1111

1212
strategy <- list(family = binomial(link = "logit"))
1313

14-
## ALD_stats() ----
15-
test_that("ALD_stats() returns mean and variance", {
16-
res <- ALD_stats(strategy, ald, scale = "log_odds")
14+
## calc_ALD_stats() ----
15+
test_that("calc_ALD_stats() returns mean and variance", {
16+
res <- calc_ALD_stats(strategy, ald, scale = "log_odds")
1717
expect_type(res, "list")
1818
expect_named(res, c("mean", "var"))
1919
expect_type(res$mean, "double")
@@ -33,39 +33,39 @@ test_that("marginal_treatment_effect() calculates correct difference", {
3333
expect_type(res, "double")
3434
})
3535

36-
## Edge Cases for ALD_stats() ----
37-
test_that("ALD_stats() handles NULL or empty ald", {
38-
expect_error(ALD_stats(strategy, NULL))
39-
expect_error(ALD_stats(strategy, list()))
36+
## Edge Cases for calc_ALD_stats() ----
37+
test_that("calc_ALD_stats() handles NULL or empty ald", {
38+
expect_error(calc_ALD_stats(strategy, NULL))
39+
expect_error(calc_ALD_stats(strategy, list()))
4040
})
4141

42-
test_that("ALD_stats() handles missing treatment labels", {
42+
test_that("calc_ALD_stats() handles missing treatment labels", {
4343
ald_missing <- list(
4444
y.B.sum = 30,
4545
N.B = 100
4646
# C is missing
4747
)
48-
expect_error(ALD_stats(strategy, ald_missing))
48+
expect_error(calc_ALD_stats(strategy, ald_missing))
4949
})
5050

51-
test_that("ALD_stats() handles incorrect data types", {
51+
test_that("calc_ALD_stats() handles incorrect data types", {
5252
ald_wrong <- list(
5353
y.B.sum = "thirty",
5454
N.B = 100,
5555
y.C.sum = 20,
5656
N.C = "one hundred"
5757
)
58-
expect_error(ALD_stats(strategy, ald_wrong))
58+
expect_error(calc_ALD_stats(strategy, ald_wrong))
5959
})
6060

61-
test_that("ALD_stats() handles extreme values", {
61+
test_that("calc_ALD_stats() handles extreme values", {
6262
ald_extreme <- list(
6363
y.B.sum = 0, # Zero events
6464
N.B = 100,
6565
y.C.sum = 100, # All events
6666
N.C = 100
6767
)
68-
res <- ALD_stats(strategy, ald_extreme, scale = "log_odds")
68+
res <- calc_ALD_stats(strategy, ald_extreme, scale = "log_odds")
6969
expect_type(res$mean, "double")
7070
expect_type(res$var, "double")
7171
})
@@ -81,7 +81,7 @@ test_that("marginal_variance() handles zero and full counts", {
8181
N.C = 100
8282
)
8383

84-
res <- ALD_stats(strategy, ald_extreme, scale = "log_odds") |> unlist()
84+
res <- calc_ALD_stats(strategy, ald_extreme, scale = "log_odds") |> unlist()
8585

8686
expect_true(all(is.finite(res)))
8787
})

tests/testthat/test-IPD_stats.R

Lines changed: 31 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -46,52 +46,52 @@ ipd <- data.frame(
4646
)
4747

4848
## General Tests ----
49-
test_that("IPD_stats() works for MAIC", {
50-
res <- IPD_stats(strategy_maic, ipd, ald, scale = "log_odds")
49+
test_that("calc_IPD_stats() works for MAIC", {
50+
res <- calc_IPD_stats(strategy_maic, ipd, ald, scale = "log_odds")
5151
expect_type(res$mean, "double")
5252
expect_type(res$var, "double")
5353
})
5454

55-
test_that("IPD_stats() works for STC", {
56-
res <- IPD_stats(strategy_stc, ipd, ald, scale = "log_odds")
55+
test_that("calc_IPD_stats() works for STC", {
56+
res <- calc_IPD_stats(strategy_stc, ipd, ald, scale = "log_odds")
5757
expect_type(res$mean, "double")
5858
expect_type(res$var, "double")
5959
})
6060

61-
test_that("IPD_stats() works for G-computation (ML)", {
62-
res <- IPD_stats(strategy_gcomp_ml, ipd, ald, scale = "log_odds")
61+
test_that("calc_IPD_stats() works for G-computation (ML)", {
62+
res <- calc_IPD_stats(strategy_gcomp_ml, ipd, ald, scale = "log_odds")
6363
expect_type(res$mean, "double")
6464
expect_type(res$var, "double")
6565
})
6666

67-
test_that("IPD_stats() works for G-computation (Stan)", {
68-
res <- IPD_stats(strategy_gcomp_stan, ipd, ald, scale = "log_odds")
67+
test_that("calc_IPD_stats() works for G-computation (Stan)", {
68+
res <- calc_IPD_stats(strategy_gcomp_stan, ipd, ald, scale = "log_odds")
6969
expect_type(res$mean, "double")
7070
expect_type(res$var, "double")
7171
})
7272

73-
test_that("IPD_stats() works for Multiple Imputation Marginalisation", {
74-
res <- IPD_stats(strategy_mim, ipd, ald, scale = "log_odds")
73+
test_that("calc_IPD_stats() works for Multiple Imputation Marginalisation", {
74+
res <- calc_IPD_stats(strategy_mim, ipd, ald, scale = "log_odds")
7575
expect_type(res$mean, "double")
7676
expect_type(res$var, "double")
7777
})
7878

7979
## Edge Cases ----
80-
test_that("IPD_stats() handles NULL or empty inputs", {
81-
expect_error(IPD_stats(strategy_maic, NULL, ald, scale = "log_odds"))
82-
expect_error(IPD_stats(strategy_maic, ipd, NULL, scale = "log_odds"))
83-
expect_error(IPD_stats(strategy_maic, list(), ald, scale = "log_odds"))
80+
test_that("calc_IPD_stats() handles NULL or empty inputs", {
81+
expect_error(calc_IPD_stats(strategy_maic, NULL, ald, scale = "log_odds"))
82+
expect_error(calc_IPD_stats(strategy_maic, ipd, NULL, scale = "log_odds"))
83+
expect_error(calc_IPD_stats(strategy_maic, list(), ald, scale = "log_odds"))
8484
})
8585

86-
test_that("IPD_stats() handles unexpected input types", {
86+
test_that("calc_IPD_stats() handles unexpected input types", {
8787
ipd_wrong <- list(y = "1", trt = "A")
8888
ald_wrong <- list(y.A.sum = "thirty", N.A = "one hundred")
8989

90-
expect_error(IPD_stats(strategy_maic, ipd_wrong, ald, scale = "log_odds"))
91-
expect_error(IPD_stats(strategy_maic, ipd, ald_wrong, scale = "log_odds"))
90+
expect_error(calc_IPD_stats(strategy_maic, ipd_wrong, ald, scale = "log_odds"))
91+
expect_error(calc_IPD_stats(strategy_maic, ipd, ald_wrong, scale = "log_odds"))
9292
})
9393

94-
test_that("IPD_stats() handles extreme values", {
94+
test_that("calc_IPD_stats() handles extreme values", {
9595
ipd_extreme <- data.frame(
9696
y = c(1, 1, 1, 1, 0, 0, 0, 0),
9797
trt = c("A", "A", "A", "A", "C", "C", "C", "C")
@@ -102,46 +102,46 @@ test_that("IPD_stats() handles extreme values", {
102102
y.C.sum = 100, # All events
103103
N.C = 100
104104
)
105-
res <- IPD_stats(strategy_maic, ipd_extreme, ald_extreme, scale = "log_odds")
105+
res <- calc_IPD_stats(strategy_maic, ipd_extreme, ald_extreme, scale = "log_odds")
106106
expect_type(res$mean, "double")
107107
expect_type(res$var, "double")
108108
})
109109

110-
test_that("IPD_stats() handles unsupported strategies", {
110+
test_that("calc_IPD_stats() handles unsupported strategies", {
111111
strategy_invalid <- list(class = "unsupported")
112-
expect_error(IPD_stats(strategy_invalid, ipd, ald, scale = "log_odds"))
112+
expect_error(calc_IPD_stats(strategy_invalid, ipd, ald, scale = "log_odds"))
113113
})
114114

115-
test_that("IPD_stats() handles missing columns", {
115+
test_that("calc_IPD_stats() handles missing columns", {
116116
ipd_missing <- data.frame(
117117
y = c(1, 0, 1, 0),
118118
# trt column missing
119119
z = c("A", "A", "C", "C")
120120
)
121-
expect_error(IPD_stats(strategy_maic, ipd_missing, ald))
121+
expect_error(calc_IPD_stats(strategy_maic, ipd_missing, ald))
122122
})
123123

124-
test_that("IPD_stats() handles different link functions", {
124+
test_that("calc_IPD_stats() handles different link functions", {
125125
strategy_log <- list(class = "stc", formula = y ~ trt, family = binomial(link = "log"))
126126
strategy_identity <- list(class = "stc", formula = y ~ trt, family = binomial(link = "identity"))
127127

128-
res_log <- IPD_stats(strategy_log, ipd, ald, scale = "log_odds")
129-
res_identity <- IPD_stats(strategy_identity, ipd, ald, scale = "log_odds")
128+
res_log <- calc_IPD_stats(strategy_log, ipd, ald, scale = "log_odds")
129+
res_identity <- calc_IPD_stats(strategy_identity, ipd, ald, scale = "log_odds")
130130

131131
expect_type(res_log$mean, "double")
132132
expect_type(res_log$var, "double")
133133
expect_type(res_identity$mean, "double")
134134
expect_type(res_identity$var, "double")
135135
})
136136

137-
test_that("IPD_stats() handles unsupported link functions", {
137+
test_that("calc_IPD_stats() handles unsupported link functions", {
138138
strategy_unknown <- list(class = "stc",
139139
formula = y ~ trt,
140140
family = list(link = "unknown"))
141-
expect_error(IPD_stats(strategy_unknown, ipd, ald, scale = "log_odds"))
141+
expect_error(calc_IPD_stats(strategy_unknown, ipd, ald, scale = "log_odds"))
142142
})
143143

144-
test_that("IPD_stats() handles negative or NA values", {
144+
test_that("calc_IPD_stats() handles negative or NA values", {
145145
ipd_negative <- data.frame(
146146
y = c(-1, 0, 1, 0),
147147
trt = c("A", "A", "C", "C")
@@ -152,6 +152,6 @@ test_that("IPD_stats() handles negative or NA values", {
152152
y.C.sum = 20,
153153
N.C = 100
154154
)
155-
expect_error(IPD_stats(strategy_maic, ipd_negative, ald, scale = "log_odds"))
156-
expect_error(IPD_stats(strategy_maic, ipd, ald_na, scale = "log_odds"))
155+
expect_error(calc_IPD_stats(strategy_maic, ipd_negative, ald, scale = "log_odds"))
156+
expect_error(calc_IPD_stats(strategy_maic, ipd, ald_na, scale = "log_odds"))
157157
})

tests/testthat/test-gcomp.R

Lines changed: 10 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -76,7 +76,8 @@ test_that("compare with stdReg2 package for continuous outcome", {
7676
select(qsmk, sex, age, wt82_71) |>
7777
rename(trt = qsmk,
7878
y = wt82_71) |>
79-
mutate(sex = as.numeric(sex) - 1)
79+
mutate(sex = as.numeric(sex) - 1,
80+
trt = factor(trt, labels = c("C", "A")))
8081

8182
lin_form <- as.formula(y ~ trt * (sex + age))
8283

@@ -146,8 +147,9 @@ test_that("compare with stdReg2 package for binary outcome", {
146147
data <- data.frame(
147148
age = rnorm(n, mean = 50, sd = 10),
148149
sex = rbinom(n, 1, 0.5),
149-
trt = rbinom(n, 1, 0.5)
150-
)
150+
trt = rbinom(n, 1, 0.5))
151+
152+
data$trt <- factor(data$trt, labels = c("C", "A"))
151153

152154
# generate a binary outcome with logit link
153155
logit_p <- -1 + data$trt * (0.03 * data$age + 0.2 * data$sex)
@@ -236,9 +238,10 @@ test_that("compare with marginaleffects package for binary outcome", {
236238
data <- data.frame(
237239
age = rnorm(n, mean = 50, sd = 10),
238240
sex = rbinom(n, 1, 0.5),
239-
trt = rbinom(n, 1, 0.5)
240-
)
241+
trt = rbinom(n, 1, 0.5))
241242

243+
data$trt <- factor(data$trt, labels = c("C", "A"))
244+
242245
# generate a binary outcome with logit link
243246
logit_p <- -1 + data$trt * (0.03 * data$age + 0.2 * data$sex)
244247
prob <- 1 / (1 + exp(-logit_p))
@@ -311,7 +314,8 @@ test_that("compare with marginaleffects package for continuous outcome", {
311314
select(qsmk, sex, age, wt82_71) |>
312315
rename(trt = qsmk,
313316
y = wt82_71) |>
314-
mutate(sex = as.numeric(sex) - 1)
317+
mutate(sex = as.numeric(sex) - 1,
318+
trt = factor(trt, labels = c("C", "A")))
315319

316320
lin_form <- as.formula(y ~ trt * (sex + age))
317321

0 commit comments

Comments
 (0)