9
$\begingroup$

How do I assign more weight to more recent observations in R?

I assume this as a commonly asked question or desire but I have a hard time figuring out exactly how to implement this. I have tried to search alot for this but I am unable to find a good practical example.

In my example I would have a large dataset over time. I want to say apply some sort of exponential weighting of the rows of data that are more recent. So I would have some sort of exponential function saying observations in 2015 are ___ more important to training the model than observations in 2012.

My dataset variables contain a mix of categorical and numerical values and my target is a numerical value - if that matters.

I would want to test/try this out using models such as GBM/Random Forest, ideally in the CARET package.

update-question

I appreciate response given below on how to exponentially decay the weight by the date distance between two points.

However, when it comes to training this model in caret, how exactly do the weights factor in? The weight value in each of the training rows is the distance between some point in the future and when that point historically occured.

Do the weights come into play only during the prediction? Because if they come into play during the training, wouldn't that cause all sorts of problems as various cross-folds would have varying weights, trying to predict something that may have actually at a point in time before it?

$\endgroup$

migrated from stackoverflow.com Feb 15 '16 at 11:50

This question came from our site for professional and enthusiast programmers.

  • 3
    $\begingroup$ Closevote because the questioner needs to clarify the statistical issues. It's not clear to me that GBN or RF are appropriate here. Suggesting this be migrated to CV.com $\endgroup$ – DWin Feb 10 '16 at 8:04
  • $\begingroup$ ok. I will add an example soon. I just see this sort of question all over the internet, but no concrete examples of how to apply/solve it. $\endgroup$ – user3788557 Feb 12 '16 at 19:28
5
$\begingroup$

How do I assign more weight to more recent observations in R?

I guess you have a timestamp associated with each observation. You can compute a variable timeElapsed = modelingTime - observationTime. Now you apply a simple exponential function as W=K*exp(-timeElapsed/T), where K is a scaling constant and T is the time-constant for the decay function. W works as case-weight.

To the best of my knowledge, many function in caret allow weight as a parameter, which is a column of case-weights to be provided to corresponding observation(thus having same length as #rows).

$\endgroup$
  • 1
    $\begingroup$ The function in caret::train is weights. $\endgroup$ – timcdlucas Feb 15 '16 at 12:50
  • $\begingroup$ Thank you Ujjwal - but what is the methodology in determining 'K' in your equation? any guidelines or best practices? Also, what do you mean by the 'time-period' for the decay function? $\endgroup$ – user3788557 Feb 18 '16 at 4:55
  • $\begingroup$ K is just a scaling constant and it won't affect the model results much. You can set to some value so that the range of case-weight values is maybe near to the range 0-1. Regarding "time-period", it is also called the the time-constant for a 1st order exponential decay function OR mean-lifetime. You can look it up on wikipedia. $\endgroup$ – Ujjwal Kumar Feb 18 '16 at 6:11
  • $\begingroup$ Please see my update to my post. Will this work correctly during the training? Will the training model have bias as the Weights can only be applied when used against the test set? When the training set is randomly shuffled, it could be bad if higher weights are used to predict prices in the past or against a time period that is not nearby in time. $\endgroup$ – user3788557 Feb 22 '16 at 9:19
  • $\begingroup$ Without case-weights, your model training would give equal importance to both old and new data but with proposed case-weights, it'll give more importance to newer data, so in that sense, it is biased towards newer observations, but that is what you wanted. I don't understand why "weights can only be allowed for test cases". Also, how higher weights would be used for older values when training set is randomly shuffled? when modelingTime is kept same for all training cases. P.S. case-weights don't apply when just using a model, it's only applicable for training period. $\endgroup$ – Ujjwal Kumar Feb 23 '16 at 6:35
0
$\begingroup$

The data (not the analyst making assumptions - guesses) can often suggest the form of the weighting scheme. This is done via GLS where the appropriate weights for the weighted least squares model are obtained from the statistically significant differences found in the error variance. Take a look at Change and outliers detection by means ARIMA (Tsay procedure) and here http://docplayer.net/12080848-Outliers-level-shifts-and-variance-changes-in-time-series.html . If you wish to post data please do so here and I will try and help further as I have routine access to software that might enlighten you.

There is an R version of the software I am using.

You might look at How to include control variables in an Intervention analysis with ARIMA? as it has example of how the weights are identified and used to stabilize the error variance thus effectively believing/dis-believing/discounting/weighting/trusting certain prior values.

$\endgroup$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.