CS计算机代考程序代写 # library we will be using next week

# library we will be using next week
library(tidyverse)

# tidyverse allows us to write code in pipe
x <- 5 sqrt(x) x %>% sqrt

# Now we are going to perform a Montecarlo Simulation Exercise
# we want to simulate and estimate the following model
# y = a + b * x + e
# where y and x are variables and e is an error term
# and plot a histogram of estimated beta’s

nobs = 100
a = 2
b = 0.5
nsim = 1000
beta = rep(0, nsim)

for(ii in 1:nsim){
e = rnorm(nobs)
x = rnorm(nobs)
y = a + b * x + e
ols.mod = lm(y ~ x)
beta[ii] = ols.mod$coefficients[2]
}

# we can increase the n. of obs.
# doing so reduces the variability of our estimated beta
# but it doesn’t make it disappear
hist(beta)

# what happens if we introduce correlation between x and e?

for(ii in 1:nsim){
e = rnorm(nobs)
x = rnorm(nobs) + 0.2 * e
y = a + b * x + e
ols.mod = lm(y ~ x)
beta[ii] = ols.mod$coefficients[2]
}

hist(beta)

Leave a Reply

Your email address will not be published. Required fields are marked *