Rstudio for Analysing ANOVA

ANOVA is a statistical test for estimating how a quantitative dependent variable changes according to the levels of one or more categorical independent variables. ANOVA tests whether there is a difference in means of the groups at each level of the independent variable.

The null hypothesis (H0) of the ANOVA is no difference in means, and the alternate hypothesis (Ha) is that the means are different from one another.

In this guide, we will walk you through the process of a one-way ANOVA (one independent variable) and a two-way ANOVA (two independent variables).

Our sample dataset contains observations from an imaginary study of the effects of fertilizer type and planting density on crop yield.

One-way ANOVA example In the one-way ANOVA, we test the effects of 3 types of fertilizer on crop yield. Two-way ANOVA example In the two-way ANOVA, we add an additional independent variable: planting density. We test the effects of 3 types of fertilizer and 2 different planting densities on crop yield. We will also include examples of how to perform and interpret a two-way ANOVA with an interaction term, and an ANOVA with a blocking variable.

Getting started in R If you haven’t used R before, start by downloading R and R Studio. Once you have both of these programs downloaded, open R Studio and click on File > New File > R Script.

Now you can copy and paste the code from the rest of this example into your script. To run the code, highlight the lines you want to run and click on the Run button on the top right of the text editor (or press ctrl + enter on the keyboard).

Install and load the packages First, install the packages you will need for the analysis (this only needs to be done once):

install.packages(c("ggplot2", "ggpubr", "tidyverse", "broom", "AICcmodavg")) Then load these packages into your R environment (do this every time you restart the R program):

library(ggplot2) library(ggpubr) library(tidyverse) library(broom) library(AICcmodavg) Library(box plots)

Step 1: Load the data into R Note that this data was generated for this example, it’s not from a real experiment!

We will use the same dataset for all of our examples in this walkthrough. The only difference between the different analyses is how many independent variables we include and in what combination we include them.

It is common for factors to be read as quantitative variables when importing a dataset into R. To avoid this, you can use the read.csv() command to read in the data, specifying within the command whether each of the variables should be quantitative (“numeric”) or categorical (“factor”).

Use the following code, replacing the path/to/your/file text with the actual path to your

filcrop.data <- read.csv("path/to/your/file/cr op.data.csv", header = TRUE, colClasses = c("factor", "factor", "factor", "numeric"))

Before proceeding with the analysis determine the summary/ details of the data imported.

   Summary( crop.data) 

Note- in the next series I will be giving a detailed example of how to use this codes in live situation.

Credit (Published on March 6, 2020 by Rebecca Bevans. Revised on May 6, 2022.)