## ---------------------------------------------------------------------------- ## R scripts for Haerdle/Mueller/Sperlich/Werwatz: "Nonparametric and ## Semiparametric Modelling", Springer Series in Statistics, 2004 ## ---------------------------------------------------------------------------- ## Script SPMdensity2D ## ---------------------------------------------------------------------------- ## Description estimates a bivariate density and shows the resultng KDE ## as a 3D graph and as a contour plot ## ---------------------------------------------------------------------------- ## Author Marlene Mueller, 2008/12/05 ## ---------------------------------------------------------------------------- library(AER) library(KernSmooth) data <- data("CPS1985") x <- CPS1985[,c("age","wage")] n <- nrow(x) sd <- sd(x) h <- sd * n^(-1/6) ## Scott's rule of thumb est <- bkde2D(x, bandwidth=h) par(mfrow=c(1,2)) persp(est$fhat, theta = 30, phi = 30, expand = 0.5, col = "lightblue", main="2D Density Estimate", xlab="age", ylab="wage", zlab="KDE") contour(est$x1, est$x2, est$fhat, main="2D Density Contours", xlab="age", ylab="wage") par(mfrow=c(1,1))