Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to calculate rolling average of past 1 month in R

Most packages and posts I found apply mean to a fixed size window or the aggregate month/week data. Is it possible to calculate rolling k month average?

For example, for 1 month rolling window, assuming the data is:

Date          Value
2012-05-28    101
2012-05-25     99
2012-05-24    102
....
2012-04-30     78
2012-04-27     82
2012-04-26     77
2012-04-25     75
2012-04-24     76

The first three rolling 1 month windows should be:

1. 2012-05-28 to 2012-04-30
2. 2012-05-25 to 2012-04-26
3. 2012-05-24 to 2012-04-25

Please note that this is NOT the fixed width rolling window. The window actually changes on the daily basis.

like image 665
Alpha Avatar asked May 29 '12 16:05

Alpha


People also ask

How do you find rolling average in R?

Calculating rolling averages To calculate a simple moving average (over 7 days), we can use the rollmean() function from the zoo package. This function takes a k , which is an 'integer width of the rolling window. The code below calculates a 3, 5, 7, 15, and 21-day rolling average for the deaths from COVID in the US.

What is a rolling 30 day average?

30-day rolling average means the arithmetic average of all valid hourly NOx emission rates of the previous 720 valid hours on a rolling basis. 30-day rolling average means the average daily emission rate or concentration during the preceding 30 days.

How do you calculate rolling months?

The 12-month rolling sum is the total amount from the past 12 months. As the 12-month period “rolls” forward each month, the amount from the latest month is added and the one-year-old amount is subtracted. The result is a 12-month sum that has rolled forward to the new month.


1 Answers

I used this code to calculate monthly averages based on daily price data.

#function for extracting month is in the lubridate package
install.packages(c("plyr", "lubridate"))
require(plyr); require(lubridate)

#read the daily data
daily = read.csv("daily_lumber_prices.csv")
price = daily$Open
date = daily$Date

#convert date to a usable format
date = strptime(date, "%d-%b-%y")
mon = month(date)
T = length(price)

#need to know when months change
change_month = rep(0,T)

for(t in 2:T){
  if(mon[t] != mon[t-1]){
    change_month[t-1] = 1
  }
}

month_avg = rep(0,T)
total = 0
days = 0

for(t in 1:T){
  if(change_month[t] == 0){
    #cumulative sums for each variable
    total = total + price[t] 
    days = days + 1
  }

  else{
    #need to include the current month in the calculation
    month_avg[t] = (total + price[t]) / (days + 1)
    #reset the variables
    total = 0
    days = 0
  }
}

So, the variable month_avg is storing the monthly averages.

Is it something like this? This code accounts for the variable lengths of months. There's certainly a more efficient way to do it, but this works!

like image 113
wcampbell Avatar answered Oct 13 '22 01:10

wcampbell