您添加到绘图中的预测线不太正确。改用这样的代码:
# 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)