Announcement

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

  • How do I quickly generate ANOVA test for difference between groups of intervention with multiple treatment arms.


    I have 4 intervention groups in my data (3 treatment groups and 1 control group). I want to generate balance tables that compare the means of the 4 groups and report the F-statistic & p-values for over 30 variables. I found - estpost ttest - does it for two groups. How do I quickly generate ANOVA test for difference between the 4 groups of intervention? Thank you.

  • #2
    You can use -oneway- with the -tabulate- option.

    Comment


    • #3
      Thanks Clyde.

      What you suggested worked for one variable at a time (i.e. oneway age treatment, tabulate). treatment has 4 intervention groups.

      But not more than one variable (i.e.oneway age hhsize treatment,tabulate).

      Stata's error message is "too many variables specified" .

      Is there a way I can automate or create a loop for this command for over 30 variables?

      Comment


      • #4
        use a -foreach- loop as in:
        Code:
        foreach var of varlist v1-v30 {
           oneway `var' treatment
        }
        see
        Code:
        help foreach
        and, of course, substitute your real variable names where I have written "v1-v30"
        if your variables are not contiguous, you will need to either change the order of the variables (see "help order") or list them out or put them in a macro

        Comment


        • #5
          Try something like the following. It uses the auto dataset for illustration. The five-category repair record is a surrogate for your four-category treatment variable, and the handful of automobile characteristics is the surrogate for your thirty, but the approach is the same.
          Code:
          sysuse auto
          local tally 0
          foreach var of varlist price mpg headroom-gear_ratio {
              local ++tally
          }
          foreach var of varlist price mpg headroom-gear_ratio {
              table rep78, contents(mean `var' sd `var' n `var')
              quietly anova `var' rep78
              quietly testparm i.rep78
              display in smcl as text "P = " as result %04.2f min(1, r(p) * `tally')
          }

          Comment


          • #6
            Thanks Rich & Joseph. Your suggestions worked well.

            Comment


            • #7
              Hi, I want so get an ANOVA over various iterations of my loop as in RSS etc values. I tried Rich's code but can't get Stata to recognize my variables.
              Any help appreciated, the more specific the better to this code: clear local mc = 10 set obs `mc' g data_store_x3 = . g data_store_x2 = . g data_store_con = . g data_store_y = . quietly{ forvalues i = 1(1) `mc' { if floor((`i'-1)/100) == ((`i'-1)/100) { noisily display "Working on `i' out of `mc' at $S_TIME" } preserve clear set obs 50 g x2 = runiform() g x3 = runiform() g e = rnormal() g y = 1 -3*x2 + 2*x3 + e reg y x2 x3 local x2coeff = _b[x2] local x3coeff = _b[x3] local const = _b[_cons] restore replace data_store_x3 = `x3coeff' in `i' replace data_store_x2 = `x2coeff' in `i' replace data_store_con = `const' in `i' foreach var of varlist `x2coeff' `x3coeff' `const' { noisily oneway `var' treatment } } }

              Comment


              • #8
                First of all, your display of the code is a mess. Please read the Forum FAQ, with special attention to #12, so you will properly display your code in a way that is readable and can be reused by others who try to troubleshoot it for you. That means using code delimiters, just as Rich Goldstein and Joseph Coveney have here, and I have in responses to some of your other posts. It is inconsiderate to post a jumble of run-on code like that and ask others to try to fix it. Properly posted, it looks like this:
                Code:
                clear
                 local mc = 10
                 set obs `mc'
                 g data_store_x3 = .
                 g data_store_x2 = .
                 g data_store_con = .
                 g data_store_y = .
                 quietly{
                    forvalues i = 1(1) `mc' {
                        if floor((`i'-1)/100) == ((`i'-1)/100) {
                            noisily display "Working on `i' out of `mc' at $S_TIME"
                        }
                        preserve
                        clear
                        set obs 50
                        g x2 = runiform()
                        g x3 = runiform()
                        g e = rnormal()
                        g y = 1 -3*x2 + 2*x3 + e
                        reg y x2 x3
                        local x2coeff = _b[x2]
                        local x3coeff = _b[x3]
                        local const = _b[_cons]
                        restore
                        replace data_store_x3 = `x3coeff' in `i'
                        replace data_store_x2 = `x2coeff' in `i'
                        replace data_store_con = `const' in `i'
                        foreach var of varlist `x2coeff' `x3coeff' `const' {
                            noisily oneway `var' treatment
                        }
                    }
                }
                That said, your final -foreach- loop contains several errors:

                1. `x2coeff' `x3coeff' `const' is not a varlist: it is a list of local macros that expand to numbers, specifically the most recently calculated values of the coefficients of x2, x3, and _cons in the current run through the -forvalues I = 1(1)`mc'- loop. I can't suggest how to fix it because I don't know what you are actually trying to do here. But you need to replace those local macros by names of variables, perhaps the data_store_* variables, (or local macros that expand to names of variables).

                2. Your -oneway- command refers to a variable, treatment ,that you have never created in your data, so, of course, this line will break.

                Comment

                Working...
                X