Announcement

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

  • Insert n and p-values in graph?

    Hi!

    I have learnt (via this fantastic forum), how to insert by betacoefficient in the legend of the graph. Can you also help me how I can insert my number of observations and my p-value?

    regress HGB Total_CO2 if(SEX==0), robust
    local betahat1= `:di %5.3f _b[Total_CO2]'
    quietly eststo crude: margins, at(Total_CO2=(0(1)10)) post


    When adding this line it does not give me the obeservations: local n1= `:di %5.3f _n'
    And how do I add the p-value?

    Thank you!
    Click image for larger version

Name:	p.png
Views:	1
Size:	98.7 KB
ID:	1734990


    Anna

  • #2
    Code:
    local n1 = e(N)
    local p_value = Ftail(e(df_m), e(df_r), e(F))
    Make sure you do that before you run the -margins- command. Do it right before or right after your -local betahat1...- command.

    Comment


    • #3
      Thank you so much Clyde!

      Do you also know how I can decrease the decimals for the p-value in this code? And is it possible to have two legends in the same plot? Code below is not working...

      regress HGB Total_CO2 if(SEX==1), robust
      local betahat1= `:di %5.3f _b[Total_CO2]'
      local n1 = e(N)
      local p_value1 = Ftail(e(df_m), e(df_r), e(F))
      quietly eststo crude: margins, at(Total_CO2=(0(1)10)) post

      regress HGB Total_CO2 AGE DATUM i.SEASON if(SEX==1), robust
      local betahat2= `:di %5.3f _b[Total_CO2]'
      local n2 = e(N)
      local p_value2 = Ftail(e(df_m), e(df_r), e(F))
      quietly eststo full: margins, at(Total_CO2=(0(1)10)) post

      coefplot crude full, at title("Haemoglobin (men)") ytitle("CO2") xtitle(CO{sub:2} (kg/day)) leg(order(1 "Crude model (B{sub:1}=`betahat1', p=`p_value1')" 2 "Fully adjusted model (B{sub:2}=`betahat2', p=`p_value2')") pos(6)) (leg(order(1 "n=`n1') pos(3)) recast(line) noci

      Comment


      • #4
        If you want, for example, the p-values to 2 decimal places:
        Code:
        local p_value: display %04.2f =Ftail(e(df_m), e(df_r), e(F))
        No, you cannot have two legends in a single graph, at least not this way. You can store two separate graphs, each with its own legend, and then use -graph combine- to put them together, and each will appear with its own legend. But I'm guessing that's not what you want to do. You want to include n in the legend somehow. But it doesn't belong there. The purpose of a legend is to identify which symbols or lines on a graph correspond to what. n doesn't qualify for a place in a legend. I would probably do that with the -note()- option. (Note: I don't use -coefplot- myself, so I can't guarantee that it accepts the -note()- option. But it is my belief that it does accept all -graph twoway- options, so I'm assuming this will work. But I could be wrong about that.)

        That said, I will also comment that -(leg(order(1 "n=`n1')- is incorrect, because the quotes around n =`n1' are not closed.

        Comment


        • #5
          Thank you so much! Do you know how to insert a 0 before the dot when showing my beta coeficients (i.e. 0.15 instead of .15)?

          I really appriciate your suggestions!

          Anna

          Comment


          • #6
            Code:
            local betahat: display %04.2f =_b[Total_CO2]

            Comment


            • #7
              It works well for my n. But not for my beta:
              local betahat2= `:di %05.3f _b[Total_CO2]'

              Comment


              • #8
                In what way does it "not work" for your beta. When I do something completely analogous, it works just fine:
                Code:
                . sysuse auto, clear
                (1978 automobile data)
                
                . regress price mpg
                
                      Source |       SS           df       MS      Number of obs   =        74
                -------------+----------------------------------   F(1, 72)        =     20.26
                       Model |   139449474         1   139449474   Prob > F        =    0.0000
                    Residual |   495615923        72  6883554.48   R-squared       =    0.2196
                -------------+----------------------------------   Adj R-squared   =    0.2087
                       Total |   635065396        73  8699525.97   Root MSE        =    2623.7
                
                ------------------------------------------------------------------------------
                       price | Coefficient  Std. err.      t    P>|t|     [95% conf. interval]
                -------------+----------------------------------------------------------------
                         mpg |  -238.8943   53.07669    -4.50   0.000    -344.7008   -133.0879
                       _cons |   11253.06   1170.813     9.61   0.000     8919.088    13587.03
                ------------------------------------------------------------------------------
                
                . local betahat2 = `:di %05.3f _b[mpg]'
                
                . display `betahat2'
                -238.894
                
                . local betahat2: display %05.3f _b[mpg]
                
                . display `betahat2'
                -238.894
                Note, by the way, that the syntax I proposed in #5 is not the same as what you used in #6, although, as I have shown here, it should work either way.

                Please show the exact code you used and the exact result you got. And, unless it is blatantly obvious, explain in what way(s) what you got is not what you wanted.

                Comment


                • #9
                  Hi again!

                  I wonder why it is not possible to collect my p-value when running a logistic regression? Is the code different? From the code below I can collect the beta-value and n, but not the p-value.

                  logit HGB_belowREF Total_CO2 AGE
                  local betahat1= `:di %5.3f _b[Total_CO2]'
                  local n1:display %04.0f = e(N)
                  local p_value1: display %04.2f =Ftail(e(df_m), e(df_r), e(F))
                  quietly eststo full: margins, at(Total_CO2=(0(1)10)) expression(100*predict(pr)) post

                  Best,
                  Anna


                  Click image for larger version

Name:	Skärmbild 2024-01-24 151007.png
Views:	1
Size:	50.3 KB
ID:	1740991

                  Comment


                  • #10
                    The output shows that no F test was applied. The overall test is a LR chi-square test.

                    Comment


                    • #11

                      Thank you, Nick! How do capture the circled p-value?


                      Click image for larger version

Name:	Skärmbild 2024-01-24 2.png
Views:	1
Size:	51.4 KB
ID:	1741000

                      Comment


                      • #12
                        Code:
                        mat li r(table)
                        will show you each of the line items including the p value for CO2 etc. You will have to do that massaging like Dr. Clyde Schechter pointed to visualize the decimal numbers in the desired format.

                        See below for a sample coming from the auto dataset

                        Code:
                        . logistic foreign weight trunk
                        
                        Logistic regression                                     Number of obs =     74
                                                                                LR chi2(2)    =  32.05
                                                                                Prob > chi2   = 0.0000
                        Log likelihood = -29.009894                             Pseudo R2     = 0.3558
                        
                        ------------------------------------------------------------------------------
                             foreign | Odds ratio   Std. err.      z    P>|z|     [95% conf. interval]
                        -------------+----------------------------------------------------------------
                              weight |   .9973189   .0006944    -3.86   0.000     .9959588    .9986808
                               trunk |   1.030975   .1063657     0.30   0.767      .842228    1.262021
                               _cons |   479.3378   782.8912     3.78   0.000     19.51615    11773.06
                        ------------------------------------------------------------------------------
                        Note: _cons estimates baseline odds.
                        
                        . mat li r(table)
                        
                        r(table)[9,3]
                                   foreign:    foreign:    foreign:
                                    weight       trunk       _cons
                             b   .99731887    1.030975   479.33779
                            se    .0006944   .10636569    782.8912
                             z  -3.8559163   .29567703   3.7791551
                        pvalue    .0001153   .76747674   .00015736
                            ll    .9959588   .84222803   19.516148
                            ul   .99868079   1.2620211   11773.057
                            df           .           .           .
                          crit    1.959964    1.959964    1.959964
                         eform           1           1           1

                        Comment


                        • #13
                          Thank you!

                          But I don't get any of those codes to collect the p-value:

                          local p_value1: `:display %4.2f _pvalue[Total_CO2]'
                          local p_value1: :display %4.2f = pvalue[Total_CO2]
                          Click image for larger version

Name:	logistic.JPG
Views:	1
Size:	82.8 KB
ID:	1741046

                          Comment


                          • #14
                            Code:
                            matrix T = r(table)
                            local p_value1: display %4.2f _T[ "pvalue", "Total_CO2"]

                            Comment


                            • #15


                              logit foreign weight price
                              mat li r(table)

                              Code:
                              local pwanted "`: di %05.4f r(table)[4,2]'"  // for price--row 4 column 2 value which is pvalue
                              di "`pwanted'"

                              Adapt as above.
                              variables names with so many upper and underscores are prone to errors too. make them all lower case if possible too. Just a best practice suggestion on the side.

                              Comment

                              Working...
                              X