Bayes Factor functions/packages
Show older comments
I am using the following two-sample tests for non-normal distributions:
and I would like to calculate the Bayes Factor as well.
I found the bayesFactor Version 1.0.0 (253 KB) by Bart Krekelberg. However, to the best of my understanfding, that package has a limited number of implemented tests:
- One sample t-test (bf.ttest)
- Two sample t-test (bf.ttest2)
- N-Way Anova with fixed and random effects, including continuous co-variates (bf.anova)
- Regression (bf.regression)
- Pearson Correlation (bf.corr)
- Binomial Test (bf.binom)
- Experimental Design & Power Analysis (bf.designAnalysis)
and I am not sure if they can be used as additional analysis to the 4 initially listed ones that I am employing.
Does anyone know if there are other Matlab functions/packages to calculate the Bayes factor, in relation to the two-sample tests I am currently using (i.e. the chi2gof, the kstest2, the ranksum, and the kruskalwallis)?
7 Comments
Umar
on 1 Aug 2024
Edited: Walter Roberson
on 1 Aug 2024
@Sim,
Take a peek by clicking the link below
Let me know if this helps you out.
Walter Roberson
on 1 Aug 2024
@Umar the link you included is the same one that they already cited, bayesFactor Version 1.0.0
Umar
on 1 Aug 2024
@Walter, you are right, thanks for bringing this to my attention.
Jeff Miller
on 1 Aug 2024
Just note that it isn't straightforward to compute Bayes factors for your two rank-based tests--some extra assumptions are needed. For example, see van Doorn et al (2020). So if you do find some software, it will depend on some specific assumptions that you may or may not be happy to make.
Umar
on 1 Aug 2024
Edited: Walter Roberson
on 2 Aug 2024
@Sim,
After doing some thorough research online, I was able to dig out some research materials and gathered all the information to share with you now. So, to address your query, you can implement Bayesian models using probabilistic programming libraries like Stan, or PyMC3, and then interface with these libraries from Matlab using appropriate tools like the MATLAB Stan interface. For more information regarding Stan,PyMC3 please refer to
Now, addressing your query about, Does anyone know if there are other Matlab functions/packages to calculate the Bayes factor, in relation to the two-sample tests I am currently using (i.e. the chi2gof, the kstest2, the ranksum, and the kruskalwallis)?
So, in order to calculate the Bayes Factor using the MATLAB Stan interface in relation to two-sample tests, ensure that you have the necessary data for your two-sample tests. This data should be appropriately formatted and ready for analysis. Then, select an appropriate Bayesian model that represents the hypotheses you want to compare. In the context of two-sample tests, you can define prior distributions for the parameters of interest and specify the likelihood function based on the test statistics used (e.g., chi-square, Kolmogorov-Smirnov, Wilcoxon rank-sum, or Kruskal-Wallis). Afterwards, you have to write the Bayesian model in the Stan modeling language by referring to their documentation. Here is a simple example of a Bayesian model for a two-sample test using Stan:
% Example Bayesian model in Stan for two-sample test
model_code = '
data {
int<lower=0> N1; // Sample size for group 1
int<lower=0> N2; // Sample size for group 2
real y1[N1]; // Data for group 1
real y2[N2]; // Data for group 2
}
parameters {
real mu1; // Mean for group 1
real mu2; // Mean for group 2
real<lower=0> sigma1; // Standard deviation for group 1
real<lower=0> sigma2; // Standard deviation for group 2
}
model {
y1 ~ normal(mu1, sigma1); // Likelihood for group 1
y2 ~ normal(mu2, sigma2); // Likelihood for group 2
// Prior distributions can be specified here
}
';
Compile the Stan model using the stan function in MATLAB and make sure to provide the necessary data inputs and specify the number of iterations for sampling. Once the Bayesian model has been sampled using your defined methods, you can calculate the Bayes Factor by comparing the marginal likelihoods of the competing hypotheses. Please bear in mind that the Bayes Factor will quantify the relative strength of evidence for one hypothesis over another based on the observed data.
If you still have any further questions, please let me know.
Sim
on 2 Aug 2024
Umar
on 2 Aug 2024
Hi @Sim,
No problem, glad to help and good luck 👍 with your project.
Answers (0)
Categories
Find more on Hypothesis Tests in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!