Announcement

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

  • Descriptive statistics for two categories

    Hey statistic pros,

    I try to make a descriptive statistic for my logistic regression. It should be in two categories successful and failed, like at the picture. The only code I could find is
    tabstat foundedumaxba foundedumaxhi nvfourec nvpromasa netwinfem netwinfpro netwforpro, statistics (n mean sd) by(grosaledu)

    But I want to have the categories horizontal and the independent variables vertical. Do you know what code I have to use to get a table like the one at the picture?

    Thanks a lot for your support in advanced.

    Maja
    Attached Files

  • #2
    Hi Maja, this is a question that's also confusing me for a long time. I am learning by reading and practicing. Given that you don't post your data, I would use auto.dta as the toy example.
    This is a link that might be helpful:
    http://fintechprofessor.com/2018/09/...stata-ms-word/
    Code:
    sysuse auto,clear
    asdoc, row(Dependent variable:domestic or foreign, Domestic mean/frequency, Domestic SD, Foreign mean/frequency, Foreign SD) title(Summary staticis) save(myfile) replace
    asdoc, row( Model independent variables, \i, \i, \i, \i) append
    foreach var of varlist price mpg rep78 headroom trunk weight length turn {
    qui sum `var' if foreign==0
    local mf=`r(mean)'/`r(N)'
    asdoc, accum(`mf', `r(sd)')
    qui sum `var' if foreign==1
    local mf=`r(mean)'/`r(N)'
    asdoc, accum(`mf', `r(sd)')
    asdoc, row(`var', $accum)
    }
    2B or not 2B, that's a question!

    Comment


    • #3
      Hi Liu,

      it works thanks a lot you saved my day. One further question do you know how I can add the t-test (or the stars as a sign for a significant difference) for the differences of means of the two categories?

      Thanks a lot

      Maja

      Comment


      • #4
        Originally posted by Maja Ennemoser View Post
        Hi Liu,

        it works thanks a lot you saved my day. One further question do you know how I can add the t-test (or the stars as a sign for a significant difference) for the differences of means of the two categories?

        Thanks a lot

        Maja
        Yes, it's not a difficult task like this:
        Code:
        sysuse auto,clear
        asdoc, row(Dependent variable:domestic or foreign, Domestic mean/frequency, Domestic SD, Foreign mean/frequency, Foreign SD, t-test) title(Summary staticis) save(myfile) replace
        asdoc, row( Model independent variables, \i, \i, \i, \i, \i) append
        foreach var of varlist price mpg rep78 headroom trunk weight length turn{
          qui sum `var' if foreign==0
          local mf=`r(mean)'/`r(N)'
          asdoc, accum(`mf', `r(sd)')
          qui sum `var' if foreign==1
          local mf=`r(mean)'/`r(N)'
          asdoc, accum(`mf', `r(sd)')
          ttest `var', by(foreign)
          local t=round(abs(`r(t)'),0.001)
          local t=substr("`t'",1,5)
          if `r(p)'<=0.01 {
              local star "***"
          }
          else if `r(p)'<=0.05{
          local star "**"
          }
          else if `r(p)'<=0.1{
          local star "*"
          }
          else {
          local star " "
          }
         local tstar `t'`star'
        asdoc, accum(`tstar')
        asdoc, row(`var', $accum)
        }
        But I face some difficulty when deleting the code of this line:
        Code:
          local t=substr("`t'",1,5)
        That is the last line would show more decimals than expected. I don't know what's wrong yet. If anyone knows, please let me know. I would be grateful.
        Last edited by Liu Qiang; 18 May 2019, 08:45.
        2B or not 2B, that's a question!

        Comment


        • #5
          Thanks a lot Liu, you've been extremely helpful.

          Comment


          • #6
            Liu Qiang You have shown some dexterity with option row of asdoc. You have commented that the t macro show more decimal places than expected. Actually, you can format macros for decimal points using the extended macro function. So
            Code:
            sysuse auto, clear
            ttest price = 0
            
            . loc t = `r(t)'
            
            . dis `t'
            17.981223
            
            
            . loc t : di %9.3f = `r(t)'
            
            . dis `t'
            17.981
            
            . loc t : di %9.2f = `r(t)'
            
            . dis `t'
            17.98

            Regards
            --------------------------------------------------
            Attaullah Shah, PhD.
            Associate Professor of Finance, Institute of Management Sciences Peshawar, Pakistan
            www.FinTechProfessor.com
            If you use MS Word, do check my asdoc program that easily sends Stata output to MS Word

            Comment


            • #7
              Originally posted by Attaullah Shah View Post
              Liu Qiang You have shown some dexterity with option row of asdoc. You have commented that the t macro show more decimal places than expected. Actually, you can format macros for decimal points using the extended macro function. So
              Code:
              sysuse auto, clear
              ttest price = 0
              
              . loc t = `r(t)'
              
              . dis `t'
              17.981223
              
              
              . loc t : di %9.3f = `r(t)'
              
              . dis `t'
              17.981
              
              . loc t : di %9.2f = `r(t)'
              
              . dis `t'
              17.98
              Many thanks, Professor Attaullah Shah I am still very unskilled in using Stata. By learning and practicing, I wish the huge gap between me and many outstanding professors here could be narrowed in the future.

              The revised code:
              Code:
              sysuse auto,clear
              asdoc, row(Dependent variable:domestic or foreign, Domestic mean/frequency, Domestic SD, Foreign mean/frequency, Foreign SD, t-test) title(Summary staticis) save(myfile) replace
              asdoc, row( Model independent variables, \i, \i, \i, \i, \i) append
              foreach var of varlist price mpg rep78 headroom trunk weight length turn{
                qui sum `var' if foreign==0
                local mf=`r(mean)'/`r(N)'
                asdoc, accum(`mf', `r(sd)')
                qui sum `var' if foreign==1
                local mf=`r(mean)'/`r(N)'
                asdoc, accum(`mf', `r(sd)')
                ttest `var', by(foreign)
                local t : di %9.3f = abs(`r(t)')
                if `r(p)'<=0.01 {
                    local star "***"
                }
                else if `r(p)'<=0.05{
                local star "**"
                }
                else if `r(p)'<=0.1{
                local star "*"
                }
                else {
                local star " "
                }
               local tstar `t'`star'
              asdoc, accum(`tstar')
              asdoc, row(`var', $accum)
              }
              Last edited by Liu Qiang; 20 May 2019, 20:54.
              2B or not 2B, that's a question!

              Comment


              • #8
                Liu Qiang thanks for creating if foreign==0this code. Is there a way to create a difference in means variable and to put the stars on the difference in means rather than on the t-stat. diff=r(mean)if foreign==1-r(mean)

                Comment


                • #9

                  Here is one way to do it

                  Code:
                  sysuse auto, clear
                  asdoc, row(Dependent variable: domestic or foreign, Domestic mean, Domestic SD, Foreign mean, Foreign SD, Mean Diff.)  ///
                  title(Summary Statistics) replace
                  
                  asdoc, row( Model independent variables, \i, \i, \i, \i, \i) 
                  
                  foreach var of varlist price mpg rep78 headroom trunk weight length turn {
                  
                    qui sum `var' if foreign==0
                    local mf1=`r(mean)'
                    asdoc, accum(`mf1', `r(sd)')
                  
                    qui sum `var' if foreign==1
                    local mf2=`r(mean)'
                    asdoc, accum(`mf2', `r(sd)')
                  
                    ttest `var', by(foreign)
                    local dif : di %9.3f = `mf1' - `mf2'
                  
                    if `r(p)'<=0.01 {
                        local star "***"
                    }
                    else if `r(p)'<=0.05{
                        local star "**"
                    }
                    else if `r(p)'<=0.1{
                        local star "*"
                    }
                    else {
                        local star " "
                    }
                  
                  local tstar `dif'`star'
                  
                  asdoc, accum(`tstar')
                  
                  asdoc, row(`var', $accum)
                  }
                  Click image for larger version

Name:	Capture.PNG
Views:	1
Size:	45.7 KB
ID:	1518980




                  For those who are not yet familiar with asdoc, asdoc can be downloaded from SSC and can be used with almost all Stata commands. Here is a short blog post that shows how asdoc can be used with any Stata command http://fintechprofessor.com/2018/02/...basic-example/. You can also watch several YouTube videos that show the use of asdoc https://www.youtube.com/watch?v=zdI6...LwodAk2oqLYhr-

                  Code:
                  * For installation of the stable version
                  ssc install asdoc
                  
                  * For installation of the new beta version
                  net install asdoc, from(http://fintechprofessor.com) replace
                  help asdoc
                  Last edited by Attaullah Shah; 03 Oct 2019, 00:39.
                  Regards
                  --------------------------------------------------
                  Attaullah Shah, PhD.
                  Associate Professor of Finance, Institute of Management Sciences Peshawar, Pakistan
                  www.FinTechProfessor.com
                  If you use MS Word, do check my asdoc program that easily sends Stata output to MS Word

                  Comment


                  • #10
                    Hi Everyone!

                    I have a question with regards to the code that was written by Prof. Shah and Liu. How do you return the output produced by tab using the same code? For example you want to display what percentage of cars are foreign vs the percentage of domestic car as well as the frequencies. So in addition to the summary statistics displayed above we could include summary stats on variables that are binary as well. I assumed the code would work could be modified as follows, but obviously it didn't produce any output.

                    Code:
                    foreach var of varlist foreign a b c d e{ // where a b c d e are other binary variables
                       qui tab `var'
                       asdoc, accum(`r(N)', `r(mean)') // instead of mean, I would prefer to report the percentage split, but I am not sure how to return that output
                       asdoc, row(`var', $accum)
                      }
                    Thank you!

                    Best,
                    Lori

                    Comment


                    • #11
                      Your query is less clear to me. If you want to report frequencies and percentages for each variable, then just use
                      Code:
                      asdoc tab `var'
                      However, if you want to append frequencies and percentages to summary statistics, then in which format will you want to report the results as the percentages and frequencies will be reported for each unique group of a variable, not at the variable level? For example, for the variable foreign, the grouping is foreign and domestic, these two groups will need two separate rows in the output table, how these two rows will be matched with the rest of the statistics.
                      Regards
                      --------------------------------------------------
                      Attaullah Shah, PhD.
                      Associate Professor of Finance, Institute of Management Sciences Peshawar, Pakistan
                      www.FinTechProfessor.com
                      If you use MS Word, do check my asdoc program that easily sends Stata output to MS Word

                      Comment


                      • #12
                        Thank you for your response Prof. Shah.

                        I was hoping to get a loop that sums and reports summary statistics for all relevant variables and in the second stage use -asdoc tab- and append those variables with percentages and frequencies on to the same table. Would that be far too tedious? At this point, I am making two different documents one with asdoc sum and another with asdoc tab and then manually merging the tables.

                        Comment


                        • #13
                          Dear Prof. Shah,

                          I apologize for not being clear earlier.

                          The issue that I am facing here is to be able to get the following code output into a single table:

                          Code:
                          sysuse auto, clear
                          asdoc sum  mpg  headroom weight length if foreign == 1, stat(mean median sd N) replace label dec(2)
                          
                          asdoc sum  mpg  headroom weight length if foreign == 0, stat(mean median sd N) append label dec(2)
                          
                          asdoc tab1 rep78 make  if foreign == 1, append stat(mean median sd N) label dec(2)
                          And the output has multiple tables instead of a single summary statistics table. How do I use -asdoc- to get a single table output like the original poster that also includes output from -asdoc tab- besides -asdoc sum-

                          Also, how do I edit the code if I want to compare it with another sub-sample that would appear as another row such as

                          Code:
                          asdoc sum  mpg  headroom weight length if foreign == 1 & weight <= 3000, stat(mean median sd N) replace label dec(2)

                          Thank you so much!

                          Lori
                          Attached Files
                          Last edited by Lorien Nair; 20 Jan 2020, 08:02.

                          Comment

                          Working...
                          X