Home > front end >  lag() from pglm seems to bug lag() from stats
lag() from pglm seems to bug lag() from stats

Time:12-28

As the title says. After I load pglm, lag stops to work properly.

library(pglm)

c(1,2,3,4) %>% lag()

the object is converted into a time series and is not compatible anymore with tibbles.

Even unloading pglm, the dependency for lag is still effective.

A solution could be to actually never load pglm, but then if I have a lag(x) in the formula

pglm:pglm(
family= poisson,
y ~ lag(x),
model = "within", index="id",
data = db
)

The algorithm cannot converge into an estimate. For some reasons this happens even forcing stats::lag(x). The fun thing is that, instead, if pglm is loaded, y ~ lag(x) works properly as y ~ stats:lag(x).

This is the only case where it works, tho! The only other thing that I think of is that outside formulas, dplyr::lag is the culprit for the conflict.

I don't know how to optimise the workflow, have you suggestions?

CodePudding user response:

dplyr is the culprit here. plm (used by pglm) defines a lag method lag.pseries which is dispatched by the generic lag in stats. Other packages such as plm supply methods for the generic lag; however, dplyr clobbers the generic with its own non-generic lag so all packages that use lag then fail. If you need to have dplyr loaded at the same time as pglm load dplyr excluding lag and if you need to use dplyr's lag use dplyr::lag . Ditto for filter.

library(dplyr, exclude = c("filter", "lag"))
  • Related