## ---------------------------------------------------------------------------- ## R scripts for Haerdle/Mueller/Sperlich/Werwatz: "Nonparametric and ## Semiparametric Modelling", Springer Series in Statistics, 2004 ## ---------------------------------------------------------------------------- ## Script SPMknnregression ## ---------------------------------------------------------------------------- ## Description computes the regression of food on net-income ## for the UK 1976 expenditure data. ## ---------------------------------------------------------------------------- ## Author Marlene Mueller, 2008/12/19 ## ---------------------------------------------------------------------------- x <- read.csv2("D:\\fes76.txt", dec=".") x <- x[,c("NIC","FOO")] x <- x[order(x$NIC),] plot(x,col="lightblue",pch=20,main="Engel Curve: k-NN regression") library(kknn) n.grid <- 100 NIC.grid <- seq(min(x$NIC),max(x$NIC),length=n.grid) test.grid <- data.frame(NIC=NIC.grid,FOO=rep(0,n.grid)) mh <- kknn(FOO ~ NIC, x, test.grid, k=50, kernel="rectangular") ##mh <- kknn(FOO ~ NIC, x, test.grid, k=50, kernel="biweight") lines(NIC.grid,fitted(mh))