[SOLVED] CS # library we will be using next week

$25

File Name: CS_#_library_we_will_be_using_next_week.zip
File Size: 367.38 KB

5/5 - (1 vote)

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

# tidyverse allows us to write code in pipe
x <- 5sqrt(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 betas

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 doesnt 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)

Reviews

There are no reviews yet.

Only logged in customers who have purchased this product may leave a review.

Shopping Cart
[SOLVED] CS # library we will be using next week
$25