使用字符串作为 ipwtm 函数的公式?

Use string as formula for ipwtm function?

我有一个特定于 ipwtm 函数的问题。我有一个很长的分子和分母,并且想为它们分配字符串。但是,我尝试了不同的方法,例如使用 getevalparseas.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,其中returns的值为“分子”,“分母”。一种选择是在源代码中的 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