Calculate the latest Z-score of all zoo time series

classic Classic list List threaded Threaded
3 messages Options
Reply | Threaded
Open this post in threaded view
|

Calculate the latest Z-score of all zoo time series

thierrydb
Hello,

I have a population of 2000+ zoo time series (but my environment also contains objects that are not zoo time series). I'm trying to calculate the latest 90 days Z-Score of all zoo time series, using the following code:


LZS<-function(ser) {
temp<-window(ser,start=Sys.Date()-90)
last((temp-mean(temp))/sd(temp))
}

sapply(ls(), LZS )


The LZS function works on individual zoo time series, but not when I try to use sapply to do it on the whole objects list. I guess this has to do with the fact that not all objects are zoo. How can I do this correctly?


Many thanks,


TDB

Reply | Threaded
Open this post in threaded view
|

Re: Calculate the latest Z-score of all zoo time series

krisspnet
This post was updated on .
The ls() function return the names of the objects not the objects.

So if you have :

a <- c(1,2,3)
b <- c('A','B','C')

ls() will return :

"a" "b" which are only 2 letters.
Christophe Poulet.
GIGA-Research.
Unit of Human Genetics.
Li├Ęge, Belgium.
Reply | Threaded
Open this post in threaded view
|

Re: Calculate the latest Z-score of all zoo time series

Gabor Grothendieck
In reply to this post by thierrydb
On Tue, Jul 26, 2011 at 9:48 AM, thierrydb <[hidden email]> wrote:

> Hello,
>
> I have a population of 2000+ zoo time series (but my environment also
> contains objects that are not zoo time series). I'm trying to calculate the
> latest 90 days Z-Score of all zoo time series, using the following code:
>
>
> LZS<-function(ser) {
> temp<-window(ser,start=Sys.Date()-90)
> last((temp-mean(temp))/sd(temp))
> }
>
> sapply(ls(), LZS )
>
>
> The LZS function works on individual zoo time series, but not when I try to
> use sapply to do it on the whole objects list. I guess this has to do with
> the fact that not all objects are zoo. How can I do this correctly?
>

LZS expects a series but the above code is passing it the name of the
series rather than the series itself.  Try replacing the line
referencing ser with the following which gets the series whose name is
held in ser and applies window to that:

temp<-window(get(ser),start=Sys.Date()-90)

or else pass it the series rather than its name:

lapply(lapply(ls(pattern = "zz.*"), get), LZS)



--
Statistics & Software Consulting
GKX Group, GKX Associates Inc.
tel: 1-877-GKX-GROUP
email: ggrothendieck at gmail.com

______________________________________________
[hidden email] 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.