简体   繁体   English

R中沿“S”曲线的加权点

[英]Weighting Points along an 'S' curve in R

I have data in an R data frame and want to weight one column ( days_since ) along an S-curve (logit curve).我在 R 数据框中有数据,并希望沿 S 曲线(logit 曲线)加权一列( days_since )。 Particularly, a backwards S-curve such that more recent events will be weighted more.特别是向后的 S 曲线,这样最近的事件将被赋予更多的权重。 My reasoning for using this curve as a weighting function is that it will heavily weight recent events as well as lightly weight distant events.我使用这条曲线作为加权函数的理由是,它将对最近的事件进行加权,对遥远的事件进行加权。 Thus, I would like to write a function that for a give number of observations I could create an S-curve that provides the weights, such that as in the image below, the weights would be on the y-axis and the days since would be on the x-axis.因此,我想编写一个函数,对于给定数量的观察,我可以创建一个提供权重的 S 曲线,如下图所示,权重将位于 y 轴上,此后的天数将位于 x 轴上。

[Please ignore the points and labels on this graph, I was able to use the model of a reverse s-curve/logit function from another stackoverflow question]. [请忽略此图上的点和标签,我能够使用另一个 stackoverflow 问题中的反向 s 曲线/logit 函数模型]。

在此处输入图像描述

This builds upon an answer provided to a similar question here .这建立在对此处类似问题的回答之上。

You can create the curve using a formula of your own or the sigmoid function from the e1071 package, and the curve() function.您可以使用自己的公式或e1071包中的 sigmoid 函数和curve()函数来创建曲线。 Then, predict the values along the curve for given days in your dataset.然后,预测数据集中给定日期的曲线值。

Here's a simple example.这是一个简单的例子。

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() function will create x and y coordinates for your S-curve. curve()函数将为您的 S 曲线创建 x 和 y 坐标。

p <- invisible(curve(-sigmoid(x) , -6,6))

Rescale x and y to ensure the range contains your data values of interest.重新调整 x 和 y 以确保范围包含您感兴趣的数据值。 You will want to be able to predict y from x later.您将希望以后能够从 x 预测 y。 Rescaling y to upper and lower bounds will set upper and lower thresholds for your weights.将 y 重新缩放到上限和下限将为您的权重设置上限和下限。

x <- rescale(p$x, to = c(1, 365))
y <- rescale(p$y, to = c(0,1))

Next, predict the values on the curve for your data of interest.接下来,为您感兴趣的数据预测曲线上的值。 In this example, days go from 1 to 365. You'll feed the loess() function to predict() .在此示例中,天数从 1 到 365。您将loess()函数提供给predict() The loess() function will fit the curve surface (setting span to 0.1 will minimize any smoothing). loess()函数将拟合曲线表面(将跨度设置为 0.1 将最小化任何平滑)。

days$pred <- predict( loess( y ~ x, span=.1 ), days$d ) 

plot(days$d,days$pred)

Your weights are now in days$pred.您的体重现在以天 $pred 为单位。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM