# load the quantreg library library(quantreg) # read the wage data for a sample of 935 persons wage<-read.table(file="C:/Documents and Settings/buck/My Documents/Courses/Econ616/Dummies01.csv", header = TRUE, sep = ",", dec = ".", na.strings = "NA",check.names = TRUE) # Put the Wage in w w<-wage[,19] #Make a couple of new variables agesq<-wage$AGE*wage$AGE educsq=wage$EDUC*wage$EDUC # To economize on typing we will attach the data arrach(wage) # Plot this wage against both education and age # First set up the graphics display so that the two graphs are side by side. par(mfrow=c(1,2)) plot(AGE,w,col="blue",cex=0.7,xlab="Age",ylab="Wage") plot(EDUC,w,col="blue",cex=0.7,xlab="Education",ylab="Wage") #Do the OLS regressions and an F-test weq1<-lm(w~AGE+agesq+EDUC+educsq) summary(weq1) RSS1<-resid(weq1)%*%resid(weq1) weq0<-lm(w~AGE+EDUC) summary(weq0) RSS0<-resid(weq0)%*%resid(weq0) F<-((RSS0-RSS1)/2)/(RSS1/930) #Do the first, second and third quantiles first<-rq(w~ AGE+agesq+EDUC+educsq, tau = 0.25) second<-rq(w~ AGE+agesq+EDUC+educsq, tau = 0.5) third<-rq(w~ AGE+agesq+EDUC+educsq, tau = 0.75) # Plot coefficient against quantile plot(summary(rq(w~AGE+agesq+EDUC+educsq,tau = 1:49/50))) # Location and scale shifts T3 <- KhmaladzeTest(w ~ AGE+agesq+EDUC+educsq, taus = -1, nullH = "location-scale") #Do the plot again for AGE and EDUC, but in regression quantiles plot(AGE,w,col="blue",cex=0.7,xlab="Age",ylab="Wage") abline(rq(w~ AGE, tau = 0.1), col = "green") abline(rq(w ~ AGE, tau = 0.5), col = "green") abline(rq(w ~ AGE, tau = 0.9), col = "green") plot(EDUC,w,col="blue",cex=0.7,xlab="Age",ylab="Wage") abline(rq(w~ EDUC, tau = 0.1), col = "green") abline(rq(w ~ EDUC, tau = 0.5), col = "green") abline(rq(w ~ EDUC, tau = 0.9), col = "green") # Do some non linear quantiles. You might need to close and reopen R to do this library(quantreg) library(splines) library(MASS) wage<-read.table(file="C:/Documents and Settings/buck/My Documents/Courses/Econ616/Dummies01.csv", header = TRUE, sep = ",", dec = ".", na.strings = "NA",check.names = TRUE) attach(wage) w<-wage[,19] agesq<-AGE*AGE educsq=EDUC*EDUC plot(AGE, w, xlab = "Age", ylab = "Wage",type = "n") points(AGE, w, cex = 0.75) X <- model.matrix(w ~ bs(AGE, df = 3)) for (tau in 1:3/4) { fit <- rq(w ~ bs(AGE, df = 3), tau = tau) w.fit <- X %*% fit$coef lines(AGE, w.fit) } legend(45, -70, c("h=1", "h=2", "h=3", "h=4"),lty = 1:length(hs))