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

You may also like...

1 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 *