Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #8.28
- library(dplyr)
- mydir <- "DIRECTORY"
- setwd(mydir)
- dsURL <- "http://qed.econ.queensu.ca/ETM/data/demand-supply.data"
- datedownloaded <- date()
- download.file(dsURL,destfile = "ds.txt")
- #Manually remove the semi-colon at the end of line 120
- ds <- read.table("ds.txt",nrows=120)
- ds <- select(ds, -V1)
- #Renaming the variables
- ds <- rename(ds, q=V2, p = V3, X_2 = V4, X_3 = V5, X_4 = V6, X_5 = V7)
- dsfit1 <- lm(q ~ X_2+X_3+p,data=ds)
- summary(dsfit1)
- #First step of 2SLS, I make the matrix W of instruments
- W1 <- cbind(ds$X_2,ds$X_3,ds$X_4,ds$X_5)
- X1 <- as.matrix(select(ds,X_2,X_3,p))
- #I then manually get the matrix of fitted values from the first stage regression
- P_W1 <- W1 %*% solve(t(W1)%*%W1) %*% t(W1)
- firstfit <- P_W1 %*% X1
- firstfit <- as.data.frame(firstfit)
- #Second stage
- second <- lm(ds$q ~ firstfit$X_2+firstfit$X_3+firstfit$p)
- summary(second)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement