Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Best way to extract Mean Square Values from aov object in r

Tags:

r

statistics

I'm trying to write a function to automate doing a variance analysis, part of which involves doing some further calculations. The method I've been using isn't very robust, if variable names change then it stops working.

For this dummy data

> dput(assayvar,"")
structure(list(Run = structure(c(1L, 1L, 1L, 2L, 2L, 2L, 3L, 
3L, 3L, 4L, 4L, 4L), .rk.invalid.fields = list(), .Label = c("1", 
"2", "3", "4"), class = "factor"), Actual = c(246.3, 253.6, 247.6, 
249, 249, 251.3, 254.9, 254.1, 253.2, 250, 248.9, 250.3)), .Names = c("Run", 
"Actual"), row.names = c(NA, 12L), class = "data.frame")

> assayaov<-aov(Actual~Run+Error(Run),data=assayvar)
> str(summary(assayaov))
List of 2
 $ Error: Run   :List of 1
  ..$ :Classes ‘anova’ and 'data.frame':    1 obs. of  3 variables:
  .. ..$ Df     : num 3
  .. ..$ Sum Sq : num 46.5
  .. ..$ Mean Sq: num 15.5
  ..- attr(*, "class")= chr [1:2] "summary.aov" "listof"
 $ Error: Within:List of 1
  ..$ :Classes ‘anova’ and 'data.frame':    1 obs. of  5 variables:
  .. ..$ Df     : num 8
  .. ..$ Sum Sq : num 36.4
  .. ..$ Mean Sq: num 4.55
  .. ..$ F value: num NA
  .. ..$ Pr(>F) : num NA
  ..- attr(*, "class")= chr [1:2] "summary.aov" "listof"
 - attr(*, "class")= chr "summary.aovlist"

But for this dummy data

> dput(BGBottles,"")
structure(list(Machine = structure(c(1L, 1L, 1L, 2L, 2L, 2L, 
3L, 3L, 3L, 4L, 4L, 4L), .rk.invalid.fields = structure(list(), .Names = character(0)), .Label = c("1", 
"2", "3", "4"), class = "factor"), Weight = c(14.23, 14.96, 14.85, 
16.46, 16.74, 15.94, 14.98, 14.88, 14.87, 15.94, 16.07, 14.91
)), .Names = c("Machine", "Weight"), row.names = c(NA, 12L), class = "data.frame")

> bgaov<-aov(Weight~Machine+Error(Machine),data=BGBottles)
> str(summary(bgaov))
List of 2
 $ Error: Machine:List of 1
  ..$ :Classes ‘anova’ and 'data.frame':    1 obs. of  3 variables:
  .. ..$ Df     : num 3
  .. ..$ Sum Sq : num 5.33
  .. ..$ Mean Sq: num 1.78
  ..- attr(*, "class")= chr [1:2] "summary.aov" "listof"
 $ Error: Within :List of 1
  ..$ :Classes ‘anova’ and 'data.frame':    1 obs. of  5 variables:
  .. ..$ Df     : num 8
  .. ..$ Sum Sq : num 1.45
  .. ..$ Mean Sq: num 0.182
  .. ..$ F value: num NA
  .. ..$ Pr(>F) : num NA
  ..- attr(*, "class")= chr [1:2] "summary.aov" "listof"
 - attr(*, "class")= chr "summary.aovlist"

So the code to get the mean square value

machine<-summary(bgaov)$"Error: Machine"[[1]]$"Mean Sq"

Doesn't work, because the Machine but won't always be Machine.

Any better way to do this ?

like image 381
PaulHurleyuk Avatar asked Dec 22 '22 09:12

PaulHurleyuk


1 Answers

> summary(assayaov)[1][[1]][[1]][[3]]
[1] 15.49
> summary(bgaov)[1][[1]][[1]][[3]]
[1] 1.776475
like image 197
Ian Fellows Avatar answered Jan 13 '23 11:01

Ian Fellows