在R的样条函数中从给定的y查找x

时间:2018-10-11 16:06:46

标签: r tidyverse spline approximation bspline

从样条函数中的给定x中找到y的正确方法是什么?

library(cobs)
library(tidyverse)

# data
dat <- tibble(
  x = c(seq(80, 360, 40),373.3),
  y = c(1.07, .85, .84, .97, 1.11, 2.02, 4.01, 7.99, 10.4)
)

# interpolation
dat_interpolated <- approx(x = dat$x, y = dat$y, xout = seq(min(dat$x), max(dat$x), 1)) %>% 
  as.tibble()

# spline
myspline <- cobs(x = dat_interpolated$x, y = dat_interpolated$y)

# data for plotting
dat2 <- predict(myspline) %>% 
  as.tibble()

dat %>% 
  ggplot(aes(x, y))+
  geom_point(shape = 21,
             size = 4,
             fill = "white")+
  geom_line(data = dat2, aes(z, fit), size = 1, color = "red")+
  theme_bw()

情节:

enter image description here

预期结果:

我的目标是在以下情况下正确找到x

  • y == 2

  • y == 3

  • y == 4

0 个答案:

没有答案