zoo (version 0.9-1)

model.frame.zoo: Model Frame for zoo Series

Description

model.frame.AsIs is used for dispatching model.frame to model.frame.zoo or model.frame.ts which create model frames from "zoo" and "ts" series respectively.

Usage

## S3 method for class 'AsIs':
model.frame(formula, data = NULL, subset = NULL, 
    na.action = na.omit, drop.unused.levels = FALSE, xlev = NULL, \dots)
## S3 method for class 'zoo':
model.frame(formula, data = NULL, subset = NULL, 
    na.action = na.omit, drop.unused.levels = FALSE, xlev = NULL, \dots)
## S3 method for class 'ts':
model.frame(formula, data = NULL, subset = NULL, 
    na.action = na.omit, drop.unused.levels = FALSE, xlev = NULL, \dots)

Arguments

formula
a symbolic description of the model to be fit. The details of model specification are given below.
data
optional object from which the data are taken. In the case of model.frame.zoo this is either a single "zoo" object or a data frame or list of "zoo" objects.
subset
an optional vector specifying a subset of observations to be used in the fitting process.
na.action
a function which indicates what should happen when the data contain NAs. The default is set by the na.action setting of options, and is
drop.unused.levels
should factors have unused levels dropped? Defaults to FALSE.
xlev
a named list of character vectors giving the full set of levels to be assumed for each factor.
...
additional arguments to be passed to the low level regression fitting functions (see below).

Value

  • A data.frame containing the variables used in formula (plus those specified in the remaining arguments).

concept

dynamic regression

Details

Regression functions such as lm typically specify the data to be used based on a formula (and optional further arguments) as in lm(formula, ...). The regression function then typically calls the generic function model.frame to convert the formula (and related arguments) to a data frame which forms the basic data used by the regression. In such a situation, the formula argument of the regression function is passed to model.frame so the class of the formula determines which method of model.frame is used for data handling by the regression function.

Normally the class of formula is "formula" which causes model.frame.formula to be called. In time series regression (and potentially in other situations as well), a more specialized model.frame method should be called depending on the class of the dependent variable. For this alternate form of dispatch, model.frame.AsIs is introduced: by insulating the formula argument in I(formula) the class is changed to "AsIs" (leaving formula unchanged) so that model.frame.AsIs is dispatched. mode.frame.AsIs does no processing of its own other than to examine the dependent variable of the formula and redispatch according to its class. Thus, if the dependent variable specified in I(formula) is of class "foo" the method model.frame.foo will be called for handling the data.

If the dependent variable in such a model is of class "zoo" then model.frame.zoo will be called. Its key role is to inspect a formula that may contain only zoo objects as variables and transform it to a model frame that can be used in various regression functions appropriately aligning the various series. If the "zoo" series should be specified using the data argument, such argument can be a list of "zoo" objects, a single zoo object, or a data frame of "zoo" objects. Similarly, a model.frame.ts method is provided for "ts" objects. Note, that despite their names these methods do not expect a normal "zoo" or "ts" object as their respective first argument but rather they expect a formula (whose dependent variable is of class "zoo" or "ts" respectively). Their behaviour is essentially the same as in the default model.frame method, but they retain the index/time information. Furthermore, they enable the user to use diff and lag in the model specification. As many regression functions in R use the same steps to extract the data from a specified formula, this approach modularizes the data management and regression based on "zoo" objects making it available in various regression functions. Hence, the user will usually not have to call any of the model.frame functions explicitly but only has to insulate the formula with I(). See the examples for an illustration. The regression functions for which this approach is known to work includes lm, glm, lrm, lqs, nnet, svm, rq, randomForest and possibly many others.

IMPORTANT: Note, that this feature is under development and might change in future versions.

See Also

model.frame

Examples

Run this code
yz <- zoo(1:12)^2
xz <- zoo(1:9)^2
fz <- zoo(gl(2,5))

# example of dispatching on response, using diff and
# lag, using a factor and returning residuals and fitted 
# zoo objects
yz.lm <- lm(I(lag(yz) ~ diff(xz) + fz))
residuals(yz.lm)
fitted(yz.lm)

# examples of using list in data argument
lm(I(lag(y) ~ diff(x) + f), list(y = yz, x = xz, f = fz))
lm(I(y ~ x+f), list(y=lag(yz), x=diff(xz), f=fz))

# examples of using a zoo object in the data argument
lm(I(lag(y) ~ diff(x)), merge(y=yz, x=xz))
lm(I(lag(yz) ~ diff(xz)), merge(yz, xz))
lm(I(y ~ x), merge(y = lag(yz), x = diff(xz)))

# using a variety of other modelling functions
# note that residuals and fitted may or may not
# work with these and if they do work may not
# return zoo objects.

# resistant regression
if(require(MASS))
lqs(I(lag(yz) ~ diff(xz)+fz))

# neural network
if(require(nnet))
nnet(I(lag(yz) ~ diff(xz)+fz), size = 1)

# quantile regression
if(require(quantreg))
rq(I(lag(yz) ~ fz), tau = 0.25)

# random forest
set.seed(1)
if(require(randomForest))
randomForest(I(lag(yz) ~ diff(xz) + fz))

# support vector machine
if(require(e1071))
svm(I(lag(yz) ~ diff(xz) + fz))

Run the code above in your browser using DataCamp Workspace