polish.Rmd
library(mRclwhip)
The idea behind mRclwhip::polish()
is to create a more ‘complete product’ than what is returned from broom::tidy()
, for example.
The function can either return a flextable::flextable()
object if the argument .flextable = TRUE
.
Or, if the argument .flextable = FALSE
, then a list with two objects is returned where the first object in the list is a dataframe of the polished results that can be further manipulated.
The second object in the list is a vector of row numbers that can then be passed into flextable::padding()
(after passing the dataframe into mRclwhip::format_flextable()
of course) to pad the rows with factor levels.
The example at the end will provide further clarification.
coxph()
mockstudy <- arsenal::mockstudy
coxph_res <- survival::coxph(survival::Surv(fu.time, fu.stat) ~ race + sex + bmi, data = mockstudy)
Hmisc::label(mockstudy$sex) <- "Gender"
coxph_res %>%
polish(
.header1 = list(values = c("", "HR (95% CI)")),
exponentiate = T
)
HR (95% CI) |
|
Race |
|
Asian |
0.58 (0.33, 1.04) |
Caucasian |
0.97 (0.79, 1.19) |
Hawaii/Pacific |
0.91 (0.34, 2.48) |
Hispanic |
0.86 (0.60, 1.22) |
Native-Am/Alaska |
5.99 (2.42, 14.81) |
Other |
0.97 (0.45, 2.10) |
Gender (Female) |
0.99 (0.88, 1.10) |
Body Mass Index (kg/m^2) |
0.98 (0.97, 0.99) |
lm()
mtcars <- mtcars %>%
dplyr::mutate_at(dplyr::vars(gear, am, carb), as.character)
mtcars <- Hmisc::upData(
mtcars,
labels = c(mpg = "MPG",
hp = "HP",
gear = "# of gears",
am = "Automatic",
wt = "Weight",
carb = "Carb"
),
print = F
)
lm_res <- lm(mpg ~ hp + gear + am + wt + carb, data = mtcars)
lm_res %>%
polish(
.header1 = list(values = c("", "Estimate (95% CI)"))
)
Estimate (95% CI) |
|
HP |
-0.03 (-0.07, 0.02) |
# of gears |
|
4 |
2.36 (-2.65, 7.37) |
5 |
2.86 (-3.10, 8.83) |
Automatic (1) |
1.48 (-2.65, 5.60) |
Weight |
-2.03 (-4.38, 0.32) |
Carb |
|
2 |
-1.29 (-4.57, 1.99) |
3 |
-1.62 (-6.42, 3.17) |
4 |
-3.55 (-8.78, 1.68) |
6 |
-4.91 (-12.77, 2.96) |
8 |
-3.82 (-15.91, 8.26) |
lm()
with labels argument used correctly
mtcars <- mtcars %>%
dplyr::mutate_at(dplyr::vars(gear, am, carb), as.character)
labs <- c(hp = "HP",
gear = "# of gears",
am = "Automatic",
wt = "Weight",
carb = "Carb"
)
lm_res <- lm(mpg ~ hp + gear + am + wt + carb, data = mtcars)
lm_res %>%
polish(
.header1 = list(values = c("", "Estimate (95% CI)")),
.labels = labs
)
Estimate (95% CI) |
|
HP |
-0.03 (-0.07, 0.02) |
# of gears |
|
4 |
2.36 (-2.65, 7.37) |
5 |
2.86 (-3.10, 8.83) |
Automatic (1) |
1.48 (-2.65, 5.60) |
Weight |
-2.03 (-4.38, 0.32) |
Carb |
|
2 |
-1.29 (-4.57, 1.99) |
3 |
-1.62 (-6.42, 3.17) |
4 |
-3.55 (-8.78, 1.68) |
6 |
-4.91 (-12.77, 2.96) |
8 |
-3.82 (-15.91, 8.26) |
lm()
with labels argument used incorrectly
mtcars <- mtcars %>%
dplyr::mutate_at(dplyr::vars(gear, am, carb), as.character)
labs <- c(mpg = "MPG",
hp = "HP",
gear = "# of gears",
am = "Automatic",
wt = "Weight",
carb = "Carb"
)
lm_res <- lm(mpg ~ hp + gear + am + wt + carb, data = mtcars)
lm_res %>%
polish(
.header1 = list(values = c("", "Estimate (95% CI)")),
.labels = labs
)
#> Error: You provided 6 labels but your model has 5 covariate(s). Please provide the same number of labels as covariates.
.flextable = FALSE
where a list with a dataframe and a vector of row numbers is returned
mtcars <- mtcars %>%
dplyr::mutate_at(dplyr::vars(gear, am, carb), as.character)
mtcars <- Hmisc::upData(
mtcars,
labels = c(mpg = "MPG",
hp = "HP",
gear = "# of gears",
am = "Automatic",
wt = "Weight",
carb = "Carb"
),
print = F
)
lm_res <- lm(mpg ~ hp + gear + am + wt + carb, data = mtcars)
res <- lm_res %>%
polish(
.flextable = FALSE
)
res
#> [[1]]
#> # A tibble: 12 x 2
#> label_lev Estimate
#> <chr> <glue>
#> 1 HP -0.03 (-0.07, 0.02)
#> 2 # of gears <NA>
#> 3 4 2.36 (-2.65, 7.37)
#> 4 5 2.86 (-3.10, 8.83)
#> 5 Automatic (1) 1.48 (-2.65, 5.60)
#> 6 Weight -2.03 (-4.38, 0.32)
#> 7 Carb <NA>
#> 8 2 -1.29 (-4.57, 1.99)
#> 9 3 -1.62 (-6.42, 3.17)
#> 10 4 -3.55 (-8.78, 1.68)
#> 11 6 -4.91 (-12.77, 2.96)
#> 12 8 -3.82 (-15.91, 8.26)
#>
#> [[2]]
#> [1] 3 4 8 9 10 11 12
Returning the dataframe allows further manipulation if desired. The row numbers returned for the rows with factor levels can then be used in flextable::padding()
to add padding
res[[1]] %>%
mRclwhip::format_flextable(header1 = list(values = c("", "Estimate (95% CI)"))) %>%
flextable::padding(i = res[[2]], j = 1, padding.left = 25)
Estimate (95% CI) |
|
HP |
-0.03 (-0.07, 0.02) |
# of gears |
|
4 |
2.36 (-2.65, 7.37) |
5 |
2.86 (-3.10, 8.83) |
Automatic (1) |
1.48 (-2.65, 5.60) |
Weight |
-2.03 (-4.38, 0.32) |
Carb |
|
2 |
-1.29 (-4.57, 1.99) |
3 |
-1.62 (-6.42, 3.17) |
4 |
-3.55 (-8.78, 1.68) |
6 |
-4.91 (-12.77, 2.96) |
8 |
-3.82 (-15.91, 8.26) |