I am currently trying to figure out the problem of endogenity, which occurs when an explantory variable is correlated with the error term. There are apparently different causes for endogenity, but one of the most frequent one seems to be omitted variable bias.
I created a short R-code in order to understand the dynamics of an omitted variable bias and how it would cause the error term to be correlated with the explanatory variable:
set.seed(111) x1 <- sample(1:20, 100, replace = T) x2 <- 3 * rnorm(n = 100, mean = x1, sd = 10) a <- 2; b = 1.5; c = - 3 y <- a + b * rnorm(100, x1, 5) + c * rnorm(100, x2, 5) my_model <- lm(y ~ x1) eps <- resid(my_model) cor(x1, eps)  -1.088585e-16
So apperently the explanatory vaariable $x_1$ is not correlated with the error term at all, despite an important explanatory variable being omitted.
What am I doing/getting wrong?