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

# S3 method for class 'pdata.frame'
model.frame(
  formula,
  data = NULL,
  ...,
  lhs = NULL,
  rhs = NULL,
  dot = "previous"
)

# S3 method for class 'pdata.frame'
formula(x, ...)

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

# S3 method for class 'pdata.frame'
model.matrix(
  object,
  model = c("pooling", "within", "Between", "Sum", "between", "mean", "random", "fd"),
  effect = c("individual", "time", "twoways", "nested"),
  rhs = 1,
  theta = NULL,
  cstcovar.rm = NULL,
  ...
)

Arguments

data

a formula, see Details,

...

further arguments.

lhs

inherited from package Formula::Formula() (see there),

rhs

inherited from package Formula::Formula() (see there),

dot

inherited from package Formula::Formula() (see there),

x

a model.frame

object, formula

an object of class "pdata.frame" or an estimated model object of class "plm",

model

one of "pooling", "within", "Sum", "Between", "between", "random", "fd" and "ht",

effect

the effects introduced in the model, one of "individual", "time", "twoways" or "nested",

theta

the parameter for the transformation if model = "random",

cstcovar.rm

remove the constant columns, one of "none", "intercept", "covariates", "all"),

Value

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

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.

See also

pmodel.response() for (transformed) response variable.
Formula::Formula() from package Formula, especially for the lhs and rhs arguments.

Author

Yves Croissant

Examples


# First, make a pdata.frame
data("Grunfeld", package = "plm")
pGrunfeld <- pdata.frame(Grunfeld)

# then make a model frame from a formula and a pdata.frame
form <- inv ~ value
mf <- model.frame(pGrunfeld, form)

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

## retrieve model frame and model matrix from an estimated plm object
fe_model <- plm(form, data = pGrunfeld, model = "within")
model.frame(fe_model)
#>             inv   value
#> 1-1935   317.60 3078.50
#> 1-1936   391.80 4661.70
#> 1-1937   410.60 5387.10
#> 1-1938   257.70 2792.20
#> 1-1939   330.80 4313.20
#> 1-1940   461.20 4643.90
#> 1-1941   512.00 4551.20
#> 1-1942   448.00 3244.10
#> 1-1943   499.60 4053.70
#> 1-1944   547.50 4379.30
#> 1-1945   561.20 4840.90
#> 1-1946   688.10 4900.90
#> 1-1947   568.90 3526.50
#> 1-1948   529.20 3254.70
#> 1-1949   555.10 3700.20
#> 1-1950   642.90 3755.60
#> 1-1951   755.90 4833.00
#> 1-1952   891.20 4924.90
#> 1-1953  1304.40 6241.70
#> 1-1954  1486.70 5593.60
#> 2-1935   209.90 1362.40
#> 2-1936   355.30 1807.10
#> 2-1937   469.90 2676.30
#> 2-1938   262.30 1801.90
#> 2-1939   230.40 1957.30
#> 2-1940   361.60 2202.90
#> 2-1941   472.80 2380.50
#> 2-1942   445.60 2168.60
#> 2-1943   361.60 1985.10
#> 2-1944   288.20 1813.90
#> 2-1945   258.70 1850.20
#> 2-1946   420.30 2067.70
#> 2-1947   420.50 1796.70
#> 2-1948   494.50 1625.80
#> 2-1949   405.10 1667.00
#> 2-1950   418.80 1677.40
#> 2-1951   588.20 2289.50
#> 2-1952   645.50 2159.40
#> 2-1953   641.00 2031.30
#> 2-1954   459.30 2115.50
#> 3-1935    33.10 1170.60
#> 3-1936    45.00 2015.80
#> 3-1937    77.20 2803.30
#> 3-1938    44.60 2039.70
#> 3-1939    48.10 2256.20
#> 3-1940    74.40 2132.20
#> 3-1941   113.00 1834.10
#> 3-1942    91.90 1588.00
#> 3-1943    61.30 1749.40
#> 3-1944    56.80 1687.20
#> 3-1945    93.60 2007.70
#> 3-1946   159.90 2208.30
#> 3-1947   147.20 1656.70
#> 3-1948   146.30 1604.40
#> 3-1949    98.30 1431.80
#> 3-1950    93.50 1610.50
#> 3-1951   135.20 1819.40
#> 3-1952   157.30 2079.70
#> 3-1953   179.50 2371.60
#> 3-1954   189.60 2759.90
#> 4-1935    40.29  417.50
#> 4-1936    72.76  837.80
#> 4-1937    66.26  883.90
#> 4-1938    51.60  437.90
#> 4-1939    52.41  679.70
#> 4-1940    69.41  727.80
#> 4-1941    68.35  643.60
#> 4-1942    46.80  410.90
#> 4-1943    47.40  588.40
#> 4-1944    59.57  698.40
#> 4-1945    88.78  846.40
#> 4-1946    74.12  893.80
#> 4-1947    62.68  579.00
#> 4-1948    89.36  694.60
#> 4-1949    78.98  590.30
#> 4-1950   100.66  693.50
#> 4-1951   160.62  809.00
#> 4-1952   145.00  727.00
#> 4-1953   174.93 1001.50
#> 4-1954   172.49  703.20
#> 5-1935    39.68  157.70
#> 5-1936    50.73  167.90
#> 5-1937    74.24  192.90
#> 5-1938    53.51  156.70
#> 5-1939    42.65  191.40
#> 5-1940    46.48  185.50
#> 5-1941    61.40  199.60
#> 5-1942    39.67  189.50
#> 5-1943    62.24  151.20
#> 5-1944    52.32  187.70
#> 5-1945    63.21  214.70
#> 5-1946    59.37  232.90
#> 5-1947    58.02  249.00
#> 5-1948    70.34  224.50
#> 5-1949    67.42  237.30
#> 5-1950    55.74  240.10
#> 5-1951    80.30  327.30
#> 5-1952    85.40  359.40
#> 5-1953    91.90  398.40
#> 5-1954    81.43  365.70
#> 6-1935    20.36  197.00
#> 6-1936    25.98  210.30
#> 6-1937    25.94  223.10
#> 6-1938    27.53  216.70
#> 6-1939    24.60  286.40
#> 6-1940    28.54  298.00
#> 6-1941    43.41  276.90
#> 6-1942    42.81  272.60
#> 6-1943    27.84  287.40
#> 6-1944    32.60  330.30
#> 6-1945    39.03  324.40
#> 6-1946    50.17  401.90
#> 6-1947    51.85  407.40
#> 6-1948    64.03  409.20
#> 6-1949    68.16  482.20
#> 6-1950    77.34  673.80
#> 6-1951    95.30  676.90
#> 6-1952    99.49  702.00
#> 6-1953   127.52  793.50
#> 6-1954   135.72  927.30
#> 7-1935    24.43  138.00
#> 7-1936    23.21  200.10
#> 7-1937    32.78  210.10
#> 7-1938    32.54  161.20
#> 7-1939    26.65  161.70
#> 7-1940    33.71  145.10
#> 7-1941    43.50  110.60
#> 7-1942    34.46   98.10
#> 7-1943    44.28  108.80
#> 7-1944    70.80  118.20
#> 7-1945    44.12  126.50
#> 7-1946    48.98  156.70
#> 7-1947    48.51  119.40
#> 7-1948    50.00  129.10
#> 7-1949    50.59  134.80
#> 7-1950    42.53  140.80
#> 7-1951    64.77  179.00
#> 7-1952    72.68  178.10
#> 7-1953    73.86  186.80
#> 7-1954    89.51  192.70
#> 8-1935    12.93  191.50
#> 8-1936    25.90  516.00
#> 8-1937    35.05  729.00
#> 8-1938    22.89  560.40
#> 8-1939    18.84  519.90
#> 8-1940    28.57  628.50
#> 8-1941    48.51  537.10
#> 8-1942    43.34  561.20
#> 8-1943    37.02  617.20
#> 8-1944    37.81  626.70
#> 8-1945    39.27  737.20
#> 8-1946    53.46  760.50
#> 8-1947    55.56  581.40
#> 8-1948    49.56  662.30
#> 8-1949    32.04  583.80
#> 8-1950    32.24  635.20
#> 8-1951    54.38  723.80
#> 8-1952    71.78  864.10
#> 8-1953    90.08 1193.50
#> 8-1954    68.60 1188.90
#> 9-1935    26.63  290.60
#> 9-1936    23.39  291.10
#> 9-1937    30.65  335.00
#> 9-1938    20.89  246.00
#> 9-1939    28.78  356.20
#> 9-1940    26.93  289.80
#> 9-1941    32.08  268.20
#> 9-1942    32.21  213.30
#> 9-1943    35.69  348.20
#> 9-1944    62.47  374.20
#> 9-1945    52.32  387.20
#> 9-1946    56.95  347.40
#> 9-1947    54.32  291.90
#> 9-1948    40.53  297.20
#> 9-1949    32.54  276.90
#> 9-1950    43.48  274.60
#> 9-1951    56.49  339.90
#> 9-1952    65.98  474.80
#> 9-1953    66.11  496.00
#> 9-1954    49.34  474.50
#> 10-1935    2.54   70.91
#> 10-1936    2.00   87.94
#> 10-1937    2.19   82.20
#> 10-1938    1.99   58.72
#> 10-1939    2.03   80.54
#> 10-1940    1.81   86.47
#> 10-1941    2.14   77.68
#> 10-1942    1.86   62.16
#> 10-1943    0.93   62.24
#> 10-1944    1.18   61.82
#> 10-1945    1.36   65.85
#> 10-1946    2.24   69.54
#> 10-1947    3.81   64.97
#> 10-1948    5.66   68.00
#> 10-1949    4.21   71.24
#> 10-1950    3.42   69.05
#> 10-1951    4.67   83.04
#> 10-1952    6.00   74.42
#> 10-1953    6.53   63.51
#> 10-1954    5.12   58.12
model.matrix(fe_model)
#>             value
#> 1-1935  -1255.345
#> 1-1936    327.855
#> 1-1937   1053.255
#> 1-1938  -1541.645
#> 1-1939    -20.645
#> 1-1940    310.055
#> 1-1941    217.355
#> 1-1942  -1089.745
#> 1-1943   -280.145
#> 1-1944     45.455
#> 1-1945    507.055
#> 1-1946    567.055
#> 1-1947   -807.345
#> 1-1948  -1079.145
#> 1-1949   -633.645
#> 1-1950   -578.245
#> 1-1951    499.155
#> 1-1952    591.055
#> 1-1953   1907.855
#> 1-1954   1259.755
#> 2-1935   -609.425
#> 2-1936   -164.725
#> 2-1937    704.475
#> 2-1938   -169.925
#> 2-1939    -14.525
#> 2-1940    231.075
#> 2-1941    408.675
#> 2-1942    196.775
#> 2-1943     13.275
#> 2-1944   -157.925
#> 2-1945   -121.625
#> 2-1946     95.875
#> 2-1947   -175.125
#> 2-1948   -346.025
#> 2-1949   -304.825
#> 2-1950   -294.425
#> 2-1951    317.675
#> 2-1952    187.575
#> 2-1953     59.475
#> 2-1954    143.675
#> 3-1935   -770.725
#> 3-1936     74.475
#> 3-1937    861.975
#> 3-1938     98.375
#> 3-1939    314.875
#> 3-1940    190.875
#> 3-1941   -107.225
#> 3-1942   -353.325
#> 3-1943   -191.925
#> 3-1944   -254.125
#> 3-1945     66.375
#> 3-1946    266.975
#> 3-1947   -284.625
#> 3-1948   -336.925
#> 3-1949   -509.525
#> 3-1950   -330.825
#> 3-1951   -121.925
#> 3-1952    138.375
#> 3-1953    430.275
#> 3-1954    818.575
#> 4-1935   -275.710
#> 4-1936    144.590
#> 4-1937    190.690
#> 4-1938   -255.310
#> 4-1939    -13.510
#> 4-1940     34.590
#> 4-1941    -49.610
#> 4-1942   -282.310
#> 4-1943   -104.810
#> 4-1944      5.190
#> 4-1945    153.190
#> 4-1946    200.590
#> 4-1947   -114.210
#> 4-1948      1.390
#> 4-1949   -102.910
#> 4-1950      0.290
#> 4-1951    115.790
#> 4-1952     33.790
#> 4-1953    308.290
#> 4-1954      9.990
#> 5-1935    -73.770
#> 5-1936    -63.570
#> 5-1937    -38.570
#> 5-1938    -74.770
#> 5-1939    -40.070
#> 5-1940    -45.970
#> 5-1941    -31.870
#> 5-1942    -41.970
#> 5-1943    -80.270
#> 5-1944    -43.770
#> 5-1945    -16.770
#> 5-1946      1.430
#> 5-1947     17.530
#> 5-1948     -6.970
#> 5-1949      5.830
#> 5-1950      8.630
#> 5-1951     95.830
#> 5-1952    127.930
#> 5-1953    166.930
#> 5-1954    134.230
#> 6-1935   -222.865
#> 6-1936   -209.565
#> 6-1937   -196.765
#> 6-1938   -203.165
#> 6-1939   -133.465
#> 6-1940   -121.865
#> 6-1941   -142.965
#> 6-1942   -147.265
#> 6-1943   -132.465
#> 6-1944    -89.565
#> 6-1945    -95.465
#> 6-1946    -17.965
#> 6-1947    -12.465
#> 6-1948    -10.665
#> 6-1949     62.335
#> 6-1950    253.935
#> 6-1951    257.035
#> 6-1952    282.135
#> 6-1953    373.635
#> 6-1954    507.435
#> 7-1935    -11.790
#> 7-1936     50.310
#> 7-1937     60.310
#> 7-1938     11.410
#> 7-1939     11.910
#> 7-1940     -4.690
#> 7-1941    -39.190
#> 7-1942    -51.690
#> 7-1943    -40.990
#> 7-1944    -31.590
#> 7-1945    -23.290
#> 7-1946      6.910
#> 7-1947    -30.390
#> 7-1948    -20.690
#> 7-1949    -14.990
#> 7-1950     -8.990
#> 7-1951     29.210
#> 7-1952     28.310
#> 7-1953     37.010
#> 7-1954     42.910
#> 8-1935   -479.410
#> 8-1936   -154.910
#> 8-1937     58.090
#> 8-1938   -110.510
#> 8-1939   -151.010
#> 8-1940    -42.410
#> 8-1941   -133.810
#> 8-1942   -109.710
#> 8-1943    -53.710
#> 8-1944    -44.210
#> 8-1945     66.290
#> 8-1946     89.590
#> 8-1947    -89.510
#> 8-1948     -8.610
#> 8-1949    -87.110
#> 8-1950    -35.710
#> 8-1951     52.890
#> 8-1952    193.190
#> 8-1953    522.590
#> 8-1954    517.990
#> 9-1935    -43.050
#> 9-1936    -42.550
#> 9-1937      1.350
#> 9-1938    -87.650
#> 9-1939     22.550
#> 9-1940    -43.850
#> 9-1941    -65.450
#> 9-1942   -120.350
#> 9-1943     14.550
#> 9-1944     40.550
#> 9-1945     53.550
#> 9-1946     13.750
#> 9-1947    -41.750
#> 9-1948    -36.450
#> 9-1949    -56.750
#> 9-1950    -59.050
#> 9-1951      6.250
#> 9-1952    141.150
#> 9-1953    162.350
#> 9-1954    140.850
#> 10-1935    -0.011
#> 10-1936    17.019
#> 10-1937    11.279
#> 10-1938   -12.201
#> 10-1939     9.619
#> 10-1940    15.549
#> 10-1941     6.759
#> 10-1942    -8.761
#> 10-1943    -8.681
#> 10-1944    -9.101
#> 10-1945    -5.071
#> 10-1946    -1.381
#> 10-1947    -5.951
#> 10-1948    -2.921
#> 10-1949     0.319
#> 10-1950    -1.871
#> 10-1951    12.119
#> 10-1952     3.499
#> 10-1953    -7.411
#> 10-1954   -12.801
#> attr(,"assign")
#> [1] 0 1
#> attr(,"index")
#>     firm year
#> 1      1 1935
#> 2      1 1936
#> 3      1 1937
#> 4      1 1938
#> 5      1 1939
#> 6      1 1940
#> 7      1 1941
#> 8      1 1942
#> 9      1 1943
#> 10     1 1944
#> 11     1 1945
#> 12     1 1946
#> 13     1 1947
#> 14     1 1948
#> 15     1 1949
#> 16     1 1950
#> 17     1 1951
#> 18     1 1952
#> 19     1 1953
#> 20     1 1954
#> 21     2 1935
#> 22     2 1936
#> 23     2 1937
#> 24     2 1938
#> 25     2 1939
#> 26     2 1940
#> 27     2 1941
#> 28     2 1942
#> 29     2 1943
#> 30     2 1944
#> 31     2 1945
#> 32     2 1946
#> 33     2 1947
#> 34     2 1948
#> 35     2 1949
#> 36     2 1950
#> 37     2 1951
#> 38     2 1952
#> 39     2 1953
#> 40     2 1954
#> 41     3 1935
#> 42     3 1936
#> 43     3 1937
#> 44     3 1938
#> 45     3 1939
#> 46     3 1940
#> 47     3 1941
#> 48     3 1942
#> 49     3 1943
#> 50     3 1944
#> 51     3 1945
#> 52     3 1946
#> 53     3 1947
#> 54     3 1948
#> 55     3 1949
#> 56     3 1950
#> 57     3 1951
#> 58     3 1952
#> 59     3 1953
#> 60     3 1954
#> 61     4 1935
#> 62     4 1936
#> 63     4 1937
#> 64     4 1938
#> 65     4 1939
#> 66     4 1940
#> 67     4 1941
#> 68     4 1942
#> 69     4 1943
#> 70     4 1944
#> 71     4 1945
#> 72     4 1946
#> 73     4 1947
#> 74     4 1948
#> 75     4 1949
#> 76     4 1950
#> 77     4 1951
#> 78     4 1952
#> 79     4 1953
#> 80     4 1954
#> 81     5 1935
#> 82     5 1936
#> 83     5 1937
#> 84     5 1938
#> 85     5 1939
#> 86     5 1940
#> 87     5 1941
#> 88     5 1942
#> 89     5 1943
#> 90     5 1944
#> 91     5 1945
#> 92     5 1946
#> 93     5 1947
#> 94     5 1948
#> 95     5 1949
#> 96     5 1950
#> 97     5 1951
#> 98     5 1952
#> 99     5 1953
#> 100    5 1954
#> 101    6 1935
#> 102    6 1936
#> 103    6 1937
#> 104    6 1938
#> 105    6 1939
#> 106    6 1940
#> 107    6 1941
#> 108    6 1942
#> 109    6 1943
#> 110    6 1944
#> 111    6 1945
#> 112    6 1946
#> 113    6 1947
#> 114    6 1948
#> 115    6 1949
#> 116    6 1950
#> 117    6 1951
#> 118    6 1952
#> 119    6 1953
#> 120    6 1954
#> 121    7 1935
#> 122    7 1936
#> 123    7 1937
#> 124    7 1938
#> 125    7 1939
#> 126    7 1940
#> 127    7 1941
#> 128    7 1942
#> 129    7 1943
#> 130    7 1944
#> 131    7 1945
#> 132    7 1946
#> 133    7 1947
#> 134    7 1948
#> 135    7 1949
#> 136    7 1950
#> 137    7 1951
#> 138    7 1952
#> 139    7 1953
#> 140    7 1954
#> 141    8 1935
#> 142    8 1936
#> 143    8 1937
#> 144    8 1938
#> 145    8 1939
#> 146    8 1940
#> 147    8 1941
#> 148    8 1942
#> 149    8 1943
#> 150    8 1944
#> 151    8 1945
#> 152    8 1946
#> 153    8 1947
#> 154    8 1948
#> 155    8 1949
#> 156    8 1950
#> 157    8 1951
#> 158    8 1952
#> 159    8 1953
#> 160    8 1954
#> 161    9 1935
#> 162    9 1936
#> 163    9 1937
#> 164    9 1938
#> 165    9 1939
#> 166    9 1940
#> 167    9 1941
#> 168    9 1942
#> 169    9 1943
#> 170    9 1944
#> 171    9 1945
#> 172    9 1946
#> 173    9 1947
#> 174    9 1948
#> 175    9 1949
#> 176    9 1950
#> 177    9 1951
#> 178    9 1952
#> 179    9 1953
#> 180    9 1954
#> 181   10 1935
#> 182   10 1936
#> 183   10 1937
#> 184   10 1938
#> 185   10 1939
#> 186   10 1940
#> 187   10 1941
#> 188   10 1942
#> 189   10 1943
#> 190   10 1944
#> 191   10 1945
#> 192   10 1946
#> 193   10 1947
#> 194   10 1948
#> 195   10 1949
#> 196   10 1950
#> 197   10 1951
#> 198   10 1952
#> 199   10 1953
#> 200   10 1954

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