5

相关: R: Marking slope changes in LOESS curve using ggplot2
This question is trying to find the min/max y (slope=0); 我想找到最小值/最大值

作为背景,我正在执行一些不同的建模技术,并认为在迭代神经网络结果时,我可能会使用斜率来衡量随机种子产生的最佳模型。

获取数据:

nn <- read.csv("http://pastebin.com/raw.php?i=6SSCb3QR", header=T)
rbf <- read.csv("http://pastebin.com/raw.php?i=hfmY1g46", header=T)

例如,以下是经过训练的神经网络对我的数据的结果:

library(ggplot2)
ggplot(nn, aes(x=x, y=y, colour=factor(group))) + 
geom_point() + stat_smooth(method="loess", se=F)

nn

同样,这是一个 rbf 模型:

ggplot(rbf, aes(x=x, y=y, colour=factor(group))) + 
geom_point() + stat_smooth(method="loess", se=F)

rbf

RBF 模型更好地拟合数据,并且与变量的背景知识更好地吻合。我想尝试计算拟合线的最小/最大斜率,以便修剪陡峭悬崖与更平缓曲线的 NN。识别交叉线将是另一种修剪方法,但这是一个不同的问题。

感谢您的任何建议。


注意:ggplot2在这里使用并相应地标记了问题,但这并不意味着它不能用其他一些功能来完成。我只是想直观地说明我为什么要这样做。我想一个循环可以用 y 1 -y 0 /x 1 -x 0做到这一点,但也许有更好的方法。?

4

2 回答 2

4

我认为最简单的解决方案是使用一阶差分(使用 function diff)作为一阶导数的近似值。

slope.loess <-function(X, data){
    # First your loess function:
    my_loess <- loess(y~x, data=data, subset=data$group==X, degree=2)
    # Then the first difference
    first_diff <- diff(my_loess$fitted)
    # Then the corresponding x and y values for the minima and maxima
    res <- cbind(my_loess$x[c(which.min(first_diff), which.max(first_diff))], 
            my_loess$fitted[c(which.min(first_diff), which.max(first_diff))])
    colnames(res) <- c("x", "y")
    rownames(res) <- c("min", "max")
    res
    }

#Then apply the function to each group
slope.rbf <- lapply(levels(rbf$group), FUN=slope.loess, data=rbf)
names(slope.rbf) <- levels(rbf$group)

slope.rbf
$A
           x        y
min 3.310345 20.30981
max 7.724138 18.47787

$B
           x        y
min 3.310345 21.75368
max 7.724138 20.06883

$C
           x        y
min 3.310345 23.53051
max 7.724138 21.47636

$D
           x        y
min 4.413793 25.02747
max 0.000000 26.22230

$E
           x        y
min 4.413793 27.45100
max 0.000000 27.39809
于 2012-09-04T07:52:37.760 回答
2

我正在自己编写一个用于超快速交易的神经网络。一开始我使用 Loess 或 Lowess 来拟合时间序列,但我想要的是 Loess 不提供的平滑导数。甚至,如果您自己实现 Loess 并使用每个点的正交多项式来计算导数,您会得到奇怪的结果。有一个原因。

您可以在 Graciela Boente 的论文中找到解决问题的方法:回归函数的高阶导数的稳健估计。公式在第 3 页。该论文可在互联网上免费获得。一旦你获得了值和导数,你可以使用它来唯一定义三次样条,这将给出连续的导数。

我不熟悉R

于 2014-10-05T21:19:13.670 回答