Announcement

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

  • Mata Code Not Working to find standard deviation

    Consider the following data
    input gvkey ROA
    1004 .053175
    1004 .0573483
    1004 .0474536
    1004 .0264029
    1004 -.0829894
    1004 -.018074
    1004 .0049401
    1004 .021104
    1004 .0359239
    1004 .054944
    1004 .0551714
    1004 .0570965
    1004 .0297313
    1004 .0409843
    1004 .0308441
    1004 .0257382
    1010 .0680225
    1010 .0206306
    1010 .0216928
    1010 .0210568
    1010 .0391341
    1010 .021526
    1010 .0729496
    1013 .1162412
    1013 .112816
    1013 .0523967
    1013 .2186375
    1013 -.5151418
    1013 -1.000699
    1013 .0114838
    1013 .0721173
    1013 .040772
    1013 .0602335
    1013 -.0218116
    1013 -.3530068
    1013 .0420482
    1019 .0426432
    1019 .0562442
    1019 .0349
    1019 .0615266
    1019 .0418342
    1021 .075502
    1021 -.1783399
    1021 -.1578007
    1021 -.0696072
    1021 -.2012739
    1021 .0107006
    1021 -.2506618
    1021 .2153723
    1021 .233043
    1021 .0700788
    1021 -.1719848
    1021 -.5162843
    end
    I am trying to find standard deviation with the following code, but it is not working. Can someone debug it. Thanks
    Code:
    gen SD=.
    mata
    mata clear
    st_view(SD=., ., "SD")
    st_view(id=., ., " gvkey ")
    st_view(roa=., ., " ROA ")
    p=panelsetup(id,1)
    for (i=1; i<=rows(p); i++) {
        for (o=p[i,1]; o<=p[i,2]; o++) {
        h = J(1,1,.)
        h = h \ roa[o,1]
        SD[o,1]=sqrt(variance(h[(2.. rows(h)),.])) 
        }
    }
    end
    Regards
    --------------------------------------------------
    Attaullah Shah, PhD.
    Professor of Finance, Institute of Management Sciences Peshawar, Pakistan
    FinTechProfessor.com
    https://asdocx.com
    Check out my asdoc program, which sends outputs to MS Word.
    For more flexibility, consider using asdocx which can send Stata outputs to MS Word, Excel, LaTeX, or HTML.

  • #2
    Hi Attaullah ,

    I assumed that you want to calculate SD for each id.

    Code:
    gen SD=.
    mata
    mata clear
    st_view(SD=., ., "SD")
    st_view(id=., ., " gvkey ")
    st_view(roa=., ., " ROA ")
    p=panelsetup(id,1)
    for (i=1; i<=rows(p); i++) {
        for (o=p[i,1]; o<=p[i,2]; o++) {
            SD[o,1]=sqrt(variance(roa[p[i,1]..p[i,2],.]))
        }
    }
    end
    Abraham

    Comment


    • #3
      Thanks again Abraham. I would appreciate if you tell me where was I making the mistake
      Regards
      --------------------------------------------------
      Attaullah Shah, PhD.
      Professor of Finance, Institute of Management Sciences Peshawar, Pakistan
      FinTechProfessor.com
      https://asdocx.com
      Check out my asdoc program, which sends outputs to MS Word.
      For more flexibility, consider using asdocx which can send Stata outputs to MS Word, Excel, LaTeX, or HTML.

      Comment


      • #4
        Attaullah,

        Here is your code corrected. For each id (p indicates that ) and each focal point (o indicates that) you go through all obs that belong to the same id (using t ), and add observations to h, compute SD and assign that to the focal point. h should be initiated anew for each focal point.

        Code:
        gen SD=.
        mata
        mata clear
        st_view(SD=., ., "SD")
        st_view(id=., ., " gvkey ")
        st_view(roa=., ., " ROA ")
        p=panelsetup(id,1)
        for (i=1; i<=rows(p); i++) {
            for (o=p[i,1]; o<=p[i,2]; o++) {
          h = J(1,1,.)
          for (t=p[i,1]; t<=p[i,2]; t++) {
          h = h \ roa[t,1] 
          }
          SD[o,1]=sqrt(variance(h[(2.. rows(h)),.]))    
         }
        }
        end
        However, It is unnecessary to do these loops as what you are interested is in one SD for all obs with the same ID. The following will give the same thing

        Code:
        gen SD=.
        mata
        mata clear
        st_view(SD=., ., "SD")
        st_view(id=., ., " gvkey ")
        st_view(roa=., ., " ROA ")
        p=panelsetup(id,1)
        for (i=1; i<=rows(p); i++) {
            SD[p[i,1]..p[i,2],1] =  sqrt(variance(roa[p[i,1]..p[i,2],1])) :* J( rows(roa[p[i,1]..p[i,2],1] )  , 1, 1)
        }
        end
        Abraham

        Comment


        • #5
          So great of you Abraham.
          Regards
          --------------------------------------------------
          Attaullah Shah, PhD.
          Professor of Finance, Institute of Management Sciences Peshawar, Pakistan
          FinTechProfessor.com
          https://asdocx.com
          Check out my asdoc program, which sends outputs to MS Word.
          For more flexibility, consider using asdocx which can send Stata outputs to MS Word, Excel, LaTeX, or HTML.

          Comment

          Working...
          X