[R] Regression using mapply?

Gray Calhoun gray.calhoun at gmail.com
Fri Sep 10 03:37:34 CEST 2010


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 <pkunze at 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 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.
>



-- 
Gray Calhoun

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



More information about the R-help mailing list