Skip to content

Instantly share code, notes, and snippets.

View strengejacke's full-sized avatar
⁉️
Focusing

Daniel strengejacke

⁉️
Focusing
View GitHub Profile
library(tidyverse)
library(parameters)
library(lme4)
library(see)
# data generation -----------------------
set.seed(123)
n <- 5
b <- seq(1, 1.5, length.out = 5)
@strengejacke
strengejacke / table-performance.R
Created May 22, 2020 10:29
performance comparison of table functions in R
See https://twitter.com/malte_grosser/status/1262862749794795520?s=20
``` r
table2 <- function(x) {
x_u <- if (is.factor(x)) sort(x[!duplicated(x)]) else sort(unique(x))
x_f <- factor(x, levels = as.character((x_u)), exclude = NULL)
t_x_f <- tabulate(x_f)
names(t_x_f) <- as.character(x_u)
t_x_f
}
# Second Generation P-value
p_delta <- function(lb, ub, delta_lb, delta_ub) {
# special case: infinite CI and H0 bounds in the same direction
if ((delta_lb == -Inf & lb == -Inf) | (delta_ub == Inf & ub == Inf)) {
return(1)
}
# usual case: non-point CI & non-point Ho
@strengejacke
strengejacke / gist:adc5e0494d8aa687f5921409a408285c
Created August 6, 2020 07:38
Explained variance for models with and without intercept
library(parameters)
library(performance)
library(ggplot2)
set.seed(123)
x <- 3 + rnorm(200)
y <- 4 + .5 * x + rnorm(100)
dat <- data.frame(x, y)
ggplot(dat, aes(x, y)) +
@strengejacke
strengejacke / ggpredict_glmmTMB.R
Created December 23, 2020 21:41
ggpredict and glmmTMB
library(glmmTMB)
library(ggeffects)
data("Salamanders")
m1 <- glmmTMB(
count ~ mined + (1 | site),
zi = ~ mined,
family = poisson,
data = Salamanders
)
@strengejacke
strengejacke / ordinal predictors.R
Last active September 26, 2022 09:45
Example showing different ways of modelling ordinal predictors, and how their model summaries and predictions look like.
library(ggeffects)
library(parameters)
library(brms)
income_options <- c("below_20", "20_to_40", "40_to_100", "greater_100")
income <- factor(sample(income_options, 100, TRUE),
levels = income_options, ordered = TRUE)
mean_ls <- c(30, 60, 70, 75)
ls <- mean_ls[income] + rnorm(100, sd = 7)
dat <- data.frame(income, ls)
library(easystats)
#> # Attaching packages: easystats 0.4.3
#> ✔ insight 0.17.0.6 ✔ datawizard 0.4.0.14
#> ✔ bayestestR 0.12.1 ✔ performance 0.9.0.3
#> ✔ parameters 0.17.0.9 ✔ effectsize 0.6.0.7
#> ✔ modelbased 0.8.0 ✔ correlation 0.8.0.1
#> ✔ see 0.7.0.2 ✔ report 0.5.1.1
library(emmeans)
library(marginaleffects)
@strengejacke
strengejacke / predictions.R
Last active May 21, 2022 09:01
Approach of different packages to generate predicted values
library(insight)
library(datawizard)
library(modelbased)
library(ggeffects)
library(emmeans)
library(marginaleffects)
data(efc, package = "ggeffects")
efc <- data_to_factor(efc, select = "c161sex")
model <- lm(neg_c_7 ~ barthtot + c160age * c161sex, data = efc)
@strengejacke
strengejacke / keybindings.json
Last active August 6, 2022 16:53
keybindings for VS Code using R
[
{
"description": "R Build Docs",
"key": "ctrl+shift+d",
"command": "r.document",
"when": "resourceLangId == 'r'"
},
{
"description": "R Pipe Operator",
"key": "ctrl+shift+m",
@strengejacke
strengejacke / .lintr
Last active May 13, 2024 01:24
VS Code setup for R
// save to windows-user directory
linters: with_defaults(object_name_linter = NULL,
object_length_linter(50),
commented_code_linter = NULL,
object_usage_linter = NULL,
line_length_linter(120),
cyclocomp_linter = cyclocomp_linter(50))