I am trying to add a tangent to my plot at the point x = 30
and I want to calculate the x-intersection of the tangent at y = 0.08
.
I already found a very useful example, which I tried to use, but for some reason it is not working for y = 0.08
.
I don't understand the meaning of deriv in the predict()
function nor the actual difference in between pred0 and pred1. Can someone please explain?
x <- seq(0,40)
y <- dnorm(seq(0,40), mean=25, sd=5)
plot(x, y)
spl <- smooth.spline(y ~ x)
lines(spl, col="green")
newx <- 30
pred0 <- predict(spl, x=newx, deriv=0)
pred1 <- predict(spl, x=newx, deriv=1)
yint <- pred0$y - (pred1$y*newx)
xint <- -yint/pred1$y
xint
plot(x, y)
abline(h=0, col="red")
lines(spl, col="red")
points(pred0,col="red", pch=19)
lines(x, yint + pred1$y*x)
points(xint, 0, col="red", pch=19)
deriv=0
there is no derivative---you get the value of the spline at that point. Withderiv = 1
, you get the value of the first derivative (slope) at that point. – Schiedampredict()
methods let you input anx
and get a value fory
. When you say you want a tangent line wheny = 0.8
, if you want to usepredict
your first step should be to find anx
value corresponding toy = 0.8
. (There might be multiple x values, or no x values, depending on they
). – Schiedam