Skip to content

Instantly share code, notes, and snippets.

@explodecomputer
Created November 16, 2017 17:19
Show Gist options
  • Save explodecomputer/744fbabc8b111add212a879c97af9d47 to your computer and use it in GitHub Desktop.
Save explodecomputer/744fbabc8b111add212a879c97af9d47 to your computer and use it in GitHub Desktop.
linear model to logistic
n <- 1000
nsim <- 100
res1 <- rep(0, nsim)
res2 <- rep(0, nsim)
res3 <- rep(0, nsim)
for(i in 1:nsim)
{
x <- rbinom(n, 2, 0.5)
y <- 0.2 * x + rnorm(n)
cc <- as.numeric(y > quantile(y, runif(1, 0.5, 0.99)))
prev <- sum(cc) / n
adj <- prev * (1 - prev)
mod1 <- coefficients(glm(cc ~ x, family="binomial"))
mod2 <- coefficients(lm(cc ~ x))
res1[i] <- mod1[2]
res2[i] <- mod2[2] / adj
res3[i] <- prev
}
library(ggplot2)
plot(res1 ~ res2)
a <- rnorm(10000)
b <- rnorm(10000)
png("pred.png", width=1500*0.7, height=500*0.7)
par(mfrow=c(1,3))
c <- b * sqrt(0.01) + a * sqrt(0.99)
plot(b ~ c, xlab="Genetic prediction", ylab="True phenotype", main="Prediction accuracy rsq = 0.01")
c <- b * sqrt(0.3) + a * sqrt(0.7)
plot(b ~ c, xlab="Genetic prediction", ylab="True phenotype", main="Prediction accuracy rsq = 0.3")
c <- b * sqrt(0.8) + a * sqrt(0.2)
plot(b ~ c, xlab="Genetic prediction", ylab="True phenotype", main="Prediction accuracy rsq = 0.8")
dev.off()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment