pglm doesn't use any variables from local environment

Viewed 73

I am writing some re-usable (hopefully) R code, and part of it is a pglm helper function:

pglm_helper <- function(family, fmla, fixed_effect, tbl) {
  pglm::pglm(
    fmla,
    family=family,
    data=tbl,
    effect="individual",
    model="within",
    index=fixed_effect
  )
}

However, it tries to pass in "fixed_effect" as the index, not the string the symbol represents. If I pass in the correct string or create a global variable with fixed_effect <<- fixed_effect, this works.

pglm_helper <- function(family, fmla, fixed_effect, tbl) {
  pglm::pglm(
    fmla,
    family=family,
    data=tbl,
    effect="individual",
    model="within",
    index="pair_id"
  )
}

However, once I get index to work, it complains about tbl being a function, as well as family. In fact, even running this:


model_3_4_pois <- function(my_tbl) {
  pglm::pglm(
    cites ~
      as.factor(year)
      + as.factor(age)
      + is_brc_window
      + is_brc_post_deposit,
    family="poisson",
    data=my_tbl,
    effect="individual",
    model="within",
    index="article_id"
  )
}

It can't find my_tbl.

It appears as though this function completely ignores the local environment; is there anyway to get this function evaluate the local environment?

1 Answers

Using the debugger helps to understand the cause of the error.
pglm() calls an internal function starting.values() which calls :

eval(startcl, parent.frame())

where startcl is the original expression:

pglm::pglm(
    cites ~
      as.factor(year)
      + as.factor(age)
      + is_brc_window
      + is_brc_post_deposit,
    family="poisson",
    data=my_tbl,
    effect="individual",
    model="within",
    index="article_id"
  )

If my_tbl isn't in the global environment (this isn't the case because you created a function), the eval function doesn't find it in parent.frame(), ie inside pglm environment, hence the error.

What you did with <<- is correct : you forced the data to the global environment, which allowed the eval to work.

As a workaround, you could write the necessary variables inside pglm environment before calling it inside the helper function:

pglm_helper <- function(family, fmla, fixed_effect, tbl) {

# Modify pglm environment 
environment(pglm) <- list2env(list(tbl=tbl,fixed_effect=fixed_effect,family=family,fmla=fmla), parent = environment(pglm))

pglm::pglm(
    fmla,
    family=family,
    data=tbl,
    effect="individual",
    model="within",
    index="pair_id"
  )
}

As you didn't provide data, I tested this with pglm Parking data :

library(pglm)
data('Fairness', package = 'pglm')
Parking <- subset(Fairness, good == 'parking')

pglm_helper <- function(my_tbl,fixed_effect) {
  environment(pglm) <- list2env(list(my_tbl=my_tbl,fixed_effect=fixed_effect), parent = environment(pglm))
  pglm(as.numeric(answer) ~ education + rule,
       data=my_tbl,
       family = ordinal('probit'), R = 5, print.level = 3,
       method = 'bfgs', index = fixed_effect, model = "random")
}

pglm_helper(Parking,"id")
#> Initial function value: -2736.51 
#> Initial gradient value:
#>      (Intercept)      educationno        ruleadmin      rulelottery 
#>      -11.4914740       -2.8847481      -16.3558672      -10.1084711 
#>    ruleaddsupply      rulequeuing        rulemoral rulecompensation 
#>        0.1128396        4.3919960       14.9698844       10.8792330 
#>             mu_1             mu_2            sigma 
#>       23.2267330       49.7890848      -97.9365186 
#> initial  value 2736.509920 
#> iter   2 value 2722.178812
#> iter   3 value 2721.077354
#> iter   4 value 2720.804310
#> iter   5 value 2720.398072
#> iter   6 value 2720.232869
#> iter   7 value 2719.917545
#> iter   8 value 2719.854690
#> iter   9 value 2719.622599
#> iter  10 value 2718.012705
#> iter  11 value 2717.614770
#> iter  12 value 2717.603369
#> iter  13 value 2717.154671
#> iter  14 value 2716.906182
#> iter  15 value 2716.688790
#> iter  16 value 2716.688702
#> final  value 2716.688698 
#> converged
#> Maximum Likelihood estimation
#> BFGS maximization, 68 iterations
#> Return code 0: successful convergence 
#> Log-Likelihood: -2716.689 (11 free parameter(s))
#> Estimate(s): -0.2670471 -0.2835027 -0.0670941 0.2372356 1.225338 1.850054 2.837842 2.628994 1.016765 2.517224 0.5495621
Related