DeclareDesign / estimatr

estimatr: Fast Estimators for Design-Based Inference
https://declaredesign.org/r/estimatr
Other
131 stars 20 forks source link

Bug: rlang::sym() doesn't work for `lm_lin` #413

Open bfifield opened 1 month ago

bfifield commented 1 month ago

I'm attempting to map() over a series of outcomes and apply lm_lin() to each outcome separately with a uniform set of control variables. However, when I do, I get the error:

Error in `map()`:
ℹ In index: 1.
Caused by error in `sym()`:
! could not find function "sym"
Run `rlang::last_trace()` to see where the error occurred.

This does not happen when using lm_robust(), which runs fine. I've included a reproducible example below:

library(tidyverse)
library(estimatr)

N = 100
df <- data.frame(
  outcome_a = rbinom(N, 1, .5),
  outcome_b = rbinom(N, 1, .5),
  treatment = rbinom(N, 1, .5),
  cov = rbinom(N, 1, .5)
)

outcomes <- c("outcome_a", "outcome_b")

# Works
outcomes |>
  map(
    ~lm_robust(!!sym(.x) ~ treatment + cov, data = df) |> 
      tidy() |>
      mutate(outcome = .x)
  ) |> bind_rows()

# Does not work
outcomes |>
  map(
    ~lm_lin(!!sym(.x) ~ treatment, covariates = ~ cov, data = df) |> 
      tidy() |>
      mutate(outcome = .x)
  ) |> bind_rows()
macartan commented 1 month ago

others will have better answers for this odd behavior but in the meantime I think this will wrk

outcomes |> map( ~lm_lin(.x |> paste(" ~ treatment") |> as.formula(), covariates = ~ cov, data = df) |> tidy() |> mutate(outcome = .x) ) |> bind_rows()

On Wed, Oct 16, 2024 at 3:50 PM Ben Fifield @.***> wrote:

I'm attempting to map() over a series of outcomes and apply lm_lin() to each outcome separately with a uniform set of control variables. However, when I do, I get the error:

Error in map(): ℹ In index: 1. Caused by error in sym(): ! could not find function "sym" Run rlang::last_trace() to see where the error occurred.

This does not happen when using lm_robust(), which runs fine. I've included a reproducible example below:

library(tidyverse) library(estimatr)

N = 100 df <- data.frame( outcome_a = rbinom(N, 1, .5), outcome_b = rbinom(N, 1, .5), treatment = rbinom(N, 1, .5), cov = rbinom(N, 1, .5) )

outcomes <- c("outcome_a", "outcome_b")

Works

outcomes |> map( ~lm_robust(!!sym(.x) ~ treatment + cov, data = df) |> tidy() |> mutate(outcome = .x) ) |> bind_rows()

Does not work

outcomes |> map( ~lm_lin(!!sym(.x) ~ treatment, covariates = ~ cov, data = df) |> tidy() |> mutate(outcome = .x) ) |> bind_rows()

— Reply to this email directly, view it on GitHub https://github.com/DeclareDesign/estimatr/issues/413, or unsubscribe https://github.com/notifications/unsubscribe-auth/ADBE57L6Z3GDIKK3USZ2HEDZ3ZVL7AVCNFSM6AAAAABQBQ3NZ2VHI2DSMVQWIX3LMV43ASLTON2WKOZSGU4TCOJWGUYTQNQ . You are receiving this because you are subscribed to this thread.Message ID: @.***>

bfifield commented 1 month ago

Thanks, @macartan - confirming this runs correctly for me. Keeping the issue open given that behavior is not consistent between lm_robust and lm_lin.