Announcement

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

  • How to execute the "multicat" command?

    Just learned about the
    Code:
    multicat
    function in the book by Lawrence C. Hamilton which looks very handy. Then I run the do file command as mentioned:

    program define multicat
    version 12.1
    syntax varlist [if] [in] [aweight fweight iweight] ///
    [, MISSing BY(varname) OVER(varname) ]
    if "`over'" != "" {
    display as error "over() option not allowed with multicat;"
    display as error "use by() option or try catplot command instead."
    exit 198
    }
    marksample touse, strok novarlist
    if "`weight'" != "" local Weighted_ = "Weighted"
    if "`c(os)'"=="Windows" {
    local filetype "emf"
    }
    else if "`c(os)'"=="Unix" {
    local filetype "eps"
    }
    else if "`c(os)'"=="MacOSX" {
    local filetype = "pdf"
    }
    else {
    display as error "unknown operating system: `c(os)'"
    exit 799
    }
    capture {
    if "`by'" != "" {
    foreach var of varlist `varlist' {
    local Vlab_: variable label `var'
    catplot hbar `var' [`weight' `exp'] if `touse', ///

    blabel(bar, format(%3.0f)) ///
    percent(`by') ytitle("`Weighted_' Percent") ///
    `missing' by(`by', title("`Vlab_'", size(medium)))
    graph save -`by'-`var'.gph, replace
    graph export -`by'-`var'.`filetype', replace
    }
    }
    else {
    foreach var of varlist `varlist' {
    quietly tab `var' if `touse', `missing'
    local Nofobs_ = r(N)
    local Vlab_: variable label `var'
    catplot hbar `var' [`weight' `exp'] if `touse', ///
    blabel(bar, format(%3.0f)) ///
    percent ytitle("`Weighted_' Percent, N = `Nofobs_'") ///
    title("`Vlab_'", size(medium)) `missing' `options'
    graph save Graph -`var'.gph, replace
    graph export -`var'.`filetype', replace
    }
    }
    }
    error _rc
    end

    to make a similar chart as mentioned on the book:
    Click image for larger version

Name:	Capture2.PNG
Views:	1
Size:	160.3 KB
ID:	1476915



    but getting this error in Stata 14:
    Code:
    . multicat fs
    invalid syntax
    r(111);
    Does anyone know if
    Code:
    multicat
    is still functional?
    Last edited by Sonnen Blume; 02 Jan 2019, 21:17.

  • #2
    I do not know about the multicat program, but this thing in the picture is a standard bar chart.

    . help graph bar

    Comment


    • #3
      Dear Sonnen, from inspection of the code you can see which command actually is used to create the graph:
      Code:
      catplot hbar `var' [`weight' `exp'] if `touse', ///
      Actually, catplot is a community-contributed program, i.e. by Nick Cox, which may be installed using ssc:
      Code:
      ssc install catplot
      after which you can type:
      Code:
      help catplot
      to view a good number of examples and details about further options.
      Note that a good way to start your search for information about any command is to type findit ... within the Stata command window, like:
      Code:
      findit catplot
      As a result you will get a new window with anything that the server can find, i.e. references to papers etc.
      Like in this case, catplot is mentioned on a Stata support page.
      Also these papers in The Stata Journal by Nick Cox are of interest:
      Speaking Stata: Graphing categorical and compositional data
      Speaking Stata: Spineplots and their kin
      given your question.
      http://publicationslist.org/eric.melse

      Comment


      • #4
        Originally posted by ericmelse View Post
        Dear Sonnen, from inspection of the code you can see which command actually is used to create the graph:
        Code:
        catplot hbar `var' [`weight' `exp'] if `touse', ///
        Actually, catplot is a community-contributed program, i.e. by Nick Cox, which may be installed using ssc:
        Code:
        ssc install catplot
        after which you can type:
        Code:
        help catplot
        to view a good number of examples and details about further options.
        Note that a good way to start your search for information about any command is to type findit ... within the Stata command window, like:
        Code:
        findit catplot
        As a result you will get a new window with anything that the server can find, i.e. references to papers etc.
        Like in this case, catplot is mentioned on a Stata support page.
        Also these papers in The Stata Journal by Nick Cox are of interest:
        Speaking Stata: Graphing categorical and compositional data
        Speaking Stata: Spineplots and their kin
        given your question.
        Thanks so much for the detailed response Eric! I am aware of the catplot program and use it regularly. One difficulty I have with catplot is that it displays the frequencies, whereas the multicat program generates (as shown by the author) the graph with percentages which is more intuitive.

        Comment


        • #5
          Come on! catplot has percent and percent() options. You can get percents relative to all the data or relative to any variable(s) you mention. So, the only objection that makes sense is that you prefer percents as the default.

          Code:
          . sysuse auto, clear
          (1978 Automobile Data)
          
          . catplot rep78, percent
          
          . catplot rep78 foreign, percent(foreign)

          Comment


          • #6
            Note that multicat uses the syntax of catplot version 1 (2002). There's probably where your syntax error comes from.

            In 2010 the syntax was changed, and -- as the help does explain -- that syntax is incompatible with previous versions. So in 2012 Larry Hamilton was already using an out-of-date copy of catplot.

            Otherwise put, I broke multicat before it was published. So, here's a fix. Not much tested.

            Code:
            * fix for -catplot- 2.0.0 up NJC 3 January 2019 
            program define multicat
                version 12.1
                syntax varlist [if] [in] [aweight fweight iweight] ///
                [, MISSing BY(varname) OVER(varname) ]
            
                if "`over'" != "" {
                    display as error "over() option not allowed with multicat;"
                    display as error "use by() option or try catplot command instead."
                    exit 198
                }
            
                marksample touse, strok novarlist
                if "`weight'" != "" local Weighted_ = "Weighted"
            
                if "`c(os)'"=="Windows" local filetype "emf"
                else if "`c(os)'"=="Unix" local filetype "eps"
                else if "`c(os)'"=="MacOSX" local filetype = "pdf"
                else {
                    display as error "unknown operating system: `c(os)'"
                    exit 799
                }
            
                capture {
                    if "`by'" != "" {
                        foreach var of varlist `varlist' {
                            local Vlab_: variable label `var'
            
                            catplot `var' [`weight' `exp'] if `touse', ///
                            blabel(bar, format(%3.0f)) ///
                            percent(`by') ytitle("`Weighted_' Percent") ///
                            `missing' by(`by', title("`Vlab_'", size(medium)))
            
                            graph save -`by'-`var'.gph, replace
                            graph export -`by'-`var'.`filetype', replace
                        }
                    }
                    else {
                        foreach var of varlist `varlist' {
                            quietly tab `var' if `touse', `missing'
                            local Nofobs_ = r(N)
                            local Vlab_: variable label `var'
            
                            catplot `var' [`weight' `exp'] if `touse', ///
                            blabel(bar, format(%3.0f)) ///
                            percent ytitle("`Weighted_' Percent, N = `Nofobs_'") ///
                            title("`Vlab_'", size(medium)) `missing' `options'
            
                            graph save Graph -`var'.gph, replace
                            graph export -`var'.`filetype', replace
                        }
                    }
                }
                error _rc
            end
            P.S. Extending #5 I note that multicat doesn't extend catplot to get percents: it just uses the options mentioned.

            The selling point of multicat is rather getting several graphs at once. With catplot you can do that too; just write your own loop.


            Comment


            • #7
              Thanks so much professor. It seems like the program is not working anymore as I keep getting the same error message
              Code:
              invalid syntax
              r(111);
              At first I thought the error is because my device is not allowing to save the ado file in c drive. Then I changed the sysdir but still no change. Also, from your description I see that the feature (getting % bars) that was fascinating me is not real. Thanks for this important insight too!

              Comment


              • #8
                What's the "program"? As said, if you run multicat as published by Larry Hamilton, then it will fail with catplot as currently on SSC. If you are using the code I posted in #6 then you should show the exact code you used and

                Code:
                set trace on
                so that you can show us where it fails. Also, showing us the results of

                Code:
                which multicat, all
                will show us what your Stata can see.

                Please keep reading the FAQ Advice, as you are failing to follow much of what is suggested there. #12 is especially relevant.

                Comment


                • #9
                  Originally posted by Nick Cox View Post
                  What's the "program"? As said, if you run multicat as published by Larry Hamilton, then it will fail with catplot as currently on SSC. If you are using the code I posted in #6 then you should show the exact code you used and

                  Code:
                  set trace on
                  so that you can show us where it fails. Also, showing us the results of

                  Code:
                  which multicat, all
                  will show us what your Stata can see.

                  Please keep reading the FAQ Advice, as you are failing to follow much of what is suggested there. #12 is especially relevant.
                  By program I meant commands like
                  Code:
                  multicat
                  This is the output to the query you mentioned:
                  Code:
                  . which multicat, all
                  
                  P:\Statawd\Stata14\ado\base\multicat.ado
                  *! L. Hamilton, Statistics with Stata (2012)
                  
                  .\multicat.ado
                  This is the result after setting trace on:
                  Here education is categorical variable with four levels (Nil, Primary, Secondary, Higher)

                  Code:
                  . multicat  Education
                  ------------------------------------------------------------------------------- begin multicat ---
                  - version 12.1
                  - syntax varlist [if] [in] [aweight fweight iweight] [, MISSing BY(varname) OVER(varname) ]
                  - if "`over'" != "" {
                  = if "" != "" {
                    display as error "over() option not allowed with multicat;"
                    display as error "use by() option or try catplot command instead."
                    exit 198
                    }
                  - marksample touse, strok novarlist
                  - if "`weight'" != "" local Weighted_ = "Weighted"
                  = if "" != "" local Weighted_ = "Weighted"
                  - if "`c(os)'"=="Windows" {
                  = if "Windows"=="Windows" {
                  - local filetype "emf"
                  - }
                  - else if "`c(os)'"=="Unix" {
                  = else if "Windows"=="Unix" {
                    local filetype "eps"
                    }
                  - else if "`c(os)'"=="MacOSX" {
                  = else if "Windows"=="MacOSX" {
                    local filetype = "pdf"
                    }
                  - else {
                    display as error "unknown operating system: `c(os)'"
                    exit 799
                    }
                  - capture {
                  - if "`by'" != "" {
                  = if "" != "" {
                    foreach var of varlist `varlist' {
                    local Vlab_: variable label `var'
                    catplot hbar `var' [`weight' `exp'] if `touse', blabel(bar, format(%3.0f)) percent(`by') ytitle(
                  > "`Weighted_' Percent") `missing' by(`by', title("`Vlab_'", size(medium)))
                    graph save -`by'-`var'.gph, replace
                    graph export -`by'-`var'.`filetype', replace
                    }
                    }
                  - else {
                  - foreach var of varlist `varlist' {
                  = foreach var of varlist Education {
                  - quietly tab `var' if `touse', `missing'
                  = quietly tab Education if __000000, 
                  - local Nofobs_ = r(N)
                  - local Vlab_: variable label `var'
                  = local Vlab_: variable label Education
                  - catplot hbar `var' [`weight' `exp'] if `touse', blabel(bar, format(%3.0f)) percent ytitle("`Weig
                  > hted_' Percent, N = `Nofobs_'") title("`Vlab_'", size(medium)) `missing' `options'
                  = catplot hbar Education [ ] if __000000, blabel(bar, format(%3.0f)) percent ytitle(" Percent, N =
                  >  30726") title("highest educational level", size(medium))  
                    ------------------------------------------------------------------------------ begin catplot ---
                    - version 8
                    - syntax varlist(max=3) [if] [in] [fweight aweight iweight/] [, PERCent(varlist) PERCent2 FRacti
                  > on(varlist) FRaction2 YTItle(str asis) by(str asis) VERTical MISSing recast(str) var1opts(str as
                  > is) var2opts(str asis) var3opts(str asis) * ]
                    -------------------------------------------------------------------------------- end catplot ---
                    graph save Graph -`var'.gph, replace
                    graph export -`var'.`filetype', replace
                    }
                    }
                    }
                  - error _rc
                  invalid syntax
                  --------------------------------------------------------------------------------- end multicat ---
                  r(111);

                  Comment


                  • #10
                    So, you are still trying to use a program that won't work for you, as you reported in #1 and I explained in #6 and #8.

                    You need to delete the contents of that file and substitute the code in #6. If that doesn't work, we will try to debug it.

                    Comment


                    • #11
                      Originally posted by Nick Cox View Post
                      So, you are still trying to use a program that won't work for you, as you reported in #1 and I explained in #6 and #8.

                      You need to delete the contents of that file and substitute the code in #6. If that doesn't work, we will try to debug it.
                      Actually I ran the code again as you provided the new options (set trace on), hoping that this might work. Anyway I am already happy to be able to generate percentage in catplot as you mentioned in #5. Thanks so much!

                      Comment


                      • #12
                        Sorry but I don't understand #11. There is no magic effect from my posting fixed code unless you install it. Your problem report is, frankly, still unclear. I ran the code again: which code? what happened? is there still a problem.

                        Comment


                        • #13
                          Originally posted by Nick Cox View Post
                          Sorry but I don't understand #11. There is no magic effect from my posting fixed code unless you install it. Your problem report is, frankly, still unclear. I ran the code again: which code? what happened? is there still a problem.
                          By code, I meant the ado file code (the long list) you mentioned in #6. I saved this code as an ado (multicat.ado) file by replacing the old one, but the multicat command still showing the same error. But if its working on your system then I think I am not being able to execute the command correctly, and should rather stick to catplot. Thank you!

                          Comment


                          • #14
                            My advice remains as in #10. Further,

                            Code:
                            discard
                            is a brutal but effective way to flush programs from memory.

                            Comment

                            Working...
                            X