Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Open
giuliogcantone opened this issue Aug 19, 2022 · 5 comments
Open

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

giuliogcantone opened this issue Aug 19, 2022 · 5 comments
Labels
enhancement New feature or request

Comments

@giuliogcantone
Copy link

giuliogcantone commented Aug 19, 2022

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
Copy link
Author

giuliogcantone commented Aug 19, 2022

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

@lmullany
Copy link

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

@giuliogcantone
Copy link
Author

.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
Copy link
Author

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

@masurp
Copy link
Owner

masurp commented Aug 20, 2022

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 masurp added the enhancement New feature or request label Aug 20, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request
Projects
None yet
Development

No branches or pull requests

3 participants