Quantcast

How can a function in R handle different types of input?

classic Classic list List threaded Threaded
3 messages Options
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate
star

How can a function in R handle different types of input?

stella
Hi,
 
How can a function in R handle different types of input?
I have written a function, which should calculate the slope from several 3-time-point measurements by linear regression

4 three-time-point-measurements:
x<-cbind(c(1,2,3,4),c(2,3,4,5),c(3,4,5,6))

time points:
time<-c(1,3,9)

function for calculating the slope by linear regression:
fit<-function(xx,t){slope <- coefficients(lm(log(xx) ~ 0 + t))[1]
return(slope)
}
alpha<-fit(x[1,],time)
 
At the moment the function output 'alpha' is calculated for x(x1=1,x2=2,x3=3). I would like to get 'alphas' for all four rows of x without using a for-loop.  If I use 'mapply', I get outputs to very entry of x (12 outputs) instead of four.
 
Thank you very much in advance! Yor help is really appreciated!
Stella
 
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate
star

Re: How can a function in R handle different types of input?

Rui Barradas
Hello,

stella wrote
Hi,
 
How can a function in R handle different types of input?
I have written a function, which should calculate the slope from several 3-time-point measurements by linear regression

4 three-time-point-measurements:
x<-cbind(c(1,2,3,4),c(2,3,4,5),c(3,4,5,6))

time points:
time<-c(1,3,9)

function for calculating the slope by linear regression:
fit<-function(xx,t){slope <- coefficients(lm(log(xx) ~ 0 + t))[1]
return(slope)
}
alpha<-fit(x[1,],time)
 
At the moment the function output 'alpha' is calculated for x(x1=1,x2=2,x3=3). I would like to get 'alphas' for all four rows of x without using a for-loop.  If I use 'mapply', I get outputs to very entry of x (12 outputs) instead of four.
 
Thank you very much in advance! Yor help is really appreciated!
Stella
It's close to what you were doing, just try

apply(x, 1, fit, time)

Hope this helps,

Rui Barradas
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate
star

Re: How can a function in R handle different types of input?

Michael Weylandt
In reply to this post by stella
You can run multiple regressions at once:

x <- 1:4
y <- x + 10
z <- 0:3
lm(cbind(x,y) ~ z)

Michael

On Wed, May 2, 2012 at 9:48 AM, stella <[hidden email]> wrote:

> Hi,
>
> How can a function in R handle different types of input?
> I have written a function, which should calculate the slope from several
> 3-time-point measurements by linear regression
>
> 4 three-time-point-measurements:
> x<-cbind(c(1,2,3,4),c(2,3,4,5),c(3,4,5,6))
>
> time points:
> time<-c(1,3,9)
>
> function for calculating the slope by linear regression:
> fit<-function(xx,t){slope <- coefficients(lm(log(xx) ~ 0 + t))[1]
> return(slope)
> }
> alpha<-fit(x[1,],time)
>
> At the moment the function output 'alpha' is calculated for
> x(x1=1,x2=2,x3=3). I would like to get 'alphas' for all four rows of x
> without using a for-loop.  If I use 'mapply', I get outputs to very entry of
> x (12 outputs) instead of four.
>
> Thank you very much in advance! Yor help is really appreciated!
> Stella
>
>
>
> --
> View this message in context: http://r.789695.n4.nabble.com/How-can-a-function-in-R-handle-different-types-of-input-tp4603263.html
> Sent from the R help mailing list archive at Nabble.com.
>
> ______________________________________________
> [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
> and provide commented, minimal, self-contained, reproducible code.

______________________________________________
[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
and provide commented, minimal, self-contained, reproducible code.
Loading...