Announcement

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

  • Error while running ttest on a local

    Hi. I am trying to run a ttest on a local of multiple variables, but am getting the error "command ttest exited with an error when run using the entire sample". However, when I run the same command individually on variables in the local "vars" the command runs pererctly. Can someone please help me here. Thank you.

    Code:
    
    local myresults "Control = r(mu_1) Treated = r(mu_2) Diff = (r(mu_2)-r(mu_1)) pvalue = r(p)"
    
    display "`myresults'"
    
    #delimit ;
    local vars pop_perkm2_2019  per60_2011 per85_2011 deathrate_2019 povertyperct ///
    malariacase2018  denguecase2018  typoidtot_case2018 exphealth_percap1516 /// 
    acuteresp_totcase2018 pneumoniatot_case2018  tb2018 influenzacase_2018 hepcases_2018  ///
    diabetes20181k hypertension20181k htnanddm20181k cvds20181k ///
    stroke20181k hepcases_20181k commoncancers20181k mentalillness_20111k ///
    hospital_total1k hospital_beds_total1k icu_beds_total1k ventilators_total1k ///
    ;
    #delimit cr
    display "`vars'"
    
    
     
    . foreach x of local vars {
      2.                 di "`x'"
      3.          table (command) (result),command(`myresults' : ttest `v'', by(stepone))                        
      4.          nformat(%6.2f  Control Treated Difference)                             
      5.         nformat(%6.0f  pvalue)
      6. }
    pop_perkm2_2019
    command ttest exited with an error when run using the entire sample
    r(198);
    
    end of do-file

  • #2
    I don't follow what you want here but

    Code:
     
     ttest `v''
    might have one illegal quotation mark.

    Comment


    • #3
      Thanks, Nick Cox . I realised the extra quotation mark right after I posted here. However, even after removing it I continue to get the error below, and am unable to figure why. However, as seen below, this works perfectly well when the ttest is run individually for each variable. Since I have a large number of variables to test, I was hoping to loop it over a local of all variables (the code in #1). Would you know what's wrong here?

      Code:
      .. 
      . foreach x of local vars {
        2.                 di "`x'"
        3.          table (command) (result),command(`myresults' : ttest `v', by(stepone))                         
        4.          nformat(%6.2f  Control Treated Difference)                             
        5.         nformat(%6.0f  pvalue)
        6. }
      pop_perkm2_2019
      command ttest exited with an error when run using the entire sample
      r(100);
      
      
      
      
      *with a single variable
      
       local myresults "Control = r(mu_1) Treated = r(mu_2) Diff = (r(mu_2)-r(mu_1)) pvalue = r(p)"
      
      . 
      . display "`myresults'"
      Control = r(mu_1) Treated = r(mu_2) Diff = (r(mu_2)-r(mu_1)) pvalue = r(p)
      
      . 
      end of do-file
      
      . table (command) (result),  command(`myresults' : ttest per60_2011 ,      by(stepone))
      
      --------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
      > ----------------------------------------------------------------------------------------------------------------------------------------------------------------
                                     |  Sample size n₁   Sample size n₂   Degrees of freedom   Confidence level   x₁ mean for population 1   x₂ mean for population 2   Two-sided p-value   Lower one-sided p-
      > value   Upper one-sided p-value   Combined std. dev.   Standard deviation for first variable   Standard deviation for second variable   Std. error   t statistic
      -------------------------------+------------------------------------------------------------------------------------------------------------------------------------------------------------------------
      > ----------------------------------------------------------------------------------------------------------------------------------------------------------------
      ttest per60_2011 , by(stepone) |              18               15                   31                 95                   8.316667                   7.633333            .3219448                  .83
      > 90276                  .1609724             1.942283                                2.029561                                  1.82978     .6788884      1.006547
      --------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
      > ----------------------------------------------------------------------------------------------------------------------------------------------------------------
      
      .

      Comment


      • #4
        You are looping over local macro x but not using it inside the loop. The local macro v is evidently not defined, so ttest has nothing to do.

        As you've shown it the nformat() options are on separate command lines, which also looks wrong to me.

        Comment


        • #5
          Nick Cox: Thanks, Nick. That was ridiculous oversight on my part.

          I am now trying to put the results into a table with the code below. However, the loop fails to append the individual ttests of each variable. As a result, my output table in word has just one row of the last variable "ventilators_total1k", as evidenced by the output under command "collect label list command, all" below. Could you please help me here? I tried moving the putdocx commands into the loop but that did not work for me. I assume I need to append the results from the table command before I do so, but am struggling to figure this out.

          Code:
          
          foreach x of local vars {
                  di "`x'"
               table (command) (result),command(`myresults' : ttest `x', by(stepone))  ///
               nformat(%6.2f  Control Treated Difference) nformat(%6.2f  pvalue)
          }
          
          collect label list command, all
          
            Collection: Table
             Dimension: command
                 Label: Command option index
          Level labels:
                     1  ttest ventilators_total1k, by(stepone)
          
          
          *Formatting the table
          collect style cell result[Control Treated Difference], nformat(%8.2f)
          collect style cell result[pvalue], nformat(%6.4f)
          collect style cell border_block, border(right, pattern(nil))
          collect preview
          
          
          *Export the table to Microsoft Word
          putdocx clear
          putdocx begin
          putdocx paragraph, style(Title)
          
          collect style putdocx, layout(autofitcontents)           ///
          title("Table 1: Descriptive Statistics and Balance")
          
          putdocx collect
          putdocx save "${paperdir}\Tables\Table1.docx", replace

          Comment


          • #6
            You're making progress, but I stop here as pushing results programmatically into Word is not something I ever do.

            You should get a better answer than that, but you would increase the chance of it by providing a proper reproducible data example (or rephrasing your code in terms of some standard Stata dataset).

            Please as always follow https://www.statalist.org/forums/help#stata in specifying data.

            Comment


            • #7
              Thanks, Nick.

              Comment

              Working...
              X