[R] apply lm() for all the columns of a matrix
Greg Snow
Greg.Snow at imail.org
Wed Apr 9 21:58:15 CEST 2008
It is still not clear what is your response (y) variable and exactly
what is your predictor (x) variable(s).
If you have a separate vector of length 10 that is the response and you
want to regress it with each of the 8 vectors mentioned, then here is
one way to do that:
> p<-1:80+rnorm(80)
> dim(p)<-c(2,2,2,10)
>
> y <- 1:10
>
> my.data <- data.frame( y=rep(y,8), x=c(aperm(p,4:1)), g=gl(8,10) )
>
> library(nlme)
>
> fits <- lmList( y ~ x | g, data=my.data )
>
Now 'fits' is a list with the 8 regressions, you can access each
regression with fits[[1]] and the like, or there are some summaries that
you can get from the entire list.
Hope this helps,
--
Gregory (Greg) L. Snow Ph.D.
Statistical Data Center
Intermountain Healthcare
greg.snow at imail.org
(801) 408-8111
> -----Original Message-----
> From: r-help-bounces at r-project.org
> [mailto:r-help-bounces at r-project.org] On Behalf Of Costas Douvis
> Sent: Wednesday, April 09, 2008 8:54 AM
> To: Dimitris Rizopoulos; r-help at r-project.org
> Subject: Re: [R] apply lm() for all the columns of a matrix
>
> Here's a simplified example
>
> p<-1:80+rnorm(80)
> dim(p)<-c(2,2,2,10)
>
> We could say that the 4-d array p consists of 2*2*2 = 8
> vectors of length 10. So what I'm asking for is a fast way to
> perform a linear fit to all those vectors.
>
> I'm sorry if I'm causing you to have a headache with all
> those dimensions :) Kostas
>
> > Well, could you provide a little bit more information
> regarding what
> > you are trying to do (e.g., reproducible example).
> >
> > Best,
> > Dimitris
> >
> > ----
> > Dimitris Rizopoulos
> > Biostatistical Centre
> > School of Public Health
> > Catholic University of Leuven
> >
> > Address: Kapucijnenvoer 35, Leuven, Belgium
> > Tel: +32/(0)16/336899
> > Fax: +32/(0)16/337015
> > Web: http://med.kuleuven.be/biostat/
> > http://www.student.kuleuven.be/~m0390867/dimitris.htm
> >
> >
> > ----- Original Message -----
> > From: "Costas Douvis" <cdouvis at geol.uoa.gr>
> > To: <r-help at r-project.org>; "Mark Leeds" <markleeds at verizon.net>;
> > "Dimitris Rizopoulos" <dimitris.rizopoulos at med.kuleuven.be>
> > Cc: "Chuck Cleland" <ccleland at optonline.net>
> > Sent: Wednesday, April 09, 2008 3:44 PM
> > Subject: Re: [R] apply lm() for all the columns of a matrix
> >
> >
> >> Thank you all very much for replying. Of course you are absolutely
> >> right but unfortunately I really deal with the case of a
> 4-d matrix
> >> so what you said does not apply. I should have specified
> but being a
> >> new R user I hadn't realized the difference between a
> matrix and an
> >> array.
> >>
> >> So please tell me if you know a fast way (not using a loop) to
> >> perform a linear fit on all the vectors of the 4-th dimension of a
> >> 4-d array.
> >>
> >> Thanks again
> >> Kostas
> >>
> >>> If you have the same design matrix then you can specify a
> matrix of
> >>> responses in lm(), e.g.,
> >>>
> >>> Y <- matrix(rnorm(100*10), 100, 10)
> >>> x <- rnorm(100)
> >>>
> >>> fit <- lm(Y ~ x)
> >>> fit
> >>> summary(fit)
> >>>
> >>>
> >>> I hope it helps.
> >>>
> >>> Best,
> >>> Dimitris
> >>>
> >>> ----
> >>> Dimitris Rizopoulos
> >>> Biostatistical Centre
> >>> School of Public Health
> >>> Catholic University of Leuven
> >>>
> >>> Address: Kapucijnenvoer 35, Leuven, Belgium
> >>> Tel: +32/(0)16/336899
> >>> Fax: +32/(0)16/337015
> >>> Web: http://med.kuleuven.be/biostat/
> >>> http://www.student.kuleuven.be/~m0390867/dimitris.htm
> >>>
> >>>
> >>> ----- Original Message -----
> >>> From: "Costas Douvis" <cdouvis at geol.uoa.gr>
> >>> To: <r-help at r-project.org>
> >>> Sent: Wednesday, April 09, 2008 12:55 PM
> >>> Subject: [R] apply lm() for all the columns of a matrix
> >>>
> >>>
> >>>> Hi all,
> >>>>
> >>>> My question is not really urgent. I can write a loop and
> solve the
> >>>> problem. But I know that I'll be in a similar situation
> many more
> >>>> times so it would be useful to find out the answer
> >>>>
> >>>> Is there a fast way to perform linear fit to all the
> columns of a
> >>>> matrix?
> >>>> (or in the one dimension of a multi-dimensional array.)
> I'm talking
> >>>> about many single linear fits, not about a multiple fit.
> I thought
> >>>> that a combination of apply and lm would do it but I
> can't make it
> >>>> work
> >>>>
> >>>> Thank you
> >>>> Kostas
> >>>>
> >>>>
> >>>> --
> >>>> Kostas Douvis
> >>>> PhD Student
> >>>> University of Athens - Department of Geography and Climatology
> >>>> Academy of Athens - Research Centre for Atmospheric Physics and
> >>>> Climatology
> >>>> email: cdouvis at geol.uoa.gr
> >>>> tel: +30-210-8832048
> >>>>
> >>>> ______________________________________________
> >>>> R-help at 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.
> >>>>
> >>>
> >>>
> >>> Disclaimer: http://www.kuleuven.be/cwis/email_disclaimer.htm
> >>>
> >>>
> >>
> >>
> >
> >
> > Disclaimer: http://www.kuleuven.be/cwis/email_disclaimer.htm
> >
> >
>
>
> --
> Kostas Douvis
> PhD Student
> University of Athens - Department of Geography and
> Climatology Academy of Athens - Research Centre for
> Atmospheric Physics and Climatology
> email: cdouvis at geol.uoa.gr
> tel: +30-210-8832048
>
> ______________________________________________
> R-help at 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.
>
More information about the R-help
mailing list