r - rowSums(Qf) 错误: 'x' 必须是至少二维数组 (msm::msm2surv)

标签 r syntax-error survival-analysis

我正在使用 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' 必须是至少两个维度的数组。

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

非常感谢!

最佳答案

我们可以将 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,我们删除了这些属性。

关于r - rowSums(Qf) 错误: 'x' 必须是至少二维数组 (msm::msm2surv),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51194732/

相关文章:

R 语言 - 等待用户输入 scan 或 readline

python - 草稿游戏中奇怪的语法错误

r - 为什么这个生存图不从 100% 开始

用于生成具有特定结构和字母组合的单词的 Python 脚本

syntax - 语法错误和逻辑错误

r - R vs Stata 中的 Cox 比例风险模型

python - 将包含 16k 字典的字典更改为 Pandas Dataframe

r - 对象错误[[名称,精确 = TRUE]] : subscript out of bounds

css - R Markdown : how to change style with internal css?

r - merge.data.table with all=True 引入 NA 行。这样对吗?