[R] regression, for loop

vito muggeo vito.muggeo at giustizia.it
Thu May 2 10:23:56 CEST 2002


Hi, you could use sapply() on the list having each component=1 data.frame:
That is:

1) Let data be the list where
>is.data.frame(data[[15]]) #say
[1] TRUE
and where the variables of interest are in the same position: 1,2 and 3 for
the response and the two explanatory variables.

2)Build some function to be applied to the list
fn.lm<-function(x,...){lm(x[,1]~x[,2]+x[,3], ...)}

3)Each component of out is a lm model.
out<-sapply(data, fn.lm)

4)Extract the p-values
fn.extract<-function(x,...){coef(summary(x))[,4]}
pvalue<-sapply(out, fn.extract)

#pvalue should be a vector of the p-values from every estimated model.

In my (small) experience using sapply() instead of for() is a bit more
efficient.
However for large list and/or model you could increase the memory limit:
memory.limit(500) #say

Also splitting the list "data" into several sub-lists should speed the
process.

Hope this help you,
best,
vito


----- Original Message -----
From: <yiping.fan at syngenta.com>
To: <r-help at stat.math.ethz.ch>
Sent: Tuesday, April 30, 2002 6:23 PM
Subject: [R] regression, for loop


> Hello, all
>    Here are the questions I have:
> data samples:
>  Data          style  x   y
> data1   r       2   3
> data1   r       3  4
> data1   r       4 5
> data1   g      4  5
> data1  g      6  7
> data1  g     6   7
> data2  r    8  9
> data2  r     8  9
> data2  r     8   9
> data2  g     8    9
> data2  g      8   9
> data2   g   8    9
> ...
> datan
>
>
> we have n data  (data1 - datan).  For each data, I want to do a simple
> regression:
> y ~ x+style
> I am using a for loop now
> for(i in 1:n){
>    data.name=Data[i];
>    res<-lm(y~x+style, data, subset=(Data == data.name) )
> }
>
> I am wondering whether it is possible to do the same without using for
loop?
> Also,  after regression, I am only interested in getting  the P value for
> style, how to extract it?
>
> Thank you very much!
>
>           Best regards,
>
> Y. Fan
> -.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.
-.-.-
> 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
>
_._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._.
_._
-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-
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