我有一个特定于ipwtm函数的问题。我有一个长分子和分母,并想为它们分配字符串。不过,我已经尝试了不同的方法,如使用get
,eval
,parse
和as.formula
,但功能不起作用。请让我知道是否有解决方法。
例子:
library("ipw")
data("haartdat")
haartdat[1:10,]
numerator <- as.formula("~ sex + age")
denominator <- as.formula("~ cd4.sqrt + sex + age")
temp <- ipwtm(exposure = haartind, family = "survival",
numerator = numerator, denominator = denominator,
id = patient, tstart = tstart, timevar = fuptime, type = "first",
data = haartdat)
正如注释中提到的@ jvargh7一样,这是因为match.call
+ deparse
,其将值返回为“分子”,“分母”。一种选择是在源代码后的两行中添加两行,match.call()
并将其作为新函数调用。
ipwtm2 <- function (exposure, family, link, numerator = NULL, denominator,
id, tstart, timevar, type, data, corstr = "ar1", trunc = NULL,
...) {
tempcall <- match.call()
tempcall$numerator <- numerator # new
tempcall$denominator <- denominator # new
...
...
}
测试
library(survival)
library(ipw)
data(haartdat)
numerator <- as.formula("~ sex + age")
denominator <- as.formula("~ sex + age + cd4.sqrt")
temp <- ipwtm2(exposure = haartind, family = "survival",
numerator = numerator, denominator = denominator,
id = patient, tstart = tstart, timevar = fuptime, type = "first",
data = haartdat)
temp_old <- ipwtm(exposure = haartind, family = "survival",
numerator = ~ sex + age, denominator = ~ sex + age + cd4.sqrt,
id = patient, tstart = tstart, timevar = fuptime, type = "first",
data = haartdat)
-检查输出
temp$num.mod
Call:
coxph(formula = Surv(tstart, fuptime, haartind) ~ sex + age,
data = haartdat, subset = tempdat$selvar == 1, na.action = na.fail,
method = "efron")
coef exp(coef) se(coef) z p
sex 0.069424 1.071891 0.124365 0.558 0.577
age 0.007521 1.007549 0.005123 1.468 0.142
Likelihood ratio test=2.22 on 2 df, p=0.3287
n= 14389, number of events= 376
temp_old$num.mod
Call:
coxph(formula = Surv(tstart, fuptime, haartind) ~ sex + age,
data = haartdat, subset = tempdat$selvar == 1, na.action = na.fail,
method = "efron")
coef exp(coef) se(coef) z p
sex 0.069424 1.071891 0.124365 0.558 0.577
age 0.007521 1.007549 0.005123 1.468 0.142
Likelihood ratio test=2.22 on 2 df, p=0.3287
n= 14389, number of events= 376
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句