Reader Comments
Post a new comment on this article
Post Your Discussion Comment
Please follow our guidelines for comments and review our competing interests policy. Comments that do not conform to our guidelines will be promptly removed and the user account disabled. The following must be avoided:
- Remarks that could be interpreted as allegations of misconduct
- Unsupported assertions or statements
- Inflammatory or insulting language
Thank You!
Thank you for taking the time to flag this posting; we review flagged postings on a regular basis.
closeANOVA optimal alpha R code version 1.1 (compatibility update for R version 3.x)
Posted by joemudge on 24 Jun 2013 at 16:30 GMT
#Optimal alpha ANOVA R code version 1.1, updated for compatibility with R version 3.x.
#Authored by Joe Mudge (questions or comments? contact: joe.mudge83@gmail.com).
optab.anova<-function (u=NULL,v=NULL,f2=NULL,T1T2cratio=1,HaHopratio=1){
beta.glm.test<-function (u = NULL, v = NULL, f2 = NULL, sig.level = 0.05, power = NULL){
if (!is.null(f2) && f2 < 0)
stop("f2 must be positive")
if (!is.null(u) && u < 1)
stop("degree of freedom u for numerator must be at least 1")
if (!is.null(v) && v < 1)
stop("degree of freedom v for denominator must be at least 1")
if (!is.null(sig.level) && !is.numeric(sig.level) || any(0 >
sig.level | sig.level > 1))
stop(sQuote("sig.level"), " must be numeric in [0, 1]")
p.body <- quote({
lambda <- f2 * (u + v + 1)
pf(qf(sig.level, u, v, lower = FALSE), u, v, lambda,lower = FALSE)
})
1-eval(p.body)
}
w.average.error.glm<-function (alpha=NULL,u=NULL,v=NULL,f2=NULL,T1T2cratio=1,HaHopratio=1)
((alpha*T1T2cratio+(HaHopratio*beta.glm.test(u=u,v=v,f2=f2,sig.level=alpha))))/(HaHopratio+T1T2cratio)
min.average.error.glm<-function (u=NULL,v=NULL,f2=NULL,T1T2cratio=1,HaHopratio=1)
unlist(optimize(w.average.error.glm,c(0,1),tol=0.00000000001,u=u,v=v,f2=f2,T1T2cratio=T1T2cratio,HaHopratio=HaHopratio))[2]
alpha.glm<-function (u=NULL,v=NULL,f2=NULL,T1T2cratio=1,HaHopratio=1)
unlist(optimize(w.average.error.glm,c(0,1),tol=0.000000000001,u=u,v=v,f2=f2,T1T2cratio=T1T2cratio,HaHopratio=HaHopratio))[1]
beta.glm<-function (u=NULL,v=NULL,f2=NULL,T1T2cratio=1,HaHopratio=1)
((T1T2cratio+HaHopratio)*min.average.error.glm(u=u,v=v,f2=f2,T1T2cratio=T1T2cratio,HaHopratio=HaHopratio)-T1T2cratio*alpha.glm(u=u,v=v,f2=f2,T1T2cratio=T1T2cratio,HaHopratio=HaHopratio))/HaHopratio
t(data.frame(
"numerator degrees of freedom"=u,
"denominator degrees of freedom"=v,
"among vs within group variance ratio effect size"=f2,
"Type I/II error cost ratio"=T1T2cratio,
"Ha/Ho prior probability ratio"=HaHopratio,
"overall probability of error"= (alpha.glm(u=u,v=v,f2=f2,T1T2cratio=T1T2cratio,HaHopratio=HaHopratio)+HaHopratio*beta.glm(u=u,v=v,f2=f2,T1T2cratio=T1T2cratio,HaHopratio=HaHopratio))/(1+HaHopratio),
"cost-weighted probability of error"= min.average.error.glm(u=u,v=v,f2=f2,T1T2cratio=T1T2cratio,HaHopratio=HaHopratio),
"optimal alpha"= alpha.glm(u=u,v=v,f2=f2,T1T2cratio=T1T2cratio,HaHopratio=HaHopratio),
"optimal beta"= beta.glm(u=u,v=v,f2=f2,T1T2cratio=T1T2cratio,HaHopratio=HaHopratio)
))
}
#The function used to calculate optimal alphas is: optab.anova(u=NULL,v=NULL,f2=NULL,T1T2cratio=1,HaHopratio=1)
#The arguments 'u' and 'v' are the numerator and denominator degrees of freedom, respectively.
#The argument 'f2' is the 'Cohen's f^2' standardized critical effect size. Cohen's f^2 = variance among group means/pooled within group variance
#The argument 'T1T2cratio' is the cost ratio of Type I errors relative to Type II errors. T1T2cratio is set at 1 as a default, making Type I and Type II errors equally serious.
#The argument 'HaHopratio' is the prior probability of the alternate hypothesis relative to the prior probability of the null hypothesis. HaHopratio is set at 1 as a default, to not weight alpha and beta by their prior probabilities (assuming they are unknown).
#This code is partially based on code modified from the R package 'pwr'(Champely 2009)