[R] regression with ordered arguments
petr.pikal at precheza.cz
Tue Sep 27 14:20:33 CEST 2011
> Dear Petr,
> thank you so much for your quick reply. I was sure that there were some
> smart ways to address my issue. I went through it and took some time to
> look at the help for lapply and mapply.
> However, some doubts still remain. Following your example, I did:
> lll <-vector(mode = "list", length = 3)
> mmm <-vector(mode = "list", length = 3)
> yyy <- lapply(lll, function(x) runif(100, min =0, max = 1))
> xxx <- lapply(mmm, function(x) runif(100, min =0, max = 1))
> but then I get stucking again. It's not clear to me how to pass a lm
> command to mapply. I tried to give a look at lapply and sapply, but I
> not manage to go much further.
> It would be of big help if you could give me some more hints on this or
> you could provide me with some references. I am sorry, but I find the
> files quite cryptic. Is there a manual or some other source that you
> advice me where I could find some more example on how to deal with
You can use for cycle
for (i in 1:3) lll[[i]] <-lm(yyy[[i]]~xxx[[i]])
put result of lm to list object lll then
gives you summary of each lm function, if you want to use mapply
mmm<-mapply(function(x,y) lm(y~x), xxx, yyy, SIMPLIFY=F)
you can see structure of any object by
Both results shall be similar, for this case I believe that for loop is
easier to understand.
> Thank you very much for your precious support,
> On 27 September 2011 10:08, Petr PIKAL <petr.pikal at precheza.cz> wrote:
> > Dear R listers,
> > I am trying to be a new R user, but life is not that easy.
> > My problem is the following one: let's assume to have 3 outcome
> > (y1, y2, y3) and 3 explanatory ones (x1, x2, x3).
> > How can I run the following three separate regressions without having
> > repeat the lm command three times?
> > fit.1 <- lm(y1 ~ x1)
> > fit.2 <- lm(y2 ~ x2)
> > fit.3 <- lm(y3 ~ x3)
> > Both the y and x variables have been generated extracting random
> > from uniform distributions using a command such as:
> > y1 <- runif(100, min = 0, max = 1)
> > I went to several introductory manuals, the manual R for stata users,
> > econometrics in R, Introductory statistics with R and several blogs
> > files, but I didn't find an answer to my question.
> > can you please help me? In Stata I wouldn't have any problem in
> > this as a loop, but I really can't figure out how to do that with R.
> You can construct loop with naming through paste, numbers and get in R
> but you will find your life much easier to use R powerfull list
> Insted of
> y1 <- runif(100, min = 0, max = 1)
> lll <- vector(mode="list", length=3)
> lll <- lapply(1, function(x) runif(100, min = 0, max = 1))
> you can use probably mapply for doing your regression.
> Or you can easily access part of the list by loop
> for (i in 1:3) lm(lll[[i]]~xx[[i]])
> (if you have your x's in list xx)
> > Thanks in advance for all your help.
> > Best,
> > f.
> > [[alternative HTML version deleted]]
> > ______________________________________________
> > R-help at r-project.org mailing list
> > https://stat.ethz.ch/mailman/listinfo/r-help
> > PLEASE do read the posting guide
> > and provide commented, minimal, self-contained, reproducible code.
More information about the R-help