Generate Sample Data
set.seed(9878)
exp=rbinom(100,20,0.52)
con=rbinom(100,20,0.5)
summary(exp)
## Min. 1st Qu. Median Mean 3rd Qu. Max.
## 6.00 9.00 11.00 10.54 12.00 15.00
summary(con)
## Min. 1st Qu. Median Mean 3rd Qu. Max.
## 5.00 8.75 10.00 9.96 12.00 15.00
df <- data.frame(condition = c(rep("exp", 100), rep("con", 100)),
value = c(exp, con))
Sequential Bayesian Analysis of t-tests
Calculate Sequential Bayes Factors
# Undirectional one-sample t-tests with broad prior
bf.exp <- bfttest(exp, mu = 10, alternative = "two.sided", prior.loc = 0, prior.r = 0.707)
## One-sample t-test (N = 100)
## Calculating Sequential Bayes Factors...
## |======================================================================| 100%
## Final Bayes Factor: BF10 = 2.640 (t-value = 2.595; p = 0.011; δ = 0.259)
print(bf.exp)
##
## Sequential Bayesian Testing
## --------------------------------
## Test: One-sample t-test (parametric)
## Sample size: 100
## Final Bayes Factor: BF10 = 2.640; BF01 = 0.379
## Prior: Cauchy(0.000, 0.707)
## Alternative hypothesis: two.sided
## t-value: 2.595; p = 0.011
## Effect size: δ ≈ 0.259 (approximation from t-statistic)
##
# Use exact=FALSE for a quicker test that does not calculate every single BF
bf.con <- bfttest(con, mu = 10, alternative = "two.sided", prior.loc = 0, prior.r = 0.707, exact=FALSE)
## One-sample t-test (N = 100)
## Calculating Sequential Bayes Factors...
## |======================================================================| 100%
## Final Bayes Factor: BF10 = 0.112; BF01 = 8.901 (t-value = -0.173; p = 0.863; δ = -0.017)
print(bf.con)
##
## Sequential Bayesian Testing
## --------------------------------
## Test: One-sample t-test (parametric)
## Sample size: 100
## Final Bayes Factor: BF10 = 0.112; BF01 = 8.901
## Prior: Cauchy(0.000, 0.707)
## Alternative hypothesis: two.sided
## t-value: -0.173; p = 0.863
## Effect size: δ ≈ -0.017 (approximation from t-statistic)
##
# only the last 5 data points
bfttest(con, mu = 10, alternative = "two.sided", prior.loc = 0, prior.r = 0.707, nstart = length(con)-5)
## One-sample t-test (N = 100)
## Calculating Sequential Bayes Factors...
## |======================================================================| 100%
## Final Bayes Factor: BF10 = 0.112; BF01 = 8.901 (t-value = -0.173; p = 0.863; δ = -0.017)
##
## Sequential Bayesian Testing
## --------------------------------
## Test: One-sample t-test (parametric)
## Sample size: 100
## Final Bayes Factor: BF10 = 0.112; BF01 = 8.901
## Prior: Cauchy(0.000, 0.707)
## Alternative hypothesis: two.sided
## t-value: -0.173; p = 0.863
## Effect size: δ ≈ -0.017 (approximation from t-statistic)
##
# Directional paired samples t-test with narrow prior
bf.paired <- bfttest(exp, con, alternative = "greater", prior.loc = 0, prior.r = 0.1)
## Paired t-test (N = 100)
## Calculating Sequential Bayes Factors...
## |======================================================================| 100%
## Final Bayes Factor: BF10 = 2.960 (t-value = 1.974; p = 0.026; δ = 0.197)
print(bf.paired)
##
## Sequential Bayesian Testing
## --------------------------------
## Test: Paired t-test (parametric)
## Sample size: 100
## Final Bayes Factor: BF10 = 2.960; BF01 = 0.338
## Prior: Cauchy(0.000, 0.100)
## Alternative hypothesis: greater
## t-value: 1.974; p = 0.026
## Effect size: δ ≈ 0.197 (approximation from t-statistic)
##
# Independent samples t-test with informed prior
bf.between <- bfttest(value ~ condition, data = df, alternative = "less", prior.loc = 0.1, prior.r = 0.1)
## Independent t-test (N = 200 [100 + 100])
## Calculating Sequential Bayes Factors...
## |======================================================================| 100%
## Final Bayes Factor: BF10 = 2.362 (t-value = -1.866; p = 0.032; δ = -0.264)
print(bf.between)
##
## Sequential Bayesian Testing
## --------------------------------
## Test: Independent t-test (parametric)
## Sample size: 100, 100
## Final Bayes Factor: BF10 = 2.362; BF01 = 0.423
## Prior: Cauchy(0.100, 0.100)
## Alternative hypothesis: less
## t-value: -1.866; p = 0.032
## Effect size: δ ≈ -0.264 (approximation from t-statistic)
##
Non-parametric Sequential Bayes Factors
# One-sample Wilcoxon signed-rank test
bf.onesample.np <- bfttest(exp, mu = 10, alternative = "two.sided", prior.loc = 0, prior.r = 0.707, exact = FALSE, parametric = FALSE)
## One-sample Wilcoxon signed-rank test (N = 100)
## MCMC samples: 250 (intermediate) → 1000 (final)
## Calculating Sequential Bayes Factors...
## |======================================================================| 100%
## Final Bayes Factor: BF10 = 2.903 (W-value = 2347.500; p = 0.011; δ = 0.262)
print(bf.onesample.np)
##
## Sequential Bayesian Testing
## --------------------------------
## Test: One-sample Wilcoxon signed-rank test (non-parametric)
## Sample size: 100
## Final Bayes Factor: BF10 = 2.903; BF01 = 0.344
## Prior: Cauchy(0.000, 0.707)
## Alternative hypothesis: two.sided
## W-value: 2347.500; p = 0.011
## Effect size: δ = 0.262, 95% CI [0.055, 0.467]
##
# Paired Wilcoxon signed-rank test
bf.paired.np <- bfttest(exp, con, alternative = "greater", prior.loc = 0, prior.r = 0.1, exact = FALSE, parametric = FALSE)
## Paired Wilcoxon signed-rank test (N = 100)
## MCMC samples: 250 (intermediate) → 1000 (final)
## Calculating Sequential Bayes Factors...
## |======================================================================| 100%
## Final Bayes Factor: BF10 = 3.096 (W-value = 2532.500; p = 0.025; δ = 0.122)
print(bf.paired.np)
##
## Sequential Bayesian Testing
## --------------------------------
## Test: Paired Wilcoxon signed-rank test (non-parametric)
## Sample size: 100
## Final Bayes Factor: BF10 = 3.096; BF01 = 0.323
## Prior: Cauchy(0.000, 0.100)
## Alternative hypothesis: greater
## W-value: 2532.500; p = 0.025
## Effect size: δ = 0.122, 95% CI [-0.034, 0.328]
##
# Mann-Whitney U test
bf.between.np <- bfttest(value ~ condition, data = df, alternative = "less", prior.loc = 0.1, prior.r = 0.1, exact = FALSE, parametric = FALSE)
## Mann-Whitney U test (N = 200 [100 + 100])
## MCMC samples: 250 (intermediate) → 1000 (final)
## Calculating Sequential Bayes Factors...
## |======================================================================| 100%
## Final Bayes Factor: BF10 = 0.302; BF01 = 3.316 (W-value = 5686.000; p = 0.955; δ = 0.122)
print(bf.between.np)
##
## Sequential Bayesian Testing
## --------------------------------
## Test: Mann-Whitney U test (non-parametric)
## Sample size: 100, 100
## Final Bayes Factor: BF10 = 0.302; BF01 = 3.316
## Prior: Cauchy(0.100, 0.100)
## Alternative hypothesis: less
## W-value: 5686.000; p = 0.955
## Effect size: δ = 0.122, 95% CI [-0.062, 0.420]
##
Plot Seq BFs
# Plot seqbf object
plot(bf.paired)

# Plot multiple BFs
plotbf(list(exp=bf.exp$BF, con=bf.con$BF))

Robustness analyis
bf.paired.robust <- bfRobustness(bf.paired)
## Highest BF = 5.18 with prior: Cauchy(0.2, 0.05)
print(bf.paired.robust)
##
## Prior Robustness Analysis
## --------------------------------
## Test type: paired
## Sample size: 100
## Tested priors:
## -- distribution: Cauchy
## -- location: 0,0.05,0.1,0.15,0.2,0.25,0.3,0.35,0.4,0.45,0.5,0.55,0.6,0.65,0.7,0.75,0.8,0.85,0.9,0.95,1
## -- scale: 0.05,0.1,0.15,0.2,0.25,0.3,0.35,0.4,0.45,0.5,0.55,0.6,0.65,0.7,0.75,0.8,0.85,0.9,0.95,1
## Highest Bayes Factor: 5.181 with prior: Cauchy(0.2, 0.05)
## Lowest Bayes Factor: 0.045 with prior: Cauchy(1, 0.05)
## Median Bayes Factor: 0.894
##
plot(bf.paired.robust)
