icenReg:' dimnames的长度' [2]不等于数组范围

时间:2017-07-10 21:18:44

标签: r

我正在尝试在R中运行间隔删失的Cox模型.icenReg包有一个函数ic_sp来执行此操作。但是,当我给它一个因子变量作为预测器时,我遇到了一个问题。

data = data.frame(R = c(2, 1, 4, 7, 4, 6, 8, 8, 12, 3),
                  L = c(3, 1, 4, 7, 5, 6, 8, 9, 12, 3),
                  gender = c("m", "f", "f", "m", "m", "f", "f", "m", "f", "m"))
s = survival::Surv(data$R, data$L, type = 'interval2')
icenReg::ic_sp(s ~ relevel(gender, "f"), model = 'ph', bs_samples = 5, data = data)

> Error in dimnames(x) <- dn : length of 'dimnames' [2] not equal to array extent

我调试了这个功能并逐步完成了。似乎问题源于

myFit <- new(callText)

在fit_ICPH子功能中因为它抱怨

Error in fit$model == "ph" : comparison (1) is possible only for atomic and list types    

我已经尝试过这个新的&#39;功能,但我当时无法遵循它。

任何帮助将不胜感激!这一切都包含在一个闪亮的应用程序中,所以我真的不想在可能的情况下编辑数据集;数据集和公式都是反应性的。

奇怪的是,虽然可能没有一次我找到解决方案,如果变量有3个级别,它就可以工作。

data = data.frame(R = c(2, 1, 4, 7, 4, 6, 8, 8, 12, 3),
                  L = c(3, 1, 4, 7, 5, 6, 8, 9, 12, 3),
                  gender = c("m", "a", "f", "m", "m", "f", "a", "m", "f", "m"))
s = survival::Surv(data$R, data$L, type = 'interval2')
icenReg::ic_sp(s ~ relevel(gender, "f"), model = 'ph', bs_samples = 5, data = data)
> results...

1 个答案:

答案 0 :(得分:0)

问题出现的原因是公式内部relevel调用。我将在未来考虑允许这个(这是icenReg的作者)。

解决方法是仅将数据重新置于函数调用之外。例如,以下内容可以正常工作:

# Same data as above, 
# but changing L to be left side of interval, R as right side
data = data.frame(L = c(2, 1, 4, 7, 4, 6, 8, 8, 12, 3),
              R = c(3, 1, 4, 7, 5, 6, 8, 9, 12, 3),
              gender = c("m", "f", "f", "m", "m", "f", "f", "m", "f", "m"))

# Releveling the code outside formula
data$gender_f <- relevel(data$gender, 'f')

# Fitting model using cbind syntax
icenReg::ic_sp(cbind(L, R) ~ gender_f, model = 'ph', bs_samples = 5, data = data)