richarddmorey / BayesFactor

BayesFactor R package for Bayesian data analysis with common statistical models.
https://richarddmorey.github.io/BayesFactor/
132 stars 49 forks source link

A print(BFBayesFactor) function #65

Closed mvuorre closed 8 years ago

mvuorre commented 9 years ago

How about including a function for printing results from a Bayes Factor analysis into a data.frame? This would make it easier to knit .Rmd files with BF results in tables (.doc, LaTeX, etc.).

Something like this might suffice

print_bf <- function(bf_obj) {
    obj <- bf_obj@bayesFactor
    model <- rownames(obj)
    rownames(obj) <- NULL
    return( as.data.frame( cbind(model, obj[,1:2]) ) )
}

Thanks for the great software.

richarddmorey commented 9 years ago

Have you tried as.data.frame?

library(BayesFactor)
data(puzzles)

bf = anovaBF(RT ~ shape*color + ID, data = puzzles, whichRandom = "ID")
as.data.frame(bf)[,1:2]

This will output:

                                        bf       error
shape + ID                        2.802278 0.008411125
color + ID                        2.771243 0.006189023
shape + color + ID               12.583364 0.073732004
shape + color + shape:color + ID  4.038964 0.013105122

The downside is that you lose the denominator, but if you're printing you can do:

bf@denominator

which will output:

---
 Model:
Type: BFlinearModel, JZS
RT ~ ID 
Data types:
ID :  random 

So you can make sure people know what the denominator is when you print it to another document.

You might also be interested in the BayesFactorExtras package (https://github.com/richarddmorey/BayesFactorExtras) which has a function to print interactive HTML output using knitr.

mvuorre commented 9 years ago

Wow, fast response. That's great, thank you! In terms of using .Rmd & knitr to write manuscripts, I think it would be best if there was a function that wrote markdown tables (or formatted the output appropriately for knitr::kable() to handle). But you are right, the simple solution loses necessary information. as.data.frame() coupled with a good caption should do, though.