这建立在answer https://stackoverflow.com/a/41660145/3019627提供给类似的问题here https://stackoverflow.com/questions/41659903/retrieve-x-and-y-value-based-on-graph-in-r.
您可以使用自己的公式或 sigmoid 函数来创建曲线e1071
包,以及curve()
功能。然后,预测数据集中给定日期沿曲线的值。
这是一个简单的例子。
library(scales)
library(e1071)
# Data set with points of interest. Suppose these are days.
days <- data.frame( d = seq.int( 1, 365, 1 ) )
The curve()
函数将为您的 S 曲线创建 x 和 y 坐标。
p <- invisible(curve(-sigmoid(x) , -6,6))
重新缩放 x 和 y 以确保范围包含您感兴趣的数据值。您稍后将希望能够根据 x 预测 y。将 y 重新调整为上限和下限将为您的权重设置上限和下限。
x <- rescale(p$x, to = c(1, 365))
y <- rescale(p$y, to = c(0,1))
接下来,预测曲线上您感兴趣的数据的值。在此示例中,天数从 1 到 365。您将喂养loess()
功能为predict()
. The loess()
函数将拟合曲线表面(将跨度设置为 0.1 将最大限度地减少平滑)。
days$pred <- predict( loess( y ~ x, span=.1 ), days$d )
plot(days$d,days$pred)
您的体重现在以天$pred 为单位。