dyn                   package:dyn                   R Documentation

_d_y_n_a_m_i_c _r_e_g_r_e_s_s_i_o_n _c_l_a_s_s

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

     dyn is used to construct objects of class '"dyn"'

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

     dyn(x)

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

       x: an object, typically a '"formula"' object or an object
          produced by '"lm"', '"glm"' or other regression function.

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

     '"dyn"' enables regression functions that were not written to
     handle time series to handle them.  Both the dependent and
     independent variables may be time series and they may have
     different time indexes (in which  case they are automatically
     aligned). The time series may also have missing values including
     internal missing values. 

     '"dyn"' currently works with any regression function that makes
     use of '"model.frame"' and is written in the style of '"lm"'. This
     includes '"lm"', '"glm"', '"loess"', '"rlm"' (from '"MASS"'),
     '"lqs"' (from pkg{"MASS"}), '"randomForest"' (from
     '"randomForest"'), '"rq"' (from '"quantreg"') and others.  The
     time series objects can be one of the following classes: '"ts"',
     '"irts"', code{"its"}, '"zoo"' or '"zooreg"'.   

     Typically '"dyn"' is used like this '"dyn$lm(y ~ lag(y, -1))"'.  
     That is, one prepends the usual '"lm"' or other regression
     function with '"dyn$"' and then uses time series including '"lag"'
     and '"diff"' operators in the formula.    The returned object has
     a class vector beginning with '"dyn"' and includes all classes
     that it would have had without '"dyn"'.  '"dyn"' methods include
     '"model.frame"', '"fitted"', '"residuals"', '"predict"',
     '"update"', '"anova"' and '"$"' methods.  These methods preprocess
     their arguments,  call the real method which does the actual work
     and then post process the returned object.  In the case of
     '"fitted"', '"residuals"' and '"predict"' they ensure that the
     result is a time series.  In the case of 'anova' the objects are
     intersected so that they all have the time indexes to ensure that
     a comparable input is provided to '"anova"'.

_V_a_l_u_e:

     '"dyn"' returns its argument with the class name '"dyn"' prepended
     to its class vector.  The '"fitted"', '"residuals"' and
     '"predict"' '"dyn"' methods return time series of the appropriate
     class.  '"model.frame"' creates a model frame with an attribute of
     '"series"' that contains a data frame of  the time series and
     factor variables as columns.

_N_o_t_e:

     '"dyn"' relies on the underlying time series classes and
     regression routines for all substantive functionality. In
     particular note these limitations: '"irts"' has no '"lag"' or
     '"diff"' methods.  The lag function of '"its"' its called
     '"lagIts"'.  '"ts"' and '"zooreg"' series can be lagged outside of
     the data range (both forward and backward) but other time series
     classes cannot represent such data and therefore will drop them.
     If the regression function in questions does not have an
     associated '"fitted"', '"residuals"', etc. method then such method
     will not be available with '"dyn"' either.  

     Internally the system uses '"zoo"'.  Additional time series 
     classes not already defined to work with '"dyn"' can be added by
     simply defining '"as"' methods between the new class and '"zoo"'
     and then creating new methods (for  '"model.frame"', '"predict"',
     '"fitted"', etc.) In most cases these method names can be set
     equal to the corresponding '"zoo"' method name (e.g. 
     '"model.frame.newclass <- model.frame.zoo"' so that no new
     function bodies need be written).

     The main requirements for new regression routines to work with
     '"dyn"' are that they use '"model.frame"', that their '"fitted"',
     '"residuals"' and '"predict"' methods return named vectors whose
     names are the corresponding indexes in the original data and that
     they follow the same style of processing as '"lm"'.  There is no
     '"dyn"' code specific to any particular regression routine.

     '"dyn$lm(formula)"' is equivalent to '"dyn(lm(dyn(formula)))"' but
     is easier to write.  When '"dyn"' is used with an argument, as
     just shown, then its effect is simply to return its argument with
     the '"dyn"' class prepended to the class vector so that further
     processing of the result is intercepted by other '"dyn"' methods.

_S_e_e _A_l_s_o:

     See Also 'model.frame',  'predict',  'fitted',  'residuals', 
     'anova',  'update',  'lm',  'glm',  'loess'

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

     y <- ts(1:12, start = c(2000,2), freq = 4)^3
     x <- ts(1:9, start = c(2000,3), freq = 4)^2

     # can be used with numerous different regression functions
     y.lm <- dyn$lm( window(y, start = c(2000,4)) ~ diff(x) )
     y.lm <- dyn$lm( y ~ diff(x) )
     y.glm <- dyn$glm( y ~ diff(x) )
     y.loess <- dyn$loess( y ~ diff(x) )

     y.lm <- dyn(lm(dyn(y ~ diff(x))))  # same
     y.lm
     summary(y.lm)
     residuals(y.lm)
     fitted(y.lm)
     y2.lm <- update(y.lm, . ~ . + lag(x,-1))
     y2.lm
     anova(y.lm, y2.lm)

     # examples of using data
     dyn$lm(y ~ diff(x), list(y = y, x = x))
     dyn$lm(y ~ diffx, list(y = y, diffx = diff(x)))

     # invoke model.frame on formula as a dyn object
     dyn$model.frame( y ~ diff(x) )

