Docsity

anova cheat sheet, Cheat Sheet of Computational and Statistical Data Analysis

Toggle sidebar
bg1
Cheat Sheet: ANOVA
Measurement and Evaluation of HCC Systems
Scenario
Use ANOVA if you want to test the difference in continuous outcome variable varY between
multiple levels (A, B, C, …) of a nominal variable varX. In this scenario varX is usually an
experimental manipulation with more than two conditions (but it can be any nominal variable,
such as race, occupation, etc). Each participant is randomly assigned to one condition.
Power analysis
- To do a power analysis for the omnibus test, use Test family ā€œF testsā€, ā€œANOVA: Fixed effects,
omnibus, one-wayā€.
- A power analysis has four variables: Effect size,
a
(usually .05), power (usually .85), and N. If
you know three of these, G*Power will calculate the fourth. Select the correct type of power
analysis, based on the information you have, and what you want to find out.
- ā€œNumber of groupsā€ is the number of levels of your nominal variable X.
- By clicking on ā€œDetermineā€, you can compute the effect size f from the mean and standard
deviation in each group, or from the partial
h
2.
- Click on ā€œCalculateā€ to calculate the missing parameter.
- To conduct power analyses for contrasts or post hoc tests, refer to power analysis of the
independent t test. Use a corrected
a
if needed.
Plotting a line plot and a box plot
- Use the ggplot2 package to plot a bar chart with error bars.
ggplot(data, aes(varX, varY)) + stat_summary(fun.y=mean, geom=ā€lineā€) +
stat_summary(fun.data=mean_cl_normal, geom=ā€errorbarā€, width=0.2)
- Plot a boxplot:
ggplot(data, aes(varX, varY)) + geom_boxplot()
Pre-testing assumptions
- In an ANOVA, Y should be independent, continuous, and unbounded. The error variance
should be normally distributed, which is true if Y is normally distributed for each level of X.
The ANOVA test is fairly robust against violations of normality. For a regular ANOVA,
pf3
pf4
pf5

Partial preview of the text

Download anova cheat sheet and more Cheat Sheet Computational and Statistical Data Analysis in PDF only on Docsity!

Cheat Sheet: ANOVA

Measurement and Evaluation of HCC Systems

Scenario

Use ANOVA if you want to test the difference in continuous outcome variable varY between multiple levels (A, B, C, …) of a nominal variable varX. In this scenario varX is usually an experimental manipulation with more than two conditions (but it can be any nominal variable, such as race, occupation, etc). Each participant is randomly assigned to one condition.

Power analysis

  • To do a power analysis for the omnibus test, use Test family ā€œ F testsā€, ā€œANOVA: Fixed effects, omnibus, one-wayā€.
  • A power analysis has four variables: Effect size, a (usually .05), power (usually .85), and N. If you know three of these, G*Power will calculate the fourth. Select the correct type of power analysis, based on the information you have, and what you want to find out.
  • ā€œNumber of groupsā€ is the number of levels of your nominal variable X.
  • By clicking on ā€œDetermineā€, you can compute the effect size f from the mean and standard

deviation in each group, or from the partial h^2.

  • Click on ā€œCalculateā€ to calculate the missing parameter.
  • To conduct power analyses for contrasts or post hoc tests, refer to power analysis of the

independent t test. Use a corrected a if needed.

Plotting a line plot and a box plot

  • Use the ggplot2 package to plot a bar chart with error bars. ggplot(data, aes(varX, varY)) + stat_summary(fun.y=mean, geom=ā€lineā€) + stat_summary(fun.data=mean_cl_normal, geom=ā€errorbarā€, width=0.2)
  • Plot a boxplot: ggplot(data, aes(varX, varY)) + geom_boxplot()

Pre-testing assumptions

  • In an ANOVA, Y should be independent, continuous, and unbounded. The error variance should be normally distributed, which is true if Y is normally distributed for each level of X. The ANOVA test is fairly robust against violations of normality. For a regular ANOVA,

variances are assumed to be equal across groups (homoscedasticity). Welch’s ANOVA does not make this assumption.

  • If your N is small: o Test for significant skewness, kurtosis, and Shapiro-Wilk test within each group using stat.desc in the pastecs package and the by function. by(data$varY, data$varX, stat.desc, basic=F, norm=T) o Multiply skew.2SE and kurt.2SE by 2 to get the Z - scores of skewness and kurtosis. Compare these values to typical cut-off values ( Z > ±1.96: p < .05, Z > ±2.58: p < .01, Z > ±3.29: p < .001). The significance of the Shapiro-Wilk test is listed under normtest.p. o Test the assumption of homoscedasticity, conduct Levene’s test in the car package. leveneTest(data$varY, data$varX, center=median) o A significant Levene’s test means that the data is heteroscedastic. In this case, Welch’s ANOVA should be used instead of a regular ANOVA.
  • If your N is large: o Subset the data into multiple groups based on varX: Adata <- subset(data, data$varX == ā€œAā€) Bdata <- subset(data, data$varX == ā€œBā€) etc… o Draw the histogram for varY in Adata, overlaid with a normal curve (using ggplot2), and visually inspect whether it follows the normal distribution: ggplot(Adata, aes(varY)) + geom_histogram(aes(y=..density..), binwidth=1, color="black", fill="white") + stat_function(fun = dnorm, args = list(mean = mean(Adata$varY), sd = sd(Adata$varY))) o Change the binwidth setting based on what is suitable for your data. o Draw normal a Q-Q plot, and visually inspect whether the data follows the diagonal line: qplot(sample = Adata$varY, stat=ā€qqā€) o Do the same for Bdata, etc. o Visually inspect the boxplot to assess the level of heteroscedasticity. You can also use the variance ratio test (Hartley’s F max). This is the ratio of the variances between the group with the biggest variance and the group with the smallest variance. max(by(data$varY,data$varX,var)) / min(by(data$varY,data$varX,var)) o Compare this value against Figure 5.8 in the book.
  • If your data has positive skew, and your data only has positive values, you can possibly fix this by transforming your Y variable: o Log transform: data$varYlog <- log(data$varY + 1) o Or, square root transform: data$varYsqrt <- sqrt(data$varY)

BvC <- c(0, - 1/2, 1/2) Then load the contrasts into VarX: contrasts(data$varX) <-cbind(AvBC, BvC) Make sure that the levels of varX are ordered A, B, C (otherwise use relevel to fix the order).

  • Note that the dummy contrast is a non-orthogonal contrast. This means that you should apply a correction to the p - values of these contrasts. See ā€œApplying correctionsā€ below.

Running the test

  • Run the ANOVA as follows: model1 <- aov(varY ~ varX, data = data)
  • Get the model summary: summary(model1)
  • The F - statistic and its p - value tells us whether there is a significant difference between the different groups.
  • (optional) if the variances are not equal across groups, use Welch’s ANOVA: oneway.test(varY~varX,data=temp)
  • You can get the effect size omega-squared using the omega_aov function I created: omega_aov(model1)

(optional) Robust versions

  • You can use functions in the WRS2 package to run trimmed and/or bootstrapped ANOVAs.
  • Robust ANOVA using 10% trimmed means (change the percentage if desired): t1way(varY ~ varX, data = data, tr = 0.1)
  • Robust ANOVA using bootstrapped median with 2000 samples: med1way(varY ~ varX, data = data, iter = 2000)
  • Robust ANOVA using 10% trimmed means and 2000 bootstrap samples: t1waybt(varY ~ varX, data = data, tr = 0.1, nboot = 2000)

(optional) Evaluate contrasts

  • Evaluate your planned contrasts using the summary.lm function. summary.lm(model1)
  • In terms of overall model fit, this output will give you the R-squared, which is the proportion of the variance of varY explained by the model. In ANOVA this value is called eta-squared.
  • Each coefficient represents the effect of one contrast. o For dummy contrasts, the coefficient represents the difference in Y between this category and the baseline category. o For Helmer contrasts, the coefficient represents the difference in Y between the listed condition and all preceding conditions.

o For polynomial contrasts, the coefficient represents the linear, quadratic, or higher-level polynomial effect of the X conditions on Y. o For custom contrasts, the coefficient represents the difference in Y between the two groups of conditions included in the contrast.

  • Each coefficient has a t test and a p - value to test if the effect is significant. Divide the p - value by 2 if you were conducting a one-sided test (this is very likely for planned contrasts!). You can get the effect size r using the formula r = √(t^2 / t^2 + df)).
  • You can get confidence intervals for the coefficients using the confint function: confint(model1)
  • (optional) Apply corrections for k non-orthogonal contrasts.

o Bonferroni: use p crit = a/ k.

o Holm: order your p - values by size, smallest first. The p crit for the first one is a/ k. If p < p crit,

move to the next (otherwise stop). The p crit for the next one is a/( k – 1). If p < p crit, move

to the next (otherwise stop). Continue until p > p crit. That p - value and all larger ones are non-significant. o Benjamini-Hochberg: order your p - values by size, largest first. The p crit for the first one is

( k / k )* a. If p > p crit, move to the next (otherwise stop). The p crit for the next one is (( k –

1)/ k )* a. If p < p crit, move to the next (otherwise stop). Continue until p < p crit. That p -

value and all larger ones are significant.

(optional) Post-hoc tests

  • If you do not have specific hypotheses about where specific differences between conditions exist, you can test all of them using post-hoc tests.
  • Bonferroni-corrected tests (use ā€œholmā€ for Holm, and ā€œBHā€ for Bejamini-Hochberg): pairwise.t.test(data$varY, data$varX, p.adjust.method = ā€œBonferroniā€)
  • Tukey post-hoc tests and confidence intervals using the package multcomp (can also conduct ā€œDunnettā€ post-hoc test): post <- glht(model1, linfct=mcp(varX = ā€œTukeyā€)) summary(post) confint(post)

(optional) Robust post-hoc tests

  • Post-hoc tests using 10% trimmed means (confidence intervals are corrected; p-values are not): lincon(varY ~ varX, data=data, tr=0.1)
  • Post-hoc tests using 10% trimmed bootstrapped means, using 2000 bootstrap samples: mcppb20(varY ~ varX, data=data, tr=0.1, nboot=2000, crit=.05)