ycroissant / plm

Panel Data Econometrics with R
GNU General Public License v2.0
49 stars 13 forks source link

Formula Expansions by . (dot) in plm Estimation Commands #35

Closed SebKrantz closed 1 year ago

SebKrantz commented 1 year ago

From Twitter: Is there any reason why in #R with the plm-command for a fixed-effects-regression I cannot for the formula use the usual " y ~ . , data = df, ..." notation? It only works if I explicitly give it " y ~ x1 + x2 + ...". Which is impracticable if you have 440 variables...

PS: It’s a matter of passing the data frame to the data argument of terms.formula e.g. terms(mpg ~ ., data = mtcars) does the job.

tappek commented 1 year ago

Thank you for bringing this to our attention! I had a first look and need to look at this more closely at some later point in time. As a reminder for myself: It stems from plm::has.intercept which in turn calls terms without the necessary data argument when a dot is present. Minimal example:

plm::has.intercept(y ~ .) # errors 
plm::has.intercept(y ~ ., data = <dataset>) # works

Another observation about Formula:::model.matrix.Formula which we use:

library(Formula)
model.matrix(as.Formula(inv ~ .), data = Grunfeld[ , -c(1:2)], rhs = 1, dot = "separate") # works
model.matrix(as.Formula(inv ~ .), data = Grunfeld[ , -c(1:2)], rhs = 1, dot = "previous") # errors

-> The branch dot = "previous" does not have the data argument in its call to terms. Edit: this observation is about Formula 1.2-4 and I addressed it to its maintainer. It is now already fixed on R-Forge in version 1.2-5 by commit https://r-forge.r-project.org/scm/viewvc.php/pkg/R/Formula.R?root=formula&r1=53&r2=54&pathrev=54

tappek commented 1 year ago

I have something 'almost ready' and hope to commit soon. Will go into next release.

tappek commented 1 year ago

fixed in 76b03dfc26c1d762e32229231025a547d3b2c810

This requieres package Formula in min. version 1.2-5 currently not on CRAN but available on R-Forge.

tappek commented 1 year ago

addendum: pkg Formula 1.2-5 now on CRAN / 2023-02-24