0

What is the correct way of finding x from a given y from a spline function?

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()

Plot:

enter image description here

Expected outcome:

My goal is to correctly find x when:

  • y == 2

  • y == 3

  • y == 4

FMM
  • 1,857
  • 1
  • 15
  • 38
  • That's nice, I will check it out! – FMM Oct 11 '18 at 16:37
  • Okay, I am not sure this is a duplicate, as this won't work when I use splines fitted with the `cobs` package (which seems to be the best option in my particular case) – FMM Oct 12 '18 at 08:14

0 Answers0