Nothing
##########################################################################################################################################
test_that("Bounds are equal to 0 or 1 when there is no selection bias,
observed effect or difference in selection and treatment.", {
# Bound for the relative risk in the total population should equal 1.
expect_equal(as.numeric(sharpbound(whichEst = "RR_tot", pY1_T1_S1 = 0.5,
pS1_T1 = 0.5, pS1_T0 = 0.5,
pY1_T0_S1 = 0.5, RR_UY_T1 = 1, RR_UY_T0 = 1,
RR_SU_11 = 1, RR_SU_00 = 1,
RR_SU_10 = 1, RR_SU_01 = 1)[1, 2]), 1)
# Bound for the risk difference in the total population should equal 0.
expect_equal(as.numeric(sharpbound(whichEst = "RD_tot", pY1_T1_S1 = 0.5,
pS1_T1 = 0.5, pS1_T0 = 0.5,
pY1_T0_S1 = 0.5, RR_UY_T1 = 1, RR_UY_T0 = 1,
RR_SU_11 = 1, RR_SU_00 = 1,
RR_SU_10 = 1, RR_SU_01 = 1)[1, 2]), 0)
# Bound for the relative risk in the subpopulation should equal 1.
expect_equal(as.numeric(sharpbound(whichEst = "RR_sub", pY1_T1_S1 = 0.5,
pY1_T0_S1 = 0.5, pT1_S1 = 0.5, pT0_S1 = 0.5,
RR_UY_S1 = 1, RR_TU_1 = 1,
RR_TU_0 = 1)[1, 2]), 1)
# Bound for the risk difference in the subpopulation should equal 0.
expect_equal(as.numeric(sharpbound(whichEst = "RD_sub", pY1_T1_S1 = 0.5,
pY1_T0_S1 = 0.5, pT1_S1 = 0.5, pT0_S1 = 0.5,
RR_UY_S1 = 1, RR_TU_1 = 1,
RR_TU_0 = 1)[1, 2]), 0)
})
##########################################################################################################################################
test_that("sharpbound throws an error if the estimand is not correctly specified.", {
expect_error(sharpbound(whichEst = "RR_t", pY1_T1_S1 = 0.5,
pY1_T0_S1 = 0.5, pS1_T1 = 0.5, pS1_T0 = 0.5,
RR_UY_T1 = 1, RR_UY_T0 = 1,
RR_SU_11 = 1, RR_SU_00 = 1,
RR_SU_10 = 1, RR_SU_01 = 1), 'The estimand must be')
})
##########################################################################################################################################
test_that("sharpbound throws an error if the wrong sensitivity parameter is equal to NULL.", {
#Sensitivity parameters missing.
expect_error(sharpbound(whichEst = "RR_tot", pY1_T1_S1 = 0.5,
pY1_T0_S1 = 0.5, pS1_T1 = 0.5, pS1_T0 = 0.5,
RR_UY_T1 = NULL, RR_UY_T0 = 1,
RR_SU_11 = 1, RR_SU_00 = 1,
RR_SU_10 = 1, RR_SU_01 = 1),
"When the total population is of interest, RR_UY_T1")
expect_error(sharpbound(whichEst = "RD_tot", pY1_T1_S1 = 0.5,
pY1_T0_S1 = 0.5, pS1_T1 = 0.5, pS1_T0 = 0.5,
RR_UY_T1 = 1, RR_UY_T0 = NULL,
RR_SU_11 = 1, RR_SU_00 = 1,
RR_SU_10 = 1, RR_SU_01 = 1),
"When the total population is of interest, RR_UY_T1")
expect_error(sharpbound(whichEst = "RR_sub", pY1_T1_S1 = 0.5,
pY1_T0_S1 = 0.5, pT1_S1 = 0.5, pT0_S1 = 0.5,
RR_UY_S1 = 1, RR_TU_1 = NULL,
RR_TU_0 = NULL),
"When the subpopulation is of interest, RR_UY_S1")
expect_error(sharpbound(whichEst = "RD_sub", pY1_T1_S1 = 0.5,
pY1_T0_S1 = 0.5, pT1_S1 = 0.5, pT0_S1 = 0.5,
RR_UY_S1 = NULL, RR_TU_1 = 1,
RR_TU_0 = 1),
"When the subpopulation is of interest, RR_UY_S1")
})
##########################################################################################################################################
test_that("sharpbound throws an error if the input takes on incorrect values.", {
# Sensitivity parameters smaller than 1.
expect_error(sharpbound(whichEst = "RR_tot", pY1_T1_S1 = 0.5,
pY1_T0_S1 = 0.5, pS1_T1 = 0.5, pS1_T0 = 0.5,
RR_UY_T1 = 0.5, RR_UY_T0 = 1,
RR_SU_11 = 1, RR_SU_00 = 1,
RR_SU_10 = 1, RR_SU_01 = 1),
"All sensitivity parameters must be greater")
expect_error(sharpbound(whichEst = "RR_tot", pY1_T1_S1 = 0.5,
pY1_T0_S1 = 0.5, pS1_T1 = 0.5, pS1_T0 = 0.5,
RR_UY_T1 = 1, RR_UY_T0 = 0.5,
RR_SU_11 = 1, RR_SU_00 = 1,
RR_SU_10 = 1, RR_SU_01 = 1),
"All sensitivity parameters must be greater")
expect_error(sharpbound(whichEst = "RR_tot", pY1_T1_S1 = 0.5,
pY1_T0_S1 = 0.5, pS1_T1 = 0.5, pS1_T0 = 0.5,
RR_UY_T1 = 1, RR_UY_T0 = 1,
RR_SU_11 = 0.5, RR_SU_00 = 1,
RR_SU_10 = 1, RR_SU_01 = 1),
"All sensitivity parameters must be greater")
expect_error(sharpbound(whichEst = "RR_tot", pY1_T1_S1 = 0.5,
pY1_T0_S1 = 0.5, pS1_T1 = 0.5, pS1_T0 = 0.5,
RR_UY_T1 = 1, RR_UY_T0 = 1,
RR_SU_11 = 1, RR_SU_00 = 0.5,
RR_SU_10 = 1, RR_SU_01 = 1),
"All sensitivity parameters must be greater")
expect_error(sharpbound(whichEst = "RR_tot", pY1_T1_S1 = 0.5,
pY1_T0_S1 = 0.5, pS1_T1 = 0.5, pS1_T0 = 0.5,
RR_UY_T1 = 1, RR_UY_T0 = 1,
RR_SU_11 = 1, RR_SU_00 = 1,
RR_SU_10 = 0.5, RR_SU_01 = 1),
"All sensitivity parameters must be greater")
expect_error(sharpbound(whichEst = "RR_tot", pY1_T1_S1 = 0.5,
pY1_T0_S1 = 0.5, pS1_T1 = 0.5, pS1_T0 = 0.5,
RR_UY_T1 = 1, RR_UY_T0 = 1,
RR_SU_11 = 1, RR_SU_00 = 1,
RR_SU_10 = 1, RR_SU_01 = 0.5),
"All sensitivity parameters must be greater")
expect_error(sharpbound(whichEst = "RR_sub", pY1_T1_S1 = 0.5,
pY1_T0_S1 = 0.5, pT1_S1 = 0.5, pT0_S1 = 0.5,
RR_UY_S1 = 0.5, RR_TU_1 = 1,
RR_TU_0 = 1),
"All sensitivity parameters must be greater")
expect_error(sharpbound(whichEst = "RR_sub", pY1_T1_S1 = 0.5,
pY1_T0_S1 = 0.5, pT1_S1 = 0.5, pT0_S1 = 0.5,
RR_UY_S1 = 1, RR_TU_1 = 0.5,
RR_TU_0 = 1),
"All sensitivity parameters must be greater")
expect_error(sharpbound(whichEst = "RR_sub", pY1_T1_S1 = 0.5,
pY1_T0_S1 = 0.5, pT1_S1 = 0.5, pT0_S1 = 0.5,
RR_UY_S1 = 1, RR_TU_1 = 1,
RR_TU_0 = 0.5),
"All sensitivity parameters must be greater")
# Probabilities not valid.
expect_error(sharpbound(whichEst = "RD_tot", pY1_T1_S1 = 0.5, pY1_T0_S1 = -1,
pS1_T1 = 0.5, pS1_T0 = 0.5,
RR_UY_T1 = 1, RR_UY_T0 = 1, RR_SU_11 = 1, RR_SU_00 = 1,
RR_SU_10 = 1, RR_SU_01 = 1),
"cannot be smaller than 0 or larger than 1")
expect_error(sharpbound(whichEst = "RD_tot", pY1_T1_S1 = 0.5, pY1_T0_S1 = 2,
pS1_T1 = 0.5, pS1_T0 = 0.5,
RR_UY_T1 = 1, RR_UY_T0 = 1, RR_SU_11 = 1, RR_SU_00 = 1,
RR_SU_10 = 1, RR_SU_01 = 1),
"cannot be smaller than 0 or larger than 1")
expect_error(sharpbound(whichEst = "RD_tot", pY1_T1_S1 = 0.5, pY1_T0_S1 = 0.5,
pS1_T1 = 0.5, pS1_T0 = -1,
RR_UY_T1 = 1, RR_UY_T0 = 1, RR_SU_11 = 1, RR_SU_00 = 1,
RR_SU_10 = 1, RR_SU_01 = 1),
"cannot be smaller than 0 or larger than 1")
expect_error(sharpbound(whichEst = "RD_tot", pY1_T1_S1 = 0.5, pY1_T0_S1 = 0.5,
pS1_T1 = 0.5, pS1_T0 = 2,
RR_UY_T1 = 1, RR_UY_T0 = 1, RR_SU_11 = 1, RR_SU_00 = 1,
RR_SU_10 = 1, RR_SU_01 = 1),
"cannot be smaller than 0 or larger than 1")
expect_error(sharpbound(whichEst = "RD_sub", pY1_T1_S1 = -1, pY1_T0_S1 = 0.5,
pT1_S1 = 0.5, pT0_S1 = 0.5,
RR_UY_S1 = 1, RR_TU_1 = 1, RR_TU_0 = 1),
"cannot be smaller than 0 or larger than 1")
expect_error(sharpbound(whichEst = "RD_sub", pY1_T1_S1 = 2, pY1_T0_S1 = 0.5,
pT1_S1 = 0.5, pT0_S1 = 0.5,
RR_UY_S1 = 1, RR_TU_1 = 1, RR_TU_0 = 1),
"cannot be smaller than 0 or larger than 1")
expect_error(sharpbound(whichEst = "RD_sub", pY1_T1_S1 = 0.5, pY1_T0_S1 = 0.5,
pT1_S1 = 0.5, pT0_S1 = -1,
RR_UY_S1 = 1, RR_TU_1 = 1, RR_TU_0 = 1),
"cannot be smaller than 0 or larger than 1")
expect_error(sharpbound(whichEst = "RD_sub", pY1_T1_S1 = 0.5, pY1_T0_S1 = 0.5,
pT1_S1 = 0.5, pT0_S1 = 2,
RR_UY_S1 = 1, RR_TU_1 = 1, RR_TU_0 = 1),
"cannot be smaller than 0 or larger than 1")
})
##########################################################################################################################################
test_that("sharpbound() works with both direct and sensitivityparametersM() input", {
# Example model setup (adapted from your documentation)
V <- matrix(c(1, 0, 0.85, 0.15), ncol = 2)
U <- matrix(c(1, 0, 0.5, 0.5), ncol = 2)
Tr <- c(-6.2, 1.75)
Y <- c(-5.2, 5.0, -1.0)
S <- matrix(c(1.2, 2.2, 0.0, 0.5, 2.0, -2.75, -4.0, 0.0), ncol = 4)
probT1 <- 0.286
probT0 <- 0.004
# Run sensitivity parameter computation
sensval <- sensitivityparametersM(
whichEst = "RR_sub", whichBound = "sharp",
Vval = V, Uval = U, Tcoef = Tr, Ycoef = Y, Scoef = S,
Mmodel = "L", pY1_T1_S1 = probT1, pY1_T0_S1 = probT0
)
# Ensure the object has the expected class
expect_s3_class(sensval, "sensparams")
# Convert to list for reference comparison
senslist <- as.list(sensval)
# Compute sharp bound using manual parameter input
res_manual <- sharpbound(
whichEst = "RR_sub",
pY1_T1_S1 = probT1, pY1_T0_S1 = probT0,
pT1_S1 = 0.8, pT0_S1 = 0.2,
RR_UY_S1 = senslist$value[3],
RR_TU_1 = senslist$value[4],
RR_TU_0 = senslist$value[5]
)
# Compute sharp bound directly from sensparams object
res_auto <- sharpbound(
sens = sensval,
whichEst = "RR_sub",
pY1_T1_S1 = probT1,
pY1_T0_S1 = probT0,
pT1_S1 = 0.8, pT0_S1 = 0.2
)
# Compare the two outputs
expect_equal(res_auto, res_manual, tolerance = 1e-10)
# Run sensitivity parameter computation for total population.
sensvalTot <- sensitivityparametersM(
whichEst = "RR_tot", whichBound = "sharp",
Vval = V, Uval = U, Tcoef = Tr, Ycoef = Y, Scoef = S,
Mmodel = "L", pY1_T1_S1 = probT1, pY1_T0_S1 = probT0
)
# Ensure the object has the expected class
expect_s3_class(sensvalTot, "sensparams")
# Convert to list for reference comparison
senslistTot <- as.list(sensvalTot)
# Compute sharp bound using manual parameter input for the total population.
res_manualTot <- sharpbound(
whichEst = "RR_tot",
pY1_T1_S1 = probT1, pY1_T0_S1 = probT0, pS1_T1 = 0.6, pS1_T0 = 0.4,
RR_UY_T1 = senslistTot$value[5], RR_UY_T0 = senslistTot$value[6],
RR_SU_11 = senslistTot$value[7], RR_SU_00 = senslistTot$value[8],
RR_SU_10 = senslistTot$value[9], RR_SU_01 = senslistTot$value[10]
)
# Compute sharp bound directly from sensparams object for the total population
res_autoTot <- sharpbound(
sens = sensvalTot,
whichEst = "RR_tot",
pY1_T1_S1 = probT1,
pY1_T0_S1 = probT0,
pS1_T1 = 0.6, pS1_T0 = 0.4
)
# Compare the two outputs
expect_equal(res_autoTot, res_manualTot, tolerance = 1e-10)
})
Any scripts or data that you put into this service are public.
Add the following code to your website.
For more information on customizing the embed code, read Embedding Snippets.