Announcement

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

  • #16
    Are you saying that my code doesn't work in 14.2?
    Yes it did not work because xla(1/`J') was not properly getting created showing an error message and mlabformat( ) was either not allowed. Therefore, I choose to define xlab() using a loop to create x-axis labels and run
    Code:
    gen str5 prop_lbl = string(proportion, "%03.2f")  
     gen str5 cum_lbl = string(cumulative, "%03.2f")
    to define an mla () Thanks,
    ​​​​​​​(Ridwan)

    Comment


    • #17
      The graph you created seems like a Pareto plot. See Wilkinson, Leland. 2006. Revising the Pareto chart. American Statistician 60(4): 332-334. https://www.tandfonline.com/doi/abs/...0313006X152243

      Comment


      • #18
        Some rummaging shows that mlabformat() was not implemented in Stata 14.

        So please try this in your Stata

        Code:
        . mat A = [1,2,3]
        
        . di colsof(A)
        3
        
        . local J = colsof(A)
        
        . di "`J'"
        3
        Last edited by Nick Cox; 07 Apr 2025, 02:36.

        Comment


        • #19
          Since Wilkinson's paper on the paper on the Pareto plot has been mentioned, see also https://www.stata.com/statalist/arch.../msg00515.html
          Last edited by Nick Cox; 07 Apr 2025, 02:30.

          Comment


          • #20
            Originally posted by Ridwan Sheikh View Post
            ... mlabformat( ) was either not allowed.
            the mlabformat(%fmtlist) suboption is introduced in Stata 18.

            Comment


            • #21
              Thank you Nick Cox and Chen Samulsion

              I will have a look at the Pareto plot. Unknowingly, that is something we created aboved.
              the mlabformat(%fmtlist) suboption is introduced in Stata 18.
              I am not using Stata 18, that is why I have follwed an indirect approach for mla() to work in my case.

              I would rather follow the suggestion by Nick Cox in #18.

              Thank you very much for the help, I really appreciate that.

              regards,
              (Ridwan)

              Comment


              • #22
                I wondered about starting from screeplot. You don't save much code, but the result may be interested.

                I've tried to reduce dependency on Stata 18.

                Code:
                sysuse auto, clear
                
                pca length weight headroom trunk displacement
                
                mat Ev = e(Ev)
                local J = colsof(Ev)
                gen eigenvalue = Ev[1, _n] in 1/`J'
                gen eigen_prop = strofreal(eigenvalue/`J', "%03.2f") in 1/`J'
                
                gen PC = _n in 1/`J'
                gen Cumulative = sum(eigenvalue) in 1/`J'
                
                capture set scheme stcolor
                if _rc {
                    set scheme s1color
                    local stretch ysc(r(. `J'.2))
                }
                
                local colour1 = cond(c(version) >= 18, "stc1", "blue")
                local colour2 = cond(c(version) >= 18, "stc2", "red")
                
                * mylabels is from Stata Jouranl
                mylabels 0.2(0.2)0.8, myscale(`J' * @) format(%02.1f) local(yla)
                
                gen Cumulative_show = strofreal(Cumulative/`J', "%03.2f")
                
                list eigenvalue eigen_prop Cumulative PC in 1/`J'
                
                screeplot , recast(bar) mla(eigen_prop) mlabpos(12) barw(0.8) lcol(`colour1') fcol(`colour1'*0.2) ///
                addplot( ///
                connected Cumulative PC, mla(Cumulative_show) mlabc(`colour2') mlabpos(12) ///
                ) ///
                legend(pos(12) row(1)) yaxis(1 2) ///
                yla(`yla' 0 `J' "1", ang(h) axis(2) labcolor(`colour2')) ///
                yla(, ang(h) axis(1) labcolor(`colour1')) ///
                ytitle(Cumulative Proportion, axis(2) color(`colour2')) ///
                ytitle(Eigenvalues, axis(1) color(`colour1')) xtitle(PC) `stretch' 
                Click image for larger version

Name:	screeplot_Pareto.png
Views:	1
Size:	55.8 KB
ID:	1775488
                Last edited by Nick Cox; 07 Apr 2025, 13:45.

                Comment


                • #23
                  Thank you Nick Cox . It is nice to rather do this exercise using screeplot. But I am getting the following error in the given code while executing it in my Stata (14.2)
                  Code:
                  gen eigenvalue = Ev[1, _n] in 1/`J'
                  
                  ' ' invalid observation number

                  Comment


                  • #24
                    That suggests that

                    you omitted the previous calculation of local J

                    OR

                    that the calculation did not work but you just ignored the error

                    OR

                    that you are executing the commands line by line from a file, in which case Stata cannot see the definition of local J.

                    On the last possibility see https://journals.sagepub.com/doi/pdf...36867X20931028

                    Comment


                    • #25
                      Thanks for pointing the error.

                      I tried executing the command together, untill the screeplot (last command). It produces the following error

                      Code:
                      mylabels 0.2(0.2)0.8, myscale(`J' * @) format(%02.1f) local(yla)
                      command mylabels is unrecognized

                      Comment


                      • #26
                        See again #22 where the comment (typo corrected here) explains:


                        Code:
                         * mylabels is from Stata Journal
                        So, you must install mylabels before you can use it.

                        You can work around its absence by using your own loop. This version of the code comments out the mylabels call.

                        (The principles are explained at Stata Tip 59: Plotting on Any Transformed Scale but the idea here is just to work out the text you want to see and where it should be put on an axis as those numbers multiplied by the number of PCs.)

                        Code:
                        sysuse auto, clear
                        
                        pca length weight headroom trunk displacement
                        
                        mat Ev = e(Ev)
                        local J = colsof(Ev)
                        gen eigenvalue = Ev[1, _n] in 1/`J'
                        gen eigen_prop = strofreal(eigenvalue/`J', "%03.2f") in 1/`J'
                        
                        gen PC = _n in 1/`J'
                        gen Cumulative = sum(eigenvalue) in 1/`J'
                        
                        capture set scheme stcolor
                        if _rc {
                            set scheme s1color
                            local stretch ysc(r(. `J'.2))
                        }
                        
                        local colour1 = cond(c(version) >= 18, "stc1", "blue")
                        local colour2 = cond(c(version) >= 18, "stc2", "red")
                        
                        * mylabels is from Stata Journal
                        * mylabels 0.2(0.2)0.8, myscale(`J' * @) format(%02.1f) local(yla)
                        
                        foreach y in 0.2 0.4 0.6 0.8 {
                            local Y = `J' * `y'
                            local yla `yla' `Y' "`y'"
                        }
                        
                        gen Cumulative_show = strofreal(Cumulative/`J', "%03.2f")
                        
                        list eigenvalue eigen_prop Cumulative PC in 1/`J'
                        
                        screeplot , recast(bar) mla(eigen_prop) mlabpos(12) barw(0.8) lcol(`colour1') fcol(`colour1'*0.2) ///
                        addplot( ///
                        connected Cumulative PC, mla(Cumulative_show) mlabc(`colour2') mlabpos(12) ///
                        ) ///
                        legend(pos(12) row(1)) yaxis(1 2) ///
                        yla(`yla' 0 `J' "1", ang(h) axis(2) labcolor(`colour2')) ///
                        yla(, ang(h) axis(1) labcolor(`colour1')) ///
                        ytitle(Cumulative Proportion, axis(2) color(`colour2')) ///
                        ytitle(Eigenvalues, axis(1) color(`colour1')) xtitle(PC) `stretch'
                        Last edited by Nick Cox; 08 Apr 2025, 07:23.

                        Comment

                        Working...
                        X