I am conducting an analysis in R markdown where I want to look at the relationship between haemoglobin (outcome) and several other variables that may influence it (exposures e.g. season and ses).
I want to start by creating several models with haemoglobin as the outcome and looking at each exposure variable individually.
I have multilevel data - I have more than one observation per person, so I am using multilevel modelling using the glmmTMB package.
Here is an example of some of my data:
data = data.frame(id = c(1,1,1,2,2,2,3,3,3),
ses = c(0.13, 1.23, -0.78, 1.32, 0.56, -0.04, -1.43, 1.45, 2.01),
season = c("good", "good", "bad", "bad", "bad", "good", "good", "good", "good" ),
haemoglobin = c(15, 14, 16, 9, 10, 11, 12, 10, 11))
# id ses season haemoglobin
# 1 1 0.13 good 15
# 2 1 1.23 good 14
# 3 1 -0.78 bad 16
# 4 2 1.32 bad 9
# 5 2 0.56 bad 10
# 6 2 -0.04 good 11
# 7 3 -1.43 good 12
# 8 3 1.45 good 10
# 9 3 2.01 good 11
Here is an example of two models I will use:
library(glmmTMB)
mod1 <- glmmTMB(haemoglobin ~ season + (1 | id), data = data)
summary(mod1)
confint(mod1)
mod2 <- glmmTMB(haemoglobin ~ ses + (1 | id), data = data)
summary(mod2)
confint(mod2)
My question is how to present my data in a table without having to enter it all manually (ie in case my estimates change, so I don't have to enter it all again!) Perhaps I would be able to do it using kable but I'm not sure how.
I want a table that looks something like this (below). It doesn't have to be the exact same, just that allows me to display the results of several bivariable models together.
table = data.frame(Variable = c("seasonbad","seasongood","ses"),
Estimate = c("ref", 0.05188, -0.650),
Confint = c("-", "-1.62, 1.72", "-1.01, 0.29"),
Pval = c("-", 0.951, 0.000442))
# Variable Estimate Confint Pval
# 1 seasonbad ref - -
# 2 seasongood 0.05188 -1.62, 1.72 0.951
# 3 ses -0.65 -1.01, 0.29 0.000442
Any help/package suggestions would be incredibly appreciated. Many thanks!
This is more a data manipulation question rather than a question about html/markdown. You need to get the data you want from the mod objects, once you have that the rest is easy. Here's an example of what I mean:
# define a function to extract the info and returns it in the shape you need
get_summary <- function(mod){
mod_summary <- summary(mod)
mod_conf <- confint(mod)
var <- dimnames(mod_summary$coefficients$cond)[[1]][-1]
res_mod <- mod_conf[paste0("cond.", var), 1:3] %>% t() %>% data.frame()
res_mod["Pval"] <- mod_summary$coefficients$cond[var, 4]
res_mod["Variable"] <- var
return(res_mod)
}
# get the data from each model and bind the rows of the results together
rbind(get_summary(mod1), get_summary(mod2)) %>%
# show the result as an HTML table
tableHTML(rownames = FALSE, round = 2,widths = rep(100, 5)) %>%
add_theme("rshiny-blue")
you should get something like this: