Advertisement
Guest User

Untitled

a guest
Aug 17th, 2017
62
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.84 KB | None | 0 0
  1. #8.28
  2. library(dplyr)
  3. mydir <- "DIRECTORY"
  4. setwd(mydir)
  5. dsURL <- "http://qed.econ.queensu.ca/ETM/data/demand-supply.data"
  6. datedownloaded <- date()
  7. download.file(dsURL,destfile = "ds.txt")
  8. #Manually remove the semi-colon at the end of line 120
  9. ds <- read.table("ds.txt",nrows=120)
  10. ds <- select(ds, -V1)
  11. #Renaming the variables
  12. ds <- rename(ds, q=V2, p = V3, X_2 = V4, X_3 = V5, X_4 = V6, X_5 = V7)
  13. dsfit1 <- lm(q ~ X_2+X_3+p,data=ds)
  14. summary(dsfit1)
  15.  
  16. #First step of 2SLS, I make the matrix W of instruments
  17. W1 <- cbind(ds$X_2,ds$X_3,ds$X_4,ds$X_5)
  18. X1 <- as.matrix(select(ds,X_2,X_3,p))
  19. #I then manually get the matrix of fitted values from the first stage regression
  20. P_W1 <- W1 %*% solve(t(W1)%*%W1) %*% t(W1)
  21. firstfit <- P_W1 %*% X1
  22. firstfit <- as.data.frame(firstfit)
  23. #Second stage
  24. second <- lm(ds$q ~ firstfit$X_2+firstfit$X_3+firstfit$p)
  25. summary(second)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement