## ---------------------------------------------------------------------------- ## R scripts for Haerdle/Mueller/Sperlich/Werwatz: "Nonparametric and ## Semiparametric Modelling", Springer Series in Statistics, 2004 ## ---------------------------------------------------------------------------- ## Script SPMkernelregression-Sliders ## ---------------------------------------------------------------------------- ## Description computes the regression of food on net-income ## for the UK 1976 expenditure data (with additional ## slider to choose bandwidth and polynomial degree) ## ---------------------------------------------------------------------------- ## Author Marlene Mueller, 2010/07/11 ## ---------------------------------------------------------------------------- library(KernSmooth) library(tcltk) library(TeachingDemos) x <- read.csv2("fes76.txt", dec=".") x <- x[,c("NIC","FOO")] x <- x[order(x$NIC),] y <- x$FOO x <- x$NIC ##s <- sample(1:length(x),1000 ) ##x <- x[s]; y <- y[s] xr <- range(x) xd <- diff(xr) xr[1] <- min(x) - xd/5 xr[2] <- max(x) + xd/5 xd <- diff(xr) kreg.refresh <- function(...) { degree <- slider(no=2) title <- "Engel Curve (Nadaraya-Watson)" if (degree>0){ title <- paste("Engel Curve (local polynomial: p = ",degree,")",sep="") } plot(x,y,col="lightblue",pch=20,main=title,xlab="netincome", ylab="food expenditure") mh <- locpoly(x,y,degree=degree,kernel="normal",bandwidth=slider(no=1)) lines(mh) } slider(sl.functions=kreg.refresh, sl.names=c("Bandwidth", "Degree"), sl.mins=c(xd/10000, 0), sl.maxs=c(xd/2, 1), sl.deltas=c(xd/1000,1), sl.defaults=c(diff(range(x))/10,0), title = "Choose Kernel Regression Parameters")