[R] How to pass a variable to a function which use variable name as a parameter

2015-05-26 Thread wong jane
There are functions which use variable names as parameters in some R
packages. However, if the variable name is stored in another variable, how
can I pass this variable to the function. Taking the rms package as an
example:

library(rms)
n - 1000
age - rnorm(n, 50, 10)
sex - factor(sample(c('female','male'), n,TRUE))

y - rnorm(n, 200, 25)
ddist - datadist(age, sex)
options(datadist='ddist')
fit - lrm(y ~ age)
Predict(fit, age, np=4)
options(datadist=NULL)

Here age was a variable name passed to Predict() function, but if age
was stored in variable var, that is, var - age, how can I pass var
to Predict() function? The purpose is that I want to change the parameter
of Predict()  in a loop.

[[alternative HTML version deleted]]

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


Re: [R] How to pass a variable to a function which use variable name as a parameter

2015-05-26 Thread John McKown
On Tue, May 26, 2015 at 5:14 AM, wong jane jane.wong...@gmail.com wrote:

 There are functions which use variable names as parameters in some R
 packages. However, if the variable name is stored in another variable, how
 can I pass this variable to the function. Taking the rms package as an
 example:

 ​​
 library(rms)
 n - 1000
 age - rnorm(n, 50, 10)
 sex - factor(sample(c('female','male'), n,TRUE))

 y - rnorm(n, 200, 25)
 ddist - datadist(age, sex)
 options(datadist='ddist')
 fit - lrm(y ~ age)
 Predict(fit, age, np=4)
 options(datadist=NULL)

 Here age was a variable name passed to Predict() function, but if age
 was stored in variable var, that is, var - age, how can I pass var
 to Predict() function? The purpose is that I want to change the parameter
 of Predict()  in a loop.


​Please turn off HMTL email. The forum software doesn't really like it.

What you want is the get() function.


var-age

​
​
library(rms)
n - 1000
age - rnorm(n, 50, 10)
sex - factor(sample(c('female','male'), n,TRUE))

y - rnorm(n, 200, 25)
​#​
ddist - datadist(age, sex)
​ddist - datadist(get(var),sex)​
options(datadist='ddist')
fit - lrm(y ~ age)
​#​
Predict(fit, age, np=4)
​Predict(fit,get(var),np=4)​
options(datadist=NULL)

-- 
My sister opened a computer store in Hawaii. She sells C shells down by the
seashore.

If someone tell you that nothing is impossible:
Ask him to dribble a football.

He's about as useful as a wax frying pan.

10 to the 12th power microphones = 1 Megaphone

Maranatha! 
John McKown

[[alternative HTML version deleted]]

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

Re: [R] How to pass a variable to a function which use variable name as a parameter

2015-05-26 Thread William Dunlap
One way to use variable names in functions like Predict() that
do not evaluate their arguments in the standard way is to use
do.call() along with as.name().  E.g.,
  varName-age
  do.call(Predict, list(fit, as.name(varName), np=4))})
gives the same result as
  Predict(fit, age, np=4)



Bill Dunlap
TIBCO Software
wdunlap tibco.com

On Tue, May 26, 2015 at 3:14 AM, wong jane jane.wong...@gmail.com wrote:

 There are functions which use variable names as parameters in some R
 packages. However, if the variable name is stored in another variable, how
 can I pass this variable to the function. Taking the rms package as an
 example:

 library(rms)
 n - 1000
 age - rnorm(n, 50, 10)
 sex - factor(sample(c('female','male'), n,TRUE))

 y - rnorm(n, 200, 25)
 ddist - datadist(age, sex)
 options(datadist='ddist')
 fit - lrm(y ~ age)
 Predict(fit, age, np=4)
 options(datadist=NULL)

 Here age was a variable name passed to Predict() function, but if age
 was stored in variable var, that is, var - age, how can I pass var
 to Predict() function? The purpose is that I want to change the parameter
 of Predict()  in a loop.

 [[alternative HTML version deleted]]

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


[[alternative HTML version deleted]]

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


Re: [R] How to pass a variable to a function which use variable name as a parameter

2015-05-26 Thread David Winsemius

On May 26, 2015, at 8:30 AM, William Dunlap wrote:

 One way to use variable names in functions like Predict() that
 do not evaluate their arguments in the standard way is to use
 do.call() along with as.name().  E.g.,
  varName-age
  do.call(Predict, list(fit, as.name(varName), np=4))})
 gives the same result as
  Predict(fit, age, np=4)
 

Here's another approach, developed after looking at the code and the help page, 
where I noticed that the factors argument was given an an alternate route for 
specifying the variables for evaluation. The requirements for the 'factors' 
argument are that it be provided as a named list and have either specific 
values or NA.

 var=age
 vlist - list(dum=NA)
 names(vlist)=var
 
 Predict(fit, factors=vlist, np=4)
#- same result ---
   age yhatlowerupper
1 25.04901 6.834836 4.856497 8.813174
2 41.14259 6.882369 4.919433 8.845306
3 57.23618 6.929903 4.967118 8.892688
4 73.32977 6.977437 4.999548 8.955325

Response variable (y): log odds 

Limits are 0.95 confidence limits

-- 
David.

 
 
 Bill Dunlap
 TIBCO Software
 wdunlap tibco.com
 
 On Tue, May 26, 2015 at 3:14 AM, wong jane jane.wong...@gmail.com wrote:
 
 There are functions which use variable names as parameters in some R
 packages. However, if the variable name is stored in another variable, how
 can I pass this variable to the function. Taking the rms package as an
 example:
 
 library(rms)
 n - 1000
 age - rnorm(n, 50, 10)
 sex - factor(sample(c('female','male'), n,TRUE))
 
 y - rnorm(n, 200, 25)
 ddist - datadist(age, sex)
 options(datadist='ddist')
 fit - lrm(y ~ age)
 Predict(fit, age, np=4)
 options(datadist=NULL)
 
 Here age was a variable name passed to Predict() function, but if age
 was stored in variable var, that is, var - age, how can I pass var
 to Predict() function? The purpose is that I want to change the parameter
 of Predict()  in a loop.
 
[[alternative HTML version deleted]]
-- 
 

David Winsemius
Alameda, CA, USA

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