1
#' Remove regression reference level row from table
2
#'
3
#' This looks for a column with a name including "Coefficient", "OR", or "HR"
4
#' (\code{\link{finalfit}} defaults) and removes any rows with "-" (the default
5
#' for the reference level). Can also be combined to produce an
6
#' \code{\link{or_plot}}, see below.
7
#'
8
#' @param .data Output from \code{\link{finalfit}} or similar.
9
#' @param only_binary Logical. Remove reference level only for two-level
10
#'   factors. When set to false, reference level for all factors removed.
11
#'
12
#' @return Data frame.
13
#' @export
14
#'
15
#' @examples
16
#' # Table example
17
#' explanatory = c("age.factor", "age", "sex.factor", "nodes", "obstruct.factor", "perfor.factor")
18
#' dependent = 'mort_5yr'
19
#' colon_s %>%
20
#' 	 finalfit(dependent, explanatory, add_dependent_label = FALSE) %>%
21
#' 	 ff_remove_ref() %>%
22
#' 	 dependent_label(colon_s, dependent)
23
#'
24
#' # Plot example
25
#' explanatory = c("age.factor", "age", "sex.factor", "nodes", "obstruct.factor", "perfor.factor")
26
#' dependent = 'mort_5yr'
27
#' colon_s %>%
28
#'   summary_factorlist(dependent, explanatory, total_col = TRUE, fit_id=TRUE) %>%
29
#'   ff_merge(
30
#'     glmuni(colon_s, dependent, explanatory) %>%
31
#'     fit2df()) %>%
32
#'   ff_remove_ref() %>%
33
#'   dplyr::select(-`OR`) -> factorlist_plot
34
#'
35
#' colon_s %>%
36
#'    or_plot(dependent, explanatory, factorlist = factorlist_plot)
37
ff_remove_ref <- function(.data, only_binary = TRUE){
38 1
	if(!any(names(.data) == "label")) stop("finalfit function must include: add_dependent_label = FALSE")
39
	# estimate_col = grep("Coefficient*|OR*|HR*", names(.data), value=TRUE)[1]
40 1
	df.out = .data %>% 
41 1
		dplyr::mutate(label = ifelse(label == "", NA, label)) %>% 
42 1
		tidyr::fill(label) %>% 
43 1
		dplyr::group_by(label)
44 1
	if(only_binary){
45 1
		df.out = df.out %>% 
46 1
			dplyr::filter(levels %in% c("Mean (SD)", "Median (IQR)") | 
47 1
											dplyr::row_number() != 1 |
48 1
											dplyr::n() > 2)
49
	} else {
50 0
		df.out = df.out %>% 
51 0
			dplyr::filter(levels %in% c("Mean (SD)", "Median (IQR)") | 
52 0
											dplyr::row_number() != 1)
53
	}
54 1
	df.out %>% 
55 1
		as.data.frame() %>% 
56 1
		rm_duplicate_labels()
57
}
58

59
#' @rdname ff_remove_ref
60
#' @export
61
finalfit_remove_ref = ff_remove_ref
62

63

64
#' Remove p-value from output
65
#'
66
#' This will work with \code{\link{finalfit}} and any \code{\link{fit2df}}
67
#' output.
68
#'
69
#' @param .data Output from \code{\link{finalfit}} or similar.
70
#'
71
#' @return Data frame.
72
#' @export
73
#'
74
#' @examples
75
#' explanatory = c("age.factor", "sex.factor", "obstruct.factor", "perfor.factor")
76
#' dependent = 'mort_5yr'
77
#' colon_s %>%
78
#'   finalfit(dependent, explanatory) %>%
79
#'   ff_remove_p()
80
ff_remove_p <- function(.data){
81 0
	.data %>% 
82 0
		dplyr::mutate_all(~ gsub(", p[=<][0123456789.]*", "", .)) 
83
}
84

85
#' @rdname ff_remove_p
86
#' @export
87
finalfit_remove_p = ff_remove_p
88

89

90

91
#' Include only percentages for factors in \code{\link{summary_factorlist}} output
92
#'
93
#' @param .data Output from \code{\link{finalfit}} or similar.
94
#'
95
#' @return Data frame.
96
#' @export
97
#'
98
#' @examples
99
#' explanatory = c("age.factor", "sex.factor", "obstruct.factor", "perfor.factor")
100
#' dependent = 'mort_5yr'
101
#' colon_s %>%
102
#'   summary_factorlist(dependent, explanatory) %>%
103
#'   ff_percent_only()
104
ff_percent_only <- function(.data){
105 0
	if(!any(names(.data) == "label")) stop("summary_factorlist() must include: add_dependent_label = FALSE")
106 0
	.data %>% 
107 0
		dplyr::mutate_at(vars(-dplyr::one_of("label", "levels", "p")), ~ dplyr::case_when(
108 0
			!levels %in% c("Mean (SD)", "Median (IQR)") ~ stringr::str_extract(., "(?<=\\().+?(?=\\))"), 
109 0
			TRUE ~ .))
110
}
111

112
#' @rdname ff_percent_only
113
#' @export
114
finalfit_percent_only = ff_percent_only

Read our documentation on viewing source code .

Loading