Announcement

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

  • Correlation coefficient for all possible combinations

    Hi all,

    I have created a wide-dataset whit the cumulative percentage change within a period of a week for each province-code in my original dataset. Here an example of the structure:
    Code:
     +------------------------------------------------------+
         | week_7~m   cumul~e1   cumul~e2   cumul~e3   cumul~e4 |
         |------------------------------------------------------|
      1. |        1          7          0          0          0 |
      2. |        2   2.433981   3.872983   3.605551   2.236068 |
      3. |        3   9.513314   5.292168   4.820598   5.574182 |
      4. |        4   25.65091   6.391198   11.52379   9.596646 |
      5. |        5   18.11543   5.083418   6.393942   6.903596 |
         |------------------------------------------------------|
      6. |        6   14.64915    3.95698   5.056757   8.415645 |
      7. |        7   11.73828   3.986464   8.661814   5.509972 |
      8. |        8   11.61636   2.641778   4.513327   7.073934 |
      9. |        9   9.731657   1.790158   2.424222    3.58781 |
     10. |       10   6.998778    .793135   1.692915   1.450931 |
         +------------------------------------------------------+
    Where the first column is the week-number and all the others the cumulative percentage change for each province code.
    I need to compute (and possibly store) the correlation coefficient for all possible combinations of province-codes foreach week-number, but I do not have any proper solution for that. Could you provide any idea?
    Thanks for your help.

  • #2
    I'm not sure what you want here. On its face, "correlation coefficient for all possible combinations of province-codes for each week-number" makes no sense because for any given week, you have only one observation of cumulative percentage change in any given province, so there is insufficient data to calculate any correlations. I'll just ignore the "for each week-number" part.

    Code:
    pwcorr cumul*
    matrix C = r(C)
    will calculate all those correlations across all weeks and store them in matrix C. If you need to store them in a dataset, you can't really do that within the structure of the data you have. You would need to set up a separate data set and then use the -svmat- command to write the correlations from C into that new data set. -help svmat-

    If that isn't what you meant, please post back with a clearer explanation.
    Last edited by Clyde Schechter; 16 Oct 2020, 15:18.

    Comment


    • #3
      Thanks Clyde for your reply. Indeed you're right and I missed a fundamental piece.
      I have days within week, and for each day the geometric change, in this way:

      Code:
      +------------------------------------------------------+
           | week_7~m   cumul~e1   cumul~e2   cumul~e3   cumul~e4 |
           |------------------------------------------------------|
       50. |        8   1.059901   .2265087   .0872686   1.851296 |
       51. |        8   1.157158   .1557761   .3224929   1.709725 |
       52. |        8   1.719501   .1377677   .8557439   .5230789 |
       53. |        8   2.440119   1.096073   1.392062    .270871 |
       54. |        8   2.005586    .378432   .3496922   .9634846 |
           |------------------------------------------------------|
       55. |        8    1.46615   .4705797   .9870706   .9089047 |
       56. |        8   1.767944   .1766406   .5189967   .8465749 |
       57. |        9    .663045   .1437843   .1008376   .2421249 |
       58. |        9   1.624383   .0795961   .3567052   .5455544 |
       59. |        9   1.990384   .3792906   1.042703   .6355635 |
           |------------------------------------------------------|
       60. |        9   1.028344   .6684927   .2165239    .287986 |
           +------------------------------------------------------+
      I still want to compute the correlation of all possible combinations of province-codes for each week, using the information of the 7days within one week.
      Something like this:
      HTML Code:
       https://www.statalist.org/forums/forum/general-stata-discussion/general/699772-generate-correlation-as-variable?p=1509575#post1509575
      adding a loop on weeks could be work?

      Thanks again and sorry if I was not clear since the beginning.

      Comment


      • #4
        Or simply your solution looping on weeks?

        Comment


        • #5
          The tableau you show (not a good way to show example data anyway--use -dataex- instead) obscures part of the name of your week variable. In the code below, I will just write week, but you should change it to the actual variable name to avoid any possibility of clash with some other variable whose name begins with week.

          Code:
          levelsof week, local(weeks)
          foreach w of local weeks {
              pwcorr cumul* if week == `w'
              matrix C`w' == r(C)
          }
          will give you a matrix of correlations for each week.

          If you are running version 16 or a fully updated version 15.1 or 14.2, -dataex- is already part of your official Stata installation. If not, run -ssc install dataex- to get it. Either way, run -help dataex- to read the simple instructions for using it. -dataex- will save you time; it is easier and quicker than typing out tables. It includes complete information about aspects of the data that are often critical to answering your question but cannot be seen from tabular displays or screenshots. It also makes it possible for those who want to help you to create a faithful representation of your example to try out their code, which in turn makes it more likely that their answer will actually work in your data.
          Last edited by Clyde Schechter; 18 Oct 2020, 16:37.

          Comment


          • #6
            Thanks Clyde for your solution! It's way easier than what I was thinking.

            I was not aware of dataex, I will use it for future post then! Thanks again for your help

            Comment

            Working...
            X