R – Functions for a manuscript-ready output for t-tests, BayesFactors and ezANOVAs

,

For quite some time I struggled to copy and paste the results from my t-tests and ANOVAs to my manuscripts. I finally decided to write a short script that puts the data in a structure I would like to present it in an article.

article.ttest(obj=NULL, unit=”ms”)

Produces article-ready output for a one- or two-sample t-test
Receives the result of the t.test()-function as input, one or two-sample
obj: a t.test object (list of 9)
unit: the measurement unit, standard is ms

> # One-sample / Paired samples t-test
> article.ttest(tt)
[1] "M = -9 ms; t(126) = -0.69, p = .493, 95% CI [-35 ms, 17 ms]"

> # Independent two-sample t-test
> apa.ttest(tt)
[1] "693 ms vs 702 ms; t(249.53) = -0.47, p = .639, 95% CI [-48 ms, 29 ms]"

article.ttest_full(obj=NULL, dz=NULL, BF=NULL, unit=”ms”)

Produces article-ready output for a one- or two-sample t-test, Cohens D, and bayesian t-test
Receives the result of the t.test()-function, the cohensD()-function and the ttestBF(function)
obj: a t.test object (list of 9)
unit: the measurement unit, standard is ms

> t.test, Cohens D, Bayesian ttest
> apa.ttest_full(tt,d,BF)
[1] "693 ms vs 702 ms; t(249.53) = -0.47, p = .639, dz = 0.06, 95% HPD [-34 ms, 16 ms], BF10 = 0.12"

article.ezANOVA(obj=NULL)

Produces article-ready output for an ezANOVA, in text form.
At the moment, p-values and DFs are not GG-corrected!
Receives the result of the ezANOVA()-function (return_aov = FALSE)
obj: a t.test object (list of 9)
unit: the measurement unit, standard is ms

> # ezANOVA
> article.ezANOVA(aov)
[1] "The main effect distractor_type was not significant: F(1, 181) = 3.12, p = .079; The main effect target_pos was significant: F(1, 181) = 10.71, p = .001; The interaction distractor_type X target_pos was significant: F(1, 181) = 5.58, p = .019"

 

Include functions

You can include the functions by adding these lines to the top of your script. I suggest you use the online version to automatically receive updates.

if(!exists("article.ttest", mode="function")) source("https://www.mariansauter.de/files/R/r_functions_article.R")
if(!exists("article.ttest_full", mode="function")) source("https://www.mariansauter.de/files/R/r_functions_article.R")
if(!exists("article.ezANOVA", mode="function")) source("https://www.mariansauter.de/files/R/r_functions_article.R")

 

If you have any questions, suggestions or found similar endeavors online, please let me know! For issues with the script, please contact me directly or refer to the github page.

One response

  1. Nice, thanks! I copy pasted portions of Lakens’ Perfect t-test (https://github.com/Lakens/Perfect-t-test) to accomplish something similar, currently just for paired-samples t-test, and crucially, without the output formatting, but maybe could be useful:

    pairedTtestStats <- function(x, y){
    diff<-x-y #difference scores
    r<-cor(x, y) #correlation between dependent measures
    m_diff<-mean(diff) #mean of difference scores
    s_diff<-sd(diff) #standard deviation of difference scores
    sd1<-sd(x) #standard deviation of group 1
    sd2<-sd(y) #standard deviation of group 2
    N <- length(x) #number of pairs

    ttestresult<-t.test(x, y, paired = TRUE, var.equal = TRUE)
    tvalue<-ttestresult$statistic #store t-value from dependent t-test
    pvalue<-ttestresult$p.value #store p-value from dependent t-test
    CI_diff<-ttestresult$conf.int #store confidence interval of mean difference
    s_av <- sqrt((sd1^2+sd2^2)/2) #calculate average standard deviation for effect size calculation

    #Effect sizes and 95% CI
    #Cohen's d_av, using s_av as standardizer

    require(MBESS) # to calculate non-central t limits

    d_av <- m_diff/s_av
    d_unb <- (1-(3/(4*(N-1)-1)))*d_av #note this is approximation of correction for Hedges'g – ESCI uses accurate correction, so should we.
    nct_limits <- conf.limits.nct(t.value = tvalue, df=N-1)
    ci_l_d_av <- nct_limits$Lower.Limit*s_diff/(s_av*sqrt(N))
    ci_u_d_av <- nct_limits$Upper.Limit*s_diff/(s_av*sqrt(N))

    #Cohen's d_z, using s_diff as standardizer
    d_z <- tvalue/sqrt(N)
    ci_l_d_z <- nct_limits$Lower.Limit/sqrt(N) #Not sure about this formula, but gives same results as Wuensch's files
    ci_u_d_z <- nct_limits$Upper.Limit/sqrt(N) #Not sure about this formula

    #BayesFactor
    require(BayesFactor) #To calculate Bayes Factor
    BF <- ttest.tstat(t = tvalue, n1 = N, simple=TRUE)

    #return values
    list(t_test = ttestresult, Hedges_g = d_unb, Hedges_CI_l = ci_l_d_av, Hedges_CI_u = ci_u_d_av,
    Cohens_dz = d_z, Cohens_CI_l = ci_l_d_z, Cohens_CI_u = ci_u_d_z, BF = BF)

    }

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.