lag operator on a zoo object - code sharing

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

lag operator on a zoo object - code sharing

miao
Hi,
   I could not find a nice lag operator on zoo object. Perhaps there is,
but I just couldn't find it. Basically I want the operator to return the
lagged zoo object (with one or more variables ) with the original date. For
example, if I write lag(x, -3), then I got the lagged series, but the first
three observations are deleted. My code could work, but is not polished.
Someone helps or comments?


lagzoo<-function(x, lag_n)
{

  if(is.zoo(x)==FALSE)
  {
    stop("zoo objects for lagzoo, please")
  }
  if(ncol(x)==1)
  {
    y<-x
  t<-time(x)
  n<-length(t)

  y[(lag_n+1):n]<-x[1:(n-lag_n)]
  y[1:lag_n]<-NA
  return(y)
  }
  else
  {
    y<-x
    n<-nrow(x)
    y[(lag_n+1):n,]<-x[1:(n-lag_n),]
    y[1:lag_n,]<-NA
    return(y)
  }
}

        [[alternative HTML version deleted]]

______________________________________________
[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.
Reply | Threaded
Open this post in threaded view
|

Re: lag operator on a zoo object - code sharing

Achim Zeileis-4
On Wed, 15 Oct 2014, jpm miao wrote:

> Hi,
>   I could not find a nice lag operator on zoo object. Perhaps there is,
> but I just couldn't find it.

See ?lag.zoo.

> Basically I want the operator to return the
> lagged zoo object (with one or more variables ) with the original date. For
> example, if I write lag(x, -3), then I got the lagged series, but the first
> three observations are deleted.

Set na.pad = TRUE.

> My code could work, but is not polished.

Yes, the ncol() does not work on vectors without dim and leads cannot be
computed.

> Someone helps or comments?
>
>
> lagzoo<-function(x, lag_n)
> {
>
>  if(is.zoo(x)==FALSE)
>  {
>    stop("zoo objects for lagzoo, please")
>  }
>  if(ncol(x)==1)
>  {
>    y<-x
>  t<-time(x)
>  n<-length(t)
>
>  y[(lag_n+1):n]<-x[1:(n-lag_n)]
>  y[1:lag_n]<-NA
>  return(y)
>  }
>  else
>  {
>    y<-x
>    n<-nrow(x)
>    y[(lag_n+1):n,]<-x[1:(n-lag_n),]
>    y[1:lag_n,]<-NA
>    return(y)
>  }
> }
>
> [[alternative HTML version deleted]]
>
> ______________________________________________
> [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.
>

______________________________________________
[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.
Reply | Threaded
Open this post in threaded view
|

Re: lag operator on a zoo object - code sharing

Pascal Oettli-2
In reply to this post by miao
Hi,

You probably missed the "na.pad" argument of the "lag" function (for
zoo objects).
?zoo:::lag.zoo

Regards,
Pascal

On Wed, Oct 15, 2014 at 6:00 PM, jpm miao <[hidden email]> wrote:

> Hi,
>    I could not find a nice lag operator on zoo object. Perhaps there is,
> but I just couldn't find it. Basically I want the operator to return the
> lagged zoo object (with one or more variables ) with the original date. For
> example, if I write lag(x, -3), then I got the lagged series, but the first
> three observations are deleted. My code could work, but is not polished.
> Someone helps or comments?
>
>
> lagzoo<-function(x, lag_n)
> {
>
>   if(is.zoo(x)==FALSE)
>   {
>     stop("zoo objects for lagzoo, please")
>   }
>   if(ncol(x)==1)
>   {
>     y<-x
>   t<-time(x)
>   n<-length(t)
>
>   y[(lag_n+1):n]<-x[1:(n-lag_n)]
>   y[1:lag_n]<-NA
>   return(y)
>   }
>   else
>   {
>     y<-x
>     n<-nrow(x)
>     y[(lag_n+1):n,]<-x[1:(n-lag_n),]
>     y[1:lag_n,]<-NA
>     return(y)
>   }
> }
>
>         [[alternative HTML version deleted]]
>
> ______________________________________________
> [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.



--
Pascal Oettli
Project Scientist
JAMSTEC
Yokohama, Japan

______________________________________________
[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.