Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
ezANOVA <-
function(
data
, dv
, wid
, within = NULL
, between = NULL
, observed = NULL
, diff = NULL
, reverse_diff = FALSE
, detailed = FALSE
){
to_return = ezANOVA_main(data,dv,wid,within,between,observed,diff,reverse_diff)
########
# Compute likelihood ratios
########
n = length(levels(to_return$data[,names(to_return$data)==wid]))
involves_within = laply(
strsplit(to_return$ANOVA$Effect,':')
, function(x){
any(x %in% as.character(within))
}
)
involves_between = laply(
strsplit(to_return$ANOVA$Effect,':')
, function(x){
any(x %in% as.character(between))
}
)
between_error = to_return$ANOVA$SSd[1]
all_between_effects = sum(to_return$ANOVA$SSn[involves_between & !involves_within])
between_null = between_error+all_between_effects
within_error = sum(to_return$ANOVA$SSd[!involves_between & involves_within])
all_within_effects = sum(to_return$ANOVA$SSn[!involves_between & involves_within])+sum(to_return$ANOVA$SSn[involves_between & involves_within])
within_null = within_error+all_within_effects
#compute the likelihood ratios for each effect
to_return$ANOVA$model_between = NA
to_return$ANOVA$model_within = NA
to_return$ANOVA$comparison_between = NA
to_return$ANOVA$comparison_within = NA
to_return$ANOVA$LR = NA
for(i in 2:nrow(to_return$ANOVA)){
model_between = between_null
model_within = within_null
effect_split = strsplit(to_return$ANOVA$Effect[i], ':')[[1]]
if(length(effect_split)>1){
from_terms = terms(
eval(
parse(
text = paste(
'1~'
, paste(
effect_split
, collapse = '*'
)
)
)
)
)
term_labels = attr(from_terms,'term.labels')
for(this_term_label in term_labels[1:(length(term_labels)-1)]){
if(involves_within[to_return$ANOVA$Effect==this_term_label]){
model_within = model_within - to_return$ANOVA$SSn[to_return$ANOVA$Effect==this_term_label]
}else{
model_between = model_between - to_return$ANOVA$SSn[to_return$ANOVA$Effect==this_term_label]
}
}
}
comparison_between = model_between
comparison_within = model_within
if(involves_within[i]){
model_within = model_within - to_return$ANOVA$SSn[i]
}else{
model_between = model_between - to_return$ANOVA$SSn[i]
}
to_return$ANOVA$model_between[i] = model_between
to_return$ANOVA$model_within[i] = model_within
to_return$ANOVA$comparison_between[i] = comparison_between
to_return$ANOVA$comparison_within[i] = comparison_within
between_LR = (comparison_between/model_between)^(n/2)
within_LR = (comparison_within/model_within)^(to_return$ANOVA$DFd[i]/2)
to_return$ANOVA$LR[i] = between_LR*within_LR
}
#compute and apply corrections for complexity
aic = exp(-to_return$ANOVA$DFn)
bic = exp(-to_return$ANOVA$DFn)^(log(n)/2)
to_return$ANOVA$LLRa = log(aic*to_return$ANOVA$LR,base=10)
to_return$ANOVA$LLRb = log(bic*to_return$ANOVA$LR,base=10)
########
# Compute effect size
########
if(!is.null(observed)){
obs = rep(F,nrow(to_return$ANOVA))
for(i in as.character(observed)){
obs = obs | str_detect(to_return$ANOVA$Effect,i)
}
obs_SSn1 = sum(to_return$ANOVA$SSn*obs)
obs_SSn2 = to_return$ANOVA$SSn*obs
}else{
obs_SSn1 = 0
obs_SSn2 = 0
}
to_return$ANOVA$ges = to_return$ANOVA$SSn/(to_return$ANOVA$SSn+sum(unique(to_return$ANOVA$SSd))+obs_SSn1-obs_SSn2)
########
# Final clean-up
########
#remove the data from to_return
temp = names(to_return)
temp = temp[temp!='data']
to_return = to_return[names(to_return)!='data']
names(to_return) = temp
#if necessary, remove extra columns and the Intercept row from the anova
if(!detailed){
to_return$ANOVA = to_return$ANOVA[,!(names(to_return$ANOVA) %in% c('SSn','SSd','between_LR','within_LR','LR','k','aic','bic'))]
to_return$ANOVA = to_return$ANOVA[2:nrow(to_return$ANOVA),]
}
#all done!
return(to_return)
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment