黄土曲线之间的R插值

时间:2019-03-19 02:49:25

标签: r interpolation loess

黄土曲线已拟合以下数据:

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)

如何提取平滑函数的值,对于x = c(1.5,2.5,3.5)来说,y是?

谢谢。

1 个答案:

答案 0 :(得分:2)

您可以使用predict.loess通过黄土拟合来进行预测

# 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

或者返回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