我在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'必须是至少二维的数组。
我检查了数据框的尺寸,对我来说没问题。但是错误在那里。每个人都知道如何解决问题/错误吗?
非常感谢!
答案 0 :(得分: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
,我们将删除这些属性。