'pglm doesn't use any variables from local environment

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?



Solution 1:[1]

Building on Santiago Squarzon's helpful comments:

  • Sorting by the .CreationTime property of the [System.IO.FileInfo] instances that Get-ChildItem outputs works fine and relies on the file-system metadata for the timestamp of each file's creation.

  • A string representation of a (creation) date embedded in a file's name may or may not reflect the file's actual creation timestamp (and even if it did, it wouldn't be an exact match, given that the file-system-maintained timestamps have sub-second granularity.

To sort by actual .CreationTime, as reported by the file-system, you can use the following simplified version of your own attempt, given that there's no need for a script block ({ ... }) to reference the .CreationTime property:

Get-ChildItem C:\File*.txt | Sort-Object CreationTime | Select-Object Name

To sort by the timestamp string embedded in your file names:

Get-ChildItem C:\File*.txt | 
  Sort-Object {
    [datetime] (
      $_.BaseName -replace '^.+_(\d{4})(\d{2})(\d{2}) (\d{2})(\d{2})', '$1-$2-$3 $4:$5:'
    ) 
  } | 
    Select-Object Name

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 mklement0