Skip to content

Commit

Permalink
More tests
Browse files Browse the repository at this point in the history
  • Loading branch information
jan-imbi committed Jul 10, 2024
1 parent 300fe7a commit 061a1a5
Show file tree
Hide file tree
Showing 3 changed files with 73 additions and 5 deletions.
9 changes: 6 additions & 3 deletions R/estimators.R
Original file line number Diff line number Diff line change
Expand Up @@ -1483,6 +1483,7 @@ setMethod("get_stagewise_estimators", signature("ScoreTestOrderingCI", "Normal")
u2 = u2
))
})
#nocov start
setClass("NeymanPearsonOrderingCI", contains = "VirtualIntervalEstimator", slots = c(mu0 = "numeric", mu1 = "numeric"))
#' @inheritParams NeymanPearsonOrderingPValue
#' @rdname IntervalEstimator-class
Expand Down Expand Up @@ -1513,7 +1514,7 @@ setMethod("get_stagewise_estimators", signature("NeymanPearsonOrderingCI", "Norm
u2 = u2
))
})

#nocov end
setClass("NaiveCI", contains = "VirtualIntervalEstimator")
#' @rdname IntervalEstimator-class
#' @export
Expand Down Expand Up @@ -1664,6 +1665,7 @@ setMethod("get_stagewise_estimators", signature("MidpointScoreTestOrderingCI", "
g2 = g2
))
})
#nocov start
setClass("MidpointNeymanPearsonOrderingCI", contains = "VirtualPointEstimator")
#' @inheritParams NeymanPearsonOrderingPValue
#' @rdname PointEstimator-class
Expand Down Expand Up @@ -1692,7 +1694,7 @@ setMethod("get_stagewise_estimators", signature("MidpointNeymanPearsonOrderingCI
g2 = g2
))
})

#nocov end
setClass("MedianUnbiasedStagewiseCombinationFunctionOrdering", contains = "VirtualPointEstimator")
#' @rdname PointEstimator-class
#' @export
Expand Down Expand Up @@ -1757,6 +1759,7 @@ setClass("MedianUnbiasedNeymanPearsonOrdering", contains = "VirtualPointEstimato
#' @inheritParams NeymanPearsonOrderingPValue
#' @rdname PointEstimator-class
#' @export
#nocov start
MedianUnbiasedNeymanPearsonOrdering <- function(mu0 = 0, mu1 = 0.4) new("MedianUnbiasedNeymanPearsonOrdering", mu0 = mu0, mu1 = mu1, label = paste0("Median unbiased (Neyman-Pearson test ordering, mu0=",format(mu0),", mu1=",format(mu1),")"))
#' @rdname get_stagewise_estimators
setMethod("get_stagewise_estimators", signature("MedianUnbiasedNeymanPearsonOrdering", "Normal"),
Expand All @@ -1769,7 +1772,7 @@ setMethod("get_stagewise_estimators", signature("MedianUnbiasedNeymanPearsonOrde
list(g1 = Vectorize(\(design, smean1, n1, sigma, two_armed, ...) find_root_p1_np(design, smean1, n1, mu0=estimator@mu0, mu1=estimator@mu1, sigma, two_armed, p_boundary = 0.5, ...), c("smean1")),
g2 = Vectorize(\(design, smean1, smean2, n1, n2, sigma, two_armed, ...) find_root_p2_np(design, smean1, smean2, n1, n2, mu0=estimator@mu0, mu1=estimator@mu1, sigma, two_armed, p_boundary = 0.5, ...), c("smean1", "smean2", "n2")))
})

#nocov end
# For the p-value plots
implied_c2 <- function(design, z1, p2, sigma, two_armed, alpha, tol_root = getOption("adestr_tol_roots", default = .adestr_options[["adestr_tol_roots"]]), tol = getOption("adestr_tol_inner", default = .adestr_options[["adestr_tol_inner"]]), maxEval = getOption("adestr_maxEval_inner", default = .adestr_options[["adestr_maxEval_inner"]]), absError = getOption("adestr_absError_inner", default = .adestr_options[["adestr_absError_inner"]]), ...){
design <- TwoStageDesignWithCache(design)
Expand Down
37 changes: 37 additions & 0 deletions tests/testthat/test_estimators.R
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
# Test everything that hasn't been tested somehwere else here.
test_that("MidpointEstimators don't error",
{expect_error(
{
ests <- get_stagewise_estimators(MidpointMLEOrderingCI(), Normal(FALSE), FALSE, designad, 1, FALSE)
ests[[1]](smean1= .1, sigma=1, two_armed=FALSE, n1=designad@n1, design=designad)
ests[[2]](smean1= .1, smean2=.2, sigma=1, two_armed=FALSE, n1=designad@n1, n2=30, design=designad)
}
, NA
)
expect_error(
{
ests <- get_stagewise_estimators(MidpointLikelihoodRatioOrderingCI(), Normal(FALSE), FALSE, designad, 1, FALSE)
ests[[1]](smean1= .1, sigma=1, two_armed=FALSE, n1=designad@n1, design=designad)
ests[[2]](smean1= .1, smean2=.2, sigma=1, two_armed=FALSE, n1=designad@n1, n2=30, design=designad)
}
, NA
)
expect_error(
{
ests <- get_stagewise_estimators(MidpointScoreTestOrderingCI(), Normal(FALSE), FALSE, designad, 1, FALSE)
ests[[1]](smean1= .1, sigma=1, two_armed=FALSE, n1=designad@n1, design=designad)
ests[[2]](smean1= .1, smean2=.2, sigma=1, two_armed=FALSE, n1=designad@n1, n2=30, design=designad)
}
, NA
)
expect_error(
{
ests <- get_stagewise_estimators(MidpointStagewiseCombinationFunctionOrderingCI(), Normal(FALSE), FALSE, designad, 1, FALSE)
ests[[1]](smean1= .1, sigma=1, two_armed=FALSE, n1=designad@n1, design=designad)
ests[[2]](smean1= .1, smean2=.2, sigma=1, two_armed=FALSE, n1=designad@n1, n2=30, design=designad)
}
, NA
)
})


32 changes: 30 additions & 2 deletions vignettes/Introduction.Rmd
Original file line number Diff line number Diff line change
Expand Up @@ -54,8 +54,8 @@ library(adestr)
get_example_design(two_armed = TRUE)
```

# Example
## Evaluating the mean squared of an estimator
# Evaluating point estimators
## Evaluating the mean squared of the sample mean and the weighted sample mean
Now that we have created an optimal adaptive design, we can investigate the performance
characteristics of various estimators for the mean in that design.
To this end, the `evaluate_estimator` function can be used.
Expand Down Expand Up @@ -99,6 +99,34 @@ mse_weighted_sample_means <- evaluate_estimator(
plot(c(mse_mle, mse_weighted_sample_means))
```


## Evaluating median unbiased estimators
Median unbiased estimators are estimators where the probability of overestimation
is equal to the probability of underestimation. They can be derived by choosing
a sample space ordering. In `adestr`, 5 different sample-space orderings are
implemented: The MLE (maximum likelihood estimator) ordering,
the LR (likelihood ratio) test ordering, the ST (score test) ordering,
the SWCF (stage-wise combination function) ordering, and the NP (Neyman-Pearson)
ordering. The latter (NP ordering) is only useful for the calculation of p-values,
estimators derived from that ordering are usually nonsense.

Lets look at the 'median-unbiased' property for estimators derived from the four
orderings:

```{r}
evaluate_estimator(
score = OverestimationProbability(),
estimator = MedianUnbiasedMLEOrdering(),
data_distribution = Normal(two_armed = TRUE),
design = get_example_design(two_armed = TRUE),
mu = 0.3,
sigma = 1
)
```




## Analyzing datasets
Next, let us look at how to the package can be used to calculate estimates
after data has been collected.
Expand Down

0 comments on commit 061a1a5

Please sign in to comment.