首页 文章

如何使用ggplot2拟合cuminc功能

提问于
浏览
0

有没有人知道一种方法来适应从cmprisk包到ggplot2包的cominc函数?

我收到此错误消息:

不知道如何自动选择cuminc类型对象的比例 . 默认为连续 - 在as.data.frame.default(x [[i]],可选= TRUE,stringsAsFactors = stringsAsFactors)中出错:kan ikke tvinge klasse»“cuminc”«ind i en data.frame ggplot2不知道如何处理类cuminc的数据

我找到了一个分割cuminc功能的功能,这样你就可以在同一个图中绘制个别原因而不是两个原因:

cs.cuminc <- function(x,cause="1"){
  if (!is.null(x$Tests)) 
    x <- x[names(x) != "Tests"]
  which.out <- which(unlist(strsplit(names(x), " "))[seq(2,length(names(x))*2,2)]!=cause)
  x[which.out] <- NULL
  class(x) <- "cuminc"
  return(x)
}

这提供了一条带有两条曲线的图 - stil ggplot2无法处理该函数 .

1 回答

  • 1

    将数据从cuminc结构移动到数据帧,然后使用ggplot geom_step():

    ciplot <- function(c, title = "Cumulative incidence", xlim = -1, ylim = 1, ...)
    {
      nc <- length(c)
      d <- data.frame()
      strata <- names(c)
      for (i in 1:nc) {
            d <- rbind(d, data.frame(time = c[[i]]$time, est = c[[i]]$est, var = c[[i]]$var, strat = strata[i]))
      }
      if (xlim == -1) xlim = max(d$time)
      ggplot(d, aes(time, est, col = strat)) + geom_step(size = 1) + ylim(0, 1) + xlim(0, xlim) + 
        ggtitle(title)
    }
    

相关问题