Data generation process from an given regression model and monte-carlo experiment in R

I want to generate data in R to solve the following problem:

Consider the following Data Generating Process (DGP) $$Y_i = β_0 + β_1 · X_i + β_2 · Z_i + u_i$$ , where $$β_0 = 0.75$$, $$β_1 = 0.50$$ and $$β_2 = 0.25$$. Imagine that somebody wishes to obtain a proper (unbiased and consistent) estimate for $$β_1$$ and decides to estimate $$Y_i = b_0 + b_1 · X_i + v_i$$ , instead of the true DGP.

Write the R code to show that:

1. when Xi and Zi are uncorrelated, the OLS estimator for b1 is unbiased and consistent;

2. when Xi and Zi are positively correlated, the OLS estimator for b1 is upward biased and inconsistent;

3. when Xi and Zi are negatively correlated, the OLS estimator for b1 is downward biased and inconsistent.

• Is this a question from a course or textbook? If so, please add the [self-study] tag & read its wiki. – deemel Dec 7 '19 at 15:55
• It is not from a text book. Is an exercise for us to learn how to generate data with similar characteristics. I am just interested in the data generating process given a specific model as a general indication for R. I will add the tag! thanks for the repply – Daniel Ortiz Dec 7 '19 at 19:21

I would use DeclareDesign for the simulation and bookkeeping, and lm to estimate the regression parameters

require(DeclareDesign)

N = 20
r = .2
b0 = 0.75
b1 = 0.50
b2 = 0.25
s = 1

design <- declare_population(N=N, X = rnorm(N),
Z = rnorm(N, r*X, sd=sqrt(1-r*r)),
Y = b0 + b1*X + b2 * Z +rnorm(N, sd=s)) +
declare_estimands(b1=b1) +
declare_estimators(Y~X, model=lm, term='X', estimand='b1')

diagnostics <- diagnose_design(redesign(design, r=-9:9/10), sims = 300, bootstrap_sims=0)


The actual amount of bias observed will of course also depend on the sample size, correlation, and variance of the error term.

For N=20 and s=1, we can plot the bias over r. Also note that without a large number of simulations, the results can be rather noisy.