gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-12-04 08:01:05 26 4
gpt4 key购买 nike

我有一个特定于 ipwtm 函数的问题。我有一个很长的分子和分母,并想为它们分配字符串。但是,我尝试了不同的方法,例如使用 get , eval , parseas.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

关于r - 使用字符串作为 ipwtm 函数的公式?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/66465182/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com