Engi
Engi

Reputation: 33

How to get any (x,y) point from smoothed kernel estimate function in R?

I have a scatter plot and I used ksmooth function to get smoothed line. Now I want to get the residuals by subtracting real value from the one from smoothed line. Maybe someone know how to find value of y for any x? Or maybe there's another way to get residuals?

plot(x,y)
kernel <- ksmooth(x,y, kernel="normal", bandwidth=0.01)
lines(kernel, col=2)

Here is the result

Upvotes: 0

Views: 410

Answers (1)

user2728808
user2728808

Reputation: 590

You can access the fitted values directly from the model object returned by ksmooth. Here is a MWE:

x <- 1:100
y <- rnorm(100, mean=(x/2000)^2)
plot(x,y)
kernel <- ksmooth(x,y, kernel="normal", bandwidth=10, x.points=x)
lines(kernel, col=2)
resid <- kernel$y - y
print(resid)

Then run

all.equal(kernel$x, x)

Upvotes: 1

Related Questions