Announcement

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

  • Help with Control Charts

    Hi everyone

    I'm having a problem with running a loop xbar control chart. What i'm trying to do is produce a control chart for every week of my observations. And i want to output the number of violations for each week and the number of observations to give me a score of [1-(violations/observations)*100]

    The code i'm using is the following:

    foreach i of local levels {
    summarize AM if week==`i'
    local x=`r(mean)'
    display `x'
    local s=`r(sd)'
    display `s'
    local u=`x'+(3*`s')
    display `u'
    local t=`x'-(3*`s')
    display `t'
    local z=`x'-(100*`s')

    replace AM=0 if AM==. & week==`i'
    xchart days AM if week==`i', lower(`t') upper(`u')
    gen total`i'=`r(N)'
    gen violations`i'=`r(out_x)'
    gen AMscore`i'=(1-(violations`i'/total`i'))*100

    }

    I'm running into three problems:
    1. For some reason the local values aren't being saved properly and the control charts are being produced with limits based on the last week of observations
    2. For some reason the values being plotted in the control chart don't match the actual values in my database
    3. The output from the control chart, ie. the number of observations, is saving as total observations, not number of observations per week.

    If anyone has any advice i'd really appreciate. I've spent the whole day today trying to figure this out;
    I'm sure its some simple mistake i'm making.

    thanks everyone
    imran


  • #2
    I don't have all your answers but everytime you loop through another `i', you are overwriting the existing locals; you need to name them by, e.g., including `i' in the name to save all of them

    Comment


    • #3
      Hi Rich

      Thanks for the advice. i don't think that's the problem cause i have a similar loop running with `i' and saving local data with no issues. In any case i tried what you suggested and it made no different to the output in my code

      I can't figure out what is causing the problem!

      Any ideas?

      Comment


      • #4
        Hi Imran,

        From reading the manual I see that -xchart- plots average of a given list of variables when variables represent repeated measurements at various times and observations represent samples (see the manual). In your case, it seems that you are trying to plot average of two variables (in a given week): days and AM. Is that what you want to do? You may probably look at -cchart-.

        Abraham
        Last edited by Abraham Wolde-Tsadick; 25 Oct 2014, 13:04.

        Comment


        • #5
          Hi Abraham

          Thanks for you comment. My data is basically one AM variable for each day, and i have the days grouped by week number.

          So for my control chart i'm looking for days on the x axis and AM on the y axis, and to do this per week of data.

          If i run the xchart outside of the loop, ie. xchart days AM if week==1, this seems to work.

          Its some error in running the loop that's causing the problem.

          Comment


          • #6
            Can you attach a small subset of your data?
            By the way, you use quotes when you refer to locals you have already defined, stored or saved. You don't need to use quotes when you define them. For example,
            Code:
            local x=`r(mean)'
            can be written as
            Code:
            local x=r(mean)
            Abraham

            Comment


            • #7


              Hi Abraham

              Thanks again for you help. I've attached an example of my data.
              To confused matters more, week is actually month

              hope this makes sense what i'm trying to do.

              thanks again

              imran
              Attached Files

              Comment


              • #8
                Imran:
                many listers (me too) on this forum follow the cautionary tale of not downloading spreadsheets (due to the risk of nasty macros or malicious contents); at the top of that, some of us do not even use .xlsx files.
                Hence, the best way to post some details about your data is via the advanced editor (click on A icon and take a tour through its capabilities).

                Kind regards,
                Carlo
                Kind regards,
                Carlo
                (Stata 19.0)

                Comment


                • #9
                  Hi Carlo

                  Thanks for the advice, that makes sense. Apologies for my error in uploading an xlsx file.
                  Days AM Week
                  1 8.4094444 1
                  2 11.143611 1
                  3 9.5861111 1
                  4 8.7144444 1
                  5 8.5336111 1
                  6 1
                  7 1
                  8 11.397778 1
                  9 9.6475 1
                  10 8.2141667 1
                  11 8.7577778 1
                  12 9.2161111 1
                  13 8.5386111 1
                  14 8.6663889 1
                  15 11.005278 1
                  16 1
                  17 9.2838889 1
                  18 11.301389 1
                  19 1
                  20 1
                  21 9.0733333 1
                  22 12.819444 1
                  23 1
                  24 8.8394444 1
                  25 1
                  26 8.5380556 1
                  27 9.07 2
                  28 2
                  29 2
                  30 10.685 2
                  31 8.1397222 2
                  32 8.5955556 2
                  33 10.493889 2
                  34 11.196111 2
                  35 10.14 2
                  36 9.2088889 2
                  37 9.9102778 2
                  38 8.6872222 2
                  39 8.8513889 2
                  40 8.2875 2
                  41 8.8644444 2
                  42 8.1977778 2
                  43 8.0816667 2
                  44 11.890278 2
                  45 10.599444 2
                  46 8.0983333 2
                  47 8.8383333 2
                  48 9.1616667 2
                  49 8.0688889 2
                  50 11.769444 2
                  51 11.086667 2
                  52 8.215 2
                  53 8.1627778 2
                  54 8.4777778 2
                  55 11.395278 2
                  56 9.1072222 2
                  57 12.509444 2
                  58 10.755833 2
                  59 2
                  60 2
                  61 2
                  62 2
                  63 2
                  64 2
                  65 2
                  66 2
                  67 2
                  68 2
                  69 2
                  70 6.7725 2
                  71 13.415833 3
                  72 12.167778 3
                  73 7.4205556 3
                  74 8.0691667 3
                  75 8.2922222 3
                  76 7.8930556 3
                  77 11.154722 3
                  78 11.507222 3
                  79 10.813611 3
                  80 3
                  81 3
                  82 7.6305556 3
                  83 12.583056 3
                  84 7.5577778 3
                  85 3
                  86 12.721667 3
                  87 8.2344444 3
                  88 8.0383333 3
                  89 7.1005556 3
                  90 3
                  91 3
                  92 11.937778 3
                  93 12.538333 3
                  94 11.807778 3
                  95 7.4519444 3
                  96 9.9233333 3
                  97 12.392778 3
                  98 6.9580556 3

                  I hope its ok i paste the data like this?

                  thanks
                  ​imran

                  Comment


                  • #10
                    Imran: please follow Carlo's recommendations about how post output -- please don't use copy/paste as you have. It is not easily useable (if posters wish to "play" with your data) and takes up a lot of screen space. Instead, use CODE delimiters -- which is what Carlo was alluding to. You can read about how to use these in the Forum FAQ. When you are re-reading the FAQ, please also note the remarks about the use of real names (firstname lastname). It would be appreciated if you re-registered. Help people want to help you.

                    Comment


                    • #11
                      Hi Stephen thanks for your advice. Sorry I'm new to this forum. I've sent an email to the website administrator to change my username to my full name.

                      In regards to putting my data in the forum, i'm not sure how to do this. If I use the advanced options, do i insert the data as a table? That'll still take up a lot of screen space. I had a read of the FAQ on using the Advanced option, but I'm still not sure what's the best way to post my data.

                      thanks again.

                      Comment


                      • #12
                        As Stephen already made specific it's best to post data between CODE delimiters. As Clyde Schechter recently posted in another thread (edits here towards your situation).

                        "Don't post your examples as attachments or screen shots. Use the list command to get representative data, and then post those in a code block. (Click on the underlined A button to open the advanced editor, then on the # button. A pair of code-block delimiters will appear. Paste your examples between them.) This is the only way to ensure that the data you show us will be readable to the rest of us."

                        Your data should look a little like this:

                        Code:
                        . list mpg weight in 1/10
                        
                             +--------------+
                             | mpg   weight |
                             |--------------|
                          1. |  22    2,930 |
                          2. |  17    3,350 |
                          3. |  22    2,640 |
                          4. |  20    3,250 |
                          5. |  15    4,080 |
                             |--------------|
                          6. |  18    3,670 |
                          7. |  26    2,230 |
                          8. |  20    3,280 |
                          9. |  16    3,880 |
                         10. |  19    3,400 |
                             +--------------+
                        Another way to get this is to type

                        open square bracket CODE close square bracket

                        <all your stuff>

                        open square bracket forward slash CODE close square bracket

                        where the punctuation around "CODE" is

                        [ ]

                        [/ ]

                        Last edited by Nick Cox; 26 Oct 2014, 06:24.

                        Comment


                        • #13
                          ok thanks for the advice Nick. So i'll post the output from stata, but i'll limit it to 60 observations so the post isn't massive. I hope this is ok:

                          Code:
                          list days AM week in 1/60
                          
                               +-------------------------+
                               | days          AM   week |
                               |-------------------------|
                            1. |    1   8.4094444      1 |
                            2. |    2   11.143611      1 |
                            3. |    3   9.5861111      1 |
                            4. |    4   8.7144444      1 |
                            5. |    5   8.5336111      1 |
                               |-------------------------|
                            6. |    6           .      1 |
                            7. |    7           .      1 |
                            8. |    8   11.397778      1 |
                            9. |    9      9.6475      1 |
                           10. |   10   8.2141667      1 |
                               |-------------------------|
                           11. |   11   8.7577778      1 |
                           12. |   12   9.2161111      1 |
                           13. |   13   8.5386111      1 |
                           14. |   14   8.6663889      1 |
                           15. |   15   11.005278      1 |
                               |-------------------------|
                           16. |   16           .      1 |
                           17. |   17   9.2838889      1 |
                           18. |   18   11.301389      1 |
                           19. |   19           .      1 |
                           20. |   20           .      1 |
                               |-------------------------|
                           21. |   21   9.0733333      1 |
                           22. |   22   12.819444      1 |
                           23. |   23           .      1 |
                           24. |   24   8.8394444      1 |
                           25. |   25           .      1 |
                               |-------------------------|
                           26. |   26   8.5380556      1 |
                           27. |   27        9.07      2 |
                           28. |   28           .      2 |
                           29. |   29           .      2 |
                           30. |   30      10.685      2 |
                               |-------------------------|
                           31. |   31   8.1397222      2 |
                           32. |   32   8.5955556      2 |
                           33. |   33   10.493889      2 |
                           34. |   34   11.196111      2 |
                           35. |   35       10.14      2 |
                               |-------------------------|
                           36. |   36   9.2088889      2 |
                           37. |   37   9.9102778      2 |
                           38. |   38   8.6872222      2 |
                           39. |   39   8.8513889      2 |
                           40. |   40      8.2875      2 |
                               |-------------------------|
                           41. |   41   8.8644444      2 |
                           42. |   42   8.1977778      2 |
                           43. |   43   8.0816667      2 |
                           44. |   44   11.890278      2 |
                           45. |   45   10.599444      2 |
                               |-------------------------|
                           46. |   46   8.0983333      2 |
                           47. |   47   8.8383333      2 |
                           48. |   48   9.1616667      2 |
                           49. |   49   8.0688889      2 |
                           50. |   50   11.769444      2 |
                               |-------------------------|
                           51. |   51   11.086667      2 |
                           52. |   52       8.215      2 |
                           53. |   53   8.1627778      2 |
                           54. |   54   8.4777778      2 |
                           55. |   55   11.395278      2 |
                               |-------------------------|
                           56. |   56   9.1072222      2 |
                           57. |   57   12.509444      2 |
                           58. |   58   10.755833      2 |
                           59. |   59           .      2 |
                           60. |   60           .      2 |
                               +-------------------------+

                          Comment


                          • #14
                            Hi Imran,

                            I may be wrong but I think you can not apply -xchart- in the current structure of your data.
                            It is posible to use -cchart- but from my reading of the manual, it does not allow placing lower and upper limits. It also does not allow -if-.
                            The following code creates and saves grahs for each week as you want without the upper and the lower limits.
                            It displays the score of each week on the screen together with the mean, sd, etc.
                            Others will have probably more to say.

                            Code:
                            levelsof week, local(levels)
                            foreach i of local levels {
                            
                                preserve
                                keep if week == `i'
                                qui sum AM, detail
                                local x = r(mean)
                                display `x'
                                local s = r(sd)
                                display `s'
                                local u =`x'+(3*`s')
                                display `u'
                                local t =`x'-(3*`s')
                                display `t'
                                local z =`x'-(100*`s')
                            
                                replace AM=0 if AM==.
                                cchart AM days, saving(graph`i', replace)
                                local total`i' = r(N)
                                local violations`i' = r(out_c)
                                local AMscore`i' = (1-(`violations`i''/`total`i''))*100
                                display `AMscore`i''
                                restore
                            
                             }
                            Abraham
                            Last edited by Abraham Wolde-Tsadick; 26 Oct 2014, 11:20.

                            Comment


                            • #15
                              Hi Abraham

                              Thanks for that. I think you are probably right about the cchart versus the xbar chart.
                              However there is a further problem. Per month/week i need to force empty values as violations, hence:

                              Code:
                              replace AM=0 if AM==.
                              if i use the cchart, where i can't force the limits, it encompasses these new values of 0 into calculating the mean and upper and lower limits so the number of violations becomes inaccurate.

                              i'm not sure how to get past this problem.

                              thanks
                              imran


                              Comment

                              Working...
                              X