Smooth out aggregated data

View source: R/smooth.r

smooth.mapR Documentation

Smooth out aggregated data


Increases the resolution of data aggregated over map regions, by either smoothing or interpolation. Also fills in missing values.

Usage, z, res = 50, span = 1/10, averages = FALSE, type = c("smooth",
"interp"), merge = FALSE)



a map object


a named vector


a vector of length two, specifying the resolution of the sampling grid in each dimension. If a single number, it is taken as the vertical resolution, with double taken as the horizontal resolution.


kernel parameter (larger = smoother). span = Inf is a special case which invokes the cubic spline kernel. span is automatically scaled by the map size, and is independent of res.


If TRUE, the values in z are interpreted as averages over the regions. Otherwise they are interpreted as totals.


see details.


If TRUE, a region named in z includes all matching regions in the map (according to If FALSE, a region named in z is assumed to refer to exactly one region on the map.


For type = "smooth", the region totals are first converted into point measurements on the sampling grid, by dividing the total for a region among all sample points inside it. Then it is a regular kernel smoothing problem. Note that the region totals are not preserved.

The prediction z_o for location x_o (a vector) is the average of z for nearby sample points:

z_o = \frac{\sum_x k(x, x_o) z(x)}{\sum_x k(x, x_o)}

k(x, x_o) = exp(-\lambda ||x - x_o||^2)

\lambda is determined from span. Note that x_o is over the same sampling grid as x, but z_o is not necessarily the same as z(x_o).

For type = "interp", the region totals are preserved by the higher-resolution function. The function is assumed to come from a Gaussian process with kernel k. The measurement z[r] is assumed to be the sum of the function over the discrete sample points inside region r. This leads to a simple formula for the covariance matrix of z and the cross-covariance between zo and z. The prediction is the cross-covariance times the inverse covariance times z. Unlike Tobler's method, the predictions are not constrained to live within the original data range, so there tends to be "ringing" effects.

See the references for more details.


A data frame with columns x, y, and z giving the smoothed value z for locations (x, y). Currently the (x, y) values form a grid, but this is not guaranteed in the future.


Tom Minka


W.F. Eddy and A. Mockus. An example of the estimation and display of a smoothly varying function of time and space - the incidence of disease mumps. Journal of the American Society for Information Science, 45(9):686-693, 1994.

W. R. Tobler. Smooth pycnophylactic interpolation for geographical regions. Journal of the American Statistical Association 74:519-530, 1979.


# compare to the example for
data(state, package = "datasets")
z = votes.repub[, "1900"]
m = map("state", fill = TRUE, plot = FALSE)
# use a small span to fill in, but not smooth, the data
# increase the resolution to get better results
fit =, z, span = 1/100, merge = TRUE, ave = TRUE)
mat = tapply(fit$z, fit[1:2], mean)
gray.colors <- function(n) gray(rev(0:(n - 1))/n)
par(bg = "blue")
filled.contour(mat, color.palette = gray.colors, nlev = 32, asp = 1)
# another way to visualize:
image(mat, col = gray.colors(100))

# for a higher degree of smoothing:
# fit =, z, merge = TRUE, ave = TRUE)
# interpolation, state averages are preserved:
# fit =, z, merge = TRUE, ave = TRUE, type = "interp")

maps documentation built on May 29, 2024, 9:04 a.m.