DirectEffects icon indicating copy to clipboard operation
DirectEffects copied to clipboard

Interactions in the blip down function don't work

Open mattblackwell opened this issue 6 years ago • 3 comments

MWE:

library(DirectEffects)

# ACDE of the plow on female participation, with income as mediator
form_main <- women_politics ~ plow + rugged | oil_pc | centered_ln_inc + centered_ln_inc:plow

direct2 <- sequential_g(formula = form_main, data = ploughs)
#> Error in XZM[, mnames, drop = FALSE]: subscript out of bounds

Created on 2019-01-03 by the reprex package (v0.2.1)

mattblackwell avatar Jan 03 '19 02:01 mattblackwell

^ edited original post to a reprex

kuriwaki avatar Jan 03 '19 09:01 kuriwaki

just documenting a bit more --

This breaks at https://github.com/mattblackwell/DirectEffects/blob/f1ee624b01ecf97ed73a55a2f07de2ce07dae5e5/R/DirectEffects.R#L137

where colnames(XZM) is

[1] "(Intercept)"   "plow"    "rugged"       "oil_pc"              
[5] "centered_ln_inc"      "plow:centered_ln_inc"

and mnames is

[1] "centered_ln_inc"    "centered_ln_inc:plow"

so at least in this case, it's just the ordering of the interaction term that is the problem.

going back further, XZM is made from the entire formula: https://github.com/mattblackwell/DirectEffects/blob/f1ee624b01ecf97ed73a55a2f07de2ce07dae5e5/R/DirectEffects.R#L127-L134 probably model.matrix swaps the ordering as plow:centered_ln_inc because the model frame is built without interactions and there plow shows up first:

> colnames(mf)
[1] "women_politics"  "plow"  "rugged"   "oil_pc"  "centered_ln_inc"

In contrast mnames is built from the only the third part of the formula RHS, which captures the original centered_ln_inc:plow notation https://github.com/mattblackwell/DirectEffects/blob/f1ee624b01ecf97ed73a55a2f07de2ce07dae5e5/R/DirectEffects.R#L124-L125

kuriwaki avatar Jan 09 '19 21:01 kuriwaki

Therefore, predictably, in these alternate specifications of the formula,

library(DirectEffects)
#> Warning: package 'DirectEffects' was built under R version 3.5.2

form_alt1 <- women_politics ~ plow + rugged | oil_pc | centered_ln_inc + plow:centered_ln_inc
sequential_g(form_alt1, data = ploughs)
#> Error in XZM[, mnames, drop = FALSE]: subscript out of bounds

form_alt2 <- women_politics ~ plow + rugged | oil_pc | plow:centered_ln_inc  + centered_ln_inc
sequential_g(form_alt2, data = ploughs)
#> 
#> Call:
#> sequential_g(formula = form_alt2, data = ploughs)
#> 
#> 
#> Coefficients:
#> (Intercept)        plow      rugged 
#>  13.3242026  -1.3964834  -0.5526104

Created on 2019-01-09 by the reprex package (v0.2.1)

The former of the two still does not work because now it is mnames that reorders plow:centered_ln_inc to centered_ln_inc:plow internally, which does not match with the colnames(XZM). However the latter does work, because plow comes up first before centered_ln_inc, just like it does in the entire formula.

A quick Google Search of variable orderings within an interaction term doesn't reveal any obvious fixes.

kuriwaki avatar Jan 09 '19 21:01 kuriwaki