R/utils-coefficent-to-p.R

Defines functions coefficent_to_p

Documented in coefficent_to_p

#' change coefficient to p value for model_table
#'
#' @param data_frame data
#' @param marginal_alpha marginal_alpha leve
#' @param show_p show por not
#' @keywords internal
#' @export
#'
coefficent_to_p = function(data_frame,
                           marginal_alpha = 0.1,
                           show_p = FALSE) {
  return_df = data_frame %>%
    dplyr::mutate(dplyr::across(dplyr::any_of(c("p", "P")), function(x) {
      dplyr::case_when(
        x == "" ~  "   ",
        x <= 0.001 ~ "***",
        x <= 0.01 & x > 0.001 ~ "** ",
        x <= 0.05 & x > 0.01 ~  "*  ",
        x <= marginal_alpha & x > 0.05 ~  ".  ",
        x > marginal_alpha ~  "   ",
        TRUE ~  "   "
      )
    })) %>% 
    dplyr::mutate(Coefficient = paste(format_round(.data$Coefficient,digits = 3),.data$p))
  
  if (show_p == TRUE) {
    return_df = data_frame %>% 
      dplyr::mutate(Coefficient = paste0(format_round(.data$Coefficient,digits = 3),' (',format_round(.data$p,digits = 3),')')) %>% 
      dplyr::mutate(dplyr::across(dplyr::any_of(c("p", "P")), function(x) {
        dplyr::case_when(
          x == "" ~  "   ",
          x <= 0.001 ~ "***",
          x <= 0.01 & x > 0.001 ~ "** ",
          x <= 0.05 & x > 0.01 ~  "*  ",
          x <= marginal_alpha & x > 0.05 ~  ".  ",
          x > marginal_alpha ~  "   ",
          TRUE ~  "   "
        )
      })) %>% 
      dplyr::mutate(Coefficient = paste(.data$Coefficient,.data$p))
    
    
  }
  
  return_df = return_df %>% 
    dplyr::select(dplyr::any_of(c('Parameter','Coefficient')))
  
  return(return_df)
}

Try the psycModel package in your browser

Any scripts or data that you put into this service are public.

psycModel documentation built on Nov. 2, 2023, 6:02 p.m.