R Markdown

This is an R Markdown document. Markdown is a simple formatting syntax for authoring HTML, PDF, and MS Word documents. For more details on using R Markdown see http://rmarkdown.rstudio.com.

When you click the Knit button a document will be generated that includes both content as well as the output of any embedded R code chunks within the document. You can embed an R code chunk like this:

library(ivreg)
# Problem 1 part a
set.seed(123)
n <- 1000
x1 <- rnorm(n, 0, 1)
u <- rnorm(n, 0, 1)

x2 <- 2 + 0.5 * x1

y <- 1 + x1 + x2 + u

v1 <- rnorm(n, 0, 1)
x3 <- x2 + v1

# Problem 1 part c

ols_model_1 <- lm(y ~ x1 + x3)
summary(ols_model_1)
## 
## Call:
## lm(formula = y ~ x1 + x3)
## 
## Residuals:
##     Min      1Q  Median      3Q     Max 
## -2.9801 -0.6834  0.0076  0.6871  3.2661 
## 
## Coefficients:
##             Estimate Std. Error t value Pr(>|t|)    
## (Intercept)  2.98345    0.07190  41.497   <2e-16 ***
## x1           1.57406    0.03572  44.061   <2e-16 ***
## x3           0.02909    0.03255   0.894    0.372    
## ---
## Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
## 
## Residual standard error: 1.006 on 997 degrees of freedom
## Multiple R-squared:  0.7105, Adjusted R-squared:  0.7099 
## F-statistic:  1223 on 2 and 997 DF,  p-value: < 2.2e-16
# Problem 1 part d

v2 <- rnorm(n, 0, 1)
x4 <- x2 + v2

iv_result_1 <- ivreg(y ~ x1 + x3 | x1 + x4)
summary(iv_result_1)
## 
## Call:
## ivreg(formula = y ~ x1 + x3 | x1 + x4)
## 
## Residuals:
##      Min       1Q   Median       3Q      Max 
## -3.25511 -0.71173  0.01997  0.70948  3.41019 
## 
## Coefficients:
##             Estimate Std. Error t value Pr(>|t|)    
## (Intercept)   3.3150     1.2934   2.563   0.0105 *  
## x1            1.6546     0.3157   5.241 1.95e-07 ***
## x3           -0.1383     0.6530  -0.212   0.8323    
## 
## Diagnostic tests:
##                  df1 df2 statistic p-value
## Weak instruments   1 997     2.550   0.111
## Wu-Hausman         1 996     0.068   0.795
## Sargan             0  NA        NA      NA
## 
## Residual standard error: 1.02 on 997 degrees of freedom
## Multiple R-Squared: 0.7028,  Adjusted R-squared: 0.7022 
## Wald test:  1191 on 2 and 997 DF,  p-value: < 2.2e-16
# Problem 1 part e

v3 <- rnorm(n, 0, 1)
x5 <- x2 + v3

iv_result_2 <- ivreg(y ~ x1 + x3 | x1 + x4 + x5)
summary(iv_result_2)
## 
## Call:
## ivreg(formula = y ~ x1 + x3 | x1 + x4 + x5)
## 
## Residuals:
##      Min       1Q   Median       3Q      Max 
## -4.03148 -0.85777  0.02593  0.78733  3.97969 
## 
## Coefficients:
##             Estimate Std. Error t value Pr(>|t|)    
## (Intercept)   4.2510     1.4464   2.939  0.00337 ** 
## x1            1.8819     0.3532   5.328 1.23e-07 ***
## x3           -0.6110     0.7302  -0.837  0.40289    
## 
## Diagnostic tests:
##                  df1 df2 statistic p-value  
## Weak instruments   2 996     1.378  0.2527  
## Wu-Hausman         1 996     1.070  0.3013  
## Sargan             1  NA     5.179  0.0229 *
## ---
## Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
## 
## Residual standard error: 1.186 on 997 degrees of freedom
## Multiple R-Squared: 0.5982,  Adjusted R-squared: 0.5974 
## Wald test: 881.5 on 2 and 997 DF,  p-value: < 2.2e-16
# Problem 2 part b
set.seed(123)
n <- 1000
x1 <- rnorm(n)
x2 <- rnorm(n)
x3 <- rnorm(n)
us <- rnorm(n)
ud <- rnorm(n)


p <- 0.5 * x3 + 0.5 * (ud - us)

y_eq <- 1 + x1 + x2 + 0.5 * x3 + 0.5 * us + 0.5 * ud

# Problem 2 part c

ols_supply <- lm(y_eq ~ p + x1 + x2)
summary(ols_supply)
## 
## Call:
## lm(formula = y_eq ~ p + x1 + x2)
## 
## Residuals:
##      Min       1Q   Median       3Q      Max 
## -2.41625 -0.54270 -0.01675  0.59500  2.26104 
## 
## Coefficients:
##             Estimate Std. Error t value Pr(>|t|)    
## (Intercept)  0.97498    0.02601   37.49   <2e-16 ***
## p            0.33220    0.03105   10.70   <2e-16 ***
## x1           0.99947    0.02630   38.00   <2e-16 ***
## x2           1.03462    0.02590   39.94   <2e-16 ***
## ---
## Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
## 
## Residual standard error: 0.8213 on 996 degrees of freedom
## Multiple R-squared:  0.7797, Adjusted R-squared:  0.779 
## F-statistic:  1175 on 3 and 996 DF,  p-value: < 2.2e-16
# Problem 2 part d

ols_demand <- lm(y_eq ~ p + x1 + x2 + x3)
summary(ols_demand)
## 
## Call:
## lm(formula = y_eq ~ p + x1 + x2 + x3)
## 
## Residuals:
##      Min       1Q   Median       3Q      Max 
## -1.99490 -0.47029 -0.00389  0.47115  1.97312 
## 
## Coefficients:
##             Estimate Std. Error t value Pr(>|t|)    
## (Intercept) 0.977826   0.022304  43.840   <2e-16 ***
## p           0.004952   0.031739   0.156    0.876    
## x1          1.011453   0.022564  44.826   <2e-16 ***
## x2          1.040707   0.022217  46.843   <2e-16 ***
## x3          0.514761   0.027162  18.952   <2e-16 ***
## ---
## Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
## 
## Residual standard error: 0.7043 on 995 degrees of freedom
## Multiple R-squared:  0.8381, Adjusted R-squared:  0.8374 
## F-statistic:  1288 on 4 and 995 DF,  p-value: < 2.2e-16
# Problem 2 part g

iv_supply <- ivreg(y_eq ~ p + x1 + x2 | x1 + x2 + x3)
summary(iv_supply)
## 
## Call:
## ivreg(formula = y_eq ~ p + x1 + x2 | x1 + x2 + x3)
## 
## Residuals:
##       Min        1Q    Median        3Q       Max 
## -3.257784 -0.670357  0.009886  0.701075  3.153253 
## 
## Coefficients:
##             Estimate Std. Error t value Pr(>|t|)    
## (Intercept)  0.99384    0.03324   29.89   <2e-16 ***
## p            1.11057    0.07289   15.24   <2e-16 ***
## x1           0.99728    0.03359   29.69   <2e-16 ***
## x2           0.98645    0.03330   29.63   <2e-16 ***
## 
## Diagnostic tests:
##                  df1 df2 statistic p-value    
## Weak instruments   1 996     418.7  <2e-16 ***
## Wu-Hausman         1 995     359.2  <2e-16 ***
## Sargan             0  NA        NA      NA    
## ---
## Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
## 
## Residual standard error: 1.049 on 996 degrees of freedom
## Multiple R-Squared: 0.6406,  Adjusted R-squared: 0.6396 
## Wald test: 774.3 on 3 and 996 DF,  p-value: < 2.2e-16
# Problem 3 part a
set.seed(123)
x1 <- rnorm(1000)
x2 <- rnorm(1000)
u <- rnorm(1000)
v <- rnorm(1000)


for (t in 2:1000) {
  u[t] <- 0.3 * u[t - 1] + v[t]
}


y <- rnorm(1000)
for (t in 2:1000) {
  y[t] <- 1 + 0.5 * y[t - 1] + x1[t] + x2[t] + u[t]
}

# Problem 3 part b

ydep <- y[2:1000]       
ylag <- y[1:999]       
x1reg <- x1[2:1000]   
x2reg <- x2[2:1000]     

x1lag <- x1[1:999]      
x2lag <- x2[1:999]      

# Problem 3 part c

ols_ts <- lm(ydep ~ ylag + x1reg + x2reg)
summary(ols_ts)
## 
## Call:
## lm(formula = ydep ~ ylag + x1reg + x2reg)
## 
## Residuals:
##     Min      1Q  Median      3Q     Max 
## -3.2882 -0.6671  0.0008  0.6769  3.1317 
## 
## Coefficients:
##             Estimate Std. Error t value Pr(>|t|)    
## (Intercept)  0.84989    0.04548   18.69   <2e-16 ***
## ylag         0.56528    0.01522   37.15   <2e-16 ***
## x1reg        1.00939    0.03285   30.73   <2e-16 ***
## x2reg        1.00098    0.03228   31.01   <2e-16 ***
## ---
## Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
## 
## Residual standard error: 1.025 on 995 degrees of freedom
## Multiple R-squared:  0.7699, Adjusted R-squared:  0.7692 
## F-statistic:  1109 on 3 and 995 DF,  p-value: < 2.2e-16
# Problem 3 part e

iv_ts <- ivreg(ydep ~ ylag + x1reg + x2reg | x1reg + x2reg + x1lag + x2lag)
summary(iv_ts)
## 
## Call:
## ivreg(formula = ydep ~ ylag + x1reg + x2reg | x1reg + x2reg + 
##     x1lag + x2lag)
## 
## Residuals:
##       Min        1Q    Median        3Q       Max 
## -3.500429 -0.687554 -0.005943  0.692897  3.306406 
## 
## Coefficients:
##             Estimate Std. Error t value Pr(>|t|)    
## (Intercept)  1.04779    0.05860   17.88   <2e-16 ***
## ylag         0.47071    0.02311   20.37   <2e-16 ***
## x1reg        1.00395    0.03350   29.97   <2e-16 ***
## x2reg        0.99509    0.03292   30.23   <2e-16 ***
## 
## Diagnostic tests:
##                  df1 df2 statistic  p-value    
## Weak instruments   2 994   407.304  < 2e-16 ***
## Wu-Hausman         1 994    32.669 1.44e-08 ***
## Sargan             1  NA     2.115    0.146    
## ---
## Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
## 
## Residual standard error: 1.045 on 995 degrees of freedom
## Multiple R-Squared: 0.7609,  Adjusted R-squared: 0.7602 
## Wald test: 763.5 on 3 and 995 DF,  p-value: < 2.2e-16