specr icon indicating copy to clipboard operation
specr copied to clipboard

Models are not found when they are elements of a list.

Open giuliogcantone opened this issue 3 years ago • 5 comments

I have a model called PQP

This code:

run_specs(df = db$db1,
          y = c("y"),
          x = c("x_000"),
          model = "PQP"
          ) -> a

runs with no errors.

Now I enlist PQP in the list models. This code:

run_specs(df = db$db1,
          y = c("y"),
          x = c("x_000"),
          model = "models$PQP"
          ) -> a

gives back this error:

Error in `dplyr::mutate()`:
! Problem while computing `res = map2(.data$model, formula,
  ~do.call(.x, list(data = df, formula = .y)))`.
Caused by error in `models$PQP`:
! could not find function "models$PQP"

UPDATE:

TimTeaFan provided a debug:

get_model <- function(x) {
  x_str <- str2lang(x)
  if (is.name(x_str)) {
    return(x)
    } else if (is.call(x_str)) {
    eval(x_str)
  }
}

Then specr:::run_spec is corrected like this:

specs %>%
  dplyr::mutate(formula = pmap(.,
                               specr:::create_formula)
  ) %>% tidyr::unnest(formula) %>% 
  dplyr::mutate(res = map2(model,
                           formula,
                           ~ do.call(get_model(.x), list(data = df, formula = .y))))

giuliogcantone avatar Aug 19 '22 10:08 giuliogcantone

I am trying to debug this, and I located the source of the bug in the internal code of run_spec

dplyr::mutate(res = map2(.data$model,
                           formula,
                           ~do.call(.x,
                                    list(data = df,
                                         formula = .y))))

giuliogcantone avatar Aug 19 '22 12:08 giuliogcantone

.x must be a character string naming the function to be used, or the function it self.. for example do.call("rbind", ...) or do.call(rbind, ...).

However, if you pass the character string "models$PQP" in run_specs(), then .x will be "models$PQP" (which is NOT a function or a character string naming the function)..

lmullany avatar Aug 19 '22 13:08 lmullany

.x must be a character string naming the function to be used, or the function it self.. for example do.call("rbind", ...) or do.call(rbind, ...).

However, if you pass the character string "models$PQP" in run_specs(), then .x will be "models$PQP" (which is NOT a function or a character string naming the function)..

My practical problem is that I want to process 32 model specifications in feglm(). Since these have a | in the formula, I have to code separately each of them as a model function, and then process them in the models param. I could set a env with 32 functions objects and that could work, but it is very not elegant. So I wanted to process them as elements of a list. If they are saved in the env, for example, they will keep popping while typing, which is ugly.

Maybe a solution is kinda... make a package p_adhoc of these 32 functions and call them with `p_adhoc::function"? I've never made a package tho.

Have you suggestions to not clog env with functions? I think that this is relevant to make advance this package for fixed effect models. EG, one may test more than 100 ad hoc fixed effects models.

giuliogcantone avatar Aug 19 '22 14:08 giuliogcantone

UPDATE:

TimTeaFan provided a debug:

get_model <- function(x) {
  x_str <- str2lang(x)
  if (is.name(x_str)) {
    return(x)
    } else if (is.call(x_str)) {
    eval(x_str)
  }
}

Then specr:::run_spec is corrected like this:

specs %>%
  dplyr::mutate(formula = pmap(.,
                               specr:::create_formula)
  ) %>% tidyr::unnest(formula) %>% 
  dplyr::mutate(res = map2(model,
                           formula,
                           ~ do.call(get_model(.x), list(data = df, formula = .y))))

giuliogcantone avatar Aug 19 '22 14:08 giuliogcantone

Hey guys, sorry for not chipping in earlier. I am currently on parental leave, hence have only limited time. Thanks to all for the contribution/discussion. I have been working on a new version of specr for a while (including several other aspects) and this will be interesting to add. Will get into it hopefully very soon!

Best, Philipp

masurp avatar Aug 20 '22 07:08 masurp