Announcement

Collapse
No announcement yet.
X
  • Filter
  • Time
  • Show
Clear All
new posts

  • error on storing regression results into matrix

    Hi,
    My model is as follows:

    Code:
     bysort Z: reg y x i.(W)
    where Z is a categorical variable and W is a vector of categorical variables (including w1, w2, w3, etc). I want to store the estimated coefficients, ste's, pvalues, and lower and upper confidence intervals into a matrix. I'm using the following codes:

    Code:
    return list
    matrix list r(table)
    mat resultsmat = r(table)
    mat list resultsmat
    
    local i=0
    foreach j in z 0b.w1 {
    local ++i
    global beta_`j'=resultsmat[1,`i']
    global ste_`j' = resultsmat[2,`i']
    gl lc_i`j' = resultsmat[5,`i']
    gl uci_`j' = resultsmat[6,`i']
    global pval_`j'=resultsmat[4,`i']
    }
    However, I get the following error: 'beta_0b.w1 invalid name.'

    I would appreciate any insights.

    Thanks,

  • #2
    Well, the message means exactly what it says. On the second run through the loop, local macro j contains 0b.w1, and beta_0b.w1 is not a valid name for a global macro (nor for a variable). Only letters, digits, and underscores are permitted.

    You can work around that problem with something like this:

    Code:
    foreach j in z 0b.w1 {
    local ++i
    local jj = subinstr local j "." "_"
    global beta_`jj' = resultsmat[1, `i']
    // etc.
    }
    This will result in names like beta_ob_w1, ste_ob_w1, etc., which are legal.

    I should point out that if you are using a recent*version of Stata, the mechanism of marching a counter i through the columns of resultsmat is not necessary. You also don't need the 1 through 6 numbering to pull the coefficient, standard error, confidence limits, etc. You can just code this as:
    Code:
    foreach j in z 0b.w1 {
        local jj: subinstr local j "." "_"
        global beta_`jj' = resultsmat["b", "`j'"]
        global ste_`jj' = resltsmat["se", "`j'"]
        // etc.
    }
    Now, this actually won't work because there is no variable z in your regression. So for whatever reason, you want to name the globals that contain the results for variable x with z. But that doesn't make much sense unless there is some special reason to do that.

    By the way, I hope you are also aware that your -bysort Z: reg...- command runs through regressions on all of the subsets defined by levels of variable Z, but at the end only the results from the final value of Z are still there. So there is really no point in doing all those regressions and having their results thrown away. Either you should just run the last regression, or, if you want to try to store all of those results, you have to abandon -bysort- and put the regressions inside a -foreach- loop along with the code to create those globals.

    As it is getting late and I'm tired, I won't hassle you over the inappropriate use of global macros here. Hopefully no harm will come of it; usually you get away with it even though it's inherently unsafe.

    *I'm not sure how recently this was introduced in Stata. It might have been version 15 or something like that. But most people nowadays are using 17 or 16, and both of them support this easier syntax. And it may even go back earlier than that.

    Comment

    Working...
    X