[R] Calculate a function repeatedly over sections of a ts object
R. Michael Weylandt
michael.weylandt at gmail.com
Mon Jan 30 05:29:09 CET 2012
It's customary to keep the list cc'd.
I can't run your code without the data, but it does seem to me that
your problem is in the FUN argument, as you guess.
You have:
FUN=function(z) SDF(adezoo,method="lag window",
window=taper(type="parzen",n.sample=n.d,cutoff=(2*sqrt(n.d))),
npad=2*n.d)
But this function doesn't actually act on it's argument: you tell it
to accept something called "z" but then it never gets told to do
anything to "z". Perhaps you meant
FUN=function(z) SDF(z,method="lag window",
window=taper(type="parzen",n.sample=n.d,cutoff=(2*sqrt(n.d))),
npad=2*n.d)
I also worry about your use of "n.d"; are you sure you don't want to
use the length of the rolling window? Something more like:
FUN=function(z){
lz <- length(z)
SDF(z,method="lag window",
window=taper(type="parzen",n.sample=lz,cutoff= 2*sqrt(lz)),
npad=2*nlz)
}
Does that fix it?
Michael
On Fri, Jan 27, 2012 at 1:06 PM, Jorge Molinos <JGARCIAM at tcd.ie> wrote:
> Hi Michael,
>
> Sorry, I've been trying to use rollapply with my function but it seems I can't get it to work properly. The function seems to be dividing the time series accordingly (every 1) and using the correct length for the time window (10 years) but when I look at the results all of them are the same for all the subseries which doesn't make sense. The problem has to be within the FUN argument though I cannot figure out what it is. Would you mind checking on the code to see if you can spot where is the problem?
>
> adets<-ts(adeery$DA,c(adeery$Year[1],adeery$Day[1]),frequency=365)
>
> adezoo<-as.zoo(adets)
>
> n.d<-length(adets)
>
> especlist<-rollapply(adezoo, width=3650, FUN=function(z) SDF(adezoo,method="lag window",
> window=taper(type="parzen",n.sample=n.d,cutoff=(2*sqrt(n.d))),
> npad=2*n.d), by = 365, align="left")
>
>
> And these are, for example, the SDF values at the last day for each 10-y subseries (all the same though they should be different as I have it verify by doing the SDF step by step using the same values for the arguments within the function):
>
> especlist1.7048
> 1978(20) 1.998068e-06
> 1979(20) 1.998068e-06
> 1980(20) 1.998068e-06
> 1981(20) 1.998068e-06
> 1982(20) 1.998068e-06
> 1983(20) 1.998068e-06
> 1984(20) 1.998068e-06
> 1985(20) 1.998068e-06
> 1986(20) 1.998068e-06
> 1987(20) 1.998068e-06
>
> Thanks a lot.
>
> Jorge
>
>
> ________________________________________
> From: R. Michael Weylandt [michael.weylandt at gmail.com]
> Sent: 26 January 2012 21:00
> To: Jorge Molinos
> Cc: r-help at R-project.org
> Subject: Re: [R] Calculate a function repeatedly over sections of a ts object
>
> I'm not sure if it's easily doable with a ts class, but the rollapply
> function in the zoo package will do this easily. (Also, I find zoo to
> be a much more natural time-series workflow than ts so it might make
> the rest of your life easier as well)
>
> Michael
>
> On Thu, Jan 26, 2012 at 2:24 PM, Jorge Molinos <JGARCIAM at tcd.ie> wrote:
>>
>> Hi,
>>
>> I want to apply a function (in my case SDF; package “sapa”) repeatedly over discrete sections of a daily time series object by sliding a time window of constant length (e.g. 10 consecutive years or 1825 days) over the entire ts at increments of 1 time unit (e.g. 1 year or 365 days). So for example, the first SDF would be calculated for the daily values of my variable recorded between years 1 to 5, SDF2 to those for years 2 to 6 and so on until the total length of the series is covered. How can I implement this into a R script? Any help is much appreciated.
>>
>> Jorge
>> ______________________________________________
>> R-help at r-project.org mailing list
>> https://stat.ethz.ch/mailman/listinfo/r-help
>> PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
>> and provide commented, minimal, self-contained, reproducible code.
More information about the R-help
mailing list