I am trying to estimate a model for an event modelled by probability of happening which is a linear function of x (distributed normally) plus an error term, u.
Then I simulate whether the event really happened for each X comparing the probability of it happening against a uniformly distributed random variable.
So, I wrote a little function that simulates this model for a given b0, b1, X (mean and sd.) and error term (mean = 0 and sd.):
SAMPLE_SIZE = 10000
underlying <- function(b0, b1, mean_x, sd_x, sd_u) {
xs <- rnorm(SAMPLE_SIZE, mean_x, sd_x)
us <- rnorm(SAMPLE_SIZE, 0.0, sd_u)
ys <- b0 + (b1 * xs) + us
ws <- runif(SAMPLE_SIZE) < ys
list(ws = ws, ys = ys, xs = xs, us = us)
}
It neatly returns both the probability of the event taking place in the ys component plus a simulation on the ws component.
I then tested whether I can correctly estimate b0 and b1 using linear regressions. And I got a very weird result.
This is how I simulated the samples and did the regressions:
b1s <- seq(from = 0.0, to = 1.0, length.out = 100)
datasets <- lapply(b1s, FUN = function(x) underlying(0.5, x, 1.0, 0.2, 0.05))
regs <- lapply(datasets, FUN = function(x) lm(data = x, ws ~ xs))
b0s_hat = sapply(regs, function(x) x$coefficients[[1]])
b1s_hat = sapply(regs, function(x) x$coefficients[[2]])
So, for different b1s (and b0 = 0.5) I can plot the estimated b0 and b1 against the real b1:
plot(b1s, b0s_hat)
plot(b1s, b1s_hat)
And what we get for b1s_hat looks sigmoid-ish like a cumulative distribution function, and b0s_hat looks like a bell curve (like the density function).
I thought I could recover the coefficients using the linear regression. What exactly is smelling weird here?