Announcement

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

  • How to store p-values as scalars after using the margins command

    Hello!

    I am using the margins command with pwcompare. Here is my code:

    regress y i.t##i.c
    margins t, pwcompare(effect)


    I am trying to write code that will store the p-value as a scalar for each simulation that I run. When I use the return list command after margins, I cannot see where the p-value is stored. When I try use scalar pval = r(p) or scalar pval = r(pw_p), the scalar is empty. In the output of the margins command, I can see the p-value, I just don't know how to extract it into a scalar.

    Thank you in advance!

  • #2
    Hey Mollie. I like that you've shown your code. But, for us to be able to truly assist you, we'll need an example of your data. You can accomplish this with dataex. See the FAQ for more.

    If you can (below this post) format your question something like this, with the data and code you've tried, helping you will be much easier for us.

    Welcome to Statalist, Mollie.

    Comment


    • #3
      Beyond Jared's useful advice, I'd note that -return list- should indicate the existence of r(table) and r(table_vs), each of which has a row named "pvalue," which should contain the p-values you're interested in. (While I'm a big advocate of taking full advantage of all the -help- documentation in Stata, sometimes it's easier to just poke around in the return list and see if you can find what you want.) The r() matrices aren't "real" Stata matrices, and one easy way to be able to address individual items in one of them is to first assign it to a regular Stata matrix. Here's a moderately verbose illustration with a built-in Stata data set that I'm thinking will illustrate what you want.
      Code:
      clear
      sysuse nlsw88
      regress wage i.race##i.married
      margins race, pwcompare(effect)
      ret list 
      mat list r(table) 
      mat T = r(table)
      scalar p1 = T[4,2]
      di "p = " p1

      Comment


      • #4
        The code in #3 will extract the intermediate p-values that margins calculate before the pairwise comparison. The right ones are in r(table_vs):

        Code:
        clear
        sysuse nlsw88
        regress wage i.race##i.married 
        margins race, pwcompare(effect)
        matrix T = r(table_vs)
        scalar pvalue2v1 = T["pvalue", "2vs1.race"]
        scalar pvalue3v1 = T["pvalue", "3vs1.race"]
        scalar pvalue3v2 = T["pvalue", "3vs2.race"]
        scalar list

        Comment

        Working...
        X