Calculating a tangent intersection with R
Asked Answered
A

1

7

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) 

enter image description here

Adenaadenauer answered 6/6, 2019 at 18:21 Comment(3)
With deriv=0 there is no derivative---you get the value of the spline at that point. With deriv = 1, you get the value of the first derivative (slope) at that point.Schiedam
Generally, the predict() methods let you input an x and get a value for y. When you say you want a tangent line when y = 0.8, if you want to use predict your first step should be to find an x value corresponding to y = 0.8. (There might be multiple x values, or no x values, depending on the y).Schiedam
@Gregor Thanks for pointing out what derivative actually meant. I still don't know how to address the problem in practical terms. How can I find the x value corresponding to y = 0.08 hence I am trying to find that out using predict. Sry, I am a little confused.Adenaadenauer
R
3

It seems like you have no problem calculating the tangent line and the intersect, but rather need some help in finding the x value for a given y value. This method will work for any smooth curve, but mark Gregors warning. There might not be a corresponding x value, or there might be several.

x <- seq(0, 40, by=0.01)
y <- dnorm(x, mean=25, sd=5)
spl <- smooth.spline(y ~ x)
plot(spl, type="l")

yval <- 0.08
ad <- abs(yval - spl$y)

if (min(ad) > max(diff(spl$y))*10) {
    warning("The supplied y value is out of bounds")
}

xval <- x[which(diff(sign(diff(ad))) > 1) + 1]
points(xval, rep(yval, length(xval)))

With that xval you can calculate the tangent as you've already done.

enter image description here

Rickety answered 6/6, 2019 at 19:5 Comment(3)
thanks for your answer. but for some reason it seems like I am still doing sth wrong. my calculated x value output is numeric(0).Adenaadenauer
Did you notice that the x in x[which(diff(sign(diff(ad))) > 1) + 1] is the same x as in the first line? The code actually finds an index, and then returns the value of x there. Other than that, inspect the result line by line, where does things go wrong?Rickety
Ah, sorry. it worked. it was just a silly mistake :) Very thanks! you all helped me a lot :)Adenaadenauer

© 2022 - 2024 — McMap. All rights reserved.