Hi > 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 variables > (y1, y2, y3) and 3 explanatory ones (x1, x2, x3). > How can I run the following three separate regressions without having to > 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 numbers > 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 and help > 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 running > 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 too but you will find your life much easier to use R powerfull list operations. 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) Regards Petr > Thanks in advance for all your help. > Best, > f. > > [[alternative HTML version deleted]] > > ______________________________________________ > 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.