[R] How does one pass arguments to a function, such as coxph, that itself is inside a function?
William Dunlap
wdun|@p @end|ng |rom t|bco@com
Mon Dec 16 19:58:07 CET 2019
You can use substitute() to fiddle with the formula. The following shows
how to do it using lm() instead of coxph(), but the manipulations are the
same. It also has an 'envir' argument in case the formula depends on
anything in the callers enviroment. The 'substitute(data)' is make the
printouts prettier.
f <- function (response, data, envir = parent.frame())
{
formula <- eval(substitute(response ~ X), envir = envir)
eval(as.call(list(quote(lm), formula, data = substitute(data))),
envir = envir)
}
> d <- data.frame(check.names=FALSE, "Y-one"=1:10, "Y-two"=sqrt(1:10),
X=log(1:10))
> lm(`Y-one` ~ X, data=d)
Call:
lm(formula = `Y-one` ~ X, data = d)
Coefficients:
(Intercept) X
-0.4371 3.9307
> f(`Y-one`, data=d)
Call:
lm(formula = `Y-one` ~ X, data = d)
Coefficients:
(Intercept) X
-0.4371 3.9307
> g <- function() {
+ Y3 <- 1/(1:10)
+ f(Y3, data=d)
+ }
> g()
Call:
lm(formula = Y3 ~ X, data = d)
Coefficients:
(Intercept) X
0.8338 -0.3581
E.g.,
Bill Dunlap
TIBCO Software
wdunlap tibco.com
On Mon, Dec 16, 2019 at 10:12 AM Sorkin, John <jsorkin using som.umaryland.edu>
wrote:
> 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 using 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.
>
[[alternative HTML version deleted]]
More information about the R-help
mailing list