-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathREADME.Rmd
158 lines (122 loc) · 5.32 KB
/
README.Rmd
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
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
---
output: github_document
editor_options:
chunk_output_type: console
---
```{r, include = FALSE}
knitr::opts_chunk$set(
collapse = TRUE,
cache = TRUE,
comment = "#>",
fig.path = "man/figures/README-",
out.width = "75%",
dpi = 300,
message = FALSE,
warning = FALSE
)
```
# Models of Motor Inhibition during Motor Imagery
[![GitHub repo size](https://img.shields.io/github/repo-size/lnalborczyk/momimi?color=brightgreen&logo=github)](https://github.com/lnalborczyk/momimi)
[![GitHub last update](https://img.shields.io/github/last-commit/lnalborczyk/momimi?color=brightgreen&logo=github)](https://github.com/lnalborczyk/momimi)
The `momimi` package implements the "threshold modulation model" (TMM) of motor imagery and provides fitting and plotting utilities in `R`.
## Installation
You can install the latest stable version of the package from GitHub with:
```{r installing, eval = FALSE}
# if needed, install.packages("devtools")
devtools::install_github(repo = "lnalborczyk/momimi", build_vignettes = TRUE)
```
## Usage
### Simulating and plotting data
We start by simulating some data (here, 100 observations or RTs and MTs) given some values for the model's parameters.
```{r simulating1}
library(tidyverse)
library(momimi)
simulated_data <- model(
nsims = 100, nsamples = 2000,
exec_threshold = 1.1, imag_threshold = 0.55,
peak_time = log(0.5), curvature = 0.5,
full_output = TRUE
)
```
We can plot the underlying activation function and the simulated distributions of RTs and MTs.
```{r plotting, warning = FALSE}
# plotting the latent activation function
plot(x = simulated_data, method = "functions")
# plotting the distributions of RTs/MTs
plot(x = simulated_data, method = "distributions")
```
### Fitting the models
We can also use the model to generate realistic data from known parameter values and then fit the model to these data to try recovering the original parameter values.
```{r simulating2}
# defining plausible "true" parameter values:
# the motor execution threshold, peak time, curvature, and between-trial variability (SD)
true_pars <- c(1.1, 0.5, 0.4, 0.1)
# simulating data using these parameter values
simulated_data <- simulating(
nsims = 200,
nsamples = 3000,
true_pars = true_pars,
action_mode = "imagined"
)
# displaying the first ten rows of these data
head(x = simulated_data, n = 10)
```
We fit the model and use realistic constraints (e.g., the RT/MT should be no less than 0.1s and no more than 2 seconds) on the initial parameter values (by setting `initial_pop_constraints = TRUE`) to facilitate convergence (fitting can take a while).
```{r fitting, results = "hide", warning = FALSE}
# fitting the model
results <- fitting(
# the data used to fit the model
data = simulated_data,
# the number of simulated trials in the model
nsims = 200,
# the g2 cost function is also used with the DDM
error_function = "g2",
# DEoptim seems the most efficient approach
method = "DEoptim",
# lower and upper bounds for the parameters values
# NB: one can fix (i.e., not estimate) a parameter
# by setting the lower and upper bounds to the same value
lower_bounds = c(1.0, 0.3, 0.4, 0.05),
upper_bounds = c(1.4, 0.7, 0.4, 0.20),
# should we generate sensible starting values?
initial_pop_constraints = TRUE,
nstudies = 200,
# which constraints?
rt_contraints = range(simulated_data$reaction_time),
mt_contraints = range(simulated_data$movement_time),
# maximum number of iteration when fitting the model (to be increased)
maxit = 10
)
```
```{r fitting-summary}
# fitting summary
summary(results)
```
We can then plot the underlying (latent) activation function. Note that this returns a `ggplot2` object that can be subsequently modified.
```{r latent}
plot(x = results, method = "latent") +
labs(title = "Estimated latent activation function")
```
We can also do "predictive checks" by comparing the data used to fit the model to data simulated from the model using the estimated parameter values.
```{r ppc}
plot(
x = results, original_data = simulated_data,
method = "ppc", action_mode = "imagined"
)
```
We can also do another form of "predictive check" by comparing the quantile values of the original and simulated data.
```{r quantiles, out.width = "100%"}
plot(
x = results, original_data = simulated_data,
method = "quantiles", action_mode = "imagined"
)
```
We can also visualise the trajectory in parameter space during optimisation interactively using `plotly` (not shown below, but you should try this).
```{r optimisation, eval = FALSE}
plot(x = results, method = "optimisation")
```
## References
Nalborczyk, L., Longcamp, M., Gajdos, T., Servant, M. & Alario, F.‐X. (in preparation). Modelling the onset and duration of imagined actions: Assessing a novel algorithmic model of motor imagery.
Nalborczyk, L., Longcamp, M., Gajdos, T., Servant, M. & Alario, F.‐X. (2024). Towards formal models of inhibitory mechanisms involved in motor imagery: A commentary on Bach et al. (2022). Psychological Research, 1‐4. <https://doi.org/10.1007/s00426-023-01915-8>. Preprint available at <https://psyarxiv.com/tz6x2/>.
## Getting help
If you encounter a bug or have a question please file an issue with a minimal reproducible example on [GitHub](https://github.com/lnalborczyk/momimi/issues).