使用黄土曲线预测,x 坐标来自给定的 y 坐标

Predict using loess curve, an x-coordinate from a given y-coordinate

我想 return 黄土预测 x 值,其中 y = 30 使用以下数据和绘图。你是如何得到这个并在下面的图中绘制成一条垂直线的?

> dput(t)
structure(list(vol = c(0L, 5L, 10L, 20L, 40L, 80L, 120L, 160L
), pc = c(0.27, 10.8, 16.4, 19.07, 53.56, 70.69, 83.85, 86.7)), class = "data.frame", row.names = c(NA, 
-8L))
library(ggplot2)
ggplot(data = t, aes(x = vol, y = pc)) +
  geom_point() +
  theme_bw() +
  geom_smooth(method = "loess", size = 1.5, span = 0.9, se = FALSE) +
  scale_x_continuous(breaks = seq(0, 160, by = 10)) +
  scale_y_continuous(breaks = seq(0, 100, by = 10)) +
  geom_hline(yintercept = 30)

因为没有等式,所以无法精确反推 x 的值,它会产生 30 的预测值。但是,您可以编写一个小函数来找到它,然后使用它在你的情节中的价值。这是一个例子:

  t <- structure(list(vol = c(0L, 5L, 10L, 20L, 40L, 80L, 120L, 160L
), pc = c(0.27, 10.8, 16.4, 19.07, 53.56, 70.69, 83.85, 86.7)), class = "data.frame", row.names = c(NA, 
                                                                                                    -8L))
lo <- loess(pc ~ vol, data=t, span=.9)

f <- function(x){
  p <- predict(lo, newdata = data.frame(vol=x))
  (p-30)^2
}

opt <- optimize(f, c(0,160))

library(ggplot2)
ggplot(data = t, aes(x = vol, y = pc)) +
  geom_point() +
  theme_bw() +
  geom_smooth(method = "loess", size = 1.5, span = 0.9, se = FALSE) +
  geom_vline(xintercept = opt$minimum) + 
  scale_x_continuous(breaks = seq(0, 160, by = 10)) +
  scale_y_continuous(breaks = seq(0, 100, by = 10)) +
  geom_hline(yintercept = 30)
#> `geom_smooth()` using formula 'y ~ x'

reprex package (v2.0.1)

于 2022-04-15 创建