-2

I use RStudio to calculate an analysis of variance (ANOVA). I use the aov function on a csv file you can find at the bottom of this post. This is my code:

filename = 'testdata.csv'
# Read in csv`
testdata <- read_csv(filename)
# add id`
testdata$id <- seq(nrow(testdata))

# Convert table to fit aov prerequisites
testdata <- testdata %>%
  gather(key = "time", value = "amplitude", c1, c2, c3)
# define factor
testdata$time <- as.factor(testdata$time)
 
# calculate ANOVA
res.aov <- aov(amplitude~factor(time)+Error(factor(id)), data = testdata)
summary(res.aov)
# use eta_sq
eta1 = eta_sq(res.aov)
# use eta_squared as eta_sq is deprecated
eta2 = eta_squared(res.aov)

When I execute this code, I get the following error message:

Error in UseMethod("anova") :
no applicable method for 'anova' applied to an object of class "c('aovlist', 'listof')"

I am not sure what that means. Any idea how to solve this?
Thanks,
Fred

CSV file:

c1,c2,c3
-0.56,-0.22,1.68
-1.71,-2.91,1.59
-0.75,-0.54,-2.52
0.47,0.45,1.16
0.68,0.78,-0.16
-1.02,-0.62,-3.26
-1.53,-0.04,-1.62
-0.35,0.17,0.32
-0.39,1.83,-0.6
2.22,2.32,0.15
0.2,-0.06,-1.13
-1.51,0.98,-2.36
-2.75,-1.09,-0.42
-0.07,-0.29,-0.67
-0.93,-0.03,-0.9
-1.06,0.09,-1.34
-2.02,-1.11,-3.47
-3.54,-3.3,-4.09
-1.71,-1.97,-2.93
-0.22,-0.36,-1.47
-1.63,0.7,-0.57
-1.24,-0.87,-2.16
-0.42,0.54,-1.06
-0.33,-0.03,-0.98
0.34,0.47,-0.4
1.12,1.25,0.13
0.09,0.64,0.25
Rui Barradas
  • 70,273
  • 8
  • 34
  • 66
  • Does `eta_squared` come from package `rstatix`? When the functions used are not base R functions, please start the scripts with `library(rstatix)` in order to load the packages needed for the code to run. – Rui Barradas Apr 28 '21 at 10:43
  • `res.aov` is a list with 3 members, are you looking for `eta_squared(res.aov[[3]])`? – Rui Barradas Apr 28 '21 at 10:59
  • please provide a reproducible example which also includes packages, see: https://stackoverflow.com/questions/5963269/how-to-make-a-great-r-reproducible-example – holzben Apr 28 '21 at 11:10
  • Sorry for not mentioning this, yes, eta_squared is from the library rstatix. Rui Barradas' solution works. Thanks! – Ben Labosch Apr 28 '21 at 13:14

1 Answers1

-1

You need to use the package lsr which contains provision to calculate eta square. The logic of using the id in anova was not very clear.. usually ANOVA is used to describe a continuous dependent variable (amplitude in this case) on basis of a discrete independent variable (time in this case). I have dropped the id from the equation and run the etaSquared from lsr package. Following is the code and output:

library(lsr)
testdata <- read_csv(file.choose())
# add id`
testdata$id <- seq(nrow(testdata))
# Convert table to fit aov prerequisites
testdata <- testdata %>%
gather(key = "time", value = "amplitude", c1, c2, c3)
# define factor
testdata$time <- as.factor(testdata$time)
# calculate ANOVA
res.aov <- aov(amplitude~factor(time), data = testdata)
summary(res.aov)
# use etaSquared from lsr package
eta2 = etaSquared(res.aov)
eta2

Output:

eta.sq eta.sq.part

factor(time) 0.07302364 0.07302364

  • Thank you very much for the effort. I did not use the package lsr. It seems what your code does it run an ANOVA for between subjects analyses. What I wanted to run is an ANOVA for repeated measures (within subjects). Hence the "id" in the formula. – Ben Labosch Apr 28 '21 at 13:07