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, 07: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, 19: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; 02 Oct 2019, 23: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

                  Working...
                  X