Announcement

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

  • correlation coefficient

    Hi,

    I have the following code for a simulation. I need help to conclude it.

    set obs 100
    gen ut=.
    gen vt=.
    gen yt=1
    generate order=_n
    gen zt=1
    capture program drop my_model
    program define my_model, rclass
    replace ut = rnormal(0,1)
    replace vt = rnormal(0,1)
    replace yt=4+yt[_n-1]+ vt if order!=1
    replace zt=2+zt[_n-1]+ ut if order!=1
    regress yt zt
    return scalar constant = _b[_cons]
    return scalar coef = _b[zt]
    return scalar t = _b[zt]/_se[zt]
    return scalar r2 = e(r2)
    exit
    end

    simulate constant=r(constant) coef=r(coef) t=r(t) r2=r(r2), reps(1000): my_model


    How can I record correlation coefficient between y and z for each simulation? Corr gives matrix but I need to record only the correlation coefficient and then take the average of them. Thanks in advance.

    Ulas

  • #2
    Given something like

    Code:
    corr yt zt
    the resulting single correlation will be returned in r(rho). This is documented in the help for correlate.

    However, as you are already returning R-squared from the corresponding regression, this hardly seems necessary. The (absolute value) of the correlation is just its (positive) square root and nothing informative is added by adding the correlation to your other results. (If the correlation is negative, the sign is also preserved as the sign of the slope.)

    By the way, I wouldn't usually average correlations; I would work with the FIsher z transform in combining correlations. For more on that, see http://www.stata-journal.com/article...article=pr0041

    EDIT: I would advise setting the seed for work like this.
    Last edited by Nick Cox; 30 Sep 2016, 09:27.

    Comment


    • #3
      Thank you for your response but I could not fix my code. I did not understand where I should add "corr yt zt" and "r(rho)". Stata help does not answer this question or I could not get it.

      Comment


      • #4
        As Nick points out, you don't even need to use -corr- in this context, because you already have R2, which is the square of the correlation you seek, and its sign will always be the same as the sign of the zt coefficient. So after you get your simulate results you can just calculated it as -gen r = sign(coef)*sqrt(r2)-.

        Comment

        Working...
        X