[R] foreach loop, stata equivalent

Jamora, Nelissa nelissa.jamora at agr.uni-goettingen.de
Mon Feb 18 17:09:03 CET 2013


Hi Milan, 

Thanks for responding to my question. I'm actually not interested in LM, it
was more  for example. 

You are right, I'm trying an enormous set of model runs. My Var1 n=14; Var2
n=255 ==> 3570!  
But first, I need be able to set up 2 variables in each model run. Those 2
variables are different in each case. I can set this up 1-by-1 but it will be
tedious and not efficient. 

To describe in more details
I have a data frame with 269 variables.
1. individual columns 1-14 can be my first variable
2. individual columns 15-269 can be my second variable.

Variable1 and variable2 are different in each case. For e.g.
Model 1: var1 and var15
Model 2: var1 and var16
Model 3: var1 and var17...
....
Model 3570: var14 and var269
So I need to write a loop command that calls for different sets of variable1
and variable2 in each run.

What do I intend to do with this? I'm running threshold vecm (package tsDyn),
and I need to summarize threshold estimates in each model run (or market
pairs, var1 and var2). The goal is to extract N=3,570 threshold estimates. 
I did similar linear VECM estimates in Stata using my foreach loop, but now I
need to make parallel run in R but using threshold model.

Hope this clears things.
Nelissa








-----Original Message-----
From: Milan Bouchet-Valat [mailto:nalimilan at club.fr] 
Sent: Monday, February 18, 2013 3:44 PM
To: Jamora, Nelissa
Cc: r-help at r-project.org
Subject: Re: [R] foreach loop, stata equivalent

Le lundi 18 février 2013 à 13:48 +0100, Jamora, Nelissa a écrit :
> Hi! I'm a recent convert from Stata, so forgive my ignorance.
> 
>  
> 
> In Stata, I can write foreach loops (example below)
> 
>  
> 
> foreach var of varlist p1-p14 {
> 
> foreach y of varlist p15-p269 {
> 
>   reg `var' `y'
> 
> }
> 
> }
> 
>  
> 
> It's looping p1-p15, p1-p16...., p1-p269, p2-p15, p2-p16,... p2-p269,...
> variable pairs.
> 
>  
> 
> How can I write something similar in R? 
> 
> I 'tried' understanding the package.foreach but can't get it to work.
You do not need package foreach, which is intended at a completely different
problem.

R does not really have the syntactic equivalent of "varlist", but you can
easily do something like:
for(var in paste0("p", 1:14)) {
    for(y in paste0("p", 15:269))
        lm(yourData[[var]] ~ yourData[[y]]) }

provided that yourData is the data frame in which the p* variables are
stored.

There are probably more direct ways of doing the same thing and storing the
resulting lm objects in a list, but you did not state what you intend to do
with this enormous set of regressions...


Regards

> Thanks for any help
> 
> Nelissa
> 
> 
> 	[[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 
> http://www.R-project.org/posting-guide.html
> and provide commented, minimal, self-contained, reproducible code.



More information about the R-help mailing list