-
Notifications
You must be signed in to change notification settings - Fork 9
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Showing
4 changed files
with
249 additions
and
70 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,34 +1,49 @@ | ||
context("Checking konfound") | ||
|
||
library(forcats) | ||
library(lme4) | ||
|
||
m1 <- lm(mpg ~ wt + hp, data = mtcars) | ||
output1 <- konfound(m1, wt, to_return = "raw_output") | ||
|
||
# d <- forcats::gss_cat | ||
# d$married <- ifelse(d$marital == "Married", 1, 0) | ||
# m2 <- glm(married ~ age, data = d, family = binomial(link = "logit")) | ||
# output2 <- konfound(m2, age, to_return = "raw_output") | ||
|
||
m3 <- fm1 <- lme4::lmer(Reaction ~ Days + (1 | Subject), sleepstudy) | ||
output3 <- konfound(m3, Days, to_return = "raw_output") | ||
|
||
m4 <- glm(outcome ~ condition, data = binary_dummy_data, family = binomial(link = "logit")) | ||
output4 <- konfound(m4, condition, two_by_two = TRUE, n_treat = 55, to_return = "raw_output") | ||
|
||
test_that("konfound works for linear model", { | ||
expect_equal(output1$perc_bias_to_change, 66.57696, tolerance = .001) | ||
library(forcats) | ||
library(lme4) | ||
m1 <- lm(mpg ~ wt + hp, data = mtcars) | ||
output1 <- konfound(m1, wt, to_return = "raw_output") | ||
expect_equal(output1$RIR_perc, 66.629, tolerance = .01) | ||
}) | ||
|
||
# test_that("konfound works for glm, 2x2 model", { | ||
# expect_equal(output2$percent_bias_to_change_inference, 35.357, tolerance = .001) | ||
# }) | ||
|
||
test_that("konfound works for lme4 model", { | ||
expect_equal(output3$perc_bias_to_change, 84.826, tolerance = .001) | ||
|
||
m3 <- lme4::lmer(Reaction ~ Days + (1 | Subject), sleepstudy) | ||
output3 <- konfound(m3, Days, to_return = "raw_output") | ||
expect_equal(output3$RIR_perc, 84.826, tolerance = .001) | ||
}) | ||
|
||
test_that("konfound works for glm, 2x2 model", { | ||
expect_equal(output4$RIR, 15) | ||
m4 <- glm(outcome ~ condition, data = binary_dummy_data, family = binomial(link = "logit")) | ||
output4 <- konfound(m4, condition, two_by_two = TRUE, n_treat = 55, to_return = "raw_output") | ||
expect_equal(output4$RIR_primary, 15) | ||
|
||
m4 <- glm(outcome ~ condition, data = binary_dummy_data, family = binomial(link = "logit")) | ||
output4_print <- capture.output(konfound(m4, condition, two_by_two = TRUE, n_treat = 55, to_return = "print")) | ||
expect_true(length(output4_print) > 0) | ||
}) | ||
|
||
|
||
test_that("konfound returns a tibble", { | ||
m5 <- lm(mpg ~ wt + hp, data = mtcars) | ||
output5 <- konfound(m5, wt, to_return = "table") | ||
|
||
expect_s3_class(output5, "tbl_df") | ||
|
||
mtcars$my_var <- runif(nrow(mtcars)) | ||
m5b <- lm(wt ~ my_var, data = mtcars) | ||
output5b <- konfound(m5b, my_var, to_return = "table") | ||
|
||
expect_s3_class(output5b, "tbl_df") | ||
}) | ||
|
||
test_that("konfound glm works", { | ||
gss_cat$married <- ifelse(gss_cat$marital == "Married", 1, 0) | ||
|
||
m6 <- glm(married ~ age, data = gss_cat, family = binomial(link = "logit")) | ||
m6_output <- konfound(m6, age, to_return = "raw_output") | ||
|
||
expect_equal(as.vector(m6_output$RIR_perc), 35.357, tolerance = .001) | ||
}) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,17 +1,12 @@ | ||
context("Checking mkonfound") | ||
|
||
library(lme4) | ||
library(mice) | ||
|
||
popmis <- popmis[1:100, ] | ||
|
||
testmod1 <- lm(teachpop ~ texp + sex, data = popmis) | ||
testmod2 <- lmer(teachpop ~ texp + sex + (1 | school), data = popmis) | ||
|
||
output1 <- konfound(testmod1, texp, test_all = TRUE, to_return = "raw_output") | ||
output2 <- konfound(testmod2, texp, test_all = TRUE, to_return = "raw_output") | ||
test_that("mkonfound produces table", { | ||
d <- mkonfound(mkonfound_ex, t, df) | ||
expect_is(d, "data.frame") | ||
}) | ||
|
||
test_that("mkonfound works for lm and lmer outout", { | ||
expect_is(output1, "data.frame") | ||
expect_is(output2, "data.frame") | ||
test_that("mkonfound produces plot", { | ||
p <- mkonfound(mkonfound_ex, t, df, return_plot = TRUE) | ||
p | ||
expect_s3_class(p, "ggplot") | ||
}) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,52 +1,170 @@ | ||
context("Checking pkonfound") | ||
|
||
library(lme4) | ||
library(mice) | ||
|
||
popmis <- popmis[1:100, ] | ||
testmod1 <- lm(teachpop ~ texp + sex, data = popmis) | ||
testmod2 <- lmer(teachpop ~ texp + sex + (1 | school), data = popmis) | ||
|
||
output1 <- konfound(testmod1, texp, test_all = TRUE, to_return = "raw_output") | ||
output2 <- konfound(testmod2, texp, test_all = TRUE, to_return = "raw_output") | ||
|
||
output3 <- pkonfound(a = 35, b = 17, c = 17, d = 38, to_return = "raw_output") | ||
|
||
test_that("pkonfound test positive, significant coefficient works", { | ||
expect_equal(pkonfound(2, .4, 100, 3, to_return = "raw_output")$perc_bias_to_change, tolerance = .001, 60.28953) # pct bias | ||
expect_equal(pkonfound(2, .4, 100, 3, to_return = "raw_output")$rxcvGz, tolerance = .001, 0.568) # r-corr | ||
expect_equal(pkonfound(2, .4, 100, 3, to_return = "raw_output")$itcvGz, tolerance = .001, 0.3224053) # impact | ||
expect_equal(pkonfound(2, .4, 100, 3, to_return = "raw_output")$perc_bias_to_change, tolerance = .001, .603 * 100) # pct bias | ||
expect_equal(pkonfound(2, .4, 100, 3, to_return = "raw_output")$rxcvGz, tolerance = .001, 0.566) # rxcvGz | ||
expect_equal(pkonfound(2, .4, 100, 3, to_return = "raw_output")$itcvGz, tolerance = .001, 0.321) # itcvGz | ||
}) | ||
|
||
test_that("pkonfound test negative, significant coefficient works", { | ||
expect_equal(pkonfound(-2, .4, 100, 3, to_return = "raw_output")$perc_bias_to_change, tolerance = .001, 60.28953) | ||
expect_equal(pkonfound(-2, .4, 100, 3, to_return = "raw_output")$rxcvGz, tolerance = .001, 0.568) | ||
expect_equal(pkonfound(-2, .4, 100, 3, to_return = "raw_output")$itcvGz, tolerance = .001, -0.3224053) | ||
expect_equal(pkonfound(-2, .4, 100, 3, to_return = "raw_output")$perc_bias_to_change, tolerance = .001, .603 * 100) | ||
expect_equal(pkonfound(-2, .4, 100, 3, to_return = "raw_output")$rxcvGz, tolerance = .001, 0.566) | ||
expect_equal(pkonfound(-2, .4, 100, 3, to_return = "raw_output")$itcvGz, tolerance = .001, -0.321) | ||
}) | ||
|
||
test_that("pkonfound test positive, not significant coefficient works", { | ||
expect_equal(pkonfound(1, .4, 100, 3, to_return = "raw_output")$perc_bias_to_change, tolerance = .001, 20.579) | ||
expect_equal(pkonfound(1, .4, 100, 3, to_return = "raw_output")$rxcvGz, tolerance = .001, 0.248) | ||
expect_equal(pkonfound(1, .4, 100, 3, to_return = "raw_output")$itcvGz, tolerance = .001, 0.061) | ||
expect_equal(pkonfound(1, .4, 100, 3, to_return = "raw_output")$perc_bias_to_change, tolerance = .001, .206 * 100) | ||
expect_equal(pkonfound(1, .4, 100, 3, to_return = "raw_output")$rxcvGz, tolerance = .001, 0.247) | ||
expect_equal(pkonfound(1, .4, 100, 3, to_return = "raw_output")$itcvGz, tolerance = .001, 0.061) | ||
}) | ||
|
||
test_that("pkonfound test negative, not significant coefficient works", { | ||
expect_equal(pkonfound(-1, .4, 100, 3, to_return = "raw_output")$perc_bias_to_change, tolerance = .001, 20.579) | ||
expect_equal(pkonfound(-1, .4, 100, 3, to_return = "raw_output")$rxcvGz, tolerance = .001, 0.248) | ||
expect_equal(pkonfound(-1, .4, 100, 3, to_return = "raw_output")$itcvGz, tolerance = .001, -.061) | ||
expect_equal(pkonfound(-1, .4, 100, 3, to_return = "raw_output")$perc_bias_to_change, tolerance = .001, 0.206 * 100) | ||
expect_equal(pkonfound(-1, .4, 100, 3, to_return = "raw_output")$rxcvGz, tolerance = .001, 0.247) | ||
expect_equal(pkonfound(-1, .4, 100, 3, to_return = "raw_output")$itcvGz, tolerance = .001, -.061) | ||
}) | ||
|
||
test_that("pkonfound creates the threshhold plot", { | ||
thresh_plot <- pkonfound(2, .4, 100, 3, to_return = "thresh_plot") | ||
|
||
expect_s3_class(thresh_plot, "ggplot") | ||
|
||
thresh_plot_null <- pkonfound(.01, .4, 100, 3, to_return = "thresh_plot") | ||
|
||
expect_s3_class(thresh_plot_null, "ggplot") | ||
}) | ||
|
||
test_that("pkonfound creates the correlation plot", { | ||
corr_plot <- pkonfound(2, .4, 100, 3, to_return = "corr_plot") | ||
|
||
expect_s3_class(corr_plot, "ggplot") | ||
|
||
corr_plot_null <- pkonfound(.01, .4, 100, 3, to_return = "corr_plot") | ||
|
||
expect_s3_class(corr_plot_null, "ggplot") | ||
}) | ||
|
||
expect_output(pkonfound(2, .4, 100, 3), ".") | ||
|
||
## PSE and COP | ||
|
||
test_that("PSE and COE work via pkonfound", { | ||
|
||
output4 <- pkonfound(est_eff = -.125, | ||
std_err = .050, | ||
n_obs = 6265, | ||
n_covariates = 7, | ||
sdx = .217, | ||
sdy = .991, | ||
R2 = .251, | ||
eff_thr = 0, | ||
FR2max = .61, | ||
index = "COP", | ||
to_return = "raw_output") | ||
|
||
output5 <- pkonfound(est_eff = .5, | ||
std_err = .056, | ||
n_obs = 6174, | ||
eff_thr = .1, | ||
sdx = 0.22, | ||
sdy = 1, | ||
R2 = .3, | ||
index = "PSE",to_return = "raw_output") | ||
|
||
expect_equal(output4$delta_exact, tolerance = .001, 1.309) # COP - delta exact | ||
|
||
expect_equal(output5$`correlation between X and CV conditional on Z`, tolerance = .001, 0.248) # PSE | ||
expect_equal(output5$`correlation between Y and CV conditional on Z`, tolerance = .001, 0.372) # PSE | ||
expect_equal(output5$`correlation between X and CV`, tolerance = .001, 0.214) # PSE | ||
expect_equal(output5$`correlation between Y and CV`, tolerance = .001, 0.313) # PSE | ||
}) | ||
|
||
test_that("PSE and COE work via pkonfound", { | ||
|
||
output6 <- pkonfound(est_eff = -.125, | ||
std_err = .050, | ||
n_obs = 6265, | ||
n_covariates = 7, | ||
sdx = .217, | ||
sdy = .991, | ||
R2 = .251, | ||
eff_thr = 0, | ||
FR2max = .61, | ||
index = "COP", | ||
to_return = "raw_output") | ||
|
||
expect_s3_class(output6$Figure, "ggplot") | ||
}) | ||
|
||
test_that("pkonfound test positive, significant coefficient, small sample size works", { | ||
expect_equal(pkonfound(2, .3, 20, 3, to_return = "raw_output")$perc_bias_to_change, tolerance = .001, 67.8282) | ||
expect_equal(pkonfound(2, .3, 20, 3, to_return = "raw_output")$rxcvGz, tolerance = .001, 0.863) | ||
expect_equal(pkonfound(2, .3, 20, 3, to_return = "raw_output")$itcvGz, tolerance = .001, 0.74545) | ||
## logistic | ||
|
||
test_that("logistic models work with pkonfound", { | ||
|
||
output7 <- pkonfound(.273, .024, 16999, 3, n_treat = 16000, model_type = "logistic", to_return = "raw_output")$RIR_primary | ||
|
||
expect_equal(output7, 1156) | ||
|
||
output8 <- pkonfound(.027, .024, 16999, 3, n_treat = 16000, model_type = "logistic", to_return = "raw_output")$RIR_primary | ||
|
||
expect_equal(output8, 803) | ||
|
||
output8_print <- capture.output(pkonfound(.027, .024, 16999, 3, n_treat = 16000, model_type = "logistic", to_return = "print")) | ||
|
||
expect_true(length(output8_print) > 0) | ||
|
||
output9 <- pkonfound(.027, .024, 16999, 3, n_treat = 16000, replace = "entire", | ||
model_type = "logistic", to_return = "raw_output")$RIR_primary | ||
|
||
expect_equal(output9, 793) | ||
|
||
output10 <- pkonfound(.027, .024, 16999, 3, n_treat = 16000, switch_trm = FALSE, | ||
model_type = "logistic", to_return = "raw_output")$RIR_primary | ||
|
||
expect_equal(output10, 52) | ||
|
||
}) | ||
|
||
## two by two table | ||
|
||
test_that("two by two works with pkonfound", { | ||
|
||
output11 <- pkonfound(a = 18, b = 12, c = 12, d = 17, to_return = "raw_output")$RIR_primary | ||
expect_equal(output11, 8) | ||
|
||
output12 <- pkonfound(a = 18, b = 12, c = 12, d = 17, switch_trm = FALSE, to_return = "raw_output")$RIR_primary | ||
expect_equal(output12, 5) | ||
|
||
output13 <- pkonfound(a = 18, b = 3, c = 12, d = 1, test = "chisq", to_return = "raw_output")$RIR_primary | ||
expect_equal(output13, 6) | ||
|
||
output14 <- pkonfound(a = 18, b = 1, c = 12, d = 1, switch_trm = FALSE, to_return = "raw_output")$RIR_primary | ||
expect_equal(output14, 5) | ||
}) | ||
|
||
test_that("tkonfound for two_by_two works", { | ||
expect_equal(output3$RIR, tolerance = .001, 14) | ||
test_that("pkonfound printed output works for a positive case", { | ||
outputa <- capture.output(pkonfound(2, .4, 100, 3, to_return = "print", index = "RIR")) | ||
expect_true(length(outputa) > 0) | ||
|
||
outputb <- capture.output(pkonfound(2, .4, 100, 3, to_return = "print", index = "IT")) | ||
expect_true(length(outputb) > 0) | ||
|
||
outputc <- capture.output(pkonfound(.01, .4, 100, 3, to_return = "print", index = "RIR")) | ||
expect_true(length(outputa) > 0) | ||
|
||
outputd <- capture.output(pkonfound(.01, .4, 100, 3, to_return = "print", index = "IT")) | ||
expect_true(length(outputb) > 0) | ||
}) | ||
|
||
test_that("test_all works for lm and lmer outout", { | ||
expect_is(output1, "data.frame") | ||
expect_is(output2, "data.frame") | ||
test_that("pkonfound printed output works for a negative case", { | ||
output <- capture.output(pkonfound(-2.2, .65, 200, 3, to_return = "print", index = "RIR")) | ||
expect_true(length(output) > 0) | ||
|
||
output <- capture.output(pkonfound(-2.2, .65, 200, 3, to_return = "print", index = "IT")) | ||
expect_true(length(output) > 0) | ||
|
||
output <- capture.output(pkonfound(-.01, .65, 200, 3, to_return = "print", index = "RIR")) | ||
expect_true(length(output) > 0) | ||
|
||
output <- capture.output(pkonfound(-.01, .65, 200, 3, to_return = "print", index = "IT")) | ||
expect_true(length(output) > 0) | ||
}) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,51 @@ | ||
context("Checking tkonfound") | ||
|
||
test_that("tkonfound creates the correlation plot", { | ||
|
||
tkonfound_fig <- tkonfound_fig(a = 35, b = 17, c = 17, d = 38)[[3]] | ||
|
||
expect_s3_class(tkonfound_fig, "ggplot") | ||
}) | ||
|
||
test_that("tkonfound creates the correlation plot when switch_trm = FALSE", { | ||
|
||
tkonfound_fig2 <- tkonfound_fig(a = 35, b = 17, c = 17, d = 38, switch_trm = FALSE)[[3]] | ||
|
||
expect_s3_class(tkonfound_fig2, "ggplot") | ||
}) | ||
|
||
test_that("tkonfound creates the correlation plot with test = chisq", { | ||
|
||
tkonfound_fig3 <- tkonfound_fig(a = 35, b = 17, c = 17, d = 38, switch_trm = TRUE, test = "chisq")[[3]] | ||
|
||
expect_s3_class(tkonfound_fig3, "ggplot") | ||
}) | ||
|
||
test_that("tkonfound creates the correlation plot with test = chisq and replace = entire", { | ||
|
||
tkonfound_fig4 <- tkonfound_fig(a = 35, b = 17, c = 17, d = 38, switch_trm = TRUE, test = "chisq", replace = "entire")[[3]] | ||
|
||
expect_s3_class(tkonfound_fig4, "ggplot") | ||
}) | ||
|
||
test_that("tkonfound creates the correlation plot when treatment is small", { | ||
|
||
tkonfound_fig5 <- tkonfound_fig(a = 35, b = 17, c = 17, d = 38, switch_trm = TRUE, test = "chisq", replace = "entire")[[3]] | ||
|
||
expect_s3_class(tkonfound_fig5, "ggplot") | ||
}) | ||
|
||
test_that("tkonfound for two_by_two works for raw output", { | ||
output3 <- pkonfound(a = 35, b = 17, c = 17, d = 38, to_return = "raw_output") | ||
|
||
expect_equal(output3$RIR_primary, tolerance = .001, 14) | ||
|
||
output3_null <- pkonfound(a = 5, b = 17, c = 17, d = 10, to_return = "raw_output") | ||
|
||
expect_equal(output3_null$RIR_primary, tolerance = .001, 6) | ||
|
||
pkonfound_two_by_two <- capture.output(pkonfound(a = 35, b = 17, c = 17, d = 38, to_return = "print")) # printed output works | ||
|
||
expect_true(length(pkonfound_two_by_two) > 0) | ||
|
||
}) |