プロットに追加した予測線は完全に正しくありません。代わりに次のようなコードを使用してください。
# 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)
しかし、黄土適合では、特定の 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)