Adding estimates to cat_plot function with a loop

19 Views Asked by At

I am trying to add RRs to to a cat_plot in r. I need to generate plots for 30 different cancer types. However, when I try to add the RRs to each plot, I somehow always end up with the RRs for the "last" cancer type in the loop.

This is my code:

For(cancer in cancer_types) {
subset_data <- subset(data, diag_group == cancer)

model <- glm (event ~cohab*sex + age + year + offset(log(TTD_year_5)),
family = poisson(link=”log”, data = subset_data)

table <- interactionR(
model,
exposure_names = c(”cohabLiving alone”, ”sexMale”),
ci.type = ”delta”, ci.level = 0.95,
em = FALSE, recode = FALSE)

values <- table$dframe

# Extract values from interactionR to add to plot
RR <- paste(values$Estimates[values$Measures == ”OR00  | values$Measures == ”OR01 | values$Measures == ”OR10 | values$Measures == ”OR11])

RR_list[[cancer]] <- list(RR = RR)

# Create plot and add RR value for specific cancer type
plot <- cat_plot(model, pred = sex, modx = sambo, data = subset_data, interval = TRUE) +
labs(title = cancer) +
geom_text(aes(y = ymax, label = RR_list[[cancer]]$RR),
group = sambo))

# Add plots to the list
Plots_list[[cancer]] <- plot

However, I can't seem to figure out how to add the "correct" RRs for all 30 plots. It seems like the items in the list is stored ok.

Hope some of you can help!!

Thank you.

0

There are 0 best solutions below