SCM

SCM Repository

[rmetrics] View of /pkg/timeSeries/R/base-diff.R
ViewVC logotype

View of /pkg/timeSeries/R/base-diff.R

Parent Directory Parent Directory | Revision Log Revision Log


Revision 4903 - (download) (annotate)
Thu Jul 8 08:48:38 2010 UTC (8 years, 7 months ago) by chalabi
File size: 2872 byte(s)
improved support of recordIDs
#
#  This program is free software; you can redistribute it and/or modify
#  it under the terms of the GNU General Public License as published by
#  the Free Software Foundation; either version 2 of the License, or
#  (at your option) any later version.
#
#  This program is distributed in the hope that it will be useful,
#  but WITHOUT ANY WARRANTY; without even the implied warranty of
#  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
#  GNU General Public License for more details.
#
#  A copy of the GNU General Public License is available at
#  ../../COPYING


################################################################################
# FUNCTION:                 DESCRIPTION:
#  diff,timeSeries           Differences a 'timeSeries' object
################################################################################


.diff.timeSeries <-
function(x, lag = 1, diff = 1, trim = FALSE, pad = NA, ...)
{
    # A function implemented by Diethelm Wuertz
    # Modified by Yohan Chalabi

    # Description:
    #   Difference 'timeSeries' objects.

    # Arguments:
    #   x - a 'timeSeries' object.
    #   lag - an integer indicating which lag to use.
    #       By default 1.
    #   diff - an integer indicating the order of the difference.
    #       By default 1.
    #   trim - a logical. Should NAs at the beginning of the
    #       series be removed?
    #   pad - a umeric value with which NAs should be replaced
    #       at the beginning of the series.

    # Value:
    #   Returns a differenced object of class 'timeSeries'.

    # FUNCTION:

    # Convert:
    y = getDataPart(x) # as.matrix(x)

    # Check NAs:
    # if (any(is.na(y))) stop("NAs are not allowed in time series")

    # Difference:
    z = diff(y, lag = lag, difference = diff)

    diffNums = dim(y)[1] - dim(z)[1]

    # Trim:
    if (!trim) {
        zpad = matrix(0*y[1:diffNums, ] + pad, nrow = diffNums)
        z = rbind(zpad, z)
    }

    pos <-
        if (!trim)
            x@positions
        else
            x@positions[-(1:diffNums)]

    # Record IDs:
    df <- x@recordIDs
    if (trim && sum(dim(df)) > 0) {
        df <- df[-seq.int(diffNums), , drop = FALSE]
        rownames(df) <- seq.int(NROW(df))
    }


    # Return Value:
    timeSeries(data = z, charvec = pos, units = colnames(z),
               format = x@format, zone = x@FinCenter,
               FinCenter = x@FinCenter, recordIDs = df,
               title = x@title, documentation = x@documentation)
}


setMethod("diff", "timeSeries",
          function(x, lag = 1, diff = 1, trim = FALSE, pad = NA, ...)
          .diff.timeSeries(x, lag, diff, trim, pad, ...))

# until UseMethod dispatches S4 methods in 'base' functions
diff.timeSeries <- function(x, ...) .diff.timeSeries(x, ...)


################################################################################


R-Forge@R-project.org
ViewVC Help
Powered by ViewVC 1.0.0  
Thanks to:
Vienna University of Economics and Business University of Wisconsin - Madison Powered By FusionForge