简体   繁体   中英

remove trend from data in R

I have two data sets, one of which shows seasonality while the other shows a trend.

I have removed seasonality from the first data set but I am not able to remove trend from the other data set.

Also, if I remove trend from the other data set and then try to make a data frame of both the altered data sets, then the number of rows will be different for both the data sets (because I have removed seasonality from the first data set using lag, so there is a difference of 52 values in the two data sets).

How do I go about it?

For de-trending a time series, you have several options, but the most commonly used one is HP filter from the " mFilter " package:

    a <- hpfilter(x,freq=270400,type="lambda",drift=FALSE)

The frequency is for the weekly nature of the data, and drift=FALSE sets no intercept. The function calculates the cyclical and trend components and gives them to you separately.

If the time indices for both your series are the same (ie weekly), you could use the following, where x and y are your dataframes:

    final <- merge(x,y,by=index(a),all=FALSE)

You can always set all.x=TRUE (all.y=TRUE) to see which rows of x (y) have no matching output in y (x). Look at the documentation for merge here .

Hope this helps.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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