Mackenzie
Mackenzie

Reputation: 91

Taking quoted variable in a function and making it bare

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)?

Upvotes: 1

Views: 188

Answers (1)

G. Grothendieck
G. Grothendieck

Reputation: 269586

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

Upvotes: 3

Related Questions