if (FALSE) {
# ====================================
# = Mendelian Randomization analysis =
# ====================================
library(umx)
df = umx_make_MR_data(10e4)
m1 = umxMR(Y ~ X, instruments = ~ qtl, data = df)
parameters(m1)
plot(m1, means = FALSE, min="") # help DiagrammR layout the plot.
m2 = umxModify(m1, "qtl_to_X", comparison=TRUE, tryHard="yes", name="QTL_affects_X") # yip
m3 = umxModify(m1, "X_to_Y" , comparison=TRUE, tryHard="yes", name="X_affects_Y") # nope
plot(m3, means = FALSE)
# Errant analysis using ordinary least squares regression (WARNING this result is CONFOUNDED!!)
m1 = lm(Y ~ X , data = df); coef(m1) # incorrect .35 effect of X on Y
m1 = lm(Y ~ X + U, data = df); coef(m1) # Controlling U reveals the true 0.1 beta weight
df = umx_make_MR_data(10e4)
m1 = umxMR(Y ~ X, instruments = ~ qtl, data = df)
coef(m1)
# ======================
# = Now with sem::tsls =
# ======================
# library(sem) # may require you to install X11
m2 = sem::tsls(formula = Y ~ X, instruments = ~ qtl, data = df)
coef(m2)
# Try with missing value for one subject: A benefit of the FIML approach in OpenMx.
m3 = tsls(formula = Y ~ X, instruments = ~ qtl, data = (df[1, "qtl"] = NA))
}
Run the code above in your browser using DataLab