0

I want to check how many times my smoothed spline intersects with the x-axis. Is there an elegant way to do this?

Example: (1 intersection in this case)

Text](https://stackoverflow.com/[![image.jpg]1)

Jdh
  • 91
  • 5
  • It's easier to help you if you include a simple [reproducible example](https://stackoverflow.com/questions/5963269/how-to-make-a-great-r-reproducible-example) with sample input and desired output that can be used to test and verify possible solutions. Exactly what type of R objects are you working with here? – MrFlick Oct 24 '19 at 15:56
  • do you mean intersect the y=0 line? – StupidWolf Oct 24 '19 at 21:55

1 Answers1

1

Check the number of times y values go from positive to negative

set.seed(1571933401)
x = 1:100
y = rnorm(100)
sp = smooth.spline(x, y)

with(sp, sum((sign(c(0, y)) * sign(c(y, 0))) == -1))
#6

graphics.off()
plot(sp, type = "l")
abline(h = 0, lty = 2)

enter image description here

d.b
  • 32,245
  • 6
  • 36
  • 77