# Articles by R on Pablo Bernabeu

### Preprocessing the Norwegian Web as Corpus (NoWaC) in R

September 27, 2023 |

The present script can be used to pre-process data from a frequency list of the Norwegian as Web Corpus (NoWaC). Before using the script, the frequency list should be downloaded from https://www.hf.uio.no/iln/english/about/organization/text-laborat... [Read more...]

### ggplotting power curves from simr package

June 26, 2023 |

The R package simr has greatly facilitated power analysis for mixed-effects models using Monte Carlo simulation (i.e., hundreds or thousands of tests under slight variations of the data). The powerCurve function is used to estimate the statistical power for various sample sizes in one go. Since it runs serially, ...

### How to break up colour variable in sjPlot into equally-sized bins

June 24, 2023 |

Whereas the direction of main effects can be interpreted from the sign of the estimate, the interpretation of interaction effects often requires plots. This task is facilitated by the R package sjPlot (Lüdecke, 2022). For instance, using the plot_model function, I plotted the interaction between two continuous variables.
```library(lme4)
library(sjPlot)
library(ggplot2)

theme_set(theme_sjplot())

# Create data using code by Ben Bolker from
# https://stackoverflow.com/a/38296264/7050882

set.seed(101)
spin = runif(600, 1, 24)
reg = runif(600, 1, 15)
ID = rep(c("1","2","3","4","5", "6", "7", "8", "9", "10"))
day = rep(1:30, each = 10)
testdata <- data.frame(spin, reg, ID, day)
testdata\$fatigue <- testdata\$spin * testdata\$reg/10 * rnorm(30, mean=3, sd=2)

fit = lmer(fatigue ~ spin * reg + (1|ID),
data = testdata, REML = TRUE)

plot_model(fit, type = 'pred', terms = c('spin', 'reg'))
#> Warning: Ignoring unknown parameters: linewidth```
...

### How to break down colour variable in sjPlot::plot_model into equally-sized bins

June 24, 2023 |

Whereas the direction of main effects can be interpreted from the sign of the estimate, the interpretation of interaction effects often requires plots. This task is facilitated by the R package sjPlot (Lüdecke, 2022). For instance, using the plot_model function, I plotted the interaction between two continuous variables.
```library(lme4)
library(sjPlot)
library(ggplot2)

theme_set(theme_sjplot())

# Create data partially based on code by Ben Bolker
# from https://stackoverflow.com/a/38296264/7050882

set.seed(101)

spin = runif(800, 1, 24)

trait = rep(1:40, each = 20)

ID = rep(1:80, each = 10)

testdata <- data.frame(spin, trait, ID)

testdata\$fatigue <-
testdata\$spin * testdata\$trait /
rnorm(800, mean = 6, sd = 2)

# Model
fit = lmer(fatigue ~ spin * trait + (1|ID),
data = testdata, REML = TRUE)
#> boundary (singular) fit: see help('isSingular')

plot_model(fit, type = 'pred', terms = c('spin', 'trait'))
#> Warning: Ignoring unknown parameters: linewidth```
...

### How to map more informative values onto fill argument of sjPlot::plot_model

June 24, 2023 |

Whereas the direction of main effects can be interpreted from the sign of the estimate, the interpretation of interaction effects often requires plots. This task is facilitated by the R package sjPlot (Lüdecke, 2022). For instance, using the plot_model function, I plotted the interaction between a continuous variable and ...

### How to visually assess the convergence of a mixed-effects model by plotting various optimizers

June 24, 2023 |

To assess whether convergence warnings render the results invalid, or on the contrary, the results can be deemed valid in spite of the warnings, Bates et al. (2023) suggest refitting models affected by convergence warnings with a variety of optimizers. The authors argue that, if the different optimizers produce practically-equivalent results, ...

### Table joins with conditional “fuzzy” string matching in R

June 23, 2023 |

Here’s an example of fuzzy-matching strings in R that I shared on StackOverflow. In stringdist_join, the max_dist argument is used to constrain the degree of fuzziness.
```library(fuzzyjoin)
library(dplyr)
#>
#> Attaching package: 'dplyr'
#> The following objects are masked from 'package:stats':
#>
#>     filter, lag
#> The following objects are masked from 'package:base':
#>
#>     intersect, setdiff, setequal, union
library(knitr)

small_tab = data.frame(Food.Name = c('Corn', 'Squash', 'Peppers'),
Food.Code = c(NA, NA, NA))

large_tab = data.frame(Food.Name = c('Sweet Corn', 'Red Corn', 'Baby Corns',
'Squash', 'Long Squash', 'Red Pepper',
'Green Pepper', 'Red Peppers'),
Food.Code = c(532, 532, 944, 111, 123, 654, 655, 654))

joined_tab = stringdist_join(small_tab, large_tab, by = 'Food.Name',
ignore_case = TRUE, method = 'cosine',
max_dist = 0.5, distance_col = 'dist') %>%

# Tidy columns
select(Food.Name = Food.Name.x, -Food.Name.y,
Food.Code = Food.Code.y, -dist) %>%

# Only keep most frequent food code per food name
group_by(Food.Name) %>% count(Food.Name, Food.Code) %>%
slice(which.max(n)) %>% select(-n) %>%

# Order food names as in the small table
arrange(factor(Food.Name, levels = small_tab\$Food.Name))

# Show table with columns renamed
joined_tab %>%
rename('Food Name' = Food.Name,
'Food Code' = Food.Code) %>%
kable()```
Food Name Food Code Corn 532 Squash 111 Peppers 654 Created on 2023-05-31 with reprex v2.0.2 [Read more...]

### A new function to plot convergence diagnostics from lme4::allFit()

June 22, 2023 |

Linear mixed-effects models (LMM) offer a consistent way of performing regression and analysis of variance tests which allows accounting for non-independence in the data. Over the past decades, LMMs have subsumed most of the General Linear Model, with a steady increase in popularity (Meteyard & Davies, 2020). Since their conception, LMMs have ...

### A table of results for frequentist mixed-effects models: Grouping variables and specifying random slopes

December 28, 2022 |

Here I share the format applied to tables presenting the results of frequentist models in Bernabeu (2022; the table for Bayesian models is covered in this other post). The sample table presents a mixed-effects model that was fitted using the R package lmerTest (Kuznetsova et al., 2022). The mixed effects were driven ... [Read more...]

### A table of results for Bayesian mixed-effects models: Grouping variables and specifying random slopes

December 28, 2022 |

Here I share the format applied to tables presenting the results of Bayesian models in Bernabeu (2022; the table for frequentist models is covered in this other post). The sample table presents a Bayesian mixed-effects model that was fitted using the R package brms (Bürkner et al., 2022). The mixed effects ... [Read more...]

### Plotting two-way interactions from mixed-effects models using ten or six bins

December 25, 2022 |

Whereas the direction of main effects can be interpreted from the sign of the estimate, the interpretation of interaction effects often requires plots. This task is facilitated by the R package sjPlot (Lüdecke, 2022). In Bernabeu (2022), the sjPlot ... [Read more...]

### Plotting two-way interactions from mixed-effects models using alias variables

December 25, 2022 |

Whereas the direction of main effects can be interpreted from the sign of the estimate, the interpretation of interaction effects often requires plots. This task is facilitated by the R package sjPlot (Lüdecke, 2022). In Bernabeu (2022), the sjPlot ...

### Why can’t we be friends? Plotting frequentist (lme4) and Bayesian (brms) mixed-effects models

December 22, 2022 |

Frequentist and Bayesian statistics are sometimes regarded as fundamentally different philosophies. Indeed, can both methods qualify as philosophies, or is one of them just a pointless ritual? Is frequentist statistics about \(p\) values only? Are frequentist estimates diametrically opposed to Bayesian posterior distributions? Are confidence intervals and credible intervals irreconcilable? ...

### Why can’t we be friends? Plotting frequentist (lmerTest) and Bayesian (brms) mixed-effects models

December 22, 2022 |

Frequentist and Bayesian statistics are sometimes regarded as fundamentally different philosophies. Indeed, can both methods qualify as philosophies, or is one of them just a pointless ritual? Is frequentist statistics about \(p\) values only? Are frequentist estimates diametrically opposed to Bayesian posterior distributions? Are confidence intervals and credible intervals irreconcilable? ...

### Bayesian workflow: Prior determination, predictive checks and sensitivity analyses

December 21, 2022 |

This post presents a code-through of a Bayesian workflow in R, which can be reproduced using the materials at https://osf.io/gt5uf. The content is closely based on Bernabeu (2022), which was in turn based on lots of other references. In addition to those, you may wish to consider ...

### Language and vision in conceptual processing: Multilevel analysis and statistical power

October 14, 2022 |

Reference Bernabeu, P., Lynott, D., & Connell, L. (2022). Language and vision in conceptual processing: Multilevel analysis and statistical power. OSF. https://osf.io/dnskh [Read more...]

### Language and sensorimotor simulation in conceptual processing: Multilevel analysis and statistical power

October 13, 2022 |

Reference Bernabeu, P. (2022). Language and sensorimotor simulation in conceptual processing: Multilevel analysis and statistical power. Lancaster University. https://doi.org/10.17635/lancaster/thesis/1795 [Read more...]

### Avoiding knitting errors in (R) Markdown using knit-deleting-service-files()

December 17, 2021 |

The function knit-deleting-service-files() is designed to avoid (R) Markdown knitting errors caused by service files from previous knittings (e.g., manuscript.tex, ZHJhZnQtYXBhLlJtZA==.Rmd, manuscript.synctex.gz). The function first suggests deleti...

### Walking the line between reproducibility and efficiency in R Markdown: Three methods

November 13, 2021 |

As technology and research methods advance, the data sets tend to be larger and the methods more exhaustive. Consequently, the analyses take longer to run. This poses a challenge when the results are to be presented using R Markdown. One has to walk the line between reproducibility and efficiency. On ... [Read more...]

### Tackling knitting errors in R Markdown

November 13, 2021 |

When knitting an R Markdown document after the first time, errors may sometimes appear. Three tips are recommended below. 1. Close PDF reader window When the document is knitted through the ‘Knit’ button, a PDF reader window opens to present the result. Closing this window can help resolve knitting errors. 2. Delete ... [Read more...]
1 2