Contrast Coding
using CairoMakie
using Unfold
using UnfoldMakie
using UnfoldSim
# Contrast coding
Unfold.jl uses the StatsModels
package for the formula interface. This allows for a wide range of contrast coding schemes. For a full tutorial, please see the StatsModels docs. Please read their tutorial, as a motivation of why one would change the contrast coding scheme is outside of the realms of this package and more a basic linear regression question.
Given we have a nice effects
implementation (mimicking emmeans
and similar packages), coding schema is typically less important.
Here we will show a simple example of how to change the contrast coding scheme. We will use the condition
variable, which has two levels, A
and B
. We will change the contrast coding from Dummy
aka Reference
aka 0/1 coding to Sum
coding, which is the default in R.
eeg, evts = UnfoldSim.predef_eeg(noiselevel = 0)
f = @formula 0 ~ 1 + condition
basis = firbasis((-0.1, 0.6), 100)
m_dummy = fit(UnfoldModel, f, evts, eeg, basis)
m_effec =
fit(UnfoldModel, f, evts, eeg, basis; contrasts = Dict(:condition => EffectsCoding()))
Unfold�[38;2;239;83;80m-�[39mType: �[38;2;206;147;216m::UnfoldLinearModelContinuousTime�[39m{{Float64}}
�[1m Any�[22m �[38;2;239;83;80m=�[39m�[38;2;239;83;80m>�[39m �[38;2;239;83;80mAny�[39m: timeexpand(�[38;2;144;202;249m1�[39m �[38;2;239;83;80m+�[39m condition) for times �[38;2;239;83;80m[�[39m�[38;2;239;83;80m-�[39m�[38;2;144;202;249m0.1�[39m, �[38;2;239;83;80m-�[39m�[38;2;144;202;249m0.09�[39m ... �[38;2;144;202;249m0.6�[39m�[38;2;239;83;80m]�[39m
�[1m�[32m✔�[22m�[39m model is fit. size(coefs) (�[38;2;144;202;249m1�[39m, �[38;2;144;202;249m142�[39m)
Useful functions: �[38;2;255;238;88m`design(uf)`�[39m, �[38;2;255;238;88m`designmatrix(uf)`�[39m, �[38;2;255;238;88m`coef(uf)`�[39m, �[38;2;255;238;88m`coeftable(uf)`�[39mwe could directly inspect the designmatrix
modelmatrix(m_dummy, false)[1][1:5, :]
5×2 Matrix{Float64}:
1.0 0.0
1.0 1.0
1.0 0.0
1.0 0.0
1.0 0.0
and the effects coding
modelmatrix(m_effec, false)[1][1:5, :]
5×2 Matrix{Float64}:
1.0 -1.0
1.0 1.0
1.0 -1.0
1.0 -1.0
1.0 -1.0
To confirm the difference in the actual fit, let's visualize them
c_d = coeftable(m_dummy)
c_e = coeftable(m_effec)
c_d.group .= "Dummy Coding"
c_e.group .= "Effects Coding"
c = vcat(c_d, c_e)
plot_erp(c; mapping = (; color = :coefname, col = :group))

As expected, the effects-coding slope of condition: face
is half the size of the dummy-coding one (because -1/1 coding was used).
This page was generated using Literate.jl.