Skip to content
Prev 317811 / 398506 Next

foreach loop, stata equivalent

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 :
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