[R] Hope fo help - functions, fits and for cycles
ripley@stats.ox.ac.uk
ripley at stats.ox.ac.uk
Fri Jun 7 10:59:36 CEST 2002
1) add1 does this for you, more efficiently!
2) You are trying to add mydata[,expl[j]]) to a formula. That makes no
sense in the context of the formula, and I think you need to define it
as a variable in your function and then try to add that variable.
3) update within a function is tricky because of the scope rules. It
follows the S scope rules, AFAICS. A much better idea is to add the
variable you want either to the data argument or to the environment of the
formula.
BTW, _ is about to be deprecated, and please do not use different
assignment operators in one piece of code. If you must use _, put spaces
around it. Don't make life unnecessarily hard for your readers.
On 7 Jun 2002, Marketa Pavlikova wrote:
>
> I need a little piece of advice concerning passing data frames
> into the functions. As I do a lot of similar fits at a time, I'd like to
> write a small function doing the fits for all relevant variables
> automatically. However, I usually get error messages of the
> following kind:
>
> (I present here a part of a test code).
>
> #####################################################
> # Data set:
>
> ID<-c(1,2,3,4,5)
> gender<-c(1,0,0,1,0)
> age<-c(25,40,33,50,15)
> G1<-c(0,0,1,2,0)#it corresponds to
> some gene mutations
> G2<-c(0,1,0,1,1)#I examine the
> effect of each of them
> G3<-c(1,1,1,1,0)#corrected for
> "clinical" variables gender and age
> response<-c(2.1,3.5,6.1,2.0,1.5)
>
> dataset<-data.frame(ID,gender,age,G1,G2,G3,response)
> GG<-c("G1","G2","G3")
>
> # here I construct a function that makes a basic fit,
> then updates with each variable from GG vector.
>
> trial_function(mydata,formule,expl,distr="binomial")
> {
> n_length(expl)
> fit.low_glm(formule, family = distr, data = mydata,
> na.action = na.exclude)
>
> for (j in 1:n)
> {
> fit_update(fit.low,~.+ mydata[,expl[j]])
> print(mydata[,expl[j]])
> }
> }
>
> result<-
> trial(mydata=dataset,formule=response~gender+age,exp
> l=GG,distr="gaussian")
>
> # when update is off, no error message is printed and
> the function works all right
> # if I untag the update, I get the following message
> ######Error in eval(expr, envir, enclos) : Object
> "mydata" not found
> # although it passed and foung mydata correctle
> beforehand.
>
> # If I break the variable passing standards and define
> mydata beforehand by
>
> mydata<-dataset
> result<-
> trial(mydata=dataset,formule=response~gender+age,exp
> l=GG,distr="gaussian")
>
> # I get
> ####Error in "[.data.frame"(mydata, , expl[j]) :
> ####Object "expl" not found
>
> ####################################################
>
> Hence, it seems to me that there is some problem in passing the
> dataset into the update function inside a for cycle. When I did the
> same thing outside the function (with the same for cycle) it worked.
> Defining the variable outside the update (inside the for cycle) didn't
> help.
>
> Is there any workaround I could use?
>
> Thank you very much for your help or connection to someone who
> could help,
>
> Marketa Pavlikova
> EuroMISE Center
>
>
> -.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-
> r-help mailing list -- Read http://www.ci.tuwien.ac.at/~hornik/R/R-FAQ.html
> Send "info", "help", or "[un]subscribe"
> (in the "body", not the subject !) To: r-help-request at stat.math.ethz.ch
> _._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._
>
--
Brian D. Ripley, ripley at stats.ox.ac.uk
Professor of Applied Statistics, http://www.stats.ox.ac.uk/~ripley/
University of Oxford, Tel: +44 1865 272861 (self)
1 South Parks Road, +44 1865 272860 (secr)
Oxford OX1 3TG, UK Fax: +44 1865 272595
-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-
r-help mailing list -- Read http://www.ci.tuwien.ac.at/~hornik/R/R-FAQ.html
Send "info", "help", or "[un]subscribe"
(in the "body", not the subject !) To: r-help-request at stat.math.ethz.ch
_._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._
More information about the R-help
mailing list