README.md

This package is no longer under development - it is recommended instead to use emmeans's custom contrast fucntions instead.

marginC

This (mini) R package allows for the definition of interaction contrasts by way of defining marginal contrasts. Mainly, this package allows:

  1. Given marginal weights, will provide a vector of weights that can be passed along to emmeans::contrast.
  2. If a reference-grid object (created by emmeans::emmeans) is provided, the following tests will be conducted before returning the vector of weights:
    1. Re-order the marginal weights to match those in the reference-grid object.
    2. Test that the provided marginal weights match those in the reference-grid object.
    3. Test that the marginal weights are of the correct length (as those)tested against the reference-grid object).

Download and Install

You can install marginC from github with:

# install.packages("devtools")
devtools::install_github("mattansb/marginC")

These functions are meant for use alongside emmeans's functions (as seen bellow).

You will also need: - magrittr - purrr (Both part of the tidyverse.)

Example using afex

Some sample anova with afex:

library(afex)

data(md_12.1)
fit <- aov_ez("id", "rt", md_12.1,
              within = c("angle", "noise"))

| Effect | df | MSE | F | pes | p.value | |:------------|:------|:--------|:-------------|:----|:----------| | angle | 2, 18 | 3560.00 | 40.72 *** | .82 | <.0001 | | noise | 1, 9 | 8460.00 | 33.77 *** | .79 | .0003 | | angle:noise | 2, 18 | 1160.00 | 45.31 *** | .83 | <.0001 |

Using emmeans it is easy to see this pattern:

Lets break down that interaction using emmeans:

library(emmeans)

# Define the interactions's refeerence grid:
rg <- emmeans(fit, ~angle*noise)

# plot it:
emmip(rg, noise~angle, CIs = T)

For this example we will focus on the difference in the effect of noise between angle-4 and angle-8. We can define this interaction-contrast using marginal weights: - noise = c(-1,1) (the difference between the levels of noise) - angle = c(0,1,-1) (the difference between the two of the levels of angle)

Lets see how marginC helps define this contrast.

Using get_marginal_weights

library(marginC)

get_marginal_weights(angle = c(0,-1,1), noise = c(-1,1))
## [1]  0  1 -1  0 -1  1
## No grid provided

If we include the reference grid (produced by emmeans), we get some additional functionality: - The weights are named. - The order in which we specify the marginal weights is dealt with internally. - The lengths of the marginal weights is also tested internally for consistency with the reference grid object.

get_marginal_weights(angle = c(0,-1,1), noise = c(-1,1), .grid = rg)
## [1]  0  1 -1  0 -1  1
## Grid provided

Finally, we can feed this vector into emmeans's contrast:

my_weights <- list(my_contrast = get_marginal_weights(angle = c(0,-1,1),
                                                     noise = c(-1,1),
                                                     .grid = rg)) 
contrast(rg, my_weights)
##  contrast    estimate       SE df t.ratio p.value
##  my_contrast       84 21.54066 18     3.9  0.0011

Using mw

mw is a wraparound function for get_marginal_weights for ease of use:

mw(angle = c(0,-1,1), noise = c(-1,1))
## [1]  0  1 -1  0 -1  1
## No grid provided
mw(angle = c(0,-1,1), noise = c(-1,1), .grid = rg)
## [1]  0  1 -1  0 -1  1
## Grid provided

Updating the Grid

Instead of providing the grid directly to get_marginal_weights (or mw), we can apply is after-the fact by using update_grid:

my_weights <- list(first = mw(noise = c(-1,1),angle = c(0,-1,1)),
                   second = mw(noise = c(-1,1),angle = c(2,1,1)))
my_weights
## $first
## [1]  0  0  1 -1 -1  1
## No grid provided
## 
## $second
## [1] -2  2 -1  1 -1  1
## No grid provided
my_weights <- update_grid(my_weights,grid = rg)
my_weights
## $first
## [1]  0  1 -1  0 -1  1
## Grid provided
## 
## $second
## [1] -2 -1 -1  2  1  1
## Grid provided

Note that after calling update_grid, the returned weighs have changed, due to us providing the marginal weights in the incorrect order when we first called mw.

Using as a contrast method:

We can insert our custom weights as a method by following the emmeans guidlines. Here is an example:

my_method.emmc <- function(x){
  data.frame(my_contrast = mw(noise = c(-1,1), .grid = rg))
}

contrast(rg,interaction = c('poly','my_method'))
## Warning in test_grid(factor_weights, grid = .grid): Some factor(s) missing
## from list.

##  angle_poly noise_my_method estimate       SE df t.ratio p.value
##  linear     my_contrast          204 21.54066 18   9.470  <.0001
##  quadratic  my_contrast          -36 37.30952 18  -0.965  0.3474
contrast(rg,interaction = c('consec','my_method'))
## Warning in test_grid(factor_weights, grid = .grid): Some factor(s) missing
## from list.

##  angle_consec noise_my_method estimate       SE df t.ratio p.value
##  X4 - X0      my_contrast          120 21.54066 18   5.571  <.0001
##  X8 - X4      my_contrast           84 21.54066 18   3.900  0.0011

Authors



mattansb/marginC documentation built on May 28, 2019, 3:39 p.m.