rstringformula

Use string as formula for ipwtm function?


I have a questions specific to the ipwtm function. I have a long numerator and denominator, and would like to assign strings to them. However, I have tried different methods like using get, eval, parse and as.formula but the function doesn't work. Please let me know if there's a way to solve this.

Example:

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)

Solution

  • As @jvargh7 mentioned in the comments, it is because of the match.call + deparse, which returns the value as "numerator", "denominator". An option is to add two lines in the source code after the match.call() and call it as a new function.

    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
      ... 
      ...
      }
    

    -testing

    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)
    

    -check the output

    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