Announcement

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

  • How to use Matrix and Loop to summarize the variable?

    Hi, guys, i got trouble about it.

    I summarize my variables and want to use matrix to fill in with observation, mean stand Dev, Min, Max and Sharp ratio(which is mean/standard deviation)

    First, i make a matrix

    command:
    matrix A = J(18,6,.)
    matrix colnames A="OBS" "MEAN" "SD" "MIN" "MAX" "SHARP"
    matrix rownames A="RR_0" "RR_5" "RR_10" "Diff_FS_0" "Diff_FS_3" "Diff_FS_6" "Diff_FS_9" "Diff_FS_12" "Diff_SL_0" "Diff_SL_3" "Diff_SL_6" "Diff_SL_9" "Diff_SL_12" "Coll_R" "Put_R" "Call_R" "Por_P" "Por_ST"

    OBS MEAN SD MIN MAX SHARP ( or see potato)
    RR_0
    RR_5
    RR_10
    Diff_FS_0
    Diff_FS_3
    Diff_FS_6
    Diff_FS_9
    Diff_FS_12
    Diff_SL_0
    Diff_SL_3
    Diff_SL_6
    Diff_SL_9
    Diff_SL_12
    Coll_R
    Put_R
    Call_R
    Por_P
    Por_ST


    Then i got this matrix. Now, i do not know how to use loop to fill the blank. The left side is my variable. Total is 18 variables. The top has 6 factors including "obs, mean, sd, min, max, sharp ratio"

    I try to use loop.
    The command:

    foreach v of var*{
    summarize `v'
    return list
    matrix A= r(N),r(mean),r(sd),r(min),r(max),r(mean)/r(sd)
    }

    But it does not work. I think I miss something, i should repeat it until all fill in, right?

    I attach a sample which includes 8 variables

    anyone can help me?

    Thank you
    Attached Files
    Last edited by Cam Xe; 09 Dec 2015, 02:20.

  • #2
    forvalues i=1/8{
    foreach v of var*{
    summarize `v'
    return list
    matrix a_`i'= r(N),r(mean),r(sd),r(min),r(max),r(mean)/r(sd)
    }
    }
    mat A=a_1\a_2\a_3\a_4\a_5\a_6\a_7\a_8



    matrix colnames A="OBS" "MEAN" "SD" "MIN" "MAX" "SHARP"
    matrix rownames A="RR_0" "RR_5" "RR_10" "Diff_FS_0" "Diff_FS_3" "Diff_FS_6" "Diff_FS_9" "Diff_FS_12"

    matrix list A


    This one does not work

    anyone has idea?

    Comment


    • #3

      foreach v of var*{
      summarize `v'
      return list
      matrix a_`v'= r(N),r(mean),r(sd),r(min),r(max),r(mean)/r(sd)
      }

      mat A=a_1\a_2\a_3\a_4\a_5\a_6\a_7\a_8\a_9\a_10\a_11\a_ 12\a_13\a_14\a_15\a_16\a_17\a_18


      matrix colnames A="OBS" "MEAN" "SD" "MIN" "MAX" "SHARP"
      matrix rownames A="RR_0" "RR_5" "RR_10" "Diff_FS_0" "Diff_FS_3" "Diff_FS_6" "Diff_FS_9" "Diff_FS_12" "Diff_SL_0" "Diff_SL_3" "Diff_SL_6" "Diff_SL_9" "Diff_SL_12" "Coll_R" "Put_R" "Call_R" "Por_P" "Por_ST"

      matrix list A

      Comment

      Working...
      X