apply               package:timeSeries               R Documentation

_A_p_p_l_y _F_u_n_c_t_i_o_n_s _O_v_e_r _T_i_m_e _S_e_r_i_e_s _P_e_r_i_o_d_s

_D_e_s_c_r_i_p_t_i_o_n:

     Apply a function over time series peridos of arbitrary positons
     and lengths.

_U_s_a_g_e:

     fapply(x, from, to, FUN, ...)

     applySeries(x, from = NULL, to = NULL, by = c("monthly", "quarterly"), 
         FUN = colMeans, units = NULL, format = x@format, zone = x@FinCenter, 
         FinCenter = x@FinCenter, recordIDs = data.frame(), title = x@title,
         documentation = x@documentation, ...)

_A_r_g_u_m_e_n_t_s:

       x: an object of class 'timeSeries'. 

from, to: starting date and end date as timeDate objects. Note, 'to'
          must be time ordered after 'from'. If 'from' and 'to' are
          missing in function 'fapply' they are set by default to
          'from=start(x)', and 'to=end(x)'.  

     FUN: the function to be applied. For the function 'applySeries'
          the default setting is 'FUN=colMeans'. 

      by: a character value either '"monthly"' or '"quarterly"' used in
          the function 'applySeries'. The default value is 
          '"monthly"'. Only operative when both arguments 'from'  and
          'to' have their default values 'NULL'. In this case the
          function 'FUN' will be applied to monthly or quarterly
          periods. 

   units: an optional character string, which allows to overwrite the 
          current column names of a 'timeSeries' object. By default 
          'NULL' which means that the column names are selected
          automatically. 

  format: the format specification of the input character vector in
          POSIX  notation. 

    zone: the time zone or financial center where the data were
          recorded. 

FinCenter: a character value with the the location of the  financial
          center  named as "continent/city", or "city".  

recordIDs: a data frame which can be used for record identification
          information. Note, this is not yet handled by the apply
          functions, an empty data.frame will be returned. 

   title: an optional title string, if not specified the inputs data 
          name is deparsed. 

documentation: optional documentation string, or a vector of character
          strings. 

     ...: arguments passed to other methods. 

_D_e_t_a_i_l_s:

     Like 'apply' applies a function to the margins of an array, the
     function 'fapply' applies a function to the time stamps or signal
     counts of a financial (therefore the "f" in front of the  function
     name) time series of class 'timeSeries"'. 

     The function 'fapply' inputs a 'timeSeries' object, and if 'from'
     and 'to' are missing, they take the start  and end time stamps of
     the series as default falues. The function  then behaves like
     'apply' on the column margin.

     Note, the function 'fapply' can be used repetitive in the
     following sense: If 'from' and 'to' are two 'timeDate' vectors of 
     equal length then for each period spanned by the elelemts of the 
     two vectors the function 'FUN' will be applied to each period. The
     resulting time stamps, are the time stamps of the 'to' vector.
     Note, the periods can be regular or irregelar, and they can  even
     overlap. 

     The function 'fapply' calls the more general function
     'applySeries' which also offers, to create automatical monthly and
     quarterly periods.

_E_x_a_m_p_l_e_s:

     ## data - 
        # Percentual Returns of Swiss Bond Index and Performance Index
        LPP = 100 * as.timeSeries(data(LPP2005REC))[,c("SBI", "SPI")]
        head(LPP, 20)
        
     ## Aggregate Quarterly Returns
        applySeries(LPP, by = "quarterly", FUN = colSums)
        
     ## Aggregate Quarterly every last Friday in Quarter
        oneDay = 24*3600
        from = unique(timeFirstDayInQuarter(time(LPP))) - oneDay
        from = timeLastNdayInMonth(from, nday = 5)
        to = unique(timeLastDayInQuarter(time(LPP)))
        to = timeLastNdayInMonth(to, nday = 5)
        data.frame(from = as.character(from), to = as.character(to))
        applySeries(LPP, from, to, FUN = colSums)
        
     ## Alternative use 
        fapply(LPP, from, to, FUN = colSums)
        
     ## Count Trading Days per Month
        colCounts = function(x) rep(NROW(x), times = NCOL(x))
        applySeries(LPP, FUN = colCounts, by = "monthly")

