A nice implementation of polynomial smoothing in R is provided by the
KernSmooth
package. The package contains the
dpill()
function, which helps to select the bandwidth of a
local linear Gaussian kernel regression estimate. A small bandwidth
means less smoothing, and a large bandwidth means more smoothing
The package includes the locpoly()
function, which
constructs a polynomial that is fitted to the near-by data points around
each data point. The resulting local polynomials create a smoothed
version of the original data series.
Both, the dpill()
and locpoly()
function
require to specify number of points (gridsize
) for which a
local polynomial is constructed.
library(KernSmooth)
library(xts)
load(url("https://userpage.fu-berlin.de/soga/data/r-data/Earth_Surface_Temperature.RData"))
dt <- index(temp_global)
y <- coredata(temp_global)
gridsize <- length(dt) / 12 # evaluate local polynomial annually (12 months)
# estimate bandwidth of a local linear Gaussian kernel
bandwidth <- dpill(dt, y, gridsize = gridsize)
bandwidth
## [1] 2.947308
# Function estimation using local polynomials
lp <- locpoly(x = dt, y = y, bandwidth = bandwidth, gridsize = gridsize)
### PLOTTING ###
plot(dt, y, type = "l", col = "gray", xlab = "", ylab = "", main = "Smoothing via local polynomials")
lines(lp, col = "red", type = "l")
legend("topleft", legend = paste0("bbandwidth = ", round(bandwidth, 2)), col = "red", lty = 1, cex = 0.6)
Citation
The E-Learning project SOGA-R was developed at the Department of Earth Sciences by Kai Hartmann, Joachim Krois and Annette Rudolph. You can reach us via mail by soga[at]zedat.fu-berlin.de.
Please cite as follow: Hartmann, K., Krois, J., Rudolph, A. (2023): Statistics and Geodata Analysis using R (SOGA-R). Department of Earth Sciences, Freie Universitaet Berlin.