[R-SIG-Finance] hourly time series
R. Michael Weylandt
michael.weylandt at gmail.com
Thu Mar 22 15:00:05 CET 2012
Well, simply, your problem is that the thing are providing is not
cut()-able in that no specific cut() method is provided. If you want
to cut on the start date, you can simply use cut(test$Start) or, for
the end date, cut(test$End). Though there may be better extractor
functions available....for the other stuff, you might be able to get
some help from the lubridate package.
And this won't work for ts objects because they don't store their
timestamps the same what at all.
Michael
On Thu, Mar 22, 2012 at 4:13 AM, Advait Godbole <advaitgodbole at gmail.com> wrote:
> thanks Michael, that worked.
>
> I now want to perform monthly and weekly decomposition. In a one-on-one
> conversation with fellow member Vlad, I was suggested to do the following.
> I'm posting it here for the forum and for other pointers.
>
> # define hourly data with monthly frequency
> wind.ts.monthbase <- ts(wind.MH, start=1, frequency=8760/12) #wind.MH is
> 8760 x 1 vector
> wind.ts.monthbase.decompose <- decompose(wind.ts.monthbase)
>
> # define hourly data with weekly frequency
> wind.ts.weekbase <- ts(wind.MH,start=1, frequency=8760/52.14286)
> #52.14286=8760/168
> wind.ts.weekbase.decompose <- decompose(wind.ts.weekbase)
>
> Vlad also suggested I could try using the "timetools" package to better
> preserve the time properties. Following on his lead, I was able to create a
> test time series:
>
> #use of timetools package
> # create an hourly time series starting on 1st April 2010, ending on 31st
> March 2011
> test <- RegularTimeIntervalDataFrame('2010-04-01','2011-03-31
> 23:00','hour',timezone="GMT",data=data.frame(test=1:8760))
>
> If anyone is familiar with this package, I would like to know how to
> extract
> 1. a particular month/group of months
> 2. the first day/first week etc of each month
> 3. all mondays, all tuesdays etc of the year
>
> I tried doing the above using the "cut" function with the following errors:
>> wind.ttools.weekly.data <- cut(test, breaks="week", start.on.monday=TRUE)
> Error in cut.default(test, breaks = "week", start.on.monday = TRUE) :
> 'x' must be numeric
>> wind.ttools.weekly.data <- cut(as.numeric(test), breaks="week",
>> start.on.monday=TRUE)
> Error in as.numeric(test) :
> cannot coerce type 'S4' to vector of type 'double'
>
> I havent yet been able to identify the cause of this issue.
>
> It will also be good to know if the same extraction can be performed on the
> "ts" object. Thanks in advance for your responses.
>
> Regards,
> Advait
>
> On Wed, Mar 14, 2012 at 6:41 PM, R. Michael Weylandt
> <michael.weylandt at gmail.com> wrote:
>>
>> I think you are interpreting your frequency argument incorrectly and
>> thus R is giving you an error message because you are asking for
>> seasonal trends but giving only one observation per "season" -- it's
>> rather subtle: as the help page says,
>>
>> frequency -- the number of observations per unit of time.
>>
>> Note that this works,
>>
>> wind_ts <- ts(cumsum(rnorm(8760)), start = 1, frequency = 24) # E.g.,
>> the relevant time scale is "daily" and you have 24 obs per day
>> (hourly)
>> decompose(wind_ts)
>>
>> This would be for a phenomenon that reoccurs daily: I'm not sure if a
>> single monsoon season (I think there's only one a year but I don't
>> really know Indian climatology so well) will give you sufficient power
>> for a statistically significant result, but you can cross that bridge
>> when you get to it.
>>
>> Hope this helps,
>>
>> Michael
>>
>> PS -- The ts class is generally considered a necessary evil around
>> here because of things like that ;-)
>>
>> On Wed, Mar 14, 2012 at 5:45 AM, Advait Godbole <advaitgodbole at gmail.com>
>> wrote:
>> > Dear all,
>> >
>> > I am new to this list and I first posted this query on the r-sig-geo
>> > forum,
>> > apologies for the cross-post. My question is not related to finance,
>> > however I am sure many users here will have experience with time series
>> > analysis and would be able to provide insight.
>> >
>> > I have one year's worth of hourly data, starting from 1st April 2010 and
>> > ending on 31st March 2011. I would like to perform time series analysis
>> > on
>> > it. New to time series analysis and not having used "ts" before, I am
>> > having trouble setting it up to correctly represent the data. I have R
>> > reading in the time series via:
>> > *wind_ts <- ts(wind.MH,start=1,frequency=1)*
>> >
>> > where "wind.MH" is a 8760x1 matrix object. I then tried to decompose the
>> > time series with the following error:
>> > *wind_ts_components <- decompose(wind_ts)*
>> > *Error in decompose(wind_ts) : time series has no or less than 2
>> > periods*
>> >
>> > The dataset is the hourly wind generation for Maharashtra, India and has
>> > some seasonality associated with the Indian monsoon. Ultimately, this is
>> > what I would like to identify. I imagine that correctly setting the
>> > "start"
>> > and "frequency" parameters is necessary to be able to parse the dataset
>> > into months and seasons. Someone suggested I use the "timetools"
>> > package
>> > instead but I havent had the chance to do so.
>> >
>> > I would greatly appreciate help on how to handle this and any leads on
>> > time
>> > series analysis for hourly data in general.
>> >
>> > Regards,
>> >
>> > --
>> > advait godbole
>> > analyst, prayas energy group
>> > pune, india
>> >
>> > [[alternative HTML version deleted]]
>> >
>> > _______________________________________________
>> > R-SIG-Finance at r-project.org mailing list
>> > https://stat.ethz.ch/mailman/listinfo/r-sig-finance
>> > -- Subscriber-posting only. If you want to post, subscribe first.
>> > -- Also note that this is not the r-help list where general R questions
>> > should go.
>
>
>
>
> --
> advait godbole
More information about the R-SIG-Finance
mailing list