AI52487963
AI52487963

Reputation: 1273

Paired means for multiple groups?

Using pairwise.t.test on some example data, I can get a matrix of P-values:

attach(airquality)
pairwise.t.test(Ozone, Month)

which gives:

Pairwise comparisons using t tests with pooled SD 

data:  Ozone and Month 

    May     Jun     Jul     Aug    
Jun 1.00000 -       -       -      
Jul 0.00026 0.05113 -       -      
Aug 0.00019 0.04987 1.00000 -      
Sep 1.00000 1.00000 0.00488 0.00388

Is there an option (or maybe different function entirely?) to do the same, but with the mean differences between each group? Every example I find directs me to get mean differences only from 2 groups, as opposed to more than 2.

Upvotes: 1

Views: 77

Answers (1)

eipi10
eipi10

Reputation: 93851

You can do this with the outer function.

# Average monthly ozone
monthMean = tapply(airquality$Ozone, airquality$Month, mean, na.rm=TRUE)

# Difference between ozone levels in each pair of months
outer(monthMean, monthMean, FUN = "-")

          5         6           7           8         9
5  0.000000 -5.829060 -35.5000000 -36.3461538 -7.832891
6  5.829060  0.000000 -29.6709402 -30.5170940 -2.003831
7 35.500000 29.670940   0.0000000  -0.8461538 27.667109
8 36.346154 30.517094   0.8461538   0.0000000 28.513263
9  7.832891  2.003831 -27.6671088 -28.5132626  0.000000

Note that the matrix values are the row-month minus the column-month. If you want to just keep the upper or lower triangles you can do this:

monthDiff = outer(monthMean, monthMean, FUN = "-")

# Keep upper triangle (set lower triangle to NA)
monthDiff[lower.tri(month.diff)] = NA

# Keep lower triangle (set upper triangle to NA)
monthDiff[upper.tri(month.diff)] = NA

If you just want the absolute value of the difference between monthly means:

outer(monthMean, monthMean, 
      FUN = function(m1, m2) {abs(m1 - m2)})

Then you can use upper.tri or lower.tri to get rid of the redundant values.

Upvotes: 3

Related Questions