[R-SIG-Finance] disaggregate from monthly to daily time series
Joshua Ulrich
josh.m.ulrich at gmail.com
Tue Jul 28 00:37:12 CEST 2009
On Mon, Jul 27, 2009 at 5:10 PM, John P. Burkett<burkett at uri.edu> wrote:
> Joshua Ulrich wrote:
>
>> Here's some code that follows Brian's suggestion.
>
> Thank you, Brian and Joshua, for your suggestions. Having tried the code on
> a Ubuntu box running R 2.8.1, I'm inserting my comments below the relevant
> lines.
I would suggest you upgrade to R-2.9.1 and the latest version of xts.
>>
>> library(xts)
>> x <- xts(cumprod(1+rnorm(999)/100),Sys.Date()-999:1)
>> m <- timeBasedSeq('2006-11/2009-07', 'Date')
>
> This line elicits the message "Error in strftime(x) : wrong class"
> However the line seems to work o.k. if shortened to
> m <- timeBasedSeq('2006-11/2009-07')
>
>> mx <- x[m]
>
This won't work because timeBasedSeq will return zoo's "yearmon" class
by default in this case, and you can't subset an xts object via "mmm
yyyy" style strings. The original code returned a Date class object,
which you can then use to extract the first values of the month from
the xts object "x".
> This line generates 50 or more warnings of the form
> "In timeBasedSeq(x, NULL) : NAs introduced by coercion"
> Are those the NAs Brian mentioned?
They are not. This line throws an error on R-2.9.1 with the latest xts.
> mx <- x[m]
Error in `[.xts`(x, m) : subscript out of bounds
> dim(mx) is 0, 1. Doing "mx" gets the following response:
> Data:
> numeric(0)
> Index:
> integer(0)
> Is that what was intended?
>
No, it's an empty xts object - the result of the warnings above.
>> M <- merge(mx,x)
>>
>> mtod.spline <- xts(spline(1:NROW(M),M[,1],n=NROW(M))$y,index(x))
>> mtod.linear <- xts(approx(1:NROW(M),M[,1],n=NROW(M))$y,index(x))
>> mtod <- merge(x,mtod.spline,mtod.linear)
>> plot.zoo(mtod)
>
> The end result on my box is three seemingly identical series: x,
> mtod.spline, mtod.linear. I'm still thinking about how they relate the
> problem of disaggregating a monthly series into smooth daily series with the
> original mean.
>
The "mx" object is empty, so "M" and "x" _are_ identical. I'm not
familiar with S+Finmetrics and I wasn't aware that's what
disaggregate(..., how="mean") implied. If my example retains the mean
of the original series, it would be completely unintentional.
Best,
Josh
--
http://www.fosstrading.com
>>
>> HTH,
>> Josh
>> --
>> http://www.fosstrading.com
>>
>>
>>
>> On Mon, Jul 27, 2009 at 1:22 PM, Brian G. Peterson<brian at braverock.com>
>> wrote:
>>>
>>> Well, leaving aside the fact that this seems like a pretty low-utility
>>> idea
>>> (ymmv), na.approx or na.spline will do this if you cbind a monthly series
>>> to
>>> a daily series, and then apply na.approx to the NA's in the (formerly)
>>> monthly data.
>>>
>>> - Brian
>>>
>>> John P. Burkett wrote:
>>>>
>>>> I would like to disaggregate a monthly average (Consumer Price Index) to
>>>> create a daily time series. The new daily series should be smooth
>>>> (i.e.--exhibit no unusual jump from the last day of a month to the first
>>>> day
>>>> of the next month) and be consistent with the original monthly data
>>>> (i.e.--the average value of the new series for the days of a month
>>>> should
>>>> equal the given value for that month).
>>>>
>>>> If all months had 30 days and I were using S+Finmetrics, I would try to
>>>> create the daily series with a command such as
>>>> disaggregate(CPI, 30, method="spline", how="mean")
>>>> where CPI is the monthly data on the Consumer Price Index.
>>>> The fact that months are of different lengths complicates matters.
>>>>
>>>> Suggestions for how to accomplish the disaggregation in R would be
>>>> greatly
>>>> appreciated.
>>>>
>>>> -John
>>>>
>>>
>>> --
>>> Brian G. Peterson
>>> http://braverock.com/brian/
>>> Ph: 773-459-4973
>>> IM: bgpbraverock
>>>
>>> _______________________________________________
>>> R-SIG-Finance at stat.math.ethz.ch mailing list
>>> https://stat.ethz.ch/mailman/listinfo/r-sig-finance
>>> -- Subscriber-posting only.
>>> -- If you want to post, subscribe first.
>>>
>>
>
>
> --
> John P. Burkett
> Department of Economics
> University of Rhode Island
> Kingston, RI 02881-0808
> USA
>
> phone (401) 874-9195
>
More information about the R-SIG-Finance
mailing list