Repeated run one model and record results in R

rstudio

#1

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)

library(foreign)
mydata = read.dta("http://dss.princeton.edu/training/Panel101.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)
summary(didreg)

Many thanks.


#2

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).


#3

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


#4

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