install.packages("leaps") install.packages("ivreg") library("leaps") library("ivreg") data = read.csv("http://hedibert.org/wp-content/uploads/2024/11/card.csv",header=TRUE) attach(data) n = nrow(data) # OLS regression with several exogenous variables reg.ols = lm(lwage~educ+exper+expersq+black+smsa+south+smsa66+ reg662+reg663+reg664+reg665+reg666+reg667+reg668+reg669) # IV regression with several exogenous variables reg.iv = ivreg(lwage ~ educ+exper+expersq+black+smsa+south+smsa66+ reg662+reg663+reg664+reg665+reg666+reg667+reg668+reg669 | nearc4+exper+expersq+black+smsa+south+smsa66+ reg662+reg663+reg664+reg665+reg666+reg667+reg668+reg669) summary(reg.ols) summary(reg.iv) # OLS regression with selection of exogenous variables X1 = cbind(educ,W) data = data.frame(y=lwage,X=X1) p = ncol(X1) regs = regsubsets(y~.,data=data,method="exhaustive",nvmax=p) bic = summary(regs)$bic ii = 1:p coef(regs,ii[bic==min(bic)]) ols = lm(lwage ~ educ+exper+expersq+black+smsa+south+reg663+reg668) # IV regression with selection of exogenous variables X1 = cbind(iv,W) data = data.frame(y=lwage,X=X1) p = ncol(X1) regs = regsubsets(y~.,data=data,method="exhaustive",nvmax=p,force.in=X1[,1]) bic = summary(regs)$bic ii = 1:p coef(regs,ii[bic==min(bic)]) reg.iv = ivreg(lwage ~ educ+exper+expersq+black+smsa+south+reg663 | nearc4+exper+expersq+black+smsa+south+reg663) summary(ols) summary(reg.iv)