beacon-biosignals/Effects.jl

Support for interactions without main effects?

Closed this issue · 2 comments

Hello,

I noticed yesterday that I receive an error when I try to use interaction terms without including them as main effects.

Specifically, it happens when I want to include a variable only as an interaction. Initially I figured that this had something to do with standardization, but it is true in general.

using StableRNGs, DataFrames, Effects, StandardizedPredictors, GLM, StatsModels
rng = StableRNG(1);

data = DataFrame(age=[13:20; 13:20], 
                        sex=repeat(["male", "female"], inner=8),
                        weight=[range(100, 155; length=8); range(100, 125; length=8)] .+ randn(rng, 16))

m = lm(@formula(weight ~ 1 + sex & age), data, contrasts=Dict(:age => Center()))

design = Dict(:sex => unique(data.sex))
eff = effects(design, m)

I receive the following:

ERROR: ArgumentError: Can't determine columns corresponding to 'age(centered: 16.5)' in matrix term 1 + sex & age(centered: 16.5)

Thank you again for this package.

palday commented

I think I was aware of this previously and had decided that it was an acceptable shortcoming because the correct use of interactions without associated main effects is rather niche/rare. I probably won't have time to figure out how to address this in the near future, but there is a workaround that you can use now: specify the typical value for age in your design:

julia> using Statistics

julia> design = Dict(:sex => unique(data.sex), :age => mean(data.age))
Dict{Symbol, Any} with 2 entries:
  :sex => ["male", "female"]
  :age => 16.5

julia> eff = effects(design, m)
2×6 DataFrame
 Row │ sex     age      weight   err      lower    upper   
     │ String  Float64  Float64  Float64  Float64  Float64 
─────┼─────────────────────────────────────────────────────
   1 │ male       16.5  127.902  1.56607  126.336  129.468
   2 │ female     16.5  111.615  1.56607  110.049  113.181

Thanks for the help, again. This works very well for me.