## ---------------------------------------------------------------------------- ## R scripts for Haerdle/Mueller/Sperlich/Werwatz: "Nonparametric and ## Semiparametric Modelling", Springer Series in Statistics, 2004 ## ---------------------------------------------------------------------------- ## Script SPMcps85lin ## ---------------------------------------------------------------------------- ## Description computes a linear regression of log wages on ## schooling, experience and experience squared for ## observations from the 1985 CPS (see Berndt, 1991). ## The marginal wage-schooling and wage-experience ## profiles and the 3d wage-schooling/experience surface ## are displayed. ## ---------------------------------------------------------------------------- ## Author Marlene Mueller, 2008/11/19 ## ---------------------------------------------------------------------------- library(scatterplot3d) library(AER) data("CPS1985") l <- lm( log(wage) ~ education + experience + I(experience^2), data=CPS1985) summary(l) b <- coef(l) m.school <- data.frame(CPS1985$education,CPS1985$education*b["education"]) m.school <- m.school[order(CPS1985$education),] # wage-schooling profile m.experi <- data.frame(CPS1985$experience, CPS1985$experience*b["experience"] +CPS1985$experience^2*b["I(experience^2)"]) m.experi <- m.experi[order(CPS1985$experience),] # wage-experience profile par(mfrow=c(1,2)) plot(m.school,col="blue",pch=19,main="Wage <-- Schooling",xlab="education",ylab="") lines(m.school,col="blue") plot(m.experi,col="magenta",pch=19,main="Wage <-- Experience",xlab="experience",ylab="") lines(m.experi,col="magenta") par(mfrow=c(1,1)) col <- rep("green",nrow(CPS1985)); col[CPS1985$education==12] <- "red" pch <- rep(1,nrow(CPS1985)); pch[CPS1985$education==12] <- 8 scatterplot3d(CPS1985$education,CPS1985$experience, fitted(l), xlab="education",ylab="experience",zlab="", main="Wage <-- Schooling, Experience",color=col,pch=pch,angle=130) ## noe use a more complicated linear model l1 <- lm( log(wage) ~ education + I(education^2) + experience + I(experience^2) + I(experience^3), data=CPS1985) summary(l1) col <- rep("green",nrow(CPS1985)); col[CPS1985$education==12] <- "red" pch <- rep(1,nrow(CPS1985)); pch[CPS1985$education==12] <- 8 scatterplot3d(CPS1985$education,CPS1985$experience, fitted(l1), xlab="education",ylab="experience",zlab="", main="Wage <-- Schooling, Experience",color=col,pch=pch,angle=130)