N.J
N.J

Reputation: 21

R:How to Create an Interaction Plot for Two-Way ANOVA

I am having a coding issue when trying to create an interaction plot of fixed-effects(Model 1) Two-Way ANOVA data.I typed and imported my data from excel into RStudio. The data was as follows.

Sex Genotype    Activity
Female  I   2.838
Female  I   4.216
Female  I   2.889
Female  I   4.198
Female  II  3.55
Female  II  4.556
Female  II  3.087
Female  II  1.943
Female  III 3.62
Female  III 3.079
Female  III 3.586
Female  III 1.943
Male    I   1.884
Male    I   2.283
Male    I   2.939
Male    I   1.486
Male    II  2.396
Male    II  2.956
Male    II  3.105
Male    II  2.649
Male    III 2.801
Male    III 3.421
Male    III 2.275
Male    III 2.11

I then saved it as an Excel Workbook. I then in RStudio went File>Import Dataset>Excel>Selected my dataset file>Selected First Row As Names & Open Data Viewer. I checked my data and it was all under the three headers in three columns. Then to do my ANOVA I did:

Model_1 <- aov(Activity ~ Sex*Genotype, data=data7)
> summary(Model_1)
         Df Sum Sq Mean Sq F value Pr(>F)  
Sex           1  3.527   3.527   6.563 0.0196 *
Genotype      2  0.178   0.089   0.165 0.8488  
Sex:Genotype  2  1.166   0.583   1.085 0.3591  
Residuals    18  9.673   0.537   

Next, I tried to make an interaction plot:

interaction.plot(Genotype,Sex,Activity, fun = mean, type= c("b"), xlab= 
"Genotype" ,ylab = "Enzyme Activity (enzyme unit (U)=1µmol min-
1)",main="Interaction Plot" ) 

But, when I do so I get this error:

 Error in tapply(response, list(x.factor, trace.factor), fun) : object 'Genotype' not found 

How do I make Genotype and Sex an object? Shouldn't they already be objects as they show up in the ANOVA table?

Also, I want to make a table of the Mean,SD, and n for each cell, but when I tried to do so using what worked for a one-way ANOVA it did not work for Two-way. How do you make such a table?

I have tried following examples online, but none of them have helped me with the getting the interaction plot to be created, and I have not seen any good examples (even on this site) on how to make a table of mean, SD, and n for Two-way ANOVA that have worked for my situation. Is there any package that I can use to help me?

If anyone can help explain what I did wrong and how to make the table I would greatly appreciate it.

Upvotes: 1

Views: 3771

Answers (2)

Susan G. Letcher
Susan G. Letcher

Reputation: 1

Here's base solution, perhaps kludgy but effective:

data7$group=paste(data7$Sex,data7$Genotype,sep="_")

tapply(data7$Activity,data7$group,sd)

Upvotes: 0

infominer
infominer

Reputation: 2011

Two ways to do it. Using base functions

#nj is your dataframe which I loaded using
nj <-read.table("nj_data.txt", header = T)
#Base R - using built-in functions
# the Dot indicates variable(column) to work on
# if you have more than one variable use their names
#good practice to use na.rm = T, to make sure you exclude
# NA - missing values, other wise mean and sd will report NA
aggregate(. ~ Sex+Genotype, data = nj,
          FUN = function(x) c(Mean = mean(x, na.rm = T),
                              n = length(x),
                              sd = sd(x, na.rm = T)))

#using dplyr - I suggest this as syntax is nicer
library(dplyr)
nj %>% group_by(Sex, Genotype) %>%
summarise_all(funs(mean(., na.rm = T),sd(., na.rm = T),n()))
#here the "." in mean and sd means using the data given 
#equivalent to x in base R above

Read-up Tidyverse ( a new bunch of libraries to make data analysis easier) at https://www.tidyverse.org/

and also ggplot - here's a nice primer on interaction plot using ggplot https://sebastiansauer.github.io/vis_interaction_effects/

Upvotes: 1

Related Questions