### Install BayesFactor Development Version from GitHub Source: https://github.com/richarddmorey/bayesfactor/blob/master/README.md Install the latest development version of BayesFactor from GitHub using the devtools package. This requires devtools to be installed first. Ensure you have the necessary build tools for your operating system. ```R install.packages('devtools') ``` ```R library(devtools) ``` ```R install_github('richarddmorey/BayesFactor', subdir='pkg/BayesFactor', dependencies = TRUE) ``` -------------------------------- ### Install BayesFactor from CRAN Source: https://github.com/richarddmorey/bayesfactor/blob/master/README.md Use this command to install the latest stable version of the BayesFactor package from CRAN. Ensure you have internet access and CRAN repositories configured. ```R install.packages('BayesFactor', dependencies = TRUE) ``` -------------------------------- ### Sample from Posterior Distribution with posterior Source: https://context7.com/richarddmorey/bayesfactor/llms.txt Samples from the posterior distribution of model parameters for any BayesFactor object. Useful for MCMC diagnostics, extracting specific parameters, and filtering columns. Requires the 'coda' package for diagnostics. ```r library(BayesFactor) data(sleep) data(puzzles) # Get Bayes factor object bf <- lmBF(extra ~ group + ID, data = sleep, whichRandom = "ID", progress = FALSE) # Sample from posterior chains <- posterior(bf, iterations = 10000, progress = FALSE) # MCMC diagnostics (uses coda package) plot(chains) summary(chains) # Extract specific parameters group_effect <- chains[, "group-1"] hist(group_effect) # With column filtering (exclude participant effects) bf2 <- lmBF(RT ~ shape + color + shape:color + ID, data = puzzles) chains_filtered <- posterior(bf2, iterations = 1000, progress = FALSE, columnFilter = "^ID$") colnames(chains_filtered) # No ID columns ``` -------------------------------- ### Compare Models via Posterior Samples Source: https://context7.com/richarddmorey/bayesfactor/llms.txt Compares two models directly using their posterior samples. ```r library(BayesFactor) data(puzzles) # Sample from two models mod_main <- lmBF(RT ~ shape + color + ID, data = puzzles, whichRandom = "ID", posterior = TRUE, iterations = 10000, progress = FALSE) mod_full <- lmBF(RT ~ shape * color + ID, data = puzzles, whichRandom = "ID", posterior = TRUE, iterations = 10000, progress = FALSE) # Compare models using posterior samples compare(mod_main, mod_full) # Returns Bayes factor for main effects vs full model ``` -------------------------------- ### compare - Compare Two Models Source: https://context7.com/richarddmorey/bayesfactor/llms.txt Directly compares two models using their posterior samples, useful when computing Bayes factors from existing MCMC objects. ```APIDOC ## compare - Compare Two Models ### Description Directly compares two models using their posterior samples. Useful when computing Bayes factors from existing MCMC objects. ### Method `compare(bf1, bf2)` ### Parameters #### Arguments - **bf1** (BayesFactor object) - The first model to compare. - **bf2** (BayesFactor object) - The second model to compare. ### Request Example ```r library(BayesFactor) data(puzzles) # Sample from two models mod_main <- lmBF(RT ~ shape + color + ID, data = puzzles, whichRandom = "ID", posterior = TRUE, iterations = 10000, progress = FALSE) mod_full <- lmBF(RT ~ shape * color + ID, data = puzzles, whichRandom = "ID", posterior = TRUE, iterations = 10000, progress = FALSE) # Compare models using posterior samples compare(mod_main, mod_full) ``` ### Response Returns the Bayes factor comparing `bf1` to `bf2`. ``` -------------------------------- ### Compute Bayesian Multiple Regression with regressionBF Source: https://context7.com/richarddmorey/bayesfactor/llms.txt Evaluates all possible regression models using Zellner-Siow g-priors to compare evidence against an intercept-only model. ```r library(BayesFactor) # Load example data data(attitude) # Compute Bayes factors for all regression models bf_all <- regressionBF(rating ~ complaints + privileges + learning + raises + critical + advance, data = attitude, progress = FALSE) head(bf_all) # Shows Bayes factors for each model vs intercept-only # Find best models head(bf_all, n = 5) # Top 5 models # Compare all models to the full model bf_full <- bf_all[63] # Full model (last one) head(bf_all / bf_full) # Relative evidence ``` -------------------------------- ### posterior - Sample from Posterior Source: https://context7.com/richarddmorey/bayesfactor/llms.txt Samples from the posterior distribution of model parameters. Works with any BayesFactor object. ```APIDOC ## posterior - Sample from Posterior ### Description Samples from the posterior distribution of model parameters. Works with any BayesFactor object. ### Method `posterior` ### Parameters #### Path Parameters None #### Query Parameters - **bf** (BayesFactor object) - The BayesFactor object from which to sample. - **iterations** (numeric) - The number of iterations (samples) to draw. - **progress** (logical, optional) - Whether to show progress bar. - **columnFilter** (character, optional) - A regular expression to filter column names. ### Request Example ```r library(BayesFactor) data(sleep) bf <- lmBF(extra ~ group + ID, data = sleep, whichRandom = "ID", progress = FALSE) chains <- posterior(bf, iterations = 10000, progress = FALSE) # Extract specific parameters group_effect <- chains[, "group-1"] hist(group_effect) ``` ### Response Returns a matrix or data frame containing samples from the posterior distribution of the model parameters. ``` -------------------------------- ### General Model Testing with generalTestBF Source: https://context7.com/richarddmorey/bayesfactor/llms.txt Computes Bayes factors for general linear models with categorical and continuous predictors. Allows specifying predictors that should always be included in models. Supports testing all models or a top-down approach. ```r library(BayesFactor) data(puzzles) # Test all models, keeping ID in every model bf_general <- generalTestBF(RT ~ shape * color + ID, data = puzzles, whichRandom = "ID", neverExclude = "ID", progress = FALSE) bf_general # Top-down approach bf_top <- generalTestBF(RT ~ shape * color + ID, data = puzzles, whichRandom = "ID", neverExclude = "ID", whichModels = "top", progress = FALSE) bf_top ``` -------------------------------- ### Perform Bottom-up and Top-down Regression Model Testing Source: https://context7.com/richarddmorey/bayesfactor/llms.txt Use regressionBF to evaluate models by adding or removing predictors. Set whichModels to 'bottom' or 'top' to control the search strategy. ```r bf_bottom <- regressionBF(rating ~ complaints + privileges + learning, data = attitude, whichModels = "bottom", progress = FALSE) bf_bottom # Top-down testing: remove one predictor at a time from full bf_top <- regressionBF(rating ~ complaints + privileges + learning, data = attitude, whichModels = "top", progress = FALSE) bf_top ``` -------------------------------- ### generalTestBF - General Model Testing Source: https://context7.com/richarddmorey/bayesfactor/llms.txt Computes Bayes factors for general linear models with both categorical and continuous predictors. Allows specifying factors that should never be excluded from models. ```APIDOC ## generalTestBF - General Model Testing ### Description Computes Bayes factors for general linear models with both categorical and continuous predictors. Allows specifying factors that should never be excluded from models. ### Method `generalTestBF` ### Parameters #### Path Parameters None #### Query Parameters - **formula** (formula) - The model formula. - **data** (data.frame) - The data frame containing the variables. - **whichRandom** (character, optional) - Specifies random effects. - **neverExclude** (character vector, optional) - Predictors that should never be excluded from models. - **whichModels** (character, optional) - Specifies which models to test (e.g., "top", "bottom"). - **progress** (logical, optional) - Whether to show progress bar. ### Request Example ```r library(BayesFactor) data(puzzles) bf_general <- generalTestBF(RT ~ shape * color + ID, data = puzzles, whichRandom = "ID", neverExclude = "ID", progress = FALSE)f_general ``` ### Response Returns a BayesFactor object containing Bayes factors for the specified models. ``` -------------------------------- ### Compute Bayes Factor from F-statistic with oneWayAOV.Fstat Source: https://context7.com/richarddmorey/bayesfactor/llms.txt Computes a Bayes factor from an F-statistic for balanced one-way ANOVA designs when raw data is unavailable. Supports specifying the F-statistic, sample size per group, and number of groups. Includes a simple output option. ```r library(BayesFactor) # From published ANOVA # F = 34.7, N = 12 per group, J = 6 groups result <- oneWayAOV.Fstat(F = 34.7, N = 12, J = 6) exp(result[["bf"]]) # Very large BF # Smaller effect result_small <- oneWayAOV.Fstat(F = 3.5, N = 20, J = 3) exp(result_small[["bf"]]) # Simple output oneWayAOV.Fstat(F = 34.7, N = 12, J = 6, simple = TRUE) ``` -------------------------------- ### recompute - Improve Bayes Factor Precision Source: https://context7.com/richarddmorey/bayesfactor/llms.txt Recomputes Bayes factors with additional sampling iterations to improve precision by combining new samples with existing ones. ```APIDOC ## recompute - Improve Bayes Factor Precision ### Description Recomputes Bayes factors with additional sampling iterations to improve precision. Combines new samples with existing ones. ### Method `recompute(obj, iterations, progress)` ### Parameters #### Arguments - **obj** (BayesFactor object or MCMC chains) - The object to recompute. - **iterations** (numeric) - The total number of iterations desired for the recomputation. - **progress** (logical) - Whether to display a progress bar. Defaults to TRUE. ### Request Example ```r library(BayesFactor) data(puzzles) # Initial computation bf <- lmBF(RT ~ shape + color + ID, data = puzzles, whichRandom = "ID", progress = FALSE) # Recompute with more iterations for better precision bf_improved <- recompute(bf, iterations = 50000, progress = FALSE) # Also works with MCMC chains chains <- posterior(bf, iterations = 1000, progress = FALSE) chains_more <- recompute(chains, iterations = 5000, progress = FALSE) ``` ### Response Returns a BayesFactor object or MCMC chains with improved precision. ``` -------------------------------- ### Test Specific Linear Models with lmBF Source: https://context7.com/richarddmorey/bayesfactor/llms.txt Compute Bayes factors for specific ANOVA or regression models and perform posterior sampling for parameter estimation. ```r library(BayesFactor) data(puzzles) # Test specific model against null bf_full <- lmBF(RT ~ shape + color + shape:color + ID, data = puzzles, whichRandom = "ID", progress = FALSE) bf_full # Test main effects only model bf_main <- lmBF(RT ~ shape + color + ID, data = puzzles, whichRandom = "ID", progress = FALSE) bf_main # Compare models directly: is the interaction needed? bf_interaction <- bf_full / bf_main bf_interaction # BF < 1 suggests main effects model is preferred # Sample from posterior of full model samples <- lmBF(RT ~ shape + color + shape:color + ID, data = puzzles, whichRandom = "ID", posterior = TRUE, iterations = 10000, progress = FALSE) plot(samples) summary(samples) # Alternative: use posterior() on existing BF object chains <- posterior(bf_full, iterations = 10000, progress = FALSE) colnames(chains) # View parameter names ``` -------------------------------- ### regressionBF - Bayesian Multiple Regression Source: https://context7.com/richarddmorey/bayesfactor/llms.txt Computes Bayes factors for all possible regression models given a set of predictors using Zellner-Siow g-priors. ```APIDOC ## regressionBF - Bayesian Multiple Regression ### Description Computes Bayes factors for all possible regression models given a set of predictors. Uses Zellner-Siow g-priors for regression coefficients. Tests whether regression slopes differ from zero. ### Method `regressionBF` ### Parameters #### Arguments - **formula** (formula) - A symbolic formula describing the regression model, e.g., `y ~ x1 + x2`. - **data** (data.frame) - The data frame containing the variables specified in the formula. - **gf** (numeric, optional) - The prior scale for the g-Jeffreys prior. Defaults to `nrow(data)`. - **progress** (logical, optional) - Whether to display progress messages. Defaults to TRUE. ### Request Example ```r library(BayesFactor) # Load example data data(attitude) # Compute Bayes factors for all regression models bf_all <- regressionBF(rating ~ complaints + privileges + learning + raises + critical + advance, data = attitude, progress = FALSE) # Display top models print(head(bf_all)) # Compare all models to the full model bf_full <- bf_all[63] # Assuming the full model is the 63rd model print(head(bf_all / bf_full)) ``` ### Response Example ``` # Output shows Bayes factors for each model compared to the intercept-only model. # Example of head(bf_all): # Bayes factor analysis # numerator.formula denominator.formula bf # 1 1 1.0000000 # 2 complaints 1.0000000 # 3 privileges 1.0000000 # 4 learning 1.0000000 # 5 raises 1.0000000 # 6 critical 1.0000000 # ... (further models and their Bayes factors) ``` ``` -------------------------------- ### anovaBF - Bayesian ANOVA Source: https://context7.com/richarddmorey/bayesfactor/llms.txt Computes Bayes factors for ANOVA designs with fixed and random effects. Supports between-subjects and within-subjects designs. ```APIDOC ## anovaBF - Bayesian ANOVA ### Description Computes Bayes factors for ANOVA designs with fixed and random effects. Tests all possible models or subsets against a null model. Supports both between-subjects and within-subjects (repeated measures) designs with proper handling of random effects. ### Method `anovaBF` ### Parameters #### Arguments - **formula** (formula) - A symbolic formula describing the model, e.g., `y ~ x1 + x2`. - **data** (data.frame) - The data frame containing the variables specified in the formula. - **whichRandom** (character or character vector, optional) - Specifies which terms in the formula are random effects (e.g., for repeated measures). - **whichModels** (character, optional) - Specifies which models to compare. Can be "all", "top", or a subset of models. - **rscaleFixed** (character or numeric, optional) - The prior scale for fixed effects. Can be "medium", "wide", "ultrawide", or a custom numeric value. - **rscaleRandom** (character or numeric, optional) - The prior scale for random effects. Can be "medium", "wide", "ultrawide", "nuisance", or a custom numeric value. - **progress** (logical, optional) - Whether to display progress messages. Defaults to TRUE. ### Request Example ```r library(BayesFactor) # Load example data data(sleep) data(puzzles) # Repeated measures ANOVA (within-subjects) # ID is specified as random factor for repeated measures bf_rm <- anovaBF(extra ~ group + ID, data = sleep, whichRandom = "ID", progress = FALSE) print(bf_rm) # Factorial ANOVA with interaction bf_factorial <- anovaBF(RT ~ shape * color + ID, data = puzzles, whichRandom = "ID", progress = FALSE) print(bf_factorial) # Top-down testing: compare full model to reduced models bf_top <- anovaBF(RT ~ shape * color + ID, data = puzzles, whichRandom = "ID", whichModels = "top", progress = FALSE) print(bf_top) # Extract Bayes factors as data frame print(extractBF(bf_factorial)) # Custom prior scales for effects bf_custom <- anovaBF(RT ~ shape * color + ID, data = puzzles, whichRandom = "ID", rscaleFixed = "wide", rscaleRandom = "nuisance", progress = FALSE) print(bf_custom) ``` ### Response Example ``` # Repeated measures ANOVA output: # Bayes factor analysis # [1] group + ID : 11.64 +/- 0.88% # Factorial ANOVA output: # Bayes factor analysis # [1] shape + color + ID + shape:color + shape:ID + color:ID + shape:color:ID : 1.23 +/- 0.02% ``` ``` -------------------------------- ### oneWayAOV.Fstat - BF from F Statistic Source: https://context7.com/richarddmorey/bayesfactor/llms.txt Computes Bayes factor from F statistic for balanced one-way ANOVA designs. ```APIDOC ## oneWayAOV.Fstat - BF from F Statistic ### Description Computes Bayes factor from F statistic for balanced one-way ANOVA designs. ### Method `oneWayAOV.Fstat` ### Parameters #### Path Parameters None #### Query Parameters - **F** (numeric) - The F-statistic. - **N** (numeric) - Total sample size. - **J** (numeric) - Number of groups. - **simple** (logical, optional) - If TRUE, returns only the Bayes factor. Defaults to FALSE. ### Request Example ```r library(BayesFactor) # From published ANOVA result <- oneWayAOV.Fstat(F = 34.7, N = 12, J = 6) exp(result[["bf"]]) # Smaller effect result_small <- oneWayAOV.Fstat(F = 3.5, N = 20, J = 3) exp(result_small[["bf"]]) # Simple output oneWayAOV.Fstat(F = 34.7, N = 12, J = 6, simple = TRUE) ``` ### Response Returns a Bayes factor or a list containing the Bayes factor and other related information. ``` -------------------------------- ### Recompute Bayes Factors for Precision Source: https://context7.com/richarddmorey/bayesfactor/llms.txt Increases sampling iterations to improve precision of Bayes factors or MCMC chains. ```r library(BayesFactor) data(puzzles) # Initial computation bf <- lmBF(RT ~ shape + color + ID, data = puzzles, whichRandom = "ID", progress = FALSE) bf # Recompute with more iterations for better precision bf_improved <- recompute(bf, iterations = 50000, progress = FALSE) bf_improved # Also works with MCMC chains chains <- posterior(bf, iterations = 1000, progress = FALSE) chains_more <- recompute(chains, iterations = 5000, progress = FALSE) nrow(chains_more) # 6000 total iterations ``` -------------------------------- ### Extract Bayes Factors with extractBF Source: https://context7.com/richarddmorey/bayesfactor/llms.txt Extracts Bayes factors from BFBayesFactor objects into a data frame or vector format. Useful for organizing and further analyzing computed Bayes factors. Shows extraction into a data frame with BF values and errors. ```r library(BayesFactor) data(puzzles) bf <- anovaBF(RT ~ shape * color + ID, data = puzzles, whichRandom = "ID", progress = FALSE) # Extract as data frame df <- extractBF(bf) df # bf error # shape + ID 0.1618569 0.01189952 # color + ID 3.7546679 0.02183406 ``` -------------------------------- ### Compute Bayesian ANOVA with anovaBF Source: https://context7.com/richarddmorey/bayesfactor/llms.txt Analyzes fixed and random effects in ANOVA designs, supporting repeated measures and custom prior scales. ```r library(BayesFactor) # Load example data data(sleep) data(puzzles) # Repeated measures ANOVA (within-subjects) # ID is specified as random factor for repeated measures bf_rm <- anovaBF(extra ~ group + ID, data = sleep, whichRandom = "ID", progress = FALSE) bf_rm # Bayes factor analysis # [1] group + ID : 11.64 +/- 0.88% # Factorial ANOVA with interaction bf_factorial <- anovaBF(RT ~ shape * color + ID, data = puzzles, whichRandom = "ID", progress = FALSE) bf_factorial # Top-down testing: compare full model to reduced models bf_top <- anovaBF(RT ~ shape * color + ID, data = puzzles, whichRandom = "ID", whichModels = "top", progress = FALSE) bf_top # Interprets as tests of individual effects # Extract Bayes factors as data frame extractBF(bf_factorial) # Custom prior scales for effects bf_custom <- anovaBF(RT ~ shape * color + ID, data = puzzles, whichRandom = "ID", rscaleFixed = "wide", rscaleRandom = "nuisance", progress = FALSE) ``` -------------------------------- ### Compute Meta-Analytic Bayes Factors with meta.ttestBF Source: https://context7.com/richarddmorey/bayesfactor/llms.txt Calculates meta-analytic Bayes factors from multiple t-statistics, assuming a common effect size. Useful for combining evidence across studies. Supports one-sided tests and sampling from the posterior of the common effect size. ```r library(BayesFactor) # Bem (2010) ESP data (from Rouder & Morey, 2011) t_values <- c(-0.15, 2.39, 2.42, 2.43) sample_sizes <- c(100, 150, 97, 99) # Meta-analytic Bayes factor bf_meta <- meta.ttestBF(t = t_values, n1 = sample_sizes, rscale = 1) bf_meta # One-sided test (effect > 0) bf_meta_pos <- meta.ttestBF(t = t_values, n1 = sample_sizes, rscale = 1, nullInterval = c(0, Inf)) bf_meta_pos[1] # Sample from posterior of common effect size samples <- posterior(bf_meta_pos[1], iterations = 10000, progress = FALSE) plot(samples[, "delta"]) summary(samples) ``` -------------------------------- ### linearReg.R2stat - BF from R-squared Source: https://context7.com/richarddmorey/bayesfactor/llms.txt Computes Bayes factor for regression from R-squared when raw data is unavailable. ```APIDOC ## linearReg.R2stat - BF from R-squared ### Description Computes Bayes factor for regression from R-squared when raw data is unavailable. ### Method `linearReg.R2stat` ### Parameters #### Path Parameters None #### Query Parameters - **N** (numeric) - Total sample size. - **p** (numeric) - Number of predictors. - **R2** (numeric) - The R-squared value. - **simple** (logical, optional) - If TRUE, returns only the Bayes factor. Defaults to FALSE. ### Request Example ```r library(BayesFactor) # Simple linear regression result <- linearReg.R2stat(N = 30, p = 1, R2 = 0.6813) exp(result[["bf"]]) # Multiple regression result_mult <- linearReg.R2stat(N = 100, p = 5, R2 = 0.35) exp(result_mult[["bf"]]) # Simple output linearReg.R2stat(N = 30, p = 1, R2 = 0.68, simple = TRUE) ``` ### Response Returns a Bayes factor or a list containing the Bayes factor and other related information. ``` -------------------------------- ### Compute Bayesian t-tests with ttestBF Source: https://context7.com/richarddmorey/bayesfactor/llms.txt Performs one-sample, two-sample, and paired t-tests, including directional testing and posterior sampling. ```r library(BayesFactor) # Load example data data(sleep) # One-sample t-test: test if mean differs from 0 x <- sleep$extra[sleep$group == 1] bf_one <- ttestBF(x = x, mu = 0) bf_one # Bayes factor analysis # [1] Alt., r=0.707 : 1.27 +/- 0% # Against denominator: # Null, mu = 0 # Two-sample independent t-test bf_indep <- ttestBF(x = sleep$extra[sleep$group == 1], y = sleep$extra[sleep$group == 2]) bf_indep # Paired t-test bf_paired <- ttestBF(x = sleep$extra[sleep$group == 1], y = sleep$extra[sleep$group == 2], paired = TRUE) bf_paired # Bayes factor analysis # [1] Alt., r=0.707 : 17.26 +/- 0% # One-sided test using nullInterval (effect > 0) bf_directional <- ttestBF(x = sleep$extra[sleep$group == 1], y = sleep$extra[sleep$group == 2], paired = TRUE, nullInterval = c(0, Inf)) bf_directional # Sample from posterior distribution samples <- ttestBF(x = sleep$extra[sleep$group == 1], y = sleep$extra[sleep$group == 2], paired = TRUE, posterior = TRUE, iterations = 10000) plot(samples[, "mu"]) summary(samples) ``` -------------------------------- ### Perform Bayesian Proportion Tests Source: https://context7.com/richarddmorey/bayesfactor/llms.txt Use proportionBF to test if a binomial proportion equals a specific value, supporting one-sided tests and posterior sampling. ```r library(BayesFactor) # Test if proportion differs from 0.5 # 15 successes out of 25 trials bf_prop <- proportionBF(y = 15, N = 25, p = 0.5) bf_prop # One-sided test: proportion > 0.5 bf_greater <- proportionBF(y = 15, N = 25, p = 0.5, nullInterval = c(0.5, 1)) bf_greater # Sample from posterior samples <- proportionBF(y = 15, N = 25, p = 0.5, posterior = TRUE, iterations = 10000) plot(samples[, "p"]) # 95% credible interval for proportion quantile(samples[, "p"], c(0.025, 0.975)) ``` -------------------------------- ### Test Independence in Contingency Tables Source: https://context7.com/richarddmorey/bayesfactor/llms.txt Use contingencyTableBF to test for independence under various sampling plans and estimate cell probabilities via posterior sampling. ```r library(BayesFactor) # Load doll race preference data data(raceDolls) raceDolls # Test independence with independent multinomial sampling # (column margins fixed) bf_indep <- contingencyTableBF(raceDolls, sampleType = "indepMulti", fixedMargin = "cols") bf_indep # Joint multinomial (total N fixed) bf_joint <- contingencyTableBF(raceDolls, sampleType = "jointMulti") bf_joint # Sample from posterior to estimate cell probabilities chains <- posterior(bf_indep, iterations = 10000, progress = FALSE) colnames(chains) # Compute posterior difference in proportions sameRaceGivenWhite <- chains[, "pi[1,1]"] / chains[, "pi[*,1]"] sameRaceGivenBlack <- chains[, "pi[1,2]"] / chains[, "pi[*,2]"] diff <- sameRaceGivenWhite - sameRaceGivenBlack hist(diff, main = "Posterior difference in probabilities", xlab = "P(same race | white) - P(same race | black)") quantile(diff, c(0.025, 0.975)) ``` -------------------------------- ### Compute Bayes Factor from R-squared with linearReg.R2stat Source: https://context7.com/richarddmorey/bayesfactor/llms.txt Calculates a Bayes factor for regression from R-squared when raw data is unavailable. Handles both simple and multiple regression scenarios. Provides an option for simple output of just the Bayes factor. ```r library(BayesFactor) # From published regression # N = 30, p = 1 predictor, R2 = 0.68 result <- linearReg.R2stat(N = 30, p = 1, R2 = 0.6813) exp(result[["bf"]]) # Very strong evidence (BF > 400,000) # Multiple regression # N = 100, p = 5 predictors, R2 = 0.35 result_mult <- linearReg.R2stat(N = 100, p = 5, R2 = 0.35) exp(result_mult[["bf"]]) # Simple output linearReg.R2stat(N = 30, p = 1, R2 = 0.68, simple = TRUE) ``` -------------------------------- ### ttestBF - Bayesian t-Test Source: https://context7.com/richarddmorey/bayesfactor/llms.txt Computes Bayes factors for one-sample, two-sample independent, and paired-sample t-tests. Supports interval null hypotheses and sampling from posterior distributions. ```APIDOC ## ttestBF - Bayesian t-Test ### Description Computes Bayes factors for one-sample, two-sample independent, and paired-sample t-tests. Uses a Cauchy prior on standardized effect sizes with configurable scale parameters ("medium", "wide", "ultrawide"). Supports interval null hypotheses for directional testing. ### Method `ttestBF` ### Parameters #### Arguments - **x** (numeric vector) - The first sample. - **y** (numeric vector, optional) - The second sample. If NULL, a one-sample test is performed. - **mu** (numeric, optional) - The hypothesized mean difference for a one-sample test, or the hypothesized difference in means for a two-sample test. Defaults to 0. - **paired** (logical, optional) - Whether to perform a paired t-test. Defaults to FALSE. - **rscale** (character or numeric, optional) - The scale of the Cauchy prior for the effect size. Can be "medium" (0.707), "wide" (1), "ultrawide" (3), or a custom numeric value. - **nullInterval** (numeric vector, optional) - A two-element vector specifying an interval for the null hypothesis (e.g., c(0, Inf) for a one-sided test). - **posterior** (logical, optional) - If TRUE, returns MCMC samples from the posterior distribution instead of Bayes factors. Defaults to FALSE. - **iterations** (integer, optional) - The number of MCMC iterations to perform if posterior = TRUE. ### Request Example ```r library(BayesFactor) # Load example data data(sleep) # One-sample t-test: test if mean differs from 0 x <- sleep$extra[sleep$group == 1] bf_one <- ttestBF(x = x, mu = 0) print(bf_one) # Two-sample independent t-test bf_indep <- ttestBF(x = sleep$extra[sleep$group == 1], y = sleep$extra[sleep$group == 2]) print(bf_indep) # Paired t-test bf_paired <- ttestBF(x = sleep$extra[sleep$group == 1], y = sleep$extra[sleep$group == 2], paired = TRUE) print(bf_paired) # One-sided test using nullInterval (effect > 0) bf_directional <- ttestBF(x = sleep$extra[sleep$group == 1], y = sleep$extra[sleep$group == 2], paired = TRUE, nullInterval = c(0, Inf)) print(bf_directional) # Sample from posterior distribution samples <- ttestBF(x = sleep$extra[sleep$group == 1], y = sleep$extra[sleep$group == 2], paired = TRUE, posterior = TRUE, iterations = 10000) plot(samples[, "mu"]) summary(samples) ``` ### Response Example ``` # One-sample t-test output: # Bayes factor analysis # [1] Alt., r=0.707 : 1.27 +/- 0% # Against denominator: # Null, mu = 0 # Paired t-test output: # Bayes factor analysis # [1] Alt., r=0.707 : 17.26 +/- 0% ``` ``` -------------------------------- ### meta.ttestBF - Meta-Analytic Bayes Factor Source: https://context7.com/richarddmorey/bayesfactor/llms.txt Computes meta-analytic Bayes factors from multiple t statistics, assuming a common underlying effect size. This is useful for combining evidence across studies. ```APIDOC ## meta.ttestBF - Meta-Analytic Bayes Factor ### Description Computes meta-analytic Bayes factors from multiple t statistics, assuming a common underlying effect size. Useful when combining evidence across studies. ### Method `meta.ttestBF` ### Parameters #### Path Parameters None #### Query Parameters - **t** (numeric vector) - t-statistics from individual studies. - **n1** (numeric vector) - Sample sizes for the first group in each study. - **n2** (numeric vector, optional) - Sample sizes for the second group in each study (for independent samples t-tests). - **rscale** (numeric, optional) - Prior scale for the effect size. Defaults to 1. - **nullInterval** (numeric vector, optional) - Interval for the null hypothesis. Defaults to `c(-Inf, Inf)`. ### Request Example ```r library(BayesFactor) t_values <- c(-0.15, 2.39, 2.42, 2.43) sample_sizes <- c(100, 150, 97, 99) bf_meta <- meta.ttestBF(t = t_values, n1 = sample_sizes, rscale = 1) bf_meta ``` ### Response Returns a BayesFactor object containing the meta-analytic Bayes factor. ``` -------------------------------- ### extractBF - Extract Bayes Factor Results Source: https://context7.com/richarddmorey/bayesfactor/llms.txt Extracts Bayes factor values or log Bayes factor values from a BayesFactor object. ```APIDOC ## extractBF - Extract Bayes Factor Results ### Description Extracts Bayes factor values or log Bayes factor values from a BayesFactor object. ### Method `extractBF(bf, logbf = FALSE, onlybf = FALSE)` ### Parameters #### Arguments - **bf** (BayesFactor object) - The BayesFactor object to extract results from. - **logbf** (logical) - If TRUE, returns log Bayes factors. Defaults to FALSE. - **onlybf** (logical) - If TRUE, returns only the BF values as a vector. Defaults to FALSE. ### Request Example ```r # Assuming 'bf' is a BayesFactor object # extractBF(bf, logbf = TRUE) # extractBF(bf, onlybf = TRUE) ``` ### Response Returns Bayes factor values, log Bayes factor values, or a vector of BF values depending on the arguments. ``` -------------------------------- ### Compute Bayes Factor from t-statistic with ttest.tstat Source: https://context7.com/richarddmorey/bayesfactor/llms.txt Calculates a Bayes factor from a t-statistic when raw data is unavailable. Useful for reanalyzing published results or for meta-analysis. Supports paired and independent two-sample tests, and one-sided tests. ```r library(BayesFactor) # From published t-test result # t = -4.06, n = 10 (paired test) result <- ttest.tstat(t = -4.0621, n1 = 10) exp(result[["bf"]]) # Bayes factor ~ 15 # Two-sample independent test # t = 2.5, n1 = 30, n2 = 35 result_2s <- ttest.tstat(t = 2.5, n1 = 30, n2 = 35) exp(result_2s[["bf"]]) # One-sided: effect is positive result_pos <- ttest.tstat(t = 2.5, n1 = 30, n2 = 35, nullInterval = c(0, Inf)) exp(result_pos[["bf"]]) # Simple output (just the BF) ttest.tstat(t = 2.5, n1 = 30, n2 = 35, simple = TRUE) ``` -------------------------------- ### Extract Bayes Factor Results Source: https://context7.com/richarddmorey/bayesfactor/llms.txt Extracts Bayes factor values from a BF object as a data frame or vector. ```r # Log Bayes factors extractBF(bf, logbf = TRUE) # Just the BF values as vector extractBF(bf, onlybf = TRUE) ``` -------------------------------- ### Perform Bayesian Correlation Tests Source: https://context7.com/richarddmorey/bayesfactor/llms.txt Use correlationBF to test if the correlation between two variables is zero, supporting directional hypotheses and posterior sampling. ```r library(BayesFactor) # Test correlation between Sepal dimensions in iris data bf_cor <- correlationBF(y = iris$Sepal.Length, x = iris$Sepal.Width) bf_cor # Bayes factor for rho != 0 vs rho = 0 # Directional test: positive correlation only bf_pos <- correlationBF(y = iris$Sepal.Length, x = iris$Sepal.Width, nullInterval = c(0, 1)) bf_pos # Sample from posterior distribution samples <- correlationBF(y = iris$Sepal.Length, x = iris$Sepal.Width, posterior = TRUE, iterations = 10000) plot(samples[, "rho"]) # Posterior distribution of correlation # Get credible interval for rho quantile(samples[, "rho"], probs = c(0.025, 0.5, 0.975)) ``` -------------------------------- ### extractBF - Extract Bayes Factors Source: https://context7.com/richarddmorey/bayesfactor/llms.txt Extracts Bayes factors from BFBayesFactor objects as a data frame or vector. ```APIDOC ## extractBF - Extract Bayes Factors ### Description Extracts Bayes factors from BFBayesFactor objects as a data frame or vector. ### Method `extractBF` ### Parameters #### Path Parameters None #### Query Parameters - **bf** (BFBayesFactor object) - The BayesFactor object from which to extract Bayes factors. ### Request Example ```r library(BayesFactor) data(puzzles) bf <- anovaBF(RT ~ shape * color + ID, data = puzzles, whichRandom = "ID", progress = FALSE) # Extract as data frame df <- extractBF(bf) df ``` ### Response Returns a data frame or vector containing the extracted Bayes factors. ``` -------------------------------- ### ttest.tstat - BF from t Statistic Source: https://context7.com/richarddmorey/bayesfactor/llms.txt Computes Bayes factor from a t statistic when raw data is unavailable. Useful for reanalyzing published results or meta-analysis. ```APIDOC ## ttest.tstat - BF from t Statistic ### Description Computes Bayes factor from a t statistic when raw data is unavailable. Useful for reanalyzing published results or meta-analysis. ### Method `ttest.tstat` ### Parameters #### Path Parameters None #### Query Parameters - **t** (numeric) - The t-statistic. - **n1** (numeric) - Sample size for the first group. - **n2** (numeric, optional) - Sample size for the second group (for independent samples t-tests). - **nullInterval** (numeric vector, optional) - Interval for the null hypothesis. Defaults to `c(-Inf, Inf)`. - **simple** (logical, optional) - If TRUE, returns only the Bayes factor. Defaults to FALSE. ### Request Example ```r library(BayesFactor) # Paired test result <- ttest.tstat(t = -4.0621, n1 = 10) exp(result[["bf"]]) # Independent samples test result_2s <- ttest.tstat(t = 2.5, n1 = 30, n2 = 35) exp(result_2s[["bf"]]) # Simple output ttest.tstat(t = 2.5, n1 = 30, n2 = 35, simple = TRUE) ``` ### Response Returns a Bayes factor or a list containing the Bayes factor and other related information. ``` === COMPLETE CONTENT === This response contains all available snippets from this library. No additional content exists. Do not make further requests.