Announcement

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

  • How to count coefficients and p-values of a regression?

    Hello,

    i have another question regarding my code.

    Code:
    set matsize 511
    
    unab y : TOYOTAMOTORTOTRETURNIND-COMMUTUREDEADDELIST280910
    
    local wc : word count `y'
    
    matrix p = J(`wc',3,.)
    matrix rownames p = `y'
    matrix colnames p = "adjR2" "coef" "p"
    
    matlist p
    
    tempfile coefficients
    capture postutil clear
    postfile handle coeff using `coefficients'
    
    
    local i = 1
    foreach var of local y {
    {    
        capture noisily reg `var' RenditenTopix PercentageChangeTWEXR if `var'>-9999, robust
        if c(rc) ==0 {
        matrix p[`i',1] = (1-(1-e(r2))*(e(df_r)+e(df_m))/(e(df_r))), _b[PercentageChangeTWEXR], 2*ttail(e(df_r), abs(_b[PercentageChangeTWEXR]/_se[PercentageChangeTWEXR]))
        post handle (_b[PercentageChangeTWEXR])
    }
    local ++i
    }
    }    
    postclose handle
    
    
    matlist p
    
    preserve 
    use `coefficients', clear
    tabstat coeff, statistics (sum mean)
    
    restore
    My Dataset consist of 502 companies and -9999 describes the no observation data.
    So my problem is that i want Stata to count the number of coefficients which are <0 and the p-values which are <0.05 (significant at 5% level).
    Is that possible?

  • #2
    What you're doing here is more complicated than it needs to be. Saving results in a matrix is really not helpful in Stata unless you are going to actually be doing matrix algebra with the results. And given what you are putting in the matrix that seems extremely unlikely. So it's simpler to just put everything into the postfile, and then you can easily count anything you like about the results.

    Code:
    unab y : TOYOTAMOTORTOTRETURNIND-COMMUTUREDEADDELIST280910
    
    local wc : word count `y'
    
    tempfile coefficients
    capture postutil clear
    postfile handle str32 yvar float(coeff adjr2 p_value) using `coefficients'
    
    
    foreach var of local y {
        replace `var' = . if `var' <= -9999
        capture noisily reg `var' RenditenTopix PercentageChangeTWEXR, robust
        matrix T = r(table)
        if c(rc) ==0 {
            post handle ("`var'") (_b[PercentageChangeTWEXR]) (e(r2_a)) (T[4, 2))
        }
    }    
    postclose handle
    
    preserve
    use `coefficients', clear
    tabstat coeff, statistics (sum mean)
    
    count if coeff < 0 & pvalue < 0.05
    
    restore
    Notes:
    1. Not tested, beware of typos or other errors.
    2. It is not a good idea in Stata to use magic numbers like -9999 to code for missing values. It makes the code unnecessarily complicated because you have to tack -if `var' < -9999 on to every command you apply to the data. And sooner or later you forget and this gives you garbage results. So it's better to replace those by genuine Stata missing values, and then you don't have to think about them when you do regressions and the like. I've done that in the loop you already have.
    3. As you can see, there is no need to code your own calculations of adjusted R2 and the p-value for a coefficient, as these can just be pulled from the results returned by -regress-.
    4. Your request was ambiguous: I have assumed that you want to count the number of coefficients that are both <0 and have an associated p-value < 0.05. You might have meant that you want two separate counts: one for the number that are < - and another count for those with p-value < 0.05. I think it's obvious how to change the code above if that's what you meant.

    Finally, counting coefficients with p-value < 0.05 sounds like you are still living in the world of statistical significance that the American Statistical Association recommends we relegate to the dustbin of history. See https://www.tandfonline.com/doi/full...5.2019.1583913.

    Added: If you really do need those results in a matrix for some reason, you can use the -mkmat- command to create that matrix while the `coefficients' data set is in memory. -help mkmat-.

    Comment

    Working...
    X