#Computing probability that a study obtains a significant effect of the wrong sign
library(pwr)
#This function computes the probability of the wrong sign result for a given sample size and power of the study;
opp=function(n,power)
{
#Syntax
#n: sample size of study per cell
#power: power of the study
#degrees of freedom for ttest
df=2*n-2
#find effect size d that gives the determiend level of power
d=pwr.t.test(n=n,power=power)$d
#find the t-vale that gives you a p<.05 of the wrong sign
x.neg=-qt(.975,df=df)
#Compute probability of obtaiing that value under the alterantive
p.opp=pt(x.neg,df=df,ncp=d*sqrt(n/2))
#Compute the conditional probability given that p<.05
pp.opp=p.opp/power
#Report results
cat("\nIf powered to ",power," with n=",n,"then there is a ",p.opp," probability of p<.05 wrong sign")
cat("\nand |p<.05 there is a prob=",pp.opp,"\n\n\n")
return(pp.opp)
}
#There is <1% chance of getting p<.05 wrong sign with power=20%, sample size does not matter nearly at all
opp(n=100,power=.20)
opp(n=50,power=.20)
opp(n=20,power=.20)
1/opp(n=100,power=.50)
1/opp(n=100,power=.80)