R: Fast Growth Rates for Time Series and Panel Data

fgrowth {collapse} | R Documentation

## Fast Growth Rates for Time Series and Panel Data

### Description

`fgrowth` is a S3 generic to compute (sequences of) suitably lagged / leaded, iterated and compounded growth rates, obtained with via the exact method of computation or through log differencing. By default growth rates are provided in percentage terms, but any scale factor can be applied. The growth operator `G` is a parsimonious wrapper around `fgrowth`, and also provides more flexibility when applied to data frames.

### Usage

    fgrowth(x, n = 1, diff = 1, ...)
          G(x, n = 1, diff = 1, ...)

    ## Default S3 method:
    fgrowth(x, n = 1, diff = 1, g = NULL, t = NULL, fill = NA,
            logdiff = FALSE, scale = 100, power = 1, stubs = TRUE, ...)
    ## Default S3 method:
    G(x, n = 1, diff = 1, g = NULL, t = NULL, fill = NA, logdiff = FALSE,
      scale = 100, power = 1, stubs = TRUE, ...)

    ## S3 method for class 'matrix'
    fgrowth(x, n = 1, diff = 1, g = NULL, t = NULL, fill = NA,
            logdiff = FALSE, scale = 100, power = 1,
            stubs = length(n) + length(diff) > 2L, ...)
    ## S3 method for class 'matrix'
    G(x, n = 1, diff = 1, g = NULL, t = NULL, fill = NA, logdiff = FALSE,
      scale = 100, power = 1, stubs = TRUE, ...)

    ## S3 method for class 'data.frame'
    fgrowth(x, n = 1, diff = 1, g = NULL, t = NULL, fill = NA,
            logdiff = FALSE, scale = 100, power = 1,
            stubs = length(n) + length(diff) > 2L, ...)
    ## S3 method for class 'data.frame'
    G(x, n = 1, diff = 1, by = NULL, t = NULL, cols = is.numeric,
      fill = NA, logdiff = FALSE, scale = 100, power = 1, stubs = TRUE,
      keep.ids = TRUE, ...)

    # Methods for indexed data / compatibility with plm:

    ## S3 method for class 'pseries'
    fgrowth(x, n = 1, diff = 1, fill = NA, logdiff = FALSE, scale = 100,
            power = 1, stubs = length(n) + length(diff) > 2L, shift = "time", ...)
    ## S3 method for class 'pseries'
    G(x, n = 1, diff = 1, fill = NA, logdiff = FALSE, scale = 100,
      power = 1, stubs = TRUE, shift = "time", ...)

    ## S3 method for class 'pdata.frame'
    fgrowth(x, n = 1, diff = 1, fill = NA, logdiff = FALSE, scale = 100,
            power = 1, stubs = length(n) + length(diff) > 2L, shift = "time", ...)
    ## S3 method for class 'pdata.frame'
    G(x, n = 1, diff = 1, cols = is.numeric, fill = NA, logdiff = FALSE,
      scale = 100, power = 1, stubs = TRUE, shift = "time", keep.ids = TRUE, ...)

    # Methods for grouped data frame / compatibility with dplyr:

    ## S3 method for class 'grouped_df'
    fgrowth(x, n = 1, diff = 1, t = NULL, fill = NA, logdiff = FALSE,
            scale = 100, power = 1, stubs = length(n) + length(diff) > 2L,
            keep.ids = TRUE, ...)
    ## S3 method for class 'grouped_df'
    G(x, n = 1, diff = 1, t = NULL, fill = NA, logdiff = FALSE,
      scale = 100, power = 1, stubs = TRUE, keep.ids = TRUE, ...)

### Arguments

`x` | a numeric vector / time series, (time series) matrix, data frame, 'indexed_series' ('pseries'), 'indexed_frame' ('pdata.frame') or grouped data frame ('grouped_df').
`n` | integer. A vector indicating the number of lags or leads.
`diff` | integer. A vector of integers > 1 indicating the order of taking growth rates, e.g. `diff = 2` means computing the growth rate of the growth rate.
`g` | a factor, `GRP` object, or atomic vector / list of vectors (internally grouped with `group`) used to group `x`.
`by` | data.frame method: Same as `g`, but also allows one- or two-sided formulas i.e. `~ group1` or `var1 + var2 ~ group1 + group2`. See Examples.
`t` | a time vector or list of vectors. See `flag`.
`cols` | data.frame method: Select columns to compute growth rates using a function, column names, indices or a logical vector. Default: All numeric variables. Note: `cols` is ignored if a two-sided formula is passed to `by`.
`fill` | value to insert when vectors are shifted. Default is `NA`.
`logdiff` | logical. Compute log-difference growth rates instead of exact growth rates. See Details.
`scale` | logical. Scale factor post-applied to growth rates, default is 100 which gives growth rates in percentage terms. See Details.
`power` | numeric. Apply a power to annualize or compound growth rates e.g. `fgrowth(AirPassengers, 12, power = 1/12)` is equivalent to `((AirPassengers/flag(AirPassengers, 12))^(1/12)-1)*100`.
`stubs` | logical. `TRUE` will rename all computed columns by adding a prefix "L`n`G`diff`." / "F`n`G`diff`.", or "L`n`Dlog`diff`." / "F`n`Dlog`diff`." if `logdiff = TRUE`.
`shift` | pseries / pdata.frame methods: character. `"time"` or `"row"`. See `flag` for details.
`keep.ids` | data.frame / pdata.frame / grouped_df methods: Logical. Drop all identifiers from the output (which includes all variables passed to `by` or `t`). Note: For 'grouped_df' / 'pdata.frame' identifiers are dropped, but the `"groups"` / `"index"` attributes are kept.
`...` | arguments to be passed to or from other methods.

### Details

`fgrowth/G` by default computes exact growth rates using `repeat(diff) ((x[i]/x[i-n])^power - 1)*scale`, so for `diff > 1` it computes growth rate of growth rates. If `logdiff = TRUE`, approximate growth rates are computed using `log(x[i]/x[i-n])*scale` for `diff = 1` and `repeat(diff-1) x[i] - x[i-n]` thereafter (usually `diff = 1` for log-differencing). For further details see the help pages of `fdiff` and `flag`.

### Value

`x` where the growth rate was taken `diff` times using lags `n` of itself, scaled by `scale`. Computations can be grouped by `g/by` and/or ordered by `t`. See Details and Examples.

### See Also

`flag/L/F`, `fdiff/D/Dlog`, Time Series and Panel Series, Collapse Overview

### Examples

    ## Simple Time Series: AirPassengers
    G(AirPassengers)                      # Growth rate, same as fgrowth(AirPassengers)
    G(AirPassengers, logdiff = TRUE)      # Log-difference
    G(AirPassengers, 1, 2)                # Growth rate of growth rate
    G(AirPassengers, 12)                  # Seasonal growth rate (data is monthly)

    head(G(AirPassengers, -2:2, 1:3))     # Sequence of leaded/lagged and iterated growth rates

    # let's do some visual analysis
    plot(G(AirPassengers, c(0, 1, 12)))
    plot(stl(window(G(AirPassengers, 12), # Taking seasonal growth rate removes most seasonal variation
                    1950), "periodic"))

    ## Time Series Matrix of 4 EU Stock Market Indicators, recorded 260 days per year
    plot(G(EuStockMarkets,c(0,260)))                    # Plot series and annual growth rates
    summary(lm(L260G1.DAX ~., G(EuStockMarkets,260)))   # Annual growth rate of DAX regressed on the
                                                        # growth rates of the other indicators

    ## World Development Panel Data
    head(fgrowth(num_vars(wlddev), 1, 1,                    # Computes growth rates of numeric variables
                 wlddev$country, wlddev$year))              # fgrowth requires external inputs..
    head(G(wlddev, 1, 1, ~country, ~year))                  # Growth of numeric variables, id's attached
    head(G(wlddev, 1, 1, ~country))                         # Without t: Works because data is ordered
    head(G(wlddev, 1, 1, PCGDP + LIFEEX ~ country, ~year))  # Growth of GDP per Capita & Life Expectancy
    head(G(wlddev, 0:1, 1, ~ country, ~year, cols = 9:10))  # Same, also retaining original series
    head(G(wlddev, 0:1, 1, ~ country, ~year, 9:10,          # Dropping id columns
           keep.ids = FALSE))

[Package collapse version 1.8.6 Index]