通过R函数'smooth.spline'确定拟合的平滑样条曲线的所有局部极值 [英] Identify all local extrema of a fitted smoothing spline via R function 'smooth.spline'

查看:1018
本文介绍了通过R函数'smooth.spline'确定拟合的平滑样条曲线的所有局部极值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个二维数据集.

按照本文中的示例,我使用R的smooth.spline函数来平滑我的点图:

I use the R's smooth.spline function to smooth my points graph following an example in this article:

https://stat.ethz.ch/R-manual/R-devel/library/stats/html/predict.smooth.spline.html

这样我得到的样条图类似于这张照片上的绿线

So that I get the spline graph similar to the green line on this picture

我想知道X值,其中平滑样条曲线的一阶导数等于零(以确定确切的最小值或最大值).

I'd like to know the X values, where the first derivative of the smoothing spline equals zero (to determine exact minimum or maximum).

我的问题是,要馈入predict()函数的初始数据集(或我可以自动生成的数据集)不包含与平滑样条极值相对应的精确X值.

My problem is that my initial dataset (or a dataset that I could auto-generate) to feed into the predict() function does not contain such exact X values that correspond to the smoothing spline extrema.

如何找到这样的X值?

这是上面绿色样条线的一阶导数的图片

Here is the picture of the first derivative of the green spline line above

但是极值的精确X坐标仍然不精确.

But exact X coordinate of extremums are still not exact.

我用来生成图片的近似R脚本如下所示

My approximate R script to generate the pictures looks like the following

sp1 <- smooth.spline(df)

pred.prime <- predict(sp1, deriv=1)
pred.second <- predict(sp1, deriv=2)

d1 <- data.frame(pred.prime)
d2 <- data.frame(pred.second)

dfMinimums <- d1[abs(d1$y) < 1e-4, c('x','y')]

推荐答案

我认为这里有两个问题.

I think that there are two problems here.

  1. 您使用的是原始x值,它们之间的距离太远且
  2. 由于x的间距较大,因此您认为导数足够接近"到零的阈值太高.

基本上,这是您的代码,但具有更多x值,并且需要较小的导数.由于您没有提供任何数据,因此我对它进行了粗略的估算,足以说明问题.

Here is basically your code but with many more x values and requiring smaller derivatives. Since you do not provide any data, I made a coarse approximation to it that should suffice for illustration.

## Coarse approximation of your data
x = runif(300, 0,45000)
y = sin(x/5000) + sin(x/950)/4 + rnorm(300, 0,0.05) 
df = data.frame(x,y)
sp1 <- smooth.spline(df)

样条代码

Sx = seq(0,45000,10)
pred.spline <- predict(sp1, Sx)
d0 <- data.frame(pred.spline)
pred.prime <- predict(sp1, Sx, deriv=1)
d1 <- data.frame(pred.prime)

Mins = which(abs(d1$y) < mean(abs(d1$y))/150)

plot(df, pch=20, col="navy")
lines(sp1, col="darkgreen")
points(d0[Mins,], pch=20, col="red")

极好看.

plot(d1, type="l")
points(d1[Mins,], pch=20, col="red")

识别出的点看起来像导数的零.

The points identified look like zeros of the derivative.

这篇关于通过R函数'smooth.spline'确定拟合的平滑样条曲线的所有局部极值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆