-
Notifications
You must be signed in to change notification settings - Fork 7
/
erp.jl
135 lines (103 loc) · 3.98 KB
/
erp.jl
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
# # [ERP Plot](@id erp_vis)
# ERP plot is plot type for visualisation of [Event-related potentials](https://www.ncbi.nlm.nih.gov/pmc/articles/PMC3016705/).
# It can fully represent time and experimental condition dimensions using lines.
# # Setup
# ## Package loading
using Unfold
using UnfoldMakie
using DataFrames
using CairoMakie
using DataFramesMeta
using UnfoldSim
using UnfoldMakie
include("../../../example_data.jl")
# ## Data generation
# Let's generate some data. We'll fit a model with a 2 level categorical predictor and a continuous predictor with interaction.
data, evts = UnfoldSim.predef_eeg(; noiselevel = 12, return_epoched = true)
data = reshape(data, (1, size(data)...))
f = @formula 0 ~ 1 + condition + continuous
se_solver = (x, y) -> Unfold.solver_default(x, y, stderror = true);
m = fit(
UnfoldModel,
Dict(Any => (f, range(0, step = 1 / 100, length = size(data, 2)))),
evts,
data,
solver = se_solver,
)
results = coeftable(m)
res_effects = effects(Dict(:continuous => -5:0.5:5), m);
# ## Figure plotting
plot_erp(results)
# # Additional features
# ## Effect plot
# Effect plot shows how ERP voltage is affected by variation of some variable (here: `:contionous`).
# - `categorical_color::Bool = true`
# Treat `:color` as continuous or categorical variable in case of numeric `:color` column.
# - `categorical_group::Bool = true`
# Treat `:group` as categorical variable by default in case of numeric `:group` column.
plot_erp(
res_effects;
mapping = (; y = :yhat, color = :continuous, group = :continuous),
legend = (; nbanks = 2),
layout = (; show_legend = true, legend_position = :right),
categorical_color = false, # perceives color (here: continuous) as contionus
categorical_group = true, # separates lines, if `false` all lines will be connected
)
# ## P-value lines
# - `pvalue` (Array, `[]`): show a pvalue (see below).
# Here we manually specify p-value lines. If array is not empty, plot shows colored lines under the plot representing the p-values.
# Below is an example in which p-values are given:
m = example_data("UnfoldLinearModel")
results = coeftable(m)
pvals = DataFrame(
from = [0.1, 0.3],
to = [0.5, 0.7],
coefname = ["(Intercept)", "condition: face"], # if coefname not specified, line should be black
)
plot_erp(results; :pvalue => pvals)
# ## Error ribbons
# - `stderror` (bool, `false`): add an error ribbon, with lower and upper limits based on the `:stderror` column.
# Display a colored band on the graph to indicate lower and higher estimates based on the standard error.
# For the generalizability of your results, it is always better to include error bands.
f = Figure()
results.coefname =
replace(results.coefname, "condition: face" => "face", "(Intercept)" => "car")
results = filter(row -> row.coefname != "continuous", results)
plot_erp!(
f[1, 1],
results;
axis = (title = "Bad example", titlegap = 12),
:stderror => false,
mapping = (; color = :coefname => "Conditions"),
)
plot_erp!(
f[2, 1],
results;
axis = (title = "Good example", titlegap = 12),
:stderror => true,
mapping = (; color = :coefname => "Conditions"),
)
ax = Axis(f[2, 1], width = Relative(1), height = Relative(1))
xlims!(ax, [-0.04, 1])
ylims!(ax, [-0.04, 1])
hidespines!(ax)
hidedecorations!(ax)
text!(0.98, 0.2, text = "* Confidence\nintervals", align = (:right, :top))
f
# There are two ways to implement it.
# First is using `:stderror = true' after `;`.
results.se_low = results.estimate .- 0.5
results.se_high = results.estimate .+ 0.15
plot_erp(select(results, Not(:stderror)); stderror = true)
# Second way is to specify manually lower and higher borders of the error bands.
# !!! note
# `:stderror` has precedence over `:se_low`/`:se_high`.
# ## Faceting
# Creation of column facets for each channel.
m7 = example_data("7channels")
results7 = coeftable(m7)
plot_erp(results7, mapping = (; col = :channel, group = :channel))
# # Configurations of ERP plot
# ```@docs
# plot_erp
# ```