Models are not found when they are elements of a list.
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))))
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))))
.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)..
.xmust be a character string naming the function to be used, or the function it self.. for exampledo.call("rbind", ...)ordo.call(rbind, ...).However, if you pass the character string
"models$PQP"inrun_specs(), then.xwill 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.
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))))
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