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

Get the CI as a data.frame after bootstrap #38

Closed
alaindanet opened this issue Mar 10, 2022 · 2 comments
Closed

Get the CI as a data.frame after bootstrap #38

alaindanet opened this issue Mar 10, 2022 · 2 comments
Labels
enhancement New feature or request

Comments

@alaindanet
Copy link

Thank you so much for the nice package!

semEff() provides a very nice print summary of the CI and all the effects, that is great.
I wanted to get this summary as a table but I did not find how to (Have I missed something?)
getEff() does return bootstrap but not the CI, right? I found hard to recompute the bca interval from getEff result.

In the meantime, I just made a function which takes the output of semEff() and extract the coefficients from the summary.

get_table_semEff <- function(semeff = NULL) {

  effect_type <-  c("^DIRECT", "^INDIRECT", "TOTAL", "MEDIATORS")
  x  <- semeff[-1, ]
  effect_type_col <- x[[1]] 
  predictor <- x[[2]] 

  row_effect_type <- map_int(effect_type, ~which(str_detect(x[[1]], .x)))

  numeric_table <- map(x, as.numeric)
  # suppr table columns
  mask_chr_column <- map_lgl(numeric_table, ~all(is.na(.x)))
  # Keep the second columns: 
  mask_chr_column[2] <- FALSE 

  tab <- x[, !mask_chr_column]
  colnames(tab)[1] <- "predictor"

  output <- rbind(
    cbind(effect_type = "direct", tab[row_effect_type[1]:row_effect_type[2]-1, ]),
    cbind(effect_type = "indirect", tab[row_effect_type[2]:row_effect_type[3]-1, ]),
    cbind(effect_type = "total", tab[row_effect_type[3]:row_effect_type[4]-1, ]),
    cbind(effect_type = "mediators", tab[row_effect_type[4]:nrow(tab), ])
  )

  numeric_rows <- str_detect(output[["Effect"]], "\\d")
  output <- output[numeric_rows,]
  output[, -c(1:2)] <- apply(output[, -c(1:2)], 2, as.numeric)

  janitor::clean_names(output)
}

from_semEff_to_table <- function(x = NULL) {

  output <- map_dfr(x$Summary[-1], get_table_semEff, .id = "response") %>%
    as_tibble()

  output[, c("response", "predictor")] <- apply(
    output[, c("response", "predictor")], 2,
    function(x) str_replace_all(x, c("\\." = "_", "\\s" = ""))
  )
  output[, colnames(output) != "bias"]

}

ci_df <- from_semEff_to_table(x = my_semeff_output)
@murphymv
Copy link
Owner

Hi Alain,

Thanks for your comment, glad you're enjoying the package! At the moment there is no way to output all effects in a single table, but individual (formatted) tables are accessible in a list via my_semeff_output$Summary, as you'll have seen. Those could be combined together, as in the way you've done (nicely). I might implement something like that in a future release, I'll let you know!

And yes getEff() is just used to extract the original calculated effects as numeric vectors/matrices, so no CIs in there.

Cheers,
Mark

@murphymv
Copy link
Owner

murphymv commented Sep 8, 2024

Hi, this functionality has finally been added to the latest version of the package (0.7.0), via getEffTable(). Thanks for the prompt and helpful contributions!

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

2 participants