Help with R: functions

classic Classic list List threaded Threaded
6 messages Options
Reply | Threaded
Open this post in threaded view
|

Help with R: functions

Pryseley Assam

Hello R-users
   
  I am new to R and trying to write some functions. I have problems writing functions that takes a data set as an arguement and uses variables in the data. I illustrate my problem with a small example below:
   
   sample data   #------------------
  visual24<-rnorm(30,3,5)
  visual52<-rt(30,7)
  dats<- data.frame(cbind(visual24,visual52))
  remove(visual24, visual52)
   
  # first code
  #--------------
  st <-function(data,x,y){
  rcc<-coef(lm(y~x))
  plot(x,y)
  abline(rcc[1],rcc[2])
  }
  st(data=dats,x=dats$visual24,y=dats$visual52)
   
  This code works fine, but with such a code the data as an arguement to the funtion is not necessary.
  However, i wish to write a function that reads the variables from the data directly.
  I tried using the function below but it does not work.
   
  # second code
  #------------------
  st <-function(data,x,y){
  rcc<-coef(lm(data$y~data$x))
  plot(data$x,data$y)
  abline(rcc[1],rcc[2])
  }
  st(dats,visual24,visual52)
   
  I wish to inquire if any one has an idea of what i need to adjust in the function so that it works.
  I believe that the referencing $x or $y in the function is not doing the correct thing.
   
  Better still, will it be a problem if i code the functions as in the first code above?
  I mean given that they will be used to create a library
   
  Best regards
  Pryseley
   


               
---------------------------------


        [[alternative HTML version deleted]]

______________________________________________
[hidden email] mailing list
https://stat.ethz.ch/mailman/listinfo/r-help
PLEASE do read the posting guide! http://www.R-project.org/posting-guide.html
Reply | Threaded
Open this post in threaded view
|

Re: Help with R: functions

TEMPL Matthias

What is wrong with your first solution:

st <-function(x,y){
  ## y ... Response
  ## x ... terms
  rcc<-coef(lm(y ~ x))
  plot(x,y)
  abline(rcc[1],rcc[2])
  }
  st(dats$visual24,dats$visual52)

Or use attach:

st <-function(data,x,y){
  attach(data)
  rcc<-coef(lm(x~y))
  plot(x,y)
  abline(rcc[1],rcc[2])
  detach(data)
  }
  st(dats,visual24,visual52)
 
 Best,
Matthias

> -----Ursprüngliche Nachricht-----
> Von: [hidden email]
> [mailto:[hidden email]] Im Auftrag von
> Pryseley Assam
> Gesendet: Montag, 30. Jänner 2006 14:14
> An: [hidden email]
> Betreff: [R] Help with R: functions
>
>
>
> Hello R-users
>    
>   I am new to R and trying to write some functions. I have
> problems writing functions that takes a data set as an
> arguement and uses variables in the data. I illustrate my
> problem with a small example below:
>    
>    sample data   #------------------
>   visual24<-rnorm(30,3,5)
>   visual52<-rt(30,7)
>   dats<- data.frame(cbind(visual24,visual52))
>   remove(visual24, visual52)
>    
>   # first code
>   #--------------
>   st <-function(data,x,y){
>   rcc<-coef(lm(y~x))
>   plot(x,y)
>   abline(rcc[1],rcc[2])
>   }
>   st(data=dats,x=dats$visual24,y=dats$visual52)
>    
>   This code works fine, but with such a code the data as an
> arguement to the funtion is not necessary.
>   However, i wish to write a function that reads the
> variables from the data directly.
>   I tried using the function below but it does not work.
>    
>   # second code
>   #------------------
>   st <-function(data,x,y){
>   rcc<-coef(lm(data$y~data$x))
>   plot(data$x,data$y)
>   abline(rcc[1],rcc[2])
>   }
>   st(dats,visual24,visual52)
>    
>   I wish to inquire if any one has an idea of what i need to
> adjust in the function so that it works.
>   I believe that the referencing $x or $y in the function is
> not doing the correct thing.
>    
>   Better still, will it be a problem if i code the functions
> as in the first code above?
>   I mean given that they will be used to create a library
>    
>   Best regards
>   Pryseley
>    
>
>
>
> ---------------------------------
>
>
> [[alternative HTML version deleted]]
>
> ______________________________________________
> [hidden email] mailing list
> https://stat.ethz.ch/mailman/listinfo/r-help
> PLEASE do read
> the posting guide! http://www.R-project.org/posting-guide.html
>

______________________________________________
[hidden email] mailing list
https://stat.ethz.ch/mailman/listinfo/r-help
PLEASE do read the posting guide! http://www.R-project.org/posting-guide.html
Reply | Threaded
Open this post in threaded view
|

Re: Help with R: functions

Philippe Grosjean
In reply to this post by Pryseley Assam
# Here it is (using the formula interface):
dats <- data.frame(visual24 = rnorm(30, 3, 5),
                    visual52 = rt(30, 7))

st <- function(formula, data, ...) {
        # Just use the formula to specify which variables to use
        rcc <- coef(lm(formula, data))
        # Make sure to keep only variable used in the data frame
        plot(data[ , rev(all.vars(formula))])
        # Draw the line. Note the ... that allows to change
        #lines features
        abline(coef = rcc, ...)
        # Return the coefficients invisibly
        return(invisible(rcc))
}

st(visual52 ~ visual24, data = dats)
# Change style and color of the line (thanks to '...')
st(visual52 ~ visual24, data = dats, lty = 2, col = "red")

Best,

Philippe Grosjean


Pryseley Assam wrote:

> Hello R-users
>    
>   I am new to R and trying to write some functions. I have problems writing functions that takes a data set as an arguement and uses variables in the data. I illustrate my problem with a small example below:
>    
>    sample data   #------------------
>   visual24<-rnorm(30,3,5)
>   visual52<-rt(30,7)
>   dats<- data.frame(cbind(visual24,visual52))
>   remove(visual24, visual52)
>    
>   # first code
>   #--------------
>   st <-function(data,x,y){
>   rcc<-coef(lm(y~x))
>   plot(x,y)
>   abline(rcc[1],rcc[2])
>   }
>   st(data=dats,x=dats$visual24,y=dats$visual52)
>    
>   This code works fine, but with such a code the data as an arguement to the funtion is not necessary.
>   However, i wish to write a function that reads the variables from the data directly.
>   I tried using the function below but it does not work.
>    
>   # second code
>   #------------------
>   st <-function(data,x,y){
>   rcc<-coef(lm(data$y~data$x))
>   plot(data$x,data$y)
>   abline(rcc[1],rcc[2])
>   }
>   st(dats,visual24,visual52)
>    
>   I wish to inquire if any one has an idea of what i need to adjust in the function so that it works.
>   I believe that the referencing $x or $y in the function is not doing the correct thing.
>    
>   Better still, will it be a problem if i code the functions as in the first code above?
>   I mean given that they will be used to create a library
>    
>   Best regards
>   Pryseley
>    
>
>
>
> ---------------------------------
>
>
> [[alternative HTML version deleted]]
>
> ______________________________________________
> [hidden email] mailing list
> https://stat.ethz.ch/mailman/listinfo/r-help
> PLEASE do read the posting guide! http://www.R-project.org/posting-guide.html
>
>

______________________________________________
[hidden email] mailing list
https://stat.ethz.ch/mailman/listinfo/r-help
PLEASE do read the posting guide! http://www.R-project.org/posting-guide.html
Reply | Threaded
Open this post in threaded view
|

Re: Help with R: functions

Jacques VESLOT
In reply to this post by Pryseley Assam
st <- function(data, x, y){
    attach(data)
    rcc <- coef(lm(y~x))
    plot(x,y)
    abline(rcc)
    detach(data)}

st(data=dats, x=visual24, y=visual52)



Pryseley Assam a écrit :

>Hello R-users
>  
>  I am new to R and trying to write some functions. I have problems writing functions that takes a data set as an arguement and uses variables in the data. I illustrate my problem with a small example below:
>  
>   sample data   #------------------
>  visual24<-rnorm(30,3,5)
>  visual52<-rt(30,7)
>  dats<- data.frame(cbind(visual24,visual52))
>  remove(visual24, visual52)
>  
>  # first code
>  #--------------
>  st <-function(data,x,y){
>  rcc<-coef(lm(y~x))
>  plot(x,y)
>  abline(rcc[1],rcc[2])
>  }
>  st(data=dats,x=dats$visual24,y=dats$visual52)
>  
>  This code works fine, but with such a code the data as an arguement to the funtion is not necessary.
>  However, i wish to write a function that reads the variables from the data directly.
>  I tried using the function below but it does not work.
>  
>  # second code
>  #------------------
>  st <-function(data,x,y){
>  rcc<-coef(lm(data$y~data$x))
>  plot(data$x,data$y)
>  abline(rcc[1],rcc[2])
>  }
>  st(dats,visual24,visual52)
>  
>  I wish to inquire if any one has an idea of what i need to adjust in the function so that it works.
>  I believe that the referencing $x or $y in the function is not doing the correct thing.
>  
>  Better still, will it be a problem if i code the functions as in the first code above?
>  I mean given that they will be used to create a library
>  
>  Best regards
>  Pryseley
>  
>
>
>
>---------------------------------
>
>
> [[alternative HTML version deleted]]
>
>______________________________________________
>[hidden email] mailing list
>https://stat.ethz.ch/mailman/listinfo/r-help
>PLEASE do read the posting guide! http://www.R-project.org/posting-guide.html
>
>  
>

______________________________________________
[hidden email] mailing list
https://stat.ethz.ch/mailman/listinfo/r-help
PLEASE do read the posting guide! http://www.R-project.org/posting-guide.html
Reply | Threaded
Open this post in threaded view
|

Re: Help with R: functions

Liaw, Andy
In reply to this post by Pryseley Assam
This might be a bit closer to what Pryseley wanted:

st <- function(dat, x, y) {
    f <- formula(substitute(y ~ x), env=environment(dat))
    plot(f, dat)
    abline(lm(f, dat))
}

Note that the variable names in the plot when tested on `dats' as Pryseley
created.

Andy

From: Jacques VESLOT

>
> st <- function(data, x, y){
>     attach(data)
>     rcc <- coef(lm(y~x))
>     plot(x,y)
>     abline(rcc)
>     detach(data)}
>
> st(data=dats, x=visual24, y=visual52)
>
>
>
> Pryseley Assam a écrit :
>
> >Hello R-users
> >  
> >  I am new to R and trying to write some functions. I have
> problems writing functions that takes a data set as an
> arguement and uses variables in the data. I illustrate my
> problem with a small example below:
> >  
> >   sample data   #------------------
> >  visual24<-rnorm(30,3,5)
> >  visual52<-rt(30,7)
> >  dats<- data.frame(cbind(visual24,visual52))
> >  remove(visual24, visual52)
> >  
> >  # first code
> >  #--------------
> >  st <-function(data,x,y){
> >  rcc<-coef(lm(y~x))
> >  plot(x,y)
> >  abline(rcc[1],rcc[2])
> >  }
> >  st(data=dats,x=dats$visual24,y=dats$visual52)
> >  
> >  This code works fine, but with such a code the data as an
> arguement to the funtion is not necessary.
> >  However, i wish to write a function that reads the
> variables from the data directly.
> >  I tried using the function below but it does not work.
> >  
> >  # second code
> >  #------------------
> >  st <-function(data,x,y){
> >  rcc<-coef(lm(data$y~data$x))
> >  plot(data$x,data$y)
> >  abline(rcc[1],rcc[2])
> >  }
> >  st(dats,visual24,visual52)
> >  
> >  I wish to inquire if any one has an idea of what i need to
> adjust in the function so that it works.
> >  I believe that the referencing $x or $y in the function is
> not doing the correct thing.
> >  
> >  Better still, will it be a problem if i code the functions
> as in the first code above?
> >  I mean given that they will be used to create a library
> >  
> >  Best regards
> >  Pryseley
> >  
> >
> >
> >
> >---------------------------------
> >
> >
> > [[alternative HTML version deleted]]
> >
> >______________________________________________
> >[hidden email] mailing list
> >https://stat.ethz.ch/mailman/listinfo/r-help
> >PLEASE do read the posting guide!
> http://www.R-project.org/posting-guide.html
> >
> >  
> >
>
> ______________________________________________
> [hidden email] mailing list
> https://stat.ethz.ch/mailman/listinfo/r-help
> PLEASE do read the posting guide!
> http://www.R-project.org/posting-guide.html
>
>

______________________________________________
[hidden email] mailing list
https://stat.ethz.ch/mailman/listinfo/r-help
PLEASE do read the posting guide! http://www.R-project.org/posting-guide.html
Reply | Threaded
Open this post in threaded view
|

Re: Help with R: functions

Pryseley Assam
In reply to this post by Pryseley Assam
Dear R-users
   
  Thanks for all the help/suggestions, they have been most helpful.
   
  Best regards
  Pryseley
   
   
   
   

               
---------------------------------
 

        [[alternative HTML version deleted]]

______________________________________________
[hidden email] mailing list
https://stat.ethz.ch/mailman/listinfo/r-help
PLEASE do read the posting guide! http://www.R-project.org/posting-guide.html