library(SOP)
# Simulate the data
set.seed(123)
n <- 1000
x <- runif(n, 0.0001, 1)
doppler.function <- function(x) sin(4 / (x + 0.1)) + 1.5
mu <- doppler.function(x)
sigma <- 0.2
y <- mu + sigma*rnorm(n)
dat <- data.frame(x = x, y = y)
# Fit the models
# With addaptive smoothing
m0 <- sop(formula = y ~ ad(x, nseg = 197, nseg.sp = 17), data = dat,
control = list(trace = FALSE, epsilon = 1e-03))
# Without addaptive smoothing
m1 <- sop(formula = y ~ f(x, nseg = 197), data = dat,
control = list(trace = FALSE, epsilon = 1e-03))
# Plot results
plot(y ~ x, data = dat)
ox <- order(dat$x)
lines(fitted(m0)[ox] ~ dat$x[ox], col = 2, lwd = 2)
lines(fitted(m1)[ox] ~ dat$x[ox], col = 4, lwd = 2)
legend("topright", c("Theoretical", "Adaptive", "Non Adaptive"),
col = c(1,2,4), lty = 1, lwd = 2, bty = "n")
Run the code above in your browser using DataLab