Calculate curvature from a smooth layer in R

Is there a way to calculate the curvature at a particular point having a smooth curve (or similar) in R? The curve is calculated by the set of points x, y.

Thanks in advance.

+4
source share
1 answer

In fact, it is very simple if you know that there is a method predict()for objects created smooth.spline(), and that this method has an argument derivthat allows you to predict this derivative (in your case, the second derivative is required) instead of points on the spline.

 cars.spl <- with(cars, smooth.spline(speed, dist, df = 3))
 with(cars, predict(cars.spl, x = speed, deriv = 2))

What gives:

$x
 [1]  4  4  7  7  8  9 10 10 10 11 11 12 12 12 12 13 13 13 13 14 14 14 14 15 15
[26] 15 16 16 17 17 17 18 18 18 18 19 19 19 20 20 20 20 20 22 23 24 24 24 24 25

$y
 [1] -6.492030e-05 -6.492030e-05  3.889944e-02  3.889944e-02  5.460044e-02
 [6]  7.142609e-02  6.944645e-02  6.944645e-02  6.944645e-02  9.273343e-02
[11]  9.273343e-02  1.034153e-01  1.034153e-01  1.034153e-01  1.034153e-01
[16]  5.057841e-02  5.057841e-02  5.057841e-02  5.057841e-02  1.920888e-02
[21]  1.920888e-02  1.920888e-02  1.920888e-02  1.111307e-01  1.111307e-01
[26]  1.111307e-01  1.616749e-01  1.616749e-01  1.801385e-01  1.801385e-01
[31]  1.801385e-01  1.550027e-01  1.550027e-01  1.550027e-01  1.550027e-01
[36]  2.409237e-01  2.409237e-01  2.409237e-01  2.897166e-01  2.897166e-01
[41]  2.897166e-01  2.897166e-01  2.897166e-01  1.752232e-01  1.095682e-01
[46] -1.855994e-03 -1.855994e-03 -1.855994e-03 -1.855994e-03  4.478382e-05

$y - , speed , . , , , , 100 , speed. :

newspeed <- with(cars, seq(min(speed), max(speed), length = 100))
curvature <- predict(cars.spl, x = newspeed, deriv = 2)

plot(curvature, type = "l")
+6

All Articles