0

我已经对一些数据进行了 LOESS 局部回归,我希望能够找到与给定 Y 值相关联的 X 值。

plot(cars, main = "Stopping Distance versus Speed")
car_loess <- loess(cars$dist~cars$speed,span=.5)
lines(1:50, predict(car_loess,data.frame(speed=1:50)))

我希望我可以使用 chemCal 包中的 inverse.predict 函数,但这不适用于 LOESS 对象。

有谁知道我如何能够以比从 X 值的长向量中预测 Y 值并查看结果拟合 Y 以获取感兴趣的 Y 值并获取其相应的 X 值更好的方式进行此校准?

实际上,在上面的示例中,假设我想找到停止距离为 15 的速度。

谢谢!

4

1 回答 1

1

您添加到绘图中的预测线不太正确。改用这样的代码:

# plot the loess line
lines(cars$speed, car_loess$fitted, col="red")

您可以使用该approx()函数在给定的 y 值处从黄土线获得线性近似值。它适用于您提供的示例:

# define a given y value at which you wish to approximate x from the loess line
givenY <- 15
estX <- approx(x=car_loess$fitted, y=car_loess$x, xout=givenY)$y
# add corresponding lines to the plot
abline(h=givenY, lty=2)
abline(v=estX, lty=2)

但是,在 loess fit 的情况下,给定的 y 可能有不止一个 x。我建议的方法不会为您提供给定 y 的所有 x 值。例如 ...

# example with non-monotonic x-y relation
y <- c(1:20, 19:1, 2:20)
x <- seq(y)
plot(x, y)
fit <- loess(y ~ x)
# plot the loess line
lines(x, fit$fitted, col="red")

# define a given y value at which you wish to approximate x from the loess line
givenY <- 15
estX <- approx(x=fit$fitted, y=fit$x, xout=givenY)$y
# add corresponding lines to the plot
abline(h=givenY, lty=2)
abline(v=estX, lty=2)
于 2014-05-30T19:23:51.340 回答