knitr::opts_chunk$set( collapse = TRUE, comment = "#>" )
If we want to compare three or more means why don't we compare pairs of means with t-tests?
knitr::include_graphics("pictures/anova/1.png")
Formula = $1 - (1-\alpha)^c$
Therefore, if you have:
r round(1-(1-.05)^3, 3)
r round(1-(1-.05)^6, 3)
r round(1-(1-.05)^10, 3)
ANOVA in Regression:
ANOVA in Experiments:
Actually can be the same question!
ANOVA is an Omnibus test
Variance created by our manipulation (IV levels)
Variance created by unknown factors
We calculate how much variability there is between scores: total sum of squares $SS_T$. This value is then divided into:
If the experiment is successful, then the model will explain more variance than it can't after accounting for differences in group sizes in these calculations
Testing the effects of Viagra on Libido using three groups:
The Outcome/Dependent Variable (DV) was an objective measure of Libido.
library(rio) master <- import("data/viagra.sav") str(master) master$dose <- factor(master$dose, levels = c(1,2,3), labels = c("Placebo", "Low Dose", "High Dose"))
knitr::include_graphics("pictures/anova/4.png")
knitr::include_graphics("pictures/anova/5.png")
In general, the df are one less than the number of values used to calculate the SS.
knitr::include_graphics("pictures/anova/6.png")
knitr::include_graphics("pictures/anova/7.png")
How many values did we use to calculate $SS_M$?
knitr::include_graphics("pictures/anova/8.png")
knitr::include_graphics("pictures/anova/9.png")
knitr::include_graphics("pictures/anova/10.png")
How many values did we use to calculate $SS_R$?
knitr::include_graphics("pictures/anova/11.png")
knitr::include_graphics("pictures/anova/12.png")
knitr::include_graphics("pictures/anova/13.png")
knitr::include_graphics("pictures/anova/14.png")
knitr::include_graphics("pictures/anova/15.png")
Think about the logic of model divided by error:
Robustness is the ability to still give you accurate results even though the assumptions are not quite met.
Levene's Test
ezANOVA
function in the ez
library. This package can analyze many types of ANOVA, run the assumption tests, and give you effect size all at once.First, you must add a participant number if you do not already have one in the dataset.
library(ez) ## you must have a participant number for ezANOVA master$partno <- 1:nrow(master) options(scipen = 20) ezANOVA(data = master, dv = libido, between = dose, wid = partno, type = 3, detailed = T)
ezANOVA(data = master, dv = libido, between = dose, wid = partno, type = 3, detailed = T)$`Levene's Test for Homogeneity of Variance`
## running a one way anova - if Levene's Test is significant oneway.test(libido ~ dose, data = master)
ezANOVA(data = master, dv = libido, between = dose, wid = partno, type = 3, detailed = T)$ANOVA
In ANOVA, there are two effect sizes - one for the overall (omnibus) test and one for the follow up tests (coming next).
F-test
library(MOTE) effect <- omega.F(dfm = 2, #this is dfn in the anova dfe = 12, #this is dfd in the anova Fvalue = 5.12, #this is F n = 15, #look at the number of rows in your dataset a = .05) #leave this as .05 effect$omega
Multiple t-tests
Orthogonal Contrasts/Comparisons
Post Hoc Tests
Trend Analysis
Other types of restricted contrasts:
## post hoc tests - p.value adjustment "none" pairwise.t.test(master$libido, master$dose, p.adjust.method = "none", paired = F, var.equal = T)
knitr::include_graphics("pictures/anova/16.png")
## post hoc tests - p.value adjustment "bonferroni" pairwise.t.test(master$libido, master$dose, p.adjust.method = "bonferroni", paired = F, var.equal = T)
knitr::include_graphics("pictures/anova/17.png")
knitr::include_graphics("pictures/anova/18.png")
## get numbers for effect size M <- tapply(master$libido, master$dose, mean) N <- tapply(master$libido, master$dose, length) STDEV <- tapply(master$libido, master$dose, sd) ## placebo to low effect1 <- d.ind.t(m1 = M[1], m2 = M[2], sd1 = STDEV[1], sd2 = STDEV[2], n1 = N[1], n2 = N[2], a = .05) effect1$d ## placebo to high effect2 = d.ind.t(m1 = M[1], m2 = M[3], sd1 = STDEV[1], sd2 = STDEV[3], n1 = N[1], n2 = N[3], a = .05) effect2$d ## low to high effect3 = d.ind.t(m1 = M[2], m2 = M[3], sd1 = STDEV[2], sd2 = STDEV[3], n1 = N[2], n2 = N[3], a = .05) effect3$d
library(ggplot2) cleanup <- theme(panel.grid.major = element_blank(), panel.grid.minor = element_blank(), panel.background = element_blank(), axis.line.x = element_line(color = "black"), axis.line.y = element_line(color = "black"), legend.key = element_rect(fill = "white"), text = element_text(size = 15)) bargraph <- ggplot(master, aes(dose, libido)) bargraph + cleanup + stat_summary(fun.y = mean, geom = "bar", fill = "White", color = "Black") + stat_summary(fun.data = mean_cl_normal, geom = "errorbar", width = .2, position = "dodge") + xlab("Dosage of Drug") + ylab("Average Libido")
knitr::include_graphics("pictures/anova/19.png")
## trend analysis k = 3 ## set to the number of groups master$dose2 <- master$dose #this changes the variable contrasts(master$dose2) <- contr.poly(k) ## note this does change the original dataset output <- aov(libido ~ dose2, data = master) summary.lm(output)
doseline <- ggplot(master, aes(dose2, libido)) doseline + stat_summary(fun.y = mean, ## adds the points geom = "point") + stat_summary(fun.y = mean, ## adds the line geom = "line", aes(group=1)) + stat_summary(fun.data = mean_cl_normal, ## adds the error bars geom = "errorbar", width = .2) + xlab("Dose of Viagra") + ylab("Average Libido") + cleanup
library(pwr) eta = .46 f_eta = sqrt(eta / (1-eta)) pwr.anova.test(k = 3, #levels n = NULL, #leave to get sample size per group f = f_eta, #f from eta sig.level = .05, #alpha power = .80) #power
You made it! Here's what we covered:
Add the following code to your website.
For more information on customizing the embed code, read Embedding Snippets.