Hi,
I am wonder if there is a function similar 'apply' but it could accept
multiple arguments?
For example, I have the following matrix.
x=matrix(1:6,nr=2)
y=matrix(1:6,nr=2)
I want to find a function that can be used to compute the linear
regression for each pair of rows in the two matrices?
multiple_apply(x,y,1,function(u,v){lm(u ~ v)}
That is, I wound like something like the above to compute the
following. Can somebody let me know if there is such an command in R?
lm(x[1,]~y[1,])
lm(x[2,]~y[2,])
Regards,
Peng
Yes, see mapply. On Sat, Sep 5, 2009 at 12:18 PM, Peng Yu <pengyu.ut@gmail.com> wrote:> Hi, > > I am wonder if there is a function similar 'apply' but it could accept > multiple arguments? > > For example, I have the following matrix. > x=matrix(1:6,nr=2) > y=matrix(1:6,nr=2) > > I want to find a function that can be used to compute the linear > regression for each pair of rows in the two matrices? > > multiple_apply(x,y,1,function(u,v){lm(u ~ v)} > > That is, I wound like something like the above to compute the > following. Can somebody let me know if there is such an command in R? > > lm(x[1,]~y[1,]) > lm(x[2,]~y[2,]) > > Regards, > Peng > > ______________________________________________ > R-help@r-project.org 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. >-- Henrique Dallazuanna Curitiba-Paraná-Brasil 25° 25' 40" S 49° 16' 22" O [[alternative HTML version deleted]]
Hi Peng,
Here is a suugestion using lapply():
res <- lapply(1:2, function(i) lm( x[i, ] ~ y[i, ]) )
names(res) <- c('row1','row2')
res
# The summaries for each regression
lapply(res, summary)
# for the coefficients only
lapply(res, coef)
# ANOVAs
lapply(res, anova)
HTH,
Jorge
On Sat, Sep 5, 2009 at 11:18 AM, Peng Yu <pengyu.ut@gmail.com> wrote:
> Hi,
>
> I am wonder if there is a function similar 'apply' but it could
accept
> multiple arguments?
>
> For example, I have the following matrix.
> x=matrix(1:6,nr=2)
> y=matrix(1:6,nr=2)
>
> I want to find a function that can be used to compute the linear
> regression for each pair of rows in the two matrices?
>
> multiple_apply(x,y,1,function(u,v){lm(u ~ v)}
>
> That is, I wound like something like the above to compute the
> following. Can somebody let me know if there is such an command in R?
>
> lm(x[1,]~y[1,])
> lm(x[2,]~y[2,])
>
> Regards,
> Peng
>
> ______________________________________________
> R-help@r-project.org 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.
>
[[alternative HTML version deleted]]