n = 100
x = runif(n, 0, 10)

m = function(x) {
  sin(2 * x) + 0.3 * cos(0.5 * x) + 0.08 * x
}
sig = function(x) {
  0.12 + 0.10 * abs(sin(x))
}

y = m(x) + sig(x) * rnorm(n)

x0 = seq(0, 10, length.out = 100)

h = 0.5
library(KernSmooth)
m_ocena_p0 = locpoly(x, y, degree = 0, bandwidth = h,
                gridsize = length(x0),
                range.x = range(x0))

m_ocena_p1 = locpoly(x, y, degree = 1, bandwidth = h,
                gridsize = length(x0),
                range.x = range(x0))

m_ocena_p2 = locpoly(x, y, degree = 2, bandwidth = h,
                gridsize = length(x0),
                range.x = range(x0))

plot(x, y, pch = 20)
lines(x0, m(x0), lwd = 1.5)
lines(x0, m_ocena_p0$y, col = 2, lwd = 1.5)
lines(x0, m_ocena_p1$y, col = 3, lwd = 1.5)
lines(x0, m_ocena_p2$y, col = 4, lwd = 1.5)
legend("bottomright",legend=c("true", "p=0","p=1","p=2"),
       col = 1:4, lty = 1, cex = 0.5)

