Announcement

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

  • Results of several ttest to excel

    Hi,

    my data looks like this:

    fyear ctar_d ctar_nd
    2000 0.5 1.1
    2001 0.7 1.3
    2000 1.1 1.6
    2001 1.4 0.2
    2002 1.0 0.4
    2003 0.3 0.5

    For every year I want to test if there is a statistical significant difference between the average cash to asset ratio of firms that pay dividends and the average cash to asset ratio of firms that don't pay dividends. I came up with the following code:

    bysort fyear: ttest cashtoassetratio_dividend == cashtoassetratio_nondividend, unpaired

    I was wondering if there's is a possibility to put the yearly test outputs to an excel file. Thank you for your hints!

    Markus

  • #2
    Use putexcel to output into Excel the returned values of ttest (like r(p) etc.).

    Comment


    • #3
      HI,
      You may try the following code if you are working with multiple continuous variables (in this example age ht wt bmi sysbp hdl crp ) and one group variable (intervention)
      First we have set the excel file name and sheet name
      Set the first row in excel sheet
      Start loop for first variable
      Extract the variable label into the local var_lab
      Check for equality of variances for one variable across the two groups using robvar
      As per the Levenes F Statistic results, the appropriate ttest is applied.
      Save the descriptive results as well as the p values and t statistics and type of test (equal / unequal variances) to teh excel sheet
      Increment row number by one
      Re-run loop for next variable

      I often use this code fragment to generate a table of summary statistics (comparing background data across groups, usually a table 1 of intervention study). My data is formatted differently from yours but it would be straightforward to modify and apply to your situation.


      Code:
      ** TABLE 1 Continuous Variables
      putexcel set "RESULTS.xlsx", sheet(Table1)  keepcellformat modify // Set the Excel File and Sheet
      local row = 5     // Start Results from Row 5
      foreach x in  age ht wt bmi sysbp hdl crp   {
        local var_lab : variable label `x' // Variable Label of x
        quietly robvar `x' , by(intervention) // W0 p value if < 0.05, variances are NOT equal
        local temp = `r(p_w0)'
        if `temp' < 0.05 {    //    Levene’s F-statistic (W0) Equal Variances
          quietly    ttest `x' , by(intervention)
          putexcel A`row'=("`var_lab'")   ///
                        C`row'=(r(N_1) ) D`row'=(r(mu_1)) E`row'=(r(sd_1))  ///
                        F`row'=(r(N_2) ) G`row'=(r(mu_2)) H`row'=(r(sd_2))  ///
                        I`row'=(r(p))  J`row'=(r(t))  K`row'=("Equal Variances")
          local row `++row'
          }
        if `temp' >= 0.05 {    //  Levene’s F-statistic (W0) Unequal Variances"
          quietly    ttest `x' , by(intervention) unequal
          putexcel A`row'=("`var_lab'")   ///
                        C`row'=(r(N_1) ) D`row'=(r(mu_1)) E`row'=(r(sd_1))  ///
                        F`row'=(r(N_2) ) G`row'=(r(mu_2)) H`row'=(r(sd_2))  ///
                        I`row'=(r(p))  J`row'=(r(t))K`row'=("Un-equal Variances")
          local row `++row'
        }
      }
      Last edited by Vivek Gupta; 06 Nov 2016, 22:14.
      Stata 15.1 (MP 2 core)
      https://www.epidemiology.tech/category/stata/
      Google Scholar Profile

      Comment

      Working...
      X