Announcement

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

  • Generating random variables based on some predefined correlations

    Dear Stata Members
    I would like to ask a few questions and clear some doubts regarding regressions but for that, I need to create some random variables with some pre-defined correlations. So please help me to create some panel data with 4 four variables (v1,v2,v3, & v4) that has the following correlations
    1. v1 and v2 (0 correlation)
    2. v2 and v3 (some positive .50 to .60 correlation)
    3. v2 and v4 (some negative .50 to .60 correlation
    4. v3 & v4 (some low correlation 0.05-.12)
    150-200 observations will be fine and if the panel is not easy then let these variables be some time series ones.

    Did I make sense by asking the question? These artificially created variable can help me in testing some assumptions of OLS. I tried excel but couldn't meet these requirements.

  • #2
    Neelakanda:
    see -help corr2data-.
    Kind regards,
    Carlo
    (StataNow 18.5)

    Comment


    • #3
      Great help Carlo! Never knew that Stata has things that are our dormant desires

      Comment


      • #4
        Dear Carlo Lazzaro I tried creating correlated variable but I am getting the following error command
        c
        Code:
        orr2data x y, means(3.5) corr(.05) cstorage(full)
        option cstorage() invalid; vectorized corr/cov matrix found
        Where I went wrong?

        Comment


        • #5
          Neelakanda:
          with two variables you have two option:
          1) plugging in their standard deviation, ignoring their correlation;
          2) plugging in a correlation matrix, ingnoring their standard deviations.
          Kind regards,
          Carlo
          (StataNow 18.5)

          Comment


          • #6
            Dear Carlo Lazzaro, then what should be my command to get two variables of 1000 obs with a correlation of .05, if I am using the below code

            -
            Code:
             corr2data x y, means(3.5) corr(.05) cstorage(full)

            Comment


            • #7
              Neelakanda:
              Code:
              . set obs 100
              Number of observations (_N) was 0, now 100.
              
              . matrix C = (1, .5 \ .5, 1)
              
              . corr2data x y, means(3.5 3.5) corr(C)
              
              . list in 1/10
              
                   +---------------------+
                   |        x          y |
                   |---------------------|
                1. | 1.988646   1.007116 |
                2. | 3.929654   3.814411 |
                3. | 5.738905   3.152861 |
                4. | 3.866165   3.457336 |
                5. | 2.777061    3.80499 |
                   |---------------------|
                6. | 4.577948    5.21319 |
                7. | 2.991868   4.185888 |
                8. | 1.951137   2.041524 |
                9. | 3.238867   1.997884 |
               10. | 4.351279    3.24526 |
                   +---------------------+
              
              .
              Kind regards,
              Carlo
              (StataNow 18.5)

              Comment


              • #8
                Dear Carlo Lazzaro
                Thanks for this excellent help. Just a follow-up question, can we have a matrix with 3 correlations that is x, y,z in a similar fashion? How should be matrix
                Code:
                matrix C = (1, .5, .05 \.05, .5, 1)

                Comment


                • #9
                  Neelakanda:
                  no, as Matrix should be symmetric.
                  Kind regards,
                  Carlo
                  (StataNow 18.5)

                  Comment


                  • #10
                    Thanks Carlo Lazzaro for the support and help

                    Comment

                    Working...
                    X