<?xml version="1.0" encoding="UTF-8" standalone="yes"?><oembed><version><![CDATA[1.0]]></version><provider_name><![CDATA[Replicability-Index]]></provider_name><provider_url><![CDATA[https://replicationindex.wordpress.com]]></provider_url><author_name><![CDATA[Dr. R]]></author_name><author_url><![CDATA[https://replicationindex.wordpress.com/author/rindex4science/]]></author_url><title><![CDATA[Subjective Bayesian T-Test&nbsp;Code]]></title><type><![CDATA[link]]></type><html><![CDATA[<h6>########################################################</h6>
<p>rm(list=ls()) #will remove ALL objects</p>
<p>##############################################################<br />
Bayes-Factor Calculations for T-tests<br />
##############################################################</p>
<p>#Start of Settings</p>
<p>### Give a title for results output<br />
Results.Title = &#8216;Normal(x,0,.5) N = 100 BS-Design, Obs.ES = 0&#8242;</p>
<p>### Criterion for Inference in Favor of H0, BF (H1/H0)<br />
BF.crit.H0 = 1/3</p>
<p>### Criterion for Inference in Favor of H1<br />
#set z.crit.H1 to Infinity to use Bayes-Factor, BF(H1/H0)<br />
BF.crit.H1 = 3<br />
z.crit.H1 = Inf</p>
<p>### Set Number of Groups<br />
gr = 2</p>
<p>### Set Total Sample size<br />
N = 100</p>
<p>### Set observed effect size<br />
### for between-subject designs and one sample designs this is Cohen&#8217;s d<br />
### for within-subject designs this is dz<br />
obs.es = 0</p>
<p>### Set the mode of the alternative hypothesis<br />
alt.mode = 0</p>
<p>### Set the variability of the alternative hypothesis<br />
alt.var = .5</p>
<p>### Set the shape of the distribution of population effect sizes<br />
alt.dist = 2  #1 = Cauchy; 2 = Normal</p>
<p>### Set the lower bound of population effect sizes<br />
### Set to zero if there is zero probability to observe effects with the opposite sign<br />
low = -3</p>
<p>### Set the upper bound of population effect sizes<br />
### For example, set to 1, if you think effect sizes greater than 1 SD are unlikely<br />
high = 3</p>
<p>### set the precision of density estimation (bigger takes longer)<br />
precision = 100</p>
<p>### set the graphic resolution (higher resolution takes longer)<br />
graphic.resolution = 20</p>
<p>### set limit for non-central t-values<br />
nct.limit = 100</p>
<p>################################<br />
# End of Settings<br />
################################</p>
<p># compute degrees of freedom<br />
df = (N &#8211; gr)</p>
<p># get range of population effect sizes<br />
pop.es=seq(low,high,(1/precision))</p>
<p># compute sampling error<br />
se = gr/sqrt(N)</p>
<p># limit population effect sizes based on non-central t-values<br />
pop.es = pop.es[pop.es/se &gt;= -nct.limit &amp; pop.es/se &lt;= nct.limit]</p>
<p># function to get weights for Cauchy or Normal Distributions<br />
get.weights=function(pop.es,alt.dist,p) {<br />
if (alt.dist == 1) w = dcauchy(pop.es,alt.mode,alt.var)<br />
if (alt.dist == 2) w = dnorm(pop.es,alt.mode,alt.var)<br />
sum(w)<br />
# get the scaling factor to scale weights to 1*precision<br />
#scale = sum(w)/precision<br />
# scale weights<br />
#w = w / scale<br />
return(w)<br />
}</p>
<p># get weights for population effect sizes<br />
weights = get.weights(pop.es,alt.dist,precision)</p>
<p>#Plot Alternative Hypothesis<br />
Title=&#8221;Alternative Hypothesis&#8221;<br />
ymax=max(max(weights)*1.2,1)<br />
plot(pop.es,weights,type=&#8217;l&#8217;,ylim=c(0,ymax),xlab=&#8221;Population Effect Size&#8221;,ylab=&#8221;Density&#8221;,main=Title,col=&#8217;blue&#8217;,lwd=3)<br />
abline(v=0,col=&#8217;red&#8217;)</p>
<p>#create observations for plotting of prediction distributions<br />
obs = seq(low,high,1/graphic.resolution)</p>
<p># Get distribution for observed effect size assuming H1<br />
H1.dist = as.numeric(lapply(obs, function(x) sum(dt(x/se,df,pop.es/se) * weights)/precision))</p>
<p>#Get Distribution for observed effect sizes assuming H0<br />
H0.dist = dt(obs/se,df,0)</p>
<p>#Compute Bayes-Factors for Prediction Distribution of H0 and H1<br />
BFs = H1.dist/H0.dist</p>
<p>#Compute z-scores (strength of evidence against H0)<br />
z = qnorm(pt(obs/se,df,log.p=TRUE),log.p=TRUE)</p>
<p># Compute H1 error rate rate<br />
BFpos = BFs<br />
BFpos[z &lt; 0] = Inf<br />
if (z.crit.H1 == Inf) z.crit.H1 = abs(z[which(abs(BFpos-BF.crit.H1) == min(abs(BFpos-BF.crit.H1)))])<br />
ncz = qnorm(pt(pop.es/se,df,log.p=TRUE),log.p=TRUE)<br />
weighted.power = sum(pnorm(abs(ncz),z.crit.H1)*weights)/sum(weights)<br />
H1.error = 1-weighted.power</p>
<p>#Compute H0 Error Rate<br />
z.crit.H0 = abs(z[which(abs(BFpos-BF.crit.H0) == min(abs(BFpos-BF.crit.H0)))])<br />
H0.error = (1-pnorm(z.crit.H0))*2</p>
<p># Get density for observed effect size assuming H0<br />
Density.Obs.H0 = dt(obs.es,df,0)</p>
<p># Get density for observed effect size assuming H1<br />
Density.Obs.H1 = sum(dt(obs.es/se,df,pop.es/se) * weights)/precision</p>
<p># Compute Bayes-Factor for observed effect size<br />
BF.obs.es = Density.Obs.H1 / Density.Obs.H0</p>
<p>#Compute z-score for observed effect size<br />
obs.z = qnorm(pt(obs.es/se,df,log.p=TRUE),log.p=TRUE)</p>
<p>#Show Results<br />
ymax=max(H0.dist,H1.dist)*1.3<br />
plot(type=&#8217;l&#8217;,z,H0.dist,ylim=c(0,ymax),xlab=&#8221;Strength of Evidence (z-value)&#8221;,ylab=&#8221;Density&#8221;,main=Results.Title,col=&#8217;black&#8217;,lwd=2)<br />
par(new=TRUE)<br />
plot(type=&#8217;l&#8217;,z,H1.dist,ylim=c(0,ymax),xlab=&#8221;&#8221;,ylab=&#8221;&#8221;,col=&#8217;blue&#8217;,lwd=2)<br />
abline(v=obs.z,lty=2,lwd=2,col=&#8217;darkgreen&#8217;)<br />
abline(v=-z.crit.H1,col=&#8217;blue&#8217;,lty=3)<br />
abline(v=z.crit.H1,col=&#8217;blue&#8217;,lty=3)<br />
abline(v=-z.crit.H0,col=&#8217;red&#8217;,lty=3)<br />
abline(v=z.crit.H0,col=&#8217;red&#8217;,lty=3)<br />
points(pch=19,c(obs.z,obs.z),c(Density.Obs.H0,Density.Obs.H1))<br />
res = paste0(&#8216;BF(H1/H0): &#8216;,format(round(BF.obs.es,3),nsmall=3))<br />
text(min(z),ymax*.95,pos=4,res)<br />
res = paste0(&#8216;BF(H0/H1): &#8216;,format(round(1/BF.obs.es,3),nsmall=3))<br />
text(min(z),ymax*.90,pos=4,res)<br />
res = paste0(&#8216;H1 Error Rate: &#8216;,format(round(H1.error,3),nsmall=3))<br />
text(min(z),ymax*.80,pos=4,res)<br />
res = paste0(&#8216;H0 Error Rate: &#8216;,format(round(H0.error,3),nsmall=3))<br />
text(min(z),ymax*.75,pos=4,res)</p>
<p>######################################################<br />
### END OF Subjective Bayesian T-Test CODE<br />
######################################################<br />
### Thank you to Jeff Rouder for posting his code that got me started.<br />
### <a href="http://jeffrouder.blogspot.ca/2016/01/what-priors-should-i-use-part-i.html" rel="nofollow">http://jeffrouder.blogspot.ca/2016/01/what-priors-should-i-use-part-i.html</a></p>
<p>&nbsp;</p>
]]></html><thumbnail_url><![CDATA[https://replicationindex.files.wordpress.com/2016/07/sbtt-normal-sd50.png?fit=440%2C330]]></thumbnail_url><thumbnail_width><![CDATA[331]]></thumbnail_width><thumbnail_height><![CDATA[330]]></thumbnail_height></oembed>