【问题标题】:Use string as formula for ipwtm function?使用字符串作为 ipwtm 函数的公式?
【发布时间】:2021-03-03 21:28:57
【问题描述】:

我有一个特定于 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)

【问题讨论】:

  • 开发人员使用了 'tempcall
  • 那么有什么办法可以将分子设置为字符串numerator &lt;- "~ sex + age" 并使函数工作(例如,temp &lt;- ipwtm(..., numerator = eval(numerator), denominator = eval(denominator), id = patient...) 之类的东西?

标签: r string formula


【解决方案1】:

正如cmets中提到的@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 

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2011-07-13
    • 2015-05-31
    • 2017-01-01
    • 2011-12-11
    • 1970-01-01
    • 2011-04-15
    • 2013-09-10
    相关资源
    最近更新 更多