Hello,

You can assemble the formula with deparse/substitute and paste.
This is the example 2 from ?coxph. I have changed list(...) to data.frame(...)


doit <- function(s1, s2, data){
  s1 <- deparse(substitute(s1))
  s2 <- deparse(substitute(s2))
  fmla <- paste("Surv(", s1, ",", s2, ", event)")
  fmla <- paste(fmla, "x", sep = "~")

  coxph(as.formula(fmla), data = data)
}

doit(start, stop, test2)


It works.

Hope this helps,

Rui Barradas

Às 18:12 de 16/12/19, Sorkin, John escreveu:
Question summary: How does one pass arguments to a function, such as coxph, 
that itself is inside a function.

I am trying to write a function that will allow me to call coxph using 
different outcome and time variables. The coxph works when the coxph is NOT 
contained in a larger function (which passes the outcome and time variable to  
use), but does not work when coxph is contained in a  larger funciton.

My code:

fit0 <- 
coxph(Surv(FUtime,Death)~Age_in_years_at_A1_max+factor(Diabetes)+factor(CKD_stage)+
                                                        
factor(Phase1_first),data=mydata)
summary(fit0)

this works:

Call:
coxph(formula = Surv(FUtime, Death) ~ Age_in_years_at_A1_max +
                             factor(Diabetes) + factor(CKD_stage) + 
factor(Phase1_first),
     data = mydata)

   n= 350, number of events= 56

                            coef exp(coef) se(coef)      z Pr(>|z|)
Age_in_years_at_A1_max  0.04618   1.04727  0.01384  3.338 0.000845 ***
factor(Diabetes)1       0.12247   1.13029  0.28282  0.433 0.664991
factor(CKD_stage)3     -0.28418   0.75263  0.38744 -0.733 0.463261
factor(CKD_stage)4      0.33938   1.40407  0.36583  0.928 0.353572
factor(CKD_stage)5      0.97121   2.64115  0.40171  2.418 0.015618 *
factor(Phase1_first)1   0.02204   1.02229  0.29713  0.074 0.940868

other output deleted.

But this code does not work:

doit <- function(time,outcome,data){
   fit0 <-
coxph(Surv(time,outcome)~Age_in_years_at_A1_max+factor(Diabetes)+factor(CKD_stage)+factor(Phase1_first),data=data)
   print(summary(fit0))
}
doit(FUtime,Death,mydata)

  Error in Surv(time, outcome) : object 'FUtime' not found

I am certain I have a scoping problem, but I don't know how to solve it.

John David Sorkin M.D., Ph.D.

Professor of Medicine

Chief, Biostatistics and Informatics

University of Maryland School of Medicine Division of Gerontology and Geriatric 
Medicine

Baltimore VA Medical Center

10 North Greene Street

GRECC (BT/18/GR)

Baltimore, MD 21201-1524

(Phone) 410-605-7119

(Fax) 410-605-7913 (Please call phone number above prior to faxing)



______________________________________________
R-help@r-project.org mailing list -- To UNSUBSCRIBE and more, see
https://stat.ethz.ch/mailman/listinfo/r-help
PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
and provide commented, minimal, self-contained, reproducible code.


______________________________________________
R-help@r-project.org mailing list -- To UNSUBSCRIBE and more, see
https://stat.ethz.ch/mailman/listinfo/r-help
PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
and provide commented, minimal, self-contained, reproducible code.

Reply via email to