Skip to content

Instantly share code, notes, and snippets.

## dumbed-down simulation of selection bias with noise (aka selecting on fielding of scale before fielding)
# set seed for reproducibility
set.seed(123)
# parameters
n_studies <- 100000
noise_dist <- seq(0.01, 0.07, 0.01)
## draw distribution of true alpha values
@rubenarslan
rubenarslan / reliability_multilevel.R
Created October 1, 2023 16:07
estimating reliability from varying intercepts
library(tidyverse)
# simulate unobserved trait and items with error
N <- 100
dat <- tibble(
id = 1:N,
trait = rnorm(N),
item1 = trait + rnorm(N),
item2 = trait + rnorm(N),
item3 = trait + rnorm(N),
item4 = trait + rnorm(N),
@rubenarslan
rubenarslan / estimand_sex_diff.R
Created August 16, 2023 09:33
misspecified estimand explains result
library(dplyr)
N = 10000
people <- tibble(
male = sample(0:1, N, TRUE),
trait = rnorm(N) + male)
true_means <- people %>%
group_by(male) %>%
summarise(sex_mean = mean(trait))
people <- people %>% left_join(true_means) %>%
left_join(true_means %>% rename(opposite_sex_mean = sex_mean) %>% mutate(male = 1-male)) %>%
@rubenarslan
rubenarslan / evb_strength.R
Created June 19, 2023 17:26
EVB strength DGM
N <- 5000
sim <- tibble(
group = rep(0:1, each = N/2),
evb_strength = 0.1 + group * 0.9,
# four orthogonal traits
trait1 = rnorm(N),
trait2 = rnorm(N),
trait3 = rnorm(N),
trait4 = rnorm(N),
# evaluative bias
@rubenarslan
rubenarslan / gpt_cov.md
Last active February 10, 2023 17:02
gpt covariance matrix
options(width=200)
library(tidyverse)
gpt <- rio::import("~/Downloads/tmp3.csv")

items <- tibble(var=c(str_c("ipipc", 1:10),  str_c("grit", 1:10)), 
                label = names(gpt)[-1])
knitr::kable(items)
n <- 1000
people <- tibble(
children = rpois(n, 1.4),
# for childless people, sat with childcare is not applicable
sat_with_child_care = if_else(children > 0, rnorm(n), NA_real_),
sat_with_housing = rnorm(n),
happiness = rnorm(n) + sat_with_housing +
if_else(children > 0, sat_with_child_care, 0),
)
mean_se_cluster <- function (x, mult = 1, cluster = NULL)
{
x_na <- is.na(x)
x <- x[!x_na]
cluster <- cluster[!x_na]
stopifnot(!is.null(cluster))
mod <- lme4::lmer(x ~ 1 + (1 | cluster))
intercept <- broom.mixed::tidy(mod, effects = "fixed")
se <- mult * intercept$std.error
mean <- intercept$estimate
@rubenarslan
rubenarslan / paper_rejection_cycle.R
Created June 16, 2020 19:44
paper rejection cycle
library(tidyverse)
n_papers <- 1000
papers <- tibble(
paper = 1:n_papers,
quality = rnorm(n_papers),
reviews = 0,
published = FALSE,
journal = NA_real_,
most_recent_assessment = NA_real_
@rubenarslan
rubenarslan / me_brms.R
Created October 13, 2017 13:25
trying different things using errors-in-variables in brms
library(brms)
library(dplyr)
n = 1000
fakedata = data_frame(
# structure
iv = rnorm(n),
dv = 0.5 * iv + 0.5 * rnorm(n),
# measurement
iv_m = iv + 1 * rnorm(n),
dv_m = dv + 1 * rnorm(n)
# Manual page
library(curl)
?multi
# Example of 100 requests over 10 connections
real = vector("list", 10)
for(i in 1:10){
h <- new_handle(url = "https://httpbin.org/get")
multi_add(h, complete = function(res){