【问题标题】:Error in rowSums(Qf) : 'x' must be an array of at least two dimensions (msm::msm2surv)rowSums(Qf) 中的错误:“x”必须是至少二维的数组 (msm::msm2surv)
【发布时间】:2018-12-14 03:19:01
【问题描述】:

我在 msm 包中使用函数 msm2surv 并尝试将纵向数据转换为 flexsurve 包喜欢的格式。以下是我的示例 tmp。

tmp <- structure(list(id = c(89, 90, 90, 91, 91, 91, 92, 92, 93, 93, 
94, 94, 94, 95, 95, 96), days = c(9157, 0, 9156, 0, 8394, 9156, 
0, 9156, 0, 8079, 0, 8933, 9003, 0, 8430, 0), event = c(1, 1, 
1, 1, 2, 2, 1, 1, 1, 5, 1, 3, 6, 1, 4, 1)), row.names = c(NA, 
-16L), class = c("grouped_df", "tbl_df", "tbl", "data.frame"), vars = "id", drop = TRUE, indices = list(
    0L, 1:2, 3:5, 6:7, 8:9, 10:12, 13:14, 15L), group_sizes = c(1L, 
2L, 3L, 2L, 2L, 3L, 2L, 1L), biggest_group_size = 3L, labels = structure(list(
    id = c(89, 90, 91, 92, 93, 94, 95, 96)), row.names = c(NA, 
-8L), class = "data.frame", vars = "id", drop = TRUE, .Names = "id"), .Names = c("id", 
"days", "event"))

运行代码:

library(msm)
Q <- matrix(c( 
  0,1,1,1,1,0, 
  0,0,1,1,1,1, 
  0,0,0,1,1,1, 
  0,0,0,0,1,1, 
  0,0,0,0,0,0, 
  0,0,0,0,0,0
), nrow=6, ncol=6, 
byrow=TRUE, 
dimnames=list(from=1:6,to=1:6)) 

dat <- msm2Surv(data=tmp, subject="id", time="days", state="event", Q=Q)

它给了我错误:rowSums(Qf) 中的错误:'x' 必须是至少二维的数组。

我检查了数据框的尺寸,这听起来不错。但是错误就在那里。每个人都知道如何解决问题/错误吗?

非常感谢!

【问题讨论】:

    标签: r syntax-error survival-analysis


    【解决方案1】:

    我们可以将tbl_df 转换为data.frame,它应该可以工作

    out <- msm2Surv(data=as.data.frame(tmp), subject="id",
                   time="days", state="event", Q=Q)
    dim(out)
    #[1] 31  8
    

    “tmp”数据集是一个分组的 tbl_df,有很多属性。通过转换为data.frame,我们删除了这些属性。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2021-10-03
      • 2018-02-28
      • 1970-01-01
      • 2018-12-04
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多