Repeated run one model and record results in R



Hello everyone,

What I want to achieve is to run the regression many times, each time R records the estimates of did in one data.frame. Everytime, I change the "ifelse" condition to different years , ie., ifelse(mydata$year >= 1993, 1, 0), thus I can run a different regression.

I attached my code as the following: Can anyone help it? (The data can be downloaded through browser if R returned errors)

mydata = read.dta("")
mydata$time = ifelse(mydata$year >= 1994, 1, 0)
mydata$did = mydata$time * mydata$treated
mydata$treated = ifelse(mydata$country == "E" | mydata$country == "F" | mydata$country == "G", 1, 0)
didreg = lm(y ~ treated + time + did, data = mydata)

Many thanks.


Hi @Lernst! Could you please upload your CSV to somewhere like Pastebin or a GitHub Gist? You can then pop the URL from one of those sites straight into your read_csv call, which is a fair bit safer than asking users to download an unknown file from Discourse (especially one whose file extension you've changed).


Many thanks, rensa. I editored my post to make it more clearly.


Acturally, I have vague idea about it: write a loop to achieve it. But, each time, how to exact the estimate of did and then store it in a data.frame?

Many thanks