1
2R version 4.1.1 (2021-08-10) -- "Kick Things"
3Copyright (C) 2021 The R Foundation for Statistical Computing
4Platform: x86_64-w64-mingw32/x64 (64-bit)
5
6R is free software and comes with ABSOLUTELY NO WARRANTY.
7You are welcome to redistribute it under certain conditions.
8Type 'license()' or 'licence()' for distribution details.
9
10R is a collaborative project with many contributors.
11Type 'contributors()' for more information and
12'citation()' on how to cite R or R packages in publications.
13
14Type 'demo()' for some demos, 'help()' for on-line help, or
15'help.start()' for an HTML browser interface to help.
16Type 'q()' to quit R.
17
18> ## Tests for model.matrix[.pFormula|.plm] and pmodel.response.[pFormula|.plm|.data.frame]
19>
20> # commented lines do not run in v1.5-15
21>
22> # 1) model.matrix[.pFormula|.plm]
23> # 2) pmodel.response.[pFormula|.plm|.data.frame]
24>
25>
26>
27> library(plm)
28> data("Grunfeld", package="plm")
29> form <- formula(inv ~ value + capital)
30> plm_pool <- plm(form, data=Grunfeld, model="pooling")
31> plm_fe   <- plm(form, data=Grunfeld, model="within")
32> plm_re   <- plm(form, data=Grunfeld, model="random")
33>
34> ########### 1) model.matrix[.pFormula|.plm] ###########
35>
36>
37>
38> # pooling and within models work pdata.frame [albeit one should input a model.frame of class pdata.frame]
39> pGrunfeld <- pdata.frame(Grunfeld, index = c("firm", "year"))
40> mf <- model.frame(pGrunfeld, form)
41>
42> #MM modmat_pFormula_pdf_pool <- plm:::model.matrix.pFormula(form, data=pGrunfeld, model="pooling") # works
43> #MM modmat_pFormula_pdf_fe   <- plm:::model.matrix.pFormula(form, data=pGrunfeld, model="within")  # works
44>
45> modmat_pFormula_pdf_pool <- plm:::model.matrix.pdata.frame(mf, model="pooling") # works
46> modmat_pFormula_pdf_fe   <- plm:::model.matrix.pdata.frame(mf, model="within")  # works
47>
48>
49> #modmat_pFormula_re2   <- plm:::model.matrix.pFormula(form, data=pGrunfeld, model="random")  # still fails in v1.5-15
50>
51> # Error:
52> # Error in plm:::model.matrix.pFormula(form, data = pGrunfeld, model = "random") :
53> #   dims [product 600] do not match the length of object [0]
54>
55>
56> #### some sanity checks if various interfaces yield the same result ###
57> modmat_plm_pool <- model.matrix(plm_pool)
58> modmat_plm_fe   <- model.matrix(plm_fe)
59> modmat_plm_re   <- model.matrix(plm_re)
60>
61>
62> ##### interfaces: plm vs. pFormula with pdata.frame
63> if(!isTRUE(all.equal(modmat_plm_pool, modmat_pFormula_pdf_pool, check.attributes = FALSE))) stop("model.matrix's are not the same")
64> if(!isTRUE(all.equal(modmat_plm_fe,   modmat_pFormula_pdf_fe,   check.attributes = FALSE))) stop("model.matrix's are not the same")
65> #if(!isTRUE(all.equal(modmat_plm_re,   modmat_pFormula_pdf_re,   check.attributes = FALSE))) stop("model.matrix's are not the same")
66>
67>
68>
69>
70> ########### 2) pmodel.response.[pFormula|.plm|.data.frame] ###########
71>
72> # pooling and within models work on a pdata.frame [the plain pdata.frame is coerced to a model.frame
73> # internally in pmodel.response.pFormula]
74> #MM resp_pFormula_pool <- plm:::pmodel.response.formula(form, data = pGrunfeld, model = "pooling")
75> #MM resp_pFormula_fe   <- plm:::pmodel.response.formula(form, data = pGrunfeld, model = "within")
76>
77> resp_pFormula_pool <- plm:::pmodel.response.formula(form, data = pGrunfeld, model = "pooling")
78> resp_pFormula_fe   <- plm:::pmodel.response.formula(form, data = pGrunfeld, model = "within")
79>
80> # still fails
81> # resp_pFormula_re <- plm:::pmodel.response.pFormula(form, data = pGrunfeld, model = "random")
82> #
83> # Error in model.matrix.pFormula(pFormula(formula), data = data, model = model,  :
84> #   dims [product 200] do not match the length of object [0]
85>
86>
87> ### pmodel.response.data.frame on data.frame/pdata.frame
88> ## the 'data' data.frame for pmodel.response.data.frame must be a model.frame created by plm's model.frame
89> ## it needs to be a model.frame because then it is ensured we find the response variable in the fist column
90> #pGrunfeld_mf <- model.frame(pFormula(form), data = pGrunfeld)
91> pGrunfeld_mf <- model.frame(pGrunfeld, form)
92>
93> resp_pdf_mf_pool <- plm:::pmodel.response.data.frame(pGrunfeld_mf, model = "pooling") # works
94> resp_pdf_mf_fe   <- plm:::pmodel.response.data.frame(pGrunfeld_mf, model = "within")   # works
95> #resp_pdf_mf_re   <- plm:::pmodel.response.data.frame(pGrunfeld_mf, model = "random") # error, likely due to missing arguments
96>
97> ## these errored pre rev. 601 due to missing 'match.arg()' to set default value:
98> #pmodel.response(pFormula(form), data = pGrunfeld)
99> pmodel.response(form, data = pGrunfeld)
100      1       2       3       4       5       6       7       8       9      10
101 317.60  391.80  410.60  257.70  330.80  461.20  512.00  448.00  499.60  547.50
102     11      12      13      14      15      16      17      18      19      20
103 561.20  688.10  568.90  529.20  555.10  642.90  755.90  891.20 1304.40 1486.70
104     21      22      23      24      25      26      27      28      29      30
105 209.90  355.30  469.90  262.30  230.40  361.60  472.80  445.60  361.60  288.20
106     31      32      33      34      35      36      37      38      39      40
107 258.70  420.30  420.50  494.50  405.10  418.80  588.20  645.50  641.00  459.30
108     41      42      43      44      45      46      47      48      49      50
109  33.10   45.00   77.20   44.60   48.10   74.40  113.00   91.90   61.30   56.80
110     51      52      53      54      55      56      57      58      59      60
111  93.60  159.90  147.20  146.30   98.30   93.50  135.20  157.30  179.50  189.60
112     61      62      63      64      65      66      67      68      69      70
113  40.29   72.76   66.26   51.60   52.41   69.41   68.35   46.80   47.40   59.57
114     71      72      73      74      75      76      77      78      79      80
115  88.78   74.12   62.68   89.36   78.98  100.66  160.62  145.00  174.93  172.49
116     81      82      83      84      85      86      87      88      89      90
117  39.68   50.73   74.24   53.51   42.65   46.48   61.40   39.67   62.24   52.32
118     91      92      93      94      95      96      97      98      99     100
119  63.21   59.37   58.02   70.34   67.42   55.74   80.30   85.40   91.90   81.43
120    101     102     103     104     105     106     107     108     109     110
121  20.36   25.98   25.94   27.53   24.60   28.54   43.41   42.81   27.84   32.60
122    111     112     113     114     115     116     117     118     119     120
123  39.03   50.17   51.85   64.03   68.16   77.34   95.30   99.49  127.52  135.72
124    121     122     123     124     125     126     127     128     129     130
125  24.43   23.21   32.78   32.54   26.65   33.71   43.50   34.46   44.28   70.80
126    131     132     133     134     135     136     137     138     139     140
127  44.12   48.98   48.51   50.00   50.59   42.53   64.77   72.68   73.86   89.51
128    141     142     143     144     145     146     147     148     149     150
129  12.93   25.90   35.05   22.89   18.84   28.57   48.51   43.34   37.02   37.81
130    151     152     153     154     155     156     157     158     159     160
131  39.27   53.46   55.56   49.56   32.04   32.24   54.38   71.78   90.08   68.60
132    161     162     163     164     165     166     167     168     169     170
133  26.63   23.39   30.65   20.89   28.78   26.93   32.08   32.21   35.69   62.47
134    171     172     173     174     175     176     177     178     179     180
135  52.32   56.95   54.32   40.53   32.54   43.48   56.49   65.98   66.11   49.34
136    181     182     183     184     185     186     187     188     189     190
137   2.54    2.00    2.19    1.99    2.03    1.81    2.14    1.86    0.93    1.18
138    191     192     193     194     195     196     197     198     199     200
139   1.36    2.24    3.81    5.66    4.21    3.42    4.67    6.00    6.53    5.12
140> pmodel.response(pGrunfeld_mf)
141      1       2       3       4       5       6       7       8       9      10
142 317.60  391.80  410.60  257.70  330.80  461.20  512.00  448.00  499.60  547.50
143     11      12      13      14      15      16      17      18      19      20
144 561.20  688.10  568.90  529.20  555.10  642.90  755.90  891.20 1304.40 1486.70
145     21      22      23      24      25      26      27      28      29      30
146 209.90  355.30  469.90  262.30  230.40  361.60  472.80  445.60  361.60  288.20
147     31      32      33      34      35      36      37      38      39      40
148 258.70  420.30  420.50  494.50  405.10  418.80  588.20  645.50  641.00  459.30
149     41      42      43      44      45      46      47      48      49      50
150  33.10   45.00   77.20   44.60   48.10   74.40  113.00   91.90   61.30   56.80
151     51      52      53      54      55      56      57      58      59      60
152  93.60  159.90  147.20  146.30   98.30   93.50  135.20  157.30  179.50  189.60
153     61      62      63      64      65      66      67      68      69      70
154  40.29   72.76   66.26   51.60   52.41   69.41   68.35   46.80   47.40   59.57
155     71      72      73      74      75      76      77      78      79      80
156  88.78   74.12   62.68   89.36   78.98  100.66  160.62  145.00  174.93  172.49
157     81      82      83      84      85      86      87      88      89      90
158  39.68   50.73   74.24   53.51   42.65   46.48   61.40   39.67   62.24   52.32
159     91      92      93      94      95      96      97      98      99     100
160  63.21   59.37   58.02   70.34   67.42   55.74   80.30   85.40   91.90   81.43
161    101     102     103     104     105     106     107     108     109     110
162  20.36   25.98   25.94   27.53   24.60   28.54   43.41   42.81   27.84   32.60
163    111     112     113     114     115     116     117     118     119     120
164  39.03   50.17   51.85   64.03   68.16   77.34   95.30   99.49  127.52  135.72
165    121     122     123     124     125     126     127     128     129     130
166  24.43   23.21   32.78   32.54   26.65   33.71   43.50   34.46   44.28   70.80
167    131     132     133     134     135     136     137     138     139     140
168  44.12   48.98   48.51   50.00   50.59   42.53   64.77   72.68   73.86   89.51
169    141     142     143     144     145     146     147     148     149     150
170  12.93   25.90   35.05   22.89   18.84   28.57   48.51   43.34   37.02   37.81
171    151     152     153     154     155     156     157     158     159     160
172  39.27   53.46   55.56   49.56   32.04   32.24   54.38   71.78   90.08   68.60
173    161     162     163     164     165     166     167     168     169     170
174  26.63   23.39   30.65   20.89   28.78   26.93   32.08   32.21   35.69   62.47
175    171     172     173     174     175     176     177     178     179     180
176  52.32   56.95   54.32   40.53   32.54   43.48   56.49   65.98   66.11   49.34
177    181     182     183     184     185     186     187     188     189     190
178   2.54    2.00    2.19    1.99    2.03    1.81    2.14    1.86    0.93    1.18
179    191     192     193     194     195     196     197     198     199     200
180   1.36    2.24    3.81    5.66    4.21    3.42    4.67    6.00    6.53    5.12
181>
182>
183>
184> #### some sanity checks if various interfaces yield the same result ###
185> resp_plm_pool <- pmodel.response(plm_pool)
186> resp_plm_fe   <- pmodel.response(plm_fe)
187> resp_plm_re   <- pmodel.response(plm_re)
188>
189>
190> # compare interface pFormula with plm
191> if(!isTRUE(all.equal(resp_pFormula_pool, resp_plm_pool, check.attributes = FALSE))) stop("responses not equal")
192> if(!isTRUE(all.equal(resp_pFormula_fe,   resp_plm_fe, check.attributes = FALSE))) stop("responses not equal")
193> #if(!isTRUE(all.equal(resp_pFormula_re, resp_plm_re, check.attributes = FALSE))) stop("responses not equal")
194>
195>
196> # compare interface data.frame with model.frame with plm
197> if(!isTRUE(all.equal(resp_pdf_mf_pool, resp_plm_pool, check.attributes = FALSE))) stop("responses not equal")
198> if(!isTRUE(all.equal(resp_pdf_mf_fe,   resp_plm_fe, check.attributes = FALSE))) stop("responses not equal")
199> #if(!isTRUE(all.equal(resp_pdf_mf_re, resp_plm_re, check.attributes = FALSE))) stop("responses not equal")
200>
201>
202>
203>
204>
205> proc.time()
206   user  system elapsed
207   3.31    0.68    3.98
208