##### 2x2x2 continceny table (Klugkist & Hojtink, 2007)
#
# (defendant's race) x (victim's race) x (death penalty)
# indexing: 0 = white/white/yes ; 1 = black/black/no
# probabilities: (p000,p001, p010,p011, p100,p101, p110,p111)
# Model2:
# p000*p101 < p100*p001 & p010*p111 < p110*p011
# observed frequencies:
k <- c(19, 132, 0, 9, 11, 52, 6, 97)
model <- function(x) {
x[1] * x[6] < x[5] * x[2] & x[3] * (1 - sum(x)) < x[7] * x[4]
}
# NOTE: "1-sum(x)" must be used instead of "x[8]"!
# compute Bayes factor (Klugkist 2007: bf_0u=1.62)
bf_nonlinear(k, 8, model, M = 50000)
# \donttest{
##### Using a C++ indicator function (much faster)
cpp_code <- "SEXP model(NumericVector x){
return wrap(x[0]*x[5] < x[4]*x[1] & x[2]*(1-sum(x)) < x[6]*x[3]);}"
# NOTE: C++ indexing starts at 0!
# define C++ pointer to indicator function:
model_cpp <- RcppXPtrUtils::cppXPtr(cpp_code)
bf_nonlinear(
k = c(19, 132, 0, 9, 11, 52, 6, 97), M = 100000,
options = 8, inside = model_cpp
)
# }
Run the code above in your browser using DataLab