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

run_specs following setup_specs #26

Closed
akarlinsky opened this issue May 29, 2022 · 4 comments
Closed

run_specs following setup_specs #26

akarlinsky opened this issue May 29, 2022 · 4 comments

Comments

@akarlinsky
Copy link

akarlinsky commented May 29, 2022

For my analysis, there's a certain control variable which I want to be included in all specifications.
This is very easy to do after the fact by filtering for only rows where it appears:

 specr_results %>% 
  filter(str_detect(string = controls, pattern = fixed("important_control_var")))

When specr_results is small, this is ok. However, when specr_results is large, making it actually estimate the models without this crucial control variable is redundant. I thought to use setup_specs to setup my estimation tibble, filter that and then run_specs on that, thus reducing computation time immensely.

However, it seems that setup_specs is for illustration purposes only(?). There's no way that I see to pass on a setup_specs tibble to run_specs.

If I'm right, I suggest that you add an option to run_specs to get a setup_specs object as input OR to get x,y, controls etc.

P.S

Thanks for a great package!

@akarlinsky
Copy link
Author

similar to #23

@akarlinsky
Copy link
Author

akarlinsky commented May 29, 2022

For my purposes, I have the following, very hacky, solution:

run_specs2 <- function (df, 
                        spec_df, 
                        subsets = NULL, 
                        conf.level = 0.95, keep.results = FALSE) {

  specs <- spec_df
  if (!is.null(subsets)) {
    if (class(subsets) != "list") {
      wrong_class <- class(subsets)
      stop(glue("Subsets must be a 'list' and not a '{wrong_class}'."))
    }
    subsets <- map(subsets, as.character)
    df_list <- create_subsets(df, subsets)
    df_list[[length(df_list) + 1]] <- df %>% dplyr::mutate(filter = "all")
    if (length(subsets) > 1) {
      suppressMessages({
        df_comb <- subsets %>% cross %>% map(~create_subsets(subsets = .x, 
          df = df) %>% map(~dplyr::select(.x, -filter)) %>% 
          reduce(dplyr::inner_join) %>% dplyr::mutate(filter = paste(names(.x), 
          .x, collapse = " & ", sep = " = ")))
        df_all <- append(df_list, df_comb)
      })
    }
    else {
      df_all <- df_list
    }
    if (conf.level > 1 | conf.level < 0) {
      stop("The confidence level must be strictly greater than 0 and less than 1.")
    }
    map_df(df_all, ~run_spec(specs, .x, conf.level = conf.level, 
      keep.results = keep.results) %>% dplyr::mutate(subsets = unique(.x$filter)))
  }
  else {
    run_spec(specs, df, conf.level = conf.level, keep.results = keep.results) %>% 
      dplyr::mutate(subsets = "all")
  }
}

environment(run_specs2) <- asNamespace('specr')

I took the original code for run_specs, removed the x y controls model input and instead made a setup_specs output the input. A much better solution would be either user inputs x y controls model OR a setup_specs object.

@masurp
Copy link
Owner

masurp commented Dec 28, 2022

A new version of specr, which I hope to push to CRAN soon will solve this. Give me a bit more time! ;)

@masurp
Copy link
Owner

masurp commented Jan 16, 2023

The new development version 1.0.0 (which will be pushed to CRAN soon) implements such a framework, where you first setup specifications and then run them afterwards. Check the newly update website in this regard: https://masurp.github.io/specr/index.html

@masurp masurp closed this as completed Jan 16, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants