# These examples are run with very few simulation runs (argument N), so they
# provide unrealistic results. For more reasonable demonstrations check the
# vignettes.
# Assuming a negative binomial count variable whose overdispersion parameter,
# k, varies as a function of the mean, and that the variance-mean relationship
# is well described with Taylor's Power Law, a function to obtain k can be:
estimate_k <- function(mean) {
a = 1.830012
b = 1.218041 # a and b are Taylor's Power Law parameters
(mean^2) / ((a * mean^(b)) - mean)
}
# Generate some counts to create an STBP object with the model specifications
counts3 <- rnbinom(20, mu = 5, size = estimate_k(5))
# Run the test to create the STBP object
test1F <- stbp_composite(data = counts3,
greater_than = TRUE,
hypothesis = 9,
density_func = "negative binomial",
overdispersion = "estimate_k",
prior = 0.5,
lower_bnd = 0,
upper_bnd = Inf,
lower_criterion = 0.01,
upper_criterion = 0.99)
test1F
# Model evaluation is carried out based on simulated counts, and more realistic
# counts could be generated if uncertainty about the overdispersion parameter is
# considered. A function to obtain values for the overdispersion parameter, k,
# with added stochasticity could be (following Binns et al. 2000):
estimate_k_stoch <- function(mean) {
a <- 1.830012
b <- 1.218041
(mean^2) /
((a * mean^(b) *
exp(truncdist::rtrunc(1,
"norm",
a = log(1 / (a * mean^(b - 1))),
b = Inf,
mean = 0,
sd = 0.3222354)))
- mean)
}
# where sd here is the the root of the mean square error for the regression
# used to estimate a and b. Note that this is a stochastic version
# of 'estimate_k'.
# Run model evaluation for testF1 with varying overdispersion and
# added stochasticity with very few simulations to save time.
eval1 <- STBP.eval(test1F,
eval.range = seq(2, 11),
n = 1, prior = 0.5,
overdispersion.sim = "estimate_k_stoch",
N = 2)
plot(seq(2, 11), eval1$AvgSamples, type = "o", xlab = "True population size",
ylab = "Average number of bouts")
plot(seq(2, 11), eval1$AcceptRate, type = "o", xlab = "True population size",
ylab = "Acceptance rate of H")
# Alternatively, the evaluation could be carried out omitting variation about
# overdispersion. For that the overdispersion argument is omitted and the same
# specification of the model is used. Very few simulations to save time.
eval2 <- STBP.eval(test1F,
eval.range = seq(2, 11),
n = 1, prior = 0.5,
N = 2)
plot(seq(2, 11), eval2$AvgSamples, type = "o", xlab = "True population size",
ylab = "Average number of bouts")
plot(seq(2, 11), eval2$AcceptRate, type = "o", xlab = "True population size",
ylab = "Acceptance rate of H")
# When there is no overdispersion (poisson or binomial distributions) the
# procedure is much simpler
test2F <- stbp_composite(data = counts3,
greater_than = TRUE,
hypothesis = 5,
density_func = "poisson",
prior = 0.5,
lower_bnd = 0,
upper_bnd = Inf,
lower_criterion = 0.01,
upper_criterion = 0.99)
test2F
# Overdispersion is omitted here. Again, very few simulations (N) to save time.
eval3 <- STBP.eval(test2F,
eval.range = seq(1, 8),
n = 1,
prior = 0.5, N = 2)
plot(seq(1, 8), eval3$AvgSamples, type = "o", xlab = "True population size",
ylab = "Average number of bouts")
plot(seq(1, 8), eval3$AcceptRate, type = "o", xlab = "True population size",
ylab = "Acceptance rate of H")
# Variations if n, the sample size within each bout, can also be changed
# (not possible in SPRT)!
## End (Not run)
Run the code above in your browser using DataLab