[R] Kernel smoothing ("ks" package)

Gary Dong
2012-07-17T23:16:10Z

Dear R users,

I'm trying to determine local population centers in a region through kernel
smoothing. What I have is population density in each neighborhood, which
can be presented as point density. So in my data set “pop”, I have three
columns for about 1000 neighborhoods: x (x coordinate), y (y coordinate),
and z (population density at xy). I searched R documentations and found
that the "kde" function in the "ks" package might be the one
I'm looking
for.

To illustrate what I have done, I use the elevation data in GeoR:

##### Kernel smooth ####

require(geoR)

data(elevation)

elev.df <- data.frame(x = 50 * elevation$coords[,"x"], y = 50 *
elevation$coords[,"y"], z = 10 * elevation$data)

H<-Hpi(elev.df)

popcnt<-kde(elev.df,H=H)

plot(popcnt, axes=TRUE, box=TRUE, drawpoints=TRUE)

#### To confirm this is what I want, I also created Loess density surface
####

require(geoR)

data(elevation)

elev.df <- data.frame(x = 50 * elevation$coords[,"x"], y = 50 *
elevation$coords[,"y"], z = 10 * elevation$data)

elev.loess <- loess(z ~ x + y, data = elev.df, degree = 2, span = 0.15)

grid.x <- seq(10, 300, 1)
grid.y <- seq(10, 300, 1)
grid.mar <- list(x=grid.x, y=grid.y)
elev.fit<-expand.grid(grid.mar)

z.pred <- predict(elev.loess, newdata = elev.fit)

persp(seq(10, 300, 1), seq(10, 300, 1), z.pred, phi = 45, theta = 45)

#####

After comparing the two, I felt I did something wrong with the kernel
smooth, but could not figure out the right way to do it. Your help would be
greatly appreciated!

Thanks.

Gary

	[[alternative HTML version deleted]]