Search code examples
rloopsregressionstataestimation

Estimate multiple Regression Models for groups and save a certain parameter value


I looking for a way to estimate various models (lets say 10) and save a certain parameter value from each estimation in a vector with stata.

Im more of a R-guy and here it is very simple working example with R-code

n1 <- 100
n2 <- 10
group <- rep(1:10,each=n1)
data <- as.data.frame(cbind(rnorm(n1*n2,0,1),rnorm(n1*n2,0,1),group))
dimnames(data)[[2]] <- c("y","x","group")
val <- names(table(group))
estimates <- vector(mode="numeric",length=length(val))

for( i in 1:length(val)){
j <- which(data$group==val[i])
estimates[i] <- coef(lm(y[j] ~ x[j], data=data))[2]
}

Alternatively

library(nlme)
mod1 <- lmList(y~x | group, data=data)
coef(mod1)[,2]

And yes, unfortunately I need to use stata :-(


Solution

  • What is you ultimate objective? The paradigms of Stata and R are different, so knowing the ultimate goal would help. In R I tend to think in terms of vectors, but not in Stata (vectors don't really exist in Stata). If you want a table, then I suggest the estout package from SSC (ssc install estout). If just want the coefficients as an end in themselves, then I suggest statsby.

    clear
    version 11.2
    set seed 2001
    
    * generate your data
    set obs 1000
    generate y = rnormal()
    generate x = rnormal()
    generate group = 1 + floor((_n - 1) / 100)
    
    * if you want a table
    * you'll need the estout package from SSC (ssc install estout)
    eststo clear
    forvalues i = 1/10 {
        eststo : regress y x if (group == `i')
    }
    esttab
    
    * if you just the coefficients
    statsby, by(group) clear : regress y x
    list
    

    Both esttab and statsby have lots of options, so check out the help files.


    Update: It seems you want time series betas by group (here a firm). In terms of economics I think you would want rolling regressions, but this framework should get you started.

    clear
    version 11.2
    set seed 2001
    
    * generate your data
    set obs 1000
    generate y = rnormal()
    generate x = rnormal()
    generate firm = 1 + floor((_n - 1) / 100)
    generate year = 1 + mod((_n - 1), 100)
    
    * regress by firm
    xtset firm year
    statsby _b, by(firm) saving(temp, replace) : regress y x
    
    * then merge back
    merge m:1 firm using temp
    list in 1/20