Skip to content

Instantly share code, notes, and snippets.

@dmarcelinobr
Created April 15, 2016 19:23
Show Gist options
  • Save dmarcelinobr/0e59caecfc15cafacaa23c137e9e05ea to your computer and use it in GitHub Desktop.
Save dmarcelinobr/0e59caecfc15cafacaa23c137e9e05ea to your computer and use it in GitHub Desktop.
Interpolation and smoothing functions in R
# 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