jepusto / clubSandwich

Cluster-robust (sandwich) variance estimators with small-sample corrections
http://jepusto.github.io/clubSandwich/
47 stars 8 forks source link

vcovCR() on a plm object doesn't work within a function #41

Closed StatPhil closed 4 years ago

StatPhil commented 4 years ago

Calling vcovCR() on a plm object does not work properly when it's called within a function.

Here's a reproducible example:

example.of.problem <- function(){
  require(plm)
  require(clubSandwich)
  our.data <- iris
  model.output <- plm(formula = "Sepal.Length ~ Sepal.Width + Petal.Length + Petal.Width", data = our.data, model = "within", index = "Species")
  vcovCR(model.output, cluster = our.data$Species, type="CR1S")
}
example.of.problem()

This results in an error message, whereas running the code that's inside the function works just fine if it's not inside a function.

jepusto commented 4 years ago

This seems to be an issue with plm rather than clubSandwich:

example.of.problem <- function(){
  require(plm)
  # require(clubSandwich)
  our.data <- iris
  model.output <- plm(formula = "Sepal.Length ~ Sepal.Width + Petal.Length + Petal.Width", data = our.data, model = "within", index = "Species")
  # vcovCR(model.output, cluster = our.data$Species, type="CR1S")
  model.output
}
example.of.problem()
#> Loading required package: plm
#> Loading required package: Formula
#> Error in pFormula(formula): inherits(object, "formula") is not TRUE
ghost commented 4 years ago

Well, it is a input error: the argument formula should not be a character. Just take:

model.output <- plm(formula = Sepal.Length ~ Sepal.Width + Petal.Length + Petal.Width, data = our.data, model = "within", index = "Species")

...and jepusto's example derived from the original post works on my side.