How to model multiple events
When dealing with overlapping data, it is often necessary to model multiple eventtypes (e.g. fixations, stimuli, responses).
Load Example Data
using Unfold
using UnfoldMakie, CairoMakie
using DataFrames
using StatsModels
using MixedModels
include(joinpath(dirname(pathof(Unfold)), "../test/test_utilities.jl")) # to load data
dat, evts = loadtestdata("test_case_4b");
evts[1:5,:]5×3 DataFrame
| Row | latency | type | intercept |
|---|---|---|---|
| Int64 | String7 | Int64 | |
| 1 | 38 | eventA | 1 |
| 2 | 50 | eventB | 1 |
| 3 | 89 | eventA | 1 |
| 4 | 102 | eventB | 1 |
| 5 | 144 | eventA | 1 |
The type column of table evts contains two conditions: eventAandeventB(if your eventstypes are specified in a different column, you need to define the keywordargumenteventcolumnin thefit` command below)
Specify formulas and basisfunctions
bf1 = firbasis(τ = (-0.4, 0.8), sfreq = 50)
bf2 = firbasis(τ = (-0.2, 1.2), sfreq = 50)For each event, a basis function and formula must be specified. The same basis and formulas may be used.
f = @formula 0 ~ 1FormulaTerm
Response:
0
Predictors:
1For each event, we must specify the formula and basis function to be used.
bfDict = [ "eventA" => (f, bf1),
"eventB" => (f, bf2) ]Finally, fitting & plotting works the same way as always
m = Unfold.fit(
UnfoldModel,
bfDict,
evts,
dat,
solver = (x, y) -> Unfold.solver_default(x, y; stderror = true),
eventcolumn = "type",
)
results = coeftable(m)
plot_erp(results; stderror = true, mapping = (; col = :eventname))