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

Reorder coefficients by regex #62

Open
zauster opened this issue Sep 13, 2017 · 6 comments
Open

Reorder coefficients by regex #62

zauster opened this issue Sep 13, 2017 · 6 comments

Comments

@zauster
Copy link
Contributor

zauster commented Sep 13, 2017

Allow coefficients to be re-ordered by a vector of regex:

Eg:
reorder.coef = c("^log", "^GDP", "^Population")
would put all coefficients that start with "log" on top, then "GDP" and then "Population" coefficients.

Motivation: I routinely have model with some fixed effects and interactions, then I would have to calculate the number of created dummy variables and based on these I could reorder them.

@leifeld leifeld added this to the 1.36.26 milestone Mar 15, 2019
@leifeld leifeld removed this from the 1.36.26 milestone May 28, 2020
@Irazall
Copy link

Irazall commented Oct 4, 2022

I would like to see this as well :)

@leifeld
Copy link
Owner

leifeld commented Jan 19, 2023

I'd be open to a pull request if anyone wants to give it a try. The relevant code fragments are the reorder function, potentially the lines in the texreg, screenreg etc functions that call the reorder function, and two places in the plotreg function, which handles reordering in a different way, I think.

@czucca
Copy link
Collaborator

czucca commented Jan 20, 2023

Yes, plotreg has its own way of reordering. I confirm that.

@maifeng
Copy link

maifeng commented Aug 11, 2024

For those interested in re-ordering variable names based on regular expression patterns, here is a workaround.

# Assume you have a list of models
models <- list(model1, model2, model3)

# Extract all unique var names from the models
all_names <- unique(unlist(lapply(models, function(model) names(coef(model)))))

regex_patterns <- c("^log", "^GDP", "^Population")

ordered_indices <- c()
matched <- rep(FALSE, length(all_names))

# Loop through each regex pattern and find matching indices
for (pattern in regex_patterns) {
    # Find indices that match the current pattern and have not been matched yet
    current_indices <- which(grepl(pattern, all_names) & !matched)

    # Append these indices to the ordered_indices
    ordered_indices <- c(ordered_indices, current_indices)

    # Mark these indices as matched
    matched[current_indices] <- TRUE
}

# Add any remaining indices that do not match any pattern
remaining_indices <- which(!matched)
ordered_indices <- c(ordered_indices, remaining_indices)

screenreg(models, reorder.coef = ordered_indices)

@maifeng
Copy link

maifeng commented Aug 11, 2024

I can submit a pull request when I test the implementation a bit more. See
master...maifeng:texreg:coef_reorder

@leifeld Is it ok if I implement it to so that reorder.coef can also take a vector of regular expressions as input? I won't touch plotreg for now.

@leifeld
Copy link
Owner

leifeld commented Aug 14, 2024

@maifeng Thanks, this looks useful. I would be happy to see a pull request, provided you can also do the following:

  • Move your function to the appropriate place in the file, after the reorder function.
  • Make sure this also works for plotreg. Liaise with @czucca, who wrote the plotreg function, if necessary.
  • Ensure nothing else breaks, and fix any issues in the GitHub Action workflows that may arise. (These are typically reported in the pull request.)
  • Add a testthat unit test that checks if the code does what it's supposed to do. This will ensure we don't break it by accident with future revisions.
  • Increment the version number to 1.39.5.

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

5 participants