-
Notifications
You must be signed in to change notification settings - Fork 1
/
README.Rmd
195 lines (151 loc) · 6.64 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
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
---
output: github_document
---
<!-- README.md is generated from README.Rmd. Please edit that file -->
```{r setup, include = FALSE}
knitr::opts_chunk$set(
echo = TRUE,
warning = FALSE,
message = FALSE,
collapse = TRUE,
comment = "#>",
fig.path = "man/figures/README-",
out.width = "100%"
)
library(faintr)
library(brms)
library(posterior)
library(dplyr)
library(tidyr)
library(ggplot2)
library(aida)
theme_set(theme_bw() + theme(plot.background = element_blank()))
custom_palette <- c("#009E73", "#B22222", "#0072B2", "#D55E00")
scale_colour_discrete <- function(...) {
scale_colour_manual(..., values = custom_palette)
}
scale_fill_discrete <- function(...) {
scale_fill_manual(..., values = custom_palette)
}
```
# faintr <img align="right" src="man/figures/faintr-logo.png" alt="logo" width=160>
<!-- badges: start -->
[![R-CMD-check](https://github.com/michael-franke/faintr/workflows/R-CMD-check/badge.svg)](https://github.com/michael-franke/faintr/actions)
[![Codecov test coverage](https://codecov.io/gh/michael-franke/faintr/branch/main/graph/badge.svg)](https://app.codecov.io/gh/michael-franke/faintr?branch=main)
<!-- badges: end -->
## Overview
The **faintr** (FActorINTerpreteR) package provides convenience functions for
interpreting [**brms**](https://paul-buerkner.github.io/brms/) model fits for data
from factorial designs. It allows for the extraction and comparison of posterior
draws for a given design cell, irrespective of the encoding scheme used in the model.
Currently, **faintr** provides the following functions:
* `get_cell_definitions` returns information on the predictor variables and how
they are encoded in the model.
* `extract_cell_draws` returns posterior draws and additional metadata
for all design cells.
* `filter_cell_draws` returns posterior draws and additional metadata
for one subset of design cells.
* `compare_groups` returns summary statistics of comparing two subsets of design cells.
## Installation
You can install the development version from GitHub with:
``` r
# install.packages("devtools")
devtools::install_github("michael-franke/faintr")
```
## Examples
In this section, we shortly introduce how to use the package. For a more detailed
overview, please refer to the [vignette](https://michael-franke.github.io/faintr/articles/faintr_basics.html).
We will use a preprocessed version of the mouse-tracking data set from the [**aida**](https://github.com/michael-franke/aida-package) package:
```{r data-import, echo=FALSE}
data <- aida::data_MT
data <- data %>%
mutate(
prototype_label = case_when(
prototype_label %in% c('curved', 'straight') ~ prototype_label,
TRUE ~ 'CoM'
),
prototype_label = factor(prototype_label,
levels = c('straight', 'curved', 'CoM')))
```
```{r data}
data %>%
select(RT, group, condition, prototype_label) %>%
head()
```
The variables relevant for us are:
* `RT`: Reaction time in milliseconds
* `group`: Whether a category is selected by click vs touch
* `condition`: Whether the animal is a typical vs atypical representative of its category
* `prototype_label`: The type of prototypical movement strategy (straight vs curved vs CoM)
Below, we regress the log-transformed reaction times as a function of factors
`group`, `condition`, `prototype_label`, and their three-way interaction using a
linear regression model fitted with [**brms**](https://paul-buerkner.github.io/brms/):
```{r model-fitting, results='hide'}
fit <- brms::brm(formula = log(RT) ~ group * condition * prototype_label,
data = data,
seed = 123
)
```
To obtain information on the factors and the coding scheme used in the model,
we can use `get_cell_definitions`:
```{r cell-defs}
get_cell_definitions(fit)
```
The output shows that factors `group`, `condition` and `prototype_label` are
dummy-coded, with `click`, `Atypical`, and `straight` being the reference levels, respectively.
To extract posterior draws for all design cells, we can use `extract_cell_draws`:
```{r extract-cell-draws}
extract_cell_draws(fit)
```
With `filter_cell_draws` we can obtain posterior draws for a specific design cell.
For instance, draws for typical exemplars in click trials, averaged over factor `prototype_label`,
can be extracted like so:
```{r filter-cell-draws}
filter_cell_draws(fit, condition == "Typical" & group == "click")
```
Parameter `colname` allows changing the default column name in the output, which
facilitates post-processing of cell draws, e.g., for plotting or summary statistics.
Here, we extract the draws for each level of `prototype_label` (averaged over `group`
and `condition`) and visualize the results:
```{r plot, out.width="70%"}
draws_straight <- filter_cell_draws(fit, prototype_label == "straight", colname = "straight")
draws_curved <- filter_cell_draws(fit, prototype_label == "curved", colname = "curved")
draws_CoM <- filter_cell_draws(fit, prototype_label == "CoM", colname = "CoM")
draws_prototype <- posterior::bind_draws(draws_straight, draws_curved, draws_CoM) %>%
pivot_longer(cols = posterior::variables(.), names_to = "prototype", values_to = "value")
draws_prototype %>%
ggplot(aes(x = value, color = prototype, fill = prototype)) +
geom_density(alpha = 0.4)
```
Finally, we can compare two subsets of design cells with `compare_groups`. Here,
we compare the estimates for atypical exemplars in click trials against typical
exemplars in click trials (averaged over the three prototypical movement strategies):
```{r group-comp}
compare_groups(fit,
higher = condition == "Atypical" & group == "click",
lower = condition == "Typical" & group == "click"
)
```
If one of two group specifications is left out, we compare against the grand mean:
```{r group-comp-grand-mean}
compare_groups(fit,
higher = group == "click"
)
```
If the Boolean flag `include_bf` is set to `TRUE` (default is `FALSE`), Bayes Factors
for the inequality (higher > lower) are approximated in comparison to the "negated hypothesis"
(lower <= higher). However, this requires specifying proper priors for all parameters:
```{r model-fitting-with-priors, results='hide'}
fit_with_priors <- brms::brm(formula = log(RT) ~ group * condition * prototype_label,
prior = prior(student_t(1, 0, 3), class = "b"),
data = data,
seed = 123
)
```
```{r group-comp-with-bf}
compare_groups(fit_with_priors,
higher = prototype_label != "straight",
lower = prototype_label == "straight",
include_bf = TRUE
)
```