Short version: Is it possible (aside from reverse processing the formatted cell contents) to extract the coefficients from a table reporting the results of regression models generated using the finalfit
package?
Background: Using the (amazing) finalfit
package I can produce tables of results from regression models. I want to report some of these same results in the text of an Rmarkdown document. I do not want to run the regression models twice, once in finalfit
for the tables and once to generate output to be used in text. Also, finalfit
processes the coefficients (e.g. exponentiates coefficients from logistic regression models to generate odds ratios, consistently formats decimal points) and I do not want to duplicate these steps.
This code below produces a formatted table of results from a logistic regression model (note: deliberately using base R code for model):
library(finalfit)
library(dplyr)
explanatory = c("age", "sex.factor")
dependent = "mort_5yr"
colon_s %>%
## Crosstable
summary_factorlist(dependent, explanatory, fit_id=TRUE) %>%
ff_merge(
glm(
mort_5yr ~ age + sex.factor, family="binomial", data = colon_s
) %>%
fit2df(estimate_suffix=" (multivariable)")
) %>%
select(-c(fit_id, index)) %>%
dependent_label(colon_s, dependent)
I cannot see how I can extract the odds ratio for Sex:Male from this table (or the pipeline to produce it), without running and processing the glm
model separately.
Extracting the cell contents directly (as suggested by @LyzandeR) results in this string: "0.98 (0.76-1.27, p=0.888)" The relevant coefficients, confidence intervals and P-value would need to be extracted. This nearly achieves the result, but is not ideal given that they have all previously been calculated and concatentated into this string.
Note: I am happy to achieve the desired result using a different package.
%>% select("OR (multivariable)") %>% slice(3)
– LyzandeRfinalfit
package processes the coefficients and adds in brackets etc to make the table look nice. If I process cells from the formatted table I will then need to strip off the formatting again, which is relatively easy to do but seems a bit cumbersome and not generalisable to other regression models or cell contents (ie. the summary columns in the above example). – ChrisP