R: model.frame and model.matrix for panel data

model.frame.pFormula {plm} | R Documentation

## model.frame and model.matrix for panel data

### Description

Methods to create model frame and model matrix for panel data.

### Usage

    ## S3 method for class 'pFormula'
    model.frame(formula, data, ...,
                                     lhs = NULL, rhs = NULL)

    ## S3 method for class 'pFormula'
    model.matrix(object, data,
                                      model = c("pooling","within","Between",
                                                "between","mean","random","fd"),
                                      effect = c("individual","time","twoways"),
                                      rhs = 1,
                                      theta = NULL, ...)

    ## S3 method for class 'plm'
    model.matrix(object, ...)

### Arguments

`object, formula` | an object of class `"pFormula"` or an estimated model object of class `"plm"`,
`data` | a `pdata.frame`, see Details,
`effect` | the effects introduced in the model, one of `"individual"`, `"time"` or `"twoways"`,
`model` | one of `"pooling"`, `"within"`, `"between"`, `"random",` `"fd"` and `"ht"`,
`theta` | the parameter for the transformation if `model = "random"`,
`lhs` | inherited from package `Formula` (see there),
`rhs` | inherited from package `Formula` (see there),
`...` | further arguments.

### Details

The `lhs` and `rhs` arguments are inherited from `Formula`, see there for more details.
The `model.frame` methods return a `pdata.frame` object suitable as an input to plm's `model.matrix`.
The `model.matrix` methods builds a model matrix with transformations performed as specified by the `model` and `effect` arguments (and `theta` if `model = "random"` is requested), in this case the supplied `data` argument should be a model frame created by plm's `model.frame` method. If not, it is tried to construct the model frame from the data. Constructing the model frame first ensures proper NA handling, see Examples.

### Value

The `model.frame` methods return a `pdata.frame`.
The `model.matrix` methods return a `matrix`.

### Author(s)

Yves Croissant

### See Also

`pmodel.response` for (transformed) response variable.
`pFormula`, especially for coercing a `formula` to a `pFormula`.
`Formula` from package `Formula`, especially for the `lhs` and `rhs` arguments.

### Examples

    # First, make a pdata.frame
    data(Grunfeld)
    pGrunfeld <- pdata.frame(Grunfeld)

    # then make a model frame from a pFormula and a pdata.frame
    pform <- pFormula(inv ~ value + capital)
    mf <- model.frame(pform, data = pGrunfeld)

    # then construct the (transformed) model matrix (design matrix)
    # from formula and model frame
    modmat <- model.matrix(pform, data = mf, model = "within")

    ## retrieve model frame and model matrix from an estimated plm object
    fe_model <- plm(pform, data = pGrunfeld, model = "within")
    model.frame(fe_model)
    model.matrix(fe_model)

    # same as constructed before
    all.equal(mf, model.frame(fe_model), check.attributes = FALSE) # TRUE
    all.equal(modmat, model.matrix(fe_model), check.attributes = FALSE) # TRUE

[Package plm version 1.6-5 Index]