[R] Hope fo help - functions, fits and for cycles

Marketa Pavlikova Marketa.Pavlikova at euromise.cz
Fri Jun 7 11:28:45 CEST 2002


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



More information about the R-help mailing list