Home > OS >  Using "marg" in a function - how to enquote correctly?
Using "marg" in a function - how to enquote correctly?

Time:11-26

Imagine I have a regression like:

regression <- glm(mpg ~ am * hp, data = mtcars)

I can easily calculate marginal effects with the modmarg package:

library(modmarg)
margins <- marg(
    regression, var_interest = "am",
    at = list("hp" = 52:110),
    type = "effects")

However, I have many models with different variables to compute margins for, so I'd like to put this into a function:

plotting_marg <- function(reg, cov) {
  margins <- marg(
    reg, var_interest = "truth",
    at = list(cov = 52:110),
    type = "effects") 
}
plotting_marg(regression, "hp", -3, 3)
Error in .marg(mod = mod, var_interest = var_interest, data = data, weights = weights,  : 
  var_interest %in% names(data) is not TRUE

This does not work, presumably because cov is not correctly enquoted. I have played around with enquo, but no luck. What is the way to go here?

CodePudding user response:

Naming the list with an object passed can be done with setNames

plotting_marg <- function(reg, cov) {
  marg(
    reg, var_interest = "am",
    at = setNames(list(52:110), cov),
    type = "effects") 
}

-testing

> plotting_marg(regression, "hp")
$`hp = 52`
   Label   Margin Standard.Error Test.Stat    P.Value Lower CI (95%) Upper CI (95%)
1 am = 0 0.000000       0.000000       NaN        NaN       0.000000       0.000000
2 am = 1 5.238604       1.918006  2.731276 0.01079466       1.309746       9.167462

$`hp = 53`
   Label   Margin Standard.Error Test.Stat    P.Value Lower CI (95%) Upper CI (95%)
1 am = 0 0.000000       0.000000       NaN        NaN       0.000000       0.000000
2 am = 1 5.239007       1.904537  2.750803 0.01030278       1.337739       9.140274

$`hp = 54`
   Label   Margin Standard.Error Test.Stat     P.Value Lower CI (95%) Upper CI (95%)
1 am = 0 0.000000       0.000000       NaN         NaN       0.000000       0.000000
2 am = 1 5.239409       1.891116  2.770539 0.009827057       1.365635       9.113184
...

Or may require lst from dplyr/purrr

plotting_marg <- function(reg, cov) {
  marg(
    reg, var_interest = "am",
    at = dplyr::lst(!!cov := 52:110),
    type = "effects") 
}

-testing

> plotting_marg(regression, "hp")
$`hp = 52`
   Label   Margin Standard.Error Test.Stat    P.Value Lower CI (95%) Upper CI (95%)
1 am = 0 0.000000       0.000000       NaN        NaN       0.000000       0.000000
2 am = 1 5.238604       1.918006  2.731276 0.01079466       1.309746       9.167462

$`hp = 53`
   Label   Margin Standard.Error Test.Stat    P.Value Lower CI (95%) Upper CI (95%)
1 am = 0 0.000000       0.000000       NaN        NaN       0.000000       0.000000
2 am = 1 5.239007       1.904537  2.750803 0.01030278       1.337739       9.140274

$`hp = 54`
   Label   Margin Standard.Error Test.Stat     P.Value Lower CI (95%) Upper CI (95%)
1 am = 0 0.000000       0.000000       NaN         NaN       0.000000       0.000000
2 am = 1 5.239409       1.891116  2.770539 0.009827057       1.365635       9.113184
...
  • Related