[R] How does one pass arguments to a function, such as coxph, that itself is inside a function?
Sorkin, John
j@ork|n @end|ng |rom @om@um@ry|@nd@edu
Mon Dec 16 19:12:15 CET 2019
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)
More information about the R-help
mailing list