Announcement

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

  • Plot looped correlation results

    Dear Statalist-Users,

    I am running a looped correlation. x(1)-x(14) is pairwise correlated with Neg.
    This is the code, I use for the correlations:

    foreach x of varlist x1- x14 {
    pwcorr `x' Neg , sig star(0.1)
    }

    Now I wish to plot the results, each x with it's correlation coefficient.
    I don't know, how to adress the correlation-results.... Maybe save in new variable? But how?
    Any help is highly appreciated.

    Magdalena

  • #2
    look at statsby (h statsby) and use the saving option

    Comment


    • #3
      Code:
      gen corr = . 
      
      quietly forval j = 1/14 { 
          corr x`j' Neg 
          replace corr = r(rho) in `j'
      } 
      
      gen what = "x" + string(_n) in 1/14 
      
      graph dot (asis) corr, over(what) ytitle(Interesting correlations)
      The display would be (much) more informative given informative variable names and/or variable labels, which should be added.

      Comment


      • #4
        I will try to present my question more precisely.
        Thank you for your suggestions so far, but I did not manage to solve my problem. I still have to learn a lot in Stata....

        I transformed my corrleation command: corr Return_1_Day- Return_14_Day Neg_Words. Return for Day 1 to 14 is correlated with negative words. I hope variables are more understandable now.
        I wish to show the progress of the correlation coefficient in a graph. I am not interested in the correlations between the returns, only between returns and negative words. Therefore the graph should show only one "line" of the coefficient matrix.

        Any further advice?

        Magdalena

        Comment


        • #5
          I am not interested in the correlations between the returns, only between returns and negative words. Therefore the graph should show only one "line" of the coefficient matrix.
          That's exactly what my code is intended to do. Sorry, but I can't tell from your latest what is wrong. You don't show what code you tried or what didn't work.

          With your variable names, different in #3 as compared with #1, there is a necessary minor change to the code suggested:

          Code:
          gen corr = .  
          quietly forval j = 1/14 {      
               corr Return_`j'_Day Neg      
               replace corr = r(rho) in `j'
          }  
          gen what = string(_n) in 1/14  
          graph dot (asis) corr, over(what) ytitle(Interesting correlations)
          Last edited by Nick Cox; 30 Sep 2016, 07:34.

          Comment


          • #6
            On the third line, it should be:
            Code:
            corr Return_`j'_Day Neg_Words
            Starting to lose it, Mr. Cox... :P

            Comment


            • #7
              Alberto: Thanks for your correction. Neg would not work correctly if there are several variables with names starting with Neg or Magdalena has disabled variable name abbreviation; there would be an error message in the first case and possibly in the second case.

              Comment


              • #8
                Thank you very much for your help. I manged to do so.
                But I still have problems to adopt the code to different time periods.
                How does the code look like, if I have the Return-Variables Return_1_Day Return_1_Week Return_1_Month-Return_12_Month.
                The code I use for these correlation is:
                foreach x of varlist Return 1_Day Return_1_Week ER_1_Monat- ER_12_Monat {
                corr `x' Neg_Words
                }


                Thanks for advice again!

                Comment


                • #9
                  That code looks good from here except that

                  Code:
                  Return 1_Day
                  should presumably be

                  Code:
                  Return_1_Day

                  Comment


                  • #10
                    Thanks. You're right.


                    How can I plot the correlation coefficients? I did not manage to modify your above suggested code for this....

                    Comment


                    • #11
                      Magdalena: Your question has the form: I changed your code but it didn't work as I wanted. I do believe you, but I can't advise without knowing what precisely went wrong.

                      Please do read and act on http://www.statalist.org/forums/help#stata

                      Comment


                      • #12
                        I am sorry for being imprecise.

                        When using
                        Code:
                        corr Return_1_Day- Return_14_Day Neg_Words
                        for correlating, your suggested code to plot the correlations was:
                        Code:
                        gen corr = .  
                        quietly forval j = 1/14 {            
                        corr Return_`j'_Day Neg            
                        replace corr = r(rho) in `j'
                        }  
                        gen what = string(_n) in 1/14  
                        graph dot (asis) corr, over(what) ytitle(Interesting correlations)
                        My new correlation command is:
                        Code:
                        foreach x of varlist Return_1_Day Return_1_Week Return_1_Month- Return_12_Month {
                        corr `x' Neg_Words
                        }
                        I again would like to plot the correlation coefficients. I am not sure, how to modify the "forvalue"-command associated with "replace"-command and the in-condition, as the forvalue-command and the in-condition only work for numbers and not for variables.

                        I tried the following:
                        Code:
                        generate Corr_Return_Neg = .
                        forvalues j=1/14 {
                        foreach x of varlist Return_1_Day Return_1_Week Return_1_Month- Return_12_Month {
                             corr `x' Neg_Words    
                             replace Corr_Return_Neg = r(rho) in `j'
                        }  
                        }
                        
                        generate index = string(_n) in 1/14
                        graph dot (asis) Corr_ER_Neg, over(index) ytitle(Interesting correlations)

                        The graph I get shows dots in a straight vertical line. It shows the correlation coefficient of Return_12_Month and Neg_Words for all return variables.

                        How do I have to change the code to plot the correlation coeffcients corectly?
                        Last edited by Magdalena Hetterich; 03 Oct 2016, 05:36.

                        Comment


                        • #13
                          Edit: See #14 instead
                          Last edited by Jesse Wursten; 03 Oct 2016, 06:14.

                          Comment


                          • #14
                            That code is just going to produce a variable with 14 copies of the correlations for the last variable in the loop. When j = 1 you cycle over the variables and repeatedly put each correlation in the first observation, but what remains is only that for the last variable. When j = 2 you do the same for the second observation. And so on.

                            It's just one loop that is needed. There is also a further bug in your last line: you refer to a quite different variable. I can't test this without your data, but I think it's closer to what you want.

                            Code:
                            generate Corr_Return_Neg = .
                            local j = 0  
                            foreach x of varlist Return_1_Day Return_1_Week Return_1_Month- Return_12_Month {      
                                corr `x' Neg_Words          
                                replace Corr_Return_Neg = r(rho) in `++j'
                            }    
                            
                            generate index = string(_n) in 1/14
                            graph dot (asis) Corr_Return_Neg, over(index) ytitle(Interesting correlations)

                            Comment


                            • #15
                              Thank you very much. This helped a lot!

                              Comment

                              Working...
                              X