Announcement

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

  • How to capture the estimate of t-test and two-way ANOVA

    Hello,

    I am analysing some datasets by using several methods and I create a loop for doing this. However, I got problems when capture the estimates of t-test and two-way ANOVA. here's my code within the loop but it is not correct.

    May I ask how can I capture the estiates?

    Thank you!

    * t-test
    ttest y, by(trt)
    local tp = `r(p)'
    local tEST =[]

    *anova
    anova y trt site
    test 1.trt
    local anop = `r(p)'
    local anoEST = _b[1.trt]

  • #2
    Hello Liyaa CC. Welcome to Statalist. If I follow, the EST in tEST and anoEST is the mean difference, is that right? In other words, you want to capture the mean difference from a t-test, and the adjusted mean difference from an ANOVA model, and you want to capture the p-values corresponding to those two mean differences too. Right?

    If so, perhaps something like this:

    Code:
    clear
    webuse systolic
    keep if drug < 3 // to make drug have 2 levels like trt in #1
    * t-test
    ttest systolic, by(drug)
    return list // delete this line if if wish
    local tp = r(p)
    * I think you want tEST = the mean difference
    local tEST = r(mu_1) - r(mu_2)
    
    *anova
    anova systolic drug disease
    * Use -margins- to get the adjusted mean difference
    margins drug
    * Use a.drug to get the desired mean difference
    margins a.drug
    matrix mtable = r(table)
    matrix list mtable // delete this line if you wish
    local anop = mtable[4,1]
    local anoEST = mtable[1,1]
    
    * Display the values of the local macros
    display _newline ///
    "        p-value from t-test = " `tp' _newline ///
    "mean difference from t-test = " `tEST' _newline ///
    "         p-value from ANOVA = " `anop' _newline ///
    " mean difference from ANOVA = " `anoEST' _newline


    --
    Bruce Weaver
    Email: [email protected]
    Version: Stata/MP 19.5 (Windows)

    Comment


    • #3
      Originally posted by Bruce Weaver View Post
      Hello Liyaa CC. Welcome to Statalist. If I follow, the EST in tEST and anoEST is the mean difference, is that right? In other words, you want to capture the mean difference from a t-test, and the adjusted mean difference from an ANOVA model, and you want to capture the p-values corresponding to those two mean differences too. Right?

      If so, perhaps something like this:

      Code:
      clear
      webuse systolic
      keep if drug < 3 // to make drug have 2 levels like trt in #1
      * t-test
      ttest systolic, by(drug)
      return list // delete this line if if wish
      local tp = r(p)
      * I think you want tEST = the mean difference
      local tEST = r(mu_1) - r(mu_2)
      
      *anova
      anova systolic drug disease
      * Use -margins- to get the adjusted mean difference
      margins drug
      * Use a.drug to get the desired mean difference
      margins a.drug
      matrix mtable = r(table)
      matrix list mtable // delete this line if you wish
      local anop = mtable[4,1]
      local anoEST = mtable[1,1]
      
      * Display the values of the local macros
      display _newline ///
      " p-value from t-test = " `tp' _newline ///
      "mean difference from t-test = " `tEST' _newline ///
      " p-value from ANOVA = " `anop' _newline ///
      " mean difference from ANOVA = " `anoEST' _newline

      Thank you! That solve my problem!

      Comment

      Working...
      X