Last active
December 9, 2019 06:56
-
-
Save andrie/2dff13157cfe46396ff0 to your computer and use it in GitHub Desktop.
Interpolation and smoothing functions in R
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# Generate data in the form of a sine wave | |
set.seed(1) | |
n <- 1e3 | |
dat <- data.frame( | |
x = 1:n, | |
y = sin(seq(0, 5*pi, length.out = n)) + rnorm(n=n, mean = 0, sd=0.1) | |
) | |
approxData <- data.frame( | |
with(dat, | |
approx(x, y, xout = seq(1, n, by = 10), method = "linear") | |
), | |
method = "approx()" | |
) | |
splineData <- data.frame( | |
with(dat, | |
spline(x, y, xout = seq(1, n, by = 10)) | |
), | |
method = "spline()" | |
) | |
smoothData <- data.frame( | |
x = 1:n, | |
y = as.vector(smooth(dat$y)), | |
method = "smooth()" | |
) | |
loessData <- data.frame( | |
x = 1:n, | |
y = predict(loess(y~x, dat, span = 0.1)), | |
method = "loess()" | |
) | |
library(ggplot2) | |
ggplot(rbind(approxData, splineData, smoothData, loessData), aes(x, y)) + | |
geom_point(dat = dat, aes(x, y), alpha = 0.2, col = "red") + | |
geom_line(col = "blue") + | |
facet_wrap(~method) + | |
ggtitle("Interpolation and smoothing functions in R") + | |
theme_bw(16) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment