Re: [R] Odp: Loop counter used in variable

2009-12-15 Thread Martin Maechler
 PP == Petr PIKAL petr.pi...@precheza.cz
 on Tue, 15 Dec 2009 09:03:11 +0100 writes:

PP Hi
PP r-help-boun...@r-project.org napsal dne 15.12.2009 00:25:10:

 
 Hi All,
 
 I need to run muliple lm functions.  My independent variables are called
 dataset$x1, x2, x3, x4 etc.
 
 How can I use a loop counter variable to replace the numbers?
 
 
 
 fit1=lm(dataset$y~dataset$x1)
 fit2=lm(dataset$y~dataset$x2)
 fit3=lm(dataset$y~dataset$x3)
 fit4=lm(dataset$y~dataset$x4)

PP I would use list for results and brackets for lm fit. Something like 
PP (untested)

PP fit- vector(list, length=4)

PP for (i in 1:4) {
PP fit[[i]] - lm(y~dataset[,i], data=dataset)
PP }

Also, in almost all such cases, 
where you have many regressions,  you'd rather  want  lm.fit()
instead of lm(), for efficiency reasons.

lm.fit() is probably among the much R functions that are less
known than they should be.
It *would* be known more if useRs *would* read help pages ...
?lm  in this case.

Martin

__
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.


Re: [R] Odp: Loop counter used in variable

2009-12-15 Thread Petr PIKAL
Hi

Martin Maechler maech...@stat.math.ethz.ch napsal dne 15.12.2009 
09:17:55:

  PP == Petr PIKAL petr.pi...@precheza.cz
  on Tue, 15 Dec 2009 09:03:11 +0100 writes:
 
 PP Hi
 PP r-help-boun...@r-project.org napsal dne 15.12.2009 00:25:10:
 
  
  Hi All,
  
  I need to run muliple lm functions.  My independent variables are 
called
  dataset$x1, x2, x3, x4 etc.
  
  How can I use a loop counter variable to replace the numbers?
  
  
  
  fit1=lm(dataset$y~dataset$x1)
  fit2=lm(dataset$y~dataset$x2)
  fit3=lm(dataset$y~dataset$x3)
  fit4=lm(dataset$y~dataset$x4)
 
 PP I would use list for results and brackets for lm fit. Something 
like 
 PP (untested)
 
 PP fit- vector(list, length=4)
 
 PP for (i in 1:4) {
 PP fit[[i]] - lm(y~dataset[,i], data=dataset)
 PP }
 
 Also, in almost all such cases, 
 where you have many regressions,  you'd rather  want  lm.fit()
 instead of lm(), for efficiency reasons.
 
 lm.fit() is probably among the much R functions that are less
 known than they should be.
 It *would* be known more if useRs *would* read help pages ...
 ?lm  in this case.

Point taken. I do not use lm in cycles very often and when doing so I did 
not encounter performance problem yet. 

R computation is almost always quicker then me when I try to think about 
the problem to be solved correctly.

And personally I prefer to put everything in nice pictures instead of 
reading output from n fits. Therefore I try to make multipage PDF plots 
which can have more than two dimensions by col, pch and size variation 
with other variables. Than I can list through it to see if there is some 
pattern.

Regards
Petr

 
 Martin

__
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.