Skip to content

Instantly share code, notes, and snippets.

View strengejacke's full-sized avatar
⁉️
Focusing

Daniel strengejacke

⁉️
Focusing
View GitHub Profile
library(easystats)
library(palmerpenguins)
library(lme4)
library(ggplot2)
# data generation -----------------------
data(penguins)
d <- penguins |>
@strengejacke
strengejacke / intersectional.R
Created June 9, 2023 06:13
Example for building intersectional strata
easystats::install_latest(force= TRUE)
library(datawizard) # data wrangling and preparation
library(parameters) # model summaries
library(glmmTMB) # multilevel modelling
# sample data set
data(efc, package = "ggeffects")
efc <- efc |>
# numeric to factors, set labels as levels
@strengejacke
strengejacke / .lintr
Last active April 25, 2024 07:56
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))
@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 / 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)
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 / 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)
@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 / 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)) +
# 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