Skip to content

Instantly share code, notes, and snippets.

View thlytras's full-sized avatar

Theodore Lytras thlytras

  • European University Cyprus, School of Medicine
  • Athens, GR and Nicosia, CY
  • X @TheodoreLytras
View GitHub Profile
@thlytras
thlytras / mwe-collider.R
Created April 5, 2023 08:40
Minimal example of collider bias, using R
set.seed(42)
dat <- data.frame(
A = rbinom(500, 1, 0.5),
B = rbinom(500, 1, 0.5)
)
dat$C = rbinom(500, 1, 0.1 + 0.3*dat$A + 0.4*dat$B)
tb <- with(dat, table(A,B))
tb0 <- with(subset(dat, C==0), table(A,B))
@thlytras
thlytras / autocorr-zscores-EuroMOMO.R
Created April 30, 2022 20:20
Show EuroMOMO z-scores are autocorrelated
# Data from: https://www.euromomo.eu/graphs-and-maps/#z-scores-by-country
dat <- read.csv2("charts-z-scores-by-country.csv")
dat$zscore <- as.numeric(dat$zscore)
png("autocorr-EuroMOMO.png", width=3000, height=1500, pointsize=15)
par(mfcol=c(7,4), family="Fira Sans", mar=c(4,4,3,2))
lapply(unique(dat$country), function(x) {
acf(subset(dat, country==x)$zscore, main=x, na.action=na.omit, xlab=NA, bty="l")
mtext("Lag (weeks)", side=1, line=1.5, cex=0.8, adj=1)
@thlytras
thlytras / EuroMOMO-average-zscore.R
Created April 30, 2022 19:50
Show that EuroMOMO average z-scores are not zero
# Data from: https://www.euromomo.eu/graphs-and-maps/#z-scores-by-country
dat <- read.csv2("charts-z-scores-by-country.csv")
dat$y <- as.integer(substr(dat$week, 1,4))
dat$zscore <- as.numeric(dat$zscore)
a <- aggregate(dat[,"zscore",drop=F], dat[,c("country","y")], mean, na.rm=TRUE)
b <- aggregate(dat[,"zscore",drop=F], dat[,"country",drop=F], mean, na.rm=TRUE)
b <- b[nrow(b):1,]
a$x <- match(a$country, b$country)