I am trying to use a monthly data to calculate the quarter average, suppose now I randomly generate 10000 month, and try to use this to get the quarterly average by using the loop:
T = 10000
a=rand(Int,T) # monthly data
u=zeros(Int64((p.T - p.T %3) /3)) # indicate size of quartly data
for i ∈ 1:3:(T - T %3)
u[i]=mean(a[i:i+3])
end
I understand that the problem is the i, since the size for u and a is different. But I don’t know how tofix this
You’re dropping the last value (at index 10000). We’ve got 3334 quarters. Assuming that the last quarter contained only one month we could do:
T = 10000
a = rand(Int, T)
u = zeros(ceil(Int, T/3))
a1 = [a; fill(last(a), 3-T%3)] # complete the last, partial quarter with the last value
for (j, i) ∈ enumerate(1:3:T)
u[j] = mean(a1[i:i+2])
end
Working with date and time can be messy. How about a missing month? How about our data start in the middle of a quarter? I would really make proper time stamps of type Date and handle the quarters with the Dates STDLIB. Also, TimeSeries.jl offers a collapse function that can handle quarterly data (see Combine methods · TimeSeries.jl).
Here an example with TimeSeries.jl. The data may start and end with partial quarters:
using Dates, Statistics, TimeSeries
T = 10000
d = firstdayofmonth(today())
timestamps = d-Month(T-1):Month(1):d # range with the latest 10000 months
ta = TimeArray(timestamps, rand(Int, T))
u = collapse(ta, Dates.quarter, first, mean)