Announcement

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

  • Add stars to numbers based on p value recorded in a given variable

    Dear list,

    I have two variables, A and B. I want to list A, and ask Stata to attach significance stars to the listed values of A based on the p-value recorded in B (so that I can copy and paste the "starred" values of A into Excel and do further editing). An example code is provided below. Does such a command exist? Thank you!

    Code:
    sysuse auto,clear
    set seed 98034
    generate pvalue = runiform()
    gsort pvalue
    list gear_ratio pvalue in 1/10
    //how to ask Stata to add significance stars to numbers in the left column based on the pvalue recorded in the right column

  • #2
    Code:
    sysuse auto,clear
    set seed 98034
    generate pvalue = runiform()
    gsort pvalue
    list gear_ratio pvalue in 1/10
    
    gen gr_str = strofreal(gear_ratio,"%4.2f")
    replace gr_str = gr_str + "***" if pvalue <= 0.01
    replace gr_str = gr_str + "**" if pvalue > 0.01 & pvalue <= 0.05
    replace gr_str = gr_str + "*" if pvalue > 0.05 & pvalue <= 0.1
    An alternative:

    Code:
    gen gr_str = strofreal(gear_ratio,"%4.2f")
    replace gr_str = cond(pvalue <= 0.01, gr_str+"***", cond(pvalue <= 0.05, gr_str+"**", cond(pvalue <= 0.1, gr_str+"*", gr_str)))
    Code:
    . list gr_str pvalue in 1/10
    
         +-------------------+
         | gr_str     pvalue |
         |-------------------|
      1. | 3.70**   .0184421 |
      2. | 3.78**    .019814 |
      3. | 2.26**   .0403476 |
      4. | 2.73**   .0491837 |
      5. |  3.54*   .0580208 |
         |-------------------|
      6. |  2.73*   .0592619 |
      7. |  2.73*   .0841409 |
      8. |   2.93   .1063732 |
      9. |   3.05   .1249034 |
     10. |   3.08   .1253679 |
         +-------------------+
    Last edited by Fei Wang; 10 Nov 2021, 22:08.

    Comment


    • #3
      Originally posted by Fei Wang View Post
      Code:
      sysuse auto,clear
      set seed 98034
      generate pvalue = runiform()
      gsort pvalue
      list gear_ratio pvalue in 1/10
      
      gen gr_str = strofreal(gear_ratio,"%4.2f")
      replace gr_str = gr_str + "***" if pvalue <= 0.01
      replace gr_str = gr_str + "**" if pvalue > 0.01 & pvalue <= 0.05
      replace gr_str = gr_str + "*" if pvalue > 0.05 & pvalue <= 0.1
      An alternative:

      Code:
      gen gr_str = strofreal(gear_ratio,"%4.2f")
      replace gr_str = cond(pvalue <= 0.01, gr_str+"***", cond(pvalue <= 0.05, gr_str+"**", cond(pvalue <= 0.1, gr_str+"*", gr_str)))
      Code:
      . list gr_str pvalue in 1/10
      
      +-------------------+
      | gr_str pvalue |
      |-------------------|
      1. | 3.70** .0184421 |
      2. | 3.78** .019814 |
      3. | 2.26** .0403476 |
      4. | 2.73** .0491837 |
      5. | 3.54* .0580208 |
      |-------------------|
      6. | 2.73* .0592619 |
      7. | 2.73* .0841409 |
      8. | 2.93 .1063732 |
      9. | 3.05 .1249034 |
      10. | 3.08 .1253679 |
      +-------------------+
      Thank you very much Dr Wang! It works for me very well. I really appreciate your quick response and helpful code.

      Comment

      Working...
      X