1

I have a function that calls the lm_robust function from the estimatr package in R. I want to be able to specify a variable on which to cluster standard errors, but the lm_robust function only allows bare (unquoted) variable names in lm_robust's cluster option while my function needs the input to be a quoted variable name.

How do I take a variable that is input into a function (such as "cl") and turn it into a unquoted variable (such as cl)?

Mackenzie
  • 91
  • 1
  • 3
  • Perhaps `?noquote`? – NelsonGon Feb 22 '19 at 17:03
  • 1
    It's easier to help you if you include a simple [reproducible example](https://stackoverflow.com/questions/5963269/how-to-make-a-great-r-reproducible-example) with sample input and desired output that can be used to test and verify possible solutions. It's not about just removing quotes. It's about turning a character value into a symbol and then making sure that symbol gets properly evaluated. Different packages do this in different ways so having an example here to test with would be valuable. – MrFlick Feb 22 '19 at 17:04

1 Answers1

3

Use as.name in do.call:

library(estimatr)
example(lm_robust)
## ... snip ...

clname <- "clusterID"
do.call("lm_robust", list(y ~ x + z, data = quote(dat), weights = quote(w),
   clusters = as.name(clname)))

giving:

              Estimate Std. Error    t value     Pr(>|t|)  CI Lower  CI Upper
(Intercept)  3.4261621  0.2009692 17.0481986 1.332761e-05  2.908643  3.943681
x           -0.6734741  0.1351184 -4.9843254 4.300590e-03 -1.022076 -0.324872
z            0.5850340  0.9436175  0.6199907 5.566933e-01 -1.689652  2.859720
                  DF
(Intercept) 4.970885
x           4.940162
z           6.396615
G. Grothendieck
  • 254,981
  • 17
  • 203
  • 341