Skip to content

Instantly share code, notes, and snippets.

@MichelNivard
Last active September 18, 2023 12:48
Show Gist options
  • Save MichelNivard/58e1e4804af728a3b481738c2dae6dc0 to your computer and use it in GitHub Desktop.
Save MichelNivard/58e1e4804af728a3b481738c2dae6dc0 to your computer and use it in GitHub Desktop.
## The problem with (personal) non-ordinality:
n <- 30000 # 10k fictional people
# a pair of exposures, no measurmennt issues:
xa <- rnorm(n)
xb <- rnorm(n)
# Personal threshold 1 - 4 for each person,
# reasonable scale design I think by which I mean the bins fill up sort of "normal" like this matters a lot!!
t1 <- runif(n,-2,0)
t2 <- t1 + runif(n,0,1.5)
t3 <- t2 + runif(n,0,2)
t4 <- t3 + runif(n,1,3)
# simulate simple interaction modle with true y:
y <- scale(xa + xb + xa*xb + rnorm(n))
# Make y ordinal with person specific thresholds
yord <- (y > t1) + (y > t2) + (y > t3) + (y > t4)
hist(yord)
# scale
yord_s <- scale(yord)
hist(yord_s)
# rerun the interactions on true y before scaling issue:
summary(lm(y ~ xa + xb + xa:xb))
summary(lm(y^2 ~ xa))
# Run on ordinal data, loose some efficiency...
summary(lm(yord_s ~ xa + xb + xa:xb))
# model the mean, the regress the variance on the predictor, which is the "variance regression model":
res <- lm(yord_s ~ xa )$residuals
summary(lm(res^2 ~ xa))
### now see about false positives, here there is no interaction at all... :
y <- scale(xa + xb + rnorm(n))
# make y ord again
yord <- (y > t1) +(y > t2) + (y > t3) + (y > t4)
# sclae again
yord_s <- scale(yord)
hist(yord_s)
# the analyses with y, no interaction
summary(lm(y ~ xa + xb + xa:xb))
# variance regression with y no evidence for interaction:
res <- lm(y ~ xa)$residuals
summary(lm(res^2 ~ xa))
# get some false positives if you use the ordinal scale:
summary(lm(yord_s ~ xa + xb + xa:xb))
# model the mean, the regress the variance on the predictor, also false positives...
res <- lm(yord_s ~ xa)$residuals
summary(lm(res^2 ~ xa)) # potenially worse false positives?
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment