[R] Time series - year on year growth rate

Berend Hasselman bhh at xs4all.nl
Fri Apr 6 11:59:37 CEST 2012


On 06-04-2012, at 10:27, jpm miao wrote:

> Hello,
> 
>   Is there a function in R that calculates the year-on-year growth rate of
> some time series?
> 
>   In EView the function is @pchy.

This might do what you need

pchy <- function(x) {
    if(!is.ts(x)) stop("x is not a timeseries")

    x.freq <- tsp(x)[3]
    if(!(x.freq %in% c(1,2,4,12))) stop("Invalid frequency of timeseries x (must be 1, 2, 4, 12)")
    
    y <- diff(x,lag=x.freq)/lag(x,-x.freq)
    return(y)
}

Berend


More information about the R-help mailing list