knitr::opts_chunk$set(
  collapse = TRUE,
  comment = "#>"
)

Comparing Means

Analysis of Variance: ANOVA

Why not use t-Tests?

knitr::include_graphics("pictures/anova/1.png")

Type I Error

Regression v ANOVA

ANOVA: Understanding the NHST

ANOVA: Understanding the NHST

ANOVA: F-ratio

ANOVA: F-ratio

ANOVA: Example

The Data

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"))

Calculate $SS_T$

knitr::include_graphics("pictures/anova/4.png")

Visualize $SS_T$:

knitr::include_graphics("pictures/anova/5.png")

$SS_T$ df

Calculate $SS_M$

knitr::include_graphics("pictures/anova/6.png")

Visualize $SS_M$

knitr::include_graphics("pictures/anova/7.png")

$SS_M$ df

Calculate $SS_R$

knitr::include_graphics("pictures/anova/8.png")

Visualize $SS_R$

knitr::include_graphics("pictures/anova/9.png")
knitr::include_graphics("pictures/anova/10.png")

$SS_R$ df

knitr::include_graphics("pictures/anova/11.png")

Double Check

knitr::include_graphics("pictures/anova/12.png")

Calculate Mean Squared Error

knitr::include_graphics("pictures/anova/13.png")

Calculate the F-Ratio

knitr::include_graphics("pictures/anova/14.png")

Construct a Summary Table

knitr::include_graphics("pictures/anova/15.png")

F-values

Assumptions

Assumptions

Assumptions

ANOVA: Analysis

ANOVA: Analysis

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)

ANOVA: Levene's

ezANOVA(data = master,
        dv = libido,
        between = dose,
        wid = partno,
        type = 3, 
        detailed = T)$`Levene's Test for Homogeneity of Variance`

ANOVA: Levene's

## running a one way anova - if Levene's Test is significant
oneway.test(libido ~ dose, data = master)

ANOVA: F-Statistic

ezANOVA(data = master,
        dv = libido,
        between = dose,
        wid = partno,
        type = 3, 
        detailed = T)$ANOVA

ANOVA: Effect Size

ANOVA: Effect Size

ANOVA: Effect Size

ANOVA: Effect Size

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

Post Hocs and Trends: Why Use Follow-Up Tests?

Post Hocs and Trends: How?

Post Hoc Tests

Post Hoc Tests

Post Hoc Tests: Bonferroni

Post Hoc Tests: Comparison

## 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: Bonferroni

## 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")

Post Hoc Tests: Effect Size

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

ANOVA: Visualization

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")

Trend Analysis

knitr::include_graphics("pictures/anova/19.png")

Trend Analysis

Trend Analysis

## 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)

Trend Analysis: Visualization

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

Power: One Way Between Subjects

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

Summary



doomlab/learnSTATS documentation built on June 9, 2022, 12:54 a.m.