Announcement

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

  • Store results of several chi2

    Hi, I have a dataset where I am trying to tabulate two variables against each-other in chi2 tests, but I need to tabulate them by group and then store the p value for each group. I've tried using:

    Code:
    foreach x in group {
        tab x y if group == `x', chi2
        gen result_`x' = r(p) if group == `x'
    }
    but it just creates a variable called "result_group" with the p value for a chi2 across the entire dataset, but I want a chi2 p value for each test of x and y in a group

  • #2
    based on what you show, group is just that (i.e., group) and not a list of anything; my guess based on what you show is that you want to use -levelsof- first and save the result in a macro and then loop thru that macro in your foreach command; see
    Code:
    h levelsof
    note also that saving each p-value as a new variable is a waste of space; I don't know where you want to go from here, but you may want to put the results into macros or scalars or ...

    Comment


    • #3
      Originally posted by Rich Goldstein View Post
      based on what you show, group is just that (i.e., group) and not a list of anything; my guess based on what you show is that you want to use -levelsof- first and save the result in a macro and then loop thru that macro in your foreach command; see
      Code:
      h levelsof
      note also that saving each p-value as a new variable is a waste of space; I don't know where you want to go from here, but you may want to put the results into macros or scalars or ...
      Ive now tried
      Code:
      levelsof group, local(unique_group)
      foreach v of local unique_group {
          tab x y if group == `v', chi2
          gen result = r(p) if group ==`v'
      }
      but now i'm just getting an invalid name error for one of the entries of the group

      edit:
      essentially,
      Code:
       by group : ta x y, chi2
      works completely fine, I just need a way to store the P values from each one of those tests to its corresponding group
      Last edited by Thomas Overend; 19 Feb 2024, 05:50.

      Comment


      • #4
        The immediate problem with your loop is that second time around the loop, the variable already exists. But you could generate the variable before the loop as missing and then replace within the loop.

        Comment


        • #5
          Originally posted by Nick Cox View Post
          The immediate problem with your loop is that second time around the loop, the variable already exists. But you could generate the variable before the loop as missing and then replace within the loop.
          This is a change I've made, but the big issue seems to be that despite "foreach x in group" it is running the chi2 test on the entire dataset instead of per group

          Comment


          • #6
            I can't see that your syntax is allowing that. Your tabulate command in #3 restricts calculations to each group in turn, Did you leave off the if qualifier when revising the code?

            Comment

            Working...
            X