I have a data frame with three variables of interest:
- survival time
- grouping factor
- event indicator (dead: yes or no)
I want to calculate incidence rate for each group. I do this daily, so it would be great to have a function doing this instead of a long script.
I've tried the following, but doesn't work.
library(survival)
data(lung) # example data
lung$death <- ifelse(lung$status==1, 0, 1) # event indicator: 0 = survived; 1 = dead.
# Function
func <- function(data_frame, group, survival_time, event) {
library(epitools)
table <- data_frame %>%
filter_(!is.na(.$group)) %>%
group_by_(.$group) %>%
summarise_(pt = round(sum(as.numeric(.$survival_time)/365.25)),
events = sum(.$event)) %>%
do(pois.exact(.$events, pt = .$pt/1000, conf.level = 0.95)) %>%
ungroup() %>%
transmute_(Category = c(levels(as.factor(.$group))),
Events = x,
Person_years = pt*1000,
Incidence_Rate = paste(format(round(rate, 2), nsmall=2), " (",
format(round(lower, 2), nsmall=2), " to ",
format(round(upper, 2), nsmall=2), ")",
sep=""))
return(table)
}
func(lung, sex, time, death)
**Error: incorrect length (0), expecting: 228 In addition: Warning message:
In is.na(.$group) : is.na() applied to non-(list or vector) of type 'NULL'**
Any ideas? I've read the post about NSE and SE in dplyr, but thought I applied the recommendations correctly?