Re: [R] Regression using mapply?

2010-09-10 Thread Greg Snow
This really depends on why you want to do this and what results you want.  If 
your main goal is to look at some basic tests, goodness of fit, then the add1 
function may do everything you need.  If you just want coefficient estimates 
then some basic matrix algebra will give those to you.

Another option would be to reshape the data to long format and use lmList from 
the nlme package (the above will be quicker if you do not need everything that 
lm gives you).

-- 
Gregory (Greg) L. Snow Ph.D.
Statistical Data Center
Intermountain Healthcare
greg.s...@imail.org
801.408.8111


 -Original Message-
 From: r-help-boun...@r-project.org [mailto:r-help-boun...@r-
 project.org] On Behalf Of Philipp Kunze
 Sent: Wednesday, September 08, 2010 5:35 AM
 To: R-help@r-project.org
 Subject: [R] Regression using mapply?
 
 Hi,
 I have huge matrices in which the response variable is in the first
 column and the regressors are in the other columns. What I wanted to do
 now is something like this:
 
 #this is just to get an example-matrix
 DataMatrix - rep(1,1000);
 Disturbance - rnorm(900);
 DataMatrix[101:1000] - DataMatrix[101:1000]+Disturbance;
 DataMatrix - matrix(DataMatrix,ncol=10,nrow=100);
 
 #estimate univariate linear model with each regressor-column, response
 in the first column
 
 for(i in 2:10){
   result - lm(DataMatrix[,1]~DataMatrix[,i])
 }
 
 
 Is there any way to get rid of the for-loop using mapply (or some other
 function)?
 
 Thanks!
 Philipp
 
 __
 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.

__
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] Regression using mapply?

2010-09-09 Thread Gray Calhoun
Hi Philipp,
  I like to use something like

lapply(2:10, function(j) lm.fit(cbind(1, DataMatrix[,j]), DataMatrix[,1]))

for this sort of thing.  I'd be curious to know if there are other
approaches that are better.

--Gray

On Wed, Sep 8, 2010 at 4:34 AM, Philipp Kunze pku...@gwdg.de wrote:
 Hi,
 I have huge matrices in which the response variable is in the first
 column and the regressors are in the other columns. What I wanted to do
 now is something like this:

 #this is just to get an example-matrix
 DataMatrix - rep(1,1000);
 Disturbance - rnorm(900);
 DataMatrix[101:1000] - DataMatrix[101:1000]+Disturbance;
 DataMatrix - matrix(DataMatrix,ncol=10,nrow=100);

 #estimate univariate linear model with each regressor-column, response
 in the first column

 for(i in 2:10){
        result - lm(DataMatrix[,1]~DataMatrix[,i])
 }


 Is there any way to get rid of the for-loop using mapply (or some other
 function)?

 Thanks!
 Philipp

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




-- 
Gray Calhoun

Assistant Professor of Economics, Iowa State University
http://www.econ.iastate.edu/~gcalhoun/

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


[R] Regression using mapply?

2010-09-08 Thread Philipp Kunze
Hi,
I have huge matrices in which the response variable is in the first
column and the regressors are in the other columns. What I wanted to do
now is something like this:

#this is just to get an example-matrix
DataMatrix - rep(1,1000);
Disturbance - rnorm(900);
DataMatrix[101:1000] - DataMatrix[101:1000]+Disturbance;
DataMatrix - matrix(DataMatrix,ncol=10,nrow=100);

#estimate univariate linear model with each regressor-column, response
in the first column

for(i in 2:10){
result - lm(DataMatrix[,1]~DataMatrix[,i])
}


Is there any way to get rid of the for-loop using mapply (or some other
function)? 

Thanks!
Philipp

__
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] Regression using mapply?

2010-09-08 Thread David Winsemius


On Sep 8, 2010, at 7:34 AM, Philipp Kunze wrote:


Hi,
I have huge matrices in which the response variable is in the first
column and the regressors are in the other columns. What I wanted to  
do

now is something like this:

#this is just to get an example-matrix
DataMatrix - rep(1,1000);
Disturbance - rnorm(900);
DataMatrix[101:1000] - DataMatrix[101:1000]+Disturbance;
DataMatrix - matrix(DataMatrix,ncol=10,nrow=100);

#estimate univariate linear model with each regressor-column, response
in the first column

for(i in 2:10){
result - lm(DataMatrix[,1]~DataMatrix[,i])
}


result - apply(DataMatrix[,2:10], 2, function (x) lm(DataMatrix[, 
1]~x) )


Which would have the added advantage that result would not be  
overwritten for iterations 3:10, which is what your code would have  
done. result will be a list of 9 models which might be a bit  
unweildy, so you might consider something like


result - apply(DataMatrix[,2:10], 2, function (x)  
coef( lm(DataMatrix[,1]~x) ) )

result

When you do so,  you uncover a fatal flaw in your strategy, which  
suggests you have not even done this once on your data or simulations.


--
David.



Is there any way to get rid of the for-loop using mapply (or some  
other

function)?

--
David Winsemius, MD
West Hartford, CT

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