A Loess curve has been fit the following data:
library(tidyverse)
data <- tribble(~x, ~y,
1, 0.254334545,
2, 0.495754951,
3, 0.711091409,
4, 0.864910881,
5, 0.941040554,
6, 0.97463615,
7, 0.989459747,
8, 0.995969154,
9, 0.998773181,
10, 1)
x <- data$x
y <- data$y
lo <- loess(y~x)
plot(x,y)
xl <- seq(min(x),max(x), (max(x) - min(x))/1000)
lines(xl, predict(lo,xl), col='red', lwd=2)
How can I extract the value of the smoothed function, y for x = c(1.5, 2.5, 3.5) say?
Thanks.
You can use predict.loess
to make predictions from a LOESS fit
# Fit LOESS model
fit <- loess(y ~ x, data = data)
# Predict y values for x = c(1.5, 2.5, 3.5)
predict(fit, data.frame(x = c(1.5, 2.5, 3.5)))
# 1 2 3
#0.3849209 0.6154395 0.7926339
Or to return a data.frame
df <- data.frame(x = c(1.5, 2.5, 3.5))
transform(df, y.pred = predict(fit, df))
# x y.pred
#1 1.5 0.3849209
#2 2.5 0.6154395
#3 3.5 0.7926339