Announcement

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

  • Loop etable with external value test (GOF)

    Hi all,

    Iam getting a problem buildings tables with external value test (GOF) and etable using a loop.

    I always get a remaining results of a last table, I can solve partially using: clear results, example ( I only expect 2 results):

    Code:
    webuse nhanes2l, clear
    
    putdocx clear
    putdocx begin
    clear results
    
    etable, column(index) mstat(chi2_gof=(r(chi2)), label(GOF χ²)) mstat(p_gof=(r(p)), label(GOF p-value) nformat(%6.4f)) showstars showstarsnote
    
    forvalues i = 1/2 {
    
       logit diabetes bpsystol age weight i.region if sex==`i'
       estat gof
       etable, append
    }
    
    putdocx collect
    putdocx save test, replace
    Click image for larger version

Name:	Sin título.jpg
Views:	1
Size:	85.7 KB
ID:	1784907


    With the same commands and If I dont include: clear results, repeat last table.

    Code:
    webuse nhanes2l, clear
    
    putdocx clear
    putdocx begin
    
    etable, column(index) mstat(chi2_gof=(r(chi2)), label(GOF χ²)) mstat(p_gof=(r(p)), label(GOF p-value) nformat(%6.4f)) showstars showstarsnote
    
    forvalues i = 1/2 {
    
       logit diabetes bpsystol age weight i.region if sex==`i'
       estat gof
       etable, append
    }
    
    putdocx collect
    putdocx save test, replace
    Click image for larger version

Name:	Sin título.png
Views:	1
Size:	58.3 KB
ID:	1784908

    Last edited by Rodrigo Badilla; 16 Feb 2026, 15:57.

  • #2
    The problem is that your standalone command
    Code:
    etable, column(index) mstat(chi2_gof=(r(chi2)), label(GOF χ²)) mstat(p_gof=(r(p)), label(GOF p-value) nformat(%6.4f)) showstars showstarsnote
    is creating an empty column in an etable collection, to which your loop then appends two more columns. The coulmn created by this command is empty because it is not actually preceded by any actual estimation command that could populate that column with any results.

    If you just do
    Code:
    clear*
    webuse nhanes2l
    
    forvalues i = 1/2 {
    
       logit diabetes bpsystol age weight i.region if sex==`i'
       estat gof
       etable, column(index) mstat(chi2_gof=(r(chi2)), label(GOF χ²)) ///
        mstat(p_gof=(r(p)), label(GOF p-value) nformat(%6.4f)) showstars ///
        showstarsnote append 
     
    }
    collect export test.docx, replace
    you will get what you are looking for.

    By the way, it's not clear to me why you are using -putdocx- when you can just add the -collect export test.docx, replace- command after the loop. So I've eliminated all of the -putdocx- lines. But if you have some reason I'm missing for using -putdocx- here, you can restore all that--it has nothing to do with the empty problem column.
    Last edited by Clyde Schechter; 16 Feb 2026, 16:50.

    Comment


    • #3
      Thanks Clyde Schechter for your reply was more easy than expected. I saw one example in https://www.stata.com/support/faqs/r...mation-etable/ but i confuse some concepts

      I only use putdocx because this test is a part of a document and is more easy for me add loop results and graphs.

      But you are right. I complicate the life with my example

      Comment

      Working...
      X