Announcement

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

  • #16
    Dear Clyde,

    It seems i have not written the right code.While it executes without any error,i fail to get the graphs as expected.This is the code i'm using


    collapse (mean) usdincome usdsavings usdcapital employees, by(trained year)
    ds usdincome usdsavings usdcapital employees
    local outcomes `r(varlist)'
    reshape wide usdincome usdsavings usdcapital employees, i(year) j(trained)

    foreach v of local outcomes {
    local content: subinstr local v "usdincome usdsavings usdcapital employees" ""
    label var `v'0 "`content' (usdincome usdsavings usdcapital employees) untrained"
    label var `v'1 "`content' (usdincome usdsavings usdcapital employees) trained"
    graph twoway connect `v'year, name(`content', replace)
    }

    Please advise if i have made some error in coming up with the code.

    Comment


    • #17
      Most of what you have put inside the loop is incorrect. It should be:

      [code]
      collapse (mean) usd* employees, by(trained year)
      ds usd* employees
      local outcomes `r(varlist)'
      reshape wide usd* employees, i(year) j(trained)

      foreach v of local outcomes {
      local content: subinstr local v "usd" ""
      label var `v'0 "`content' (usd) untrained"
      label var `v'1 "`content' (usd) trained"
      graph twoway connect `v'* year, name(`content', replace)
      }

      Comment


      • #18
        Actually, on second thought, the following is a bit simpler and gets the variable labeling right in all cases:

        Code:
        collapse (mean) usd* employees, by(trained year)
        rename usd* *
        ds trained year, not
        local outcomes `r(varlist)'
        reshape wide `outcomes', i(year) j(trained)
        local outcomes: subinstr local outcomes "usd" "", all
        
        foreach v of local outcomes {
            label var `v'0 "`v' - untrained"
            label var `v'1 "`v' - trained"
            graph twoway connect `v'* year, name(`v', replace)
        }

        Comment


        • #19
          Thanks Clyde,

          The first code works though in labeling it adds usd after 'employees' yet employees are not measured in usd.

          The second code which is simpler has an error arising from the renamed variable and presents the error below.


          1 new variable name invalid
          You attempted to rename usd to . That is invalid Stata variable names.
          r(198);

          Thanks for your further reply.

          Comment


          • #20
            The first code works though in labeling it adds usd after 'employees' yet employees are not measured in usd.
            Yes. It was recognizing that problem in the output that prompted me to write the second code.

            The second code which is simpler has an error arising from the renamed variable and presents the error below.


            1 new variable name invalid
            You attempted to rename usd to . That is invalid Stata variable names.
            r(198);
            I cannot replicate this problem on my setup. The code runs without error on the example data for me. Have you changed the data from what you showed in #10? Or did you somehow mis-copy the code?

            Comment


            • #21
              data is still as i indicated but that part (rename usd* *) is giving an error.I have copied the code afresh and i still get the same error alert.
              Last edited by henry mwangi; 06 Jul 2018, 11:14.

              Comment


              • #22
                Then, sorry, but I don't know what to tell you.

                Comment


                • #23
                  Clyde,very grateful though.I will try it more to see if it works.

                  Comment


                  • #24
                    Dear Clyde, the code you suggested is also what I have used. I have recieved comments that confidence intervals of the treated and untreated should be include, which can be performed in different ways I guess. However, I have never seen that before - is it any easy way to include that?

                    Best,

                    Comment


                    • #25
                      Well, you would need to first calculate the confidence limits themselves. As I don't know what maneuvers have brought you to this point, I can't really comment on that. But once you have created variables with the lower and upper limits, including them on the graph could be done using the -twoway rcap- command, overlaying that on top of the main trend graph in the usual way.

                      Comment


                      • #26
                        Hi Clyde,

                        I am working with a repeated cross-sectional survey data (pulled from the women's India module of the demography and health surveys). I wanted to run a similar exercise but before I include any variables for treatment.

                        For example, I have a variable on contraceptive use (contuse) that indicates whether the individual uses contraceptive or not. I want to see how contraceptive use rates were trending in some states before year 1998. Here the variable contuse is a dummy if the individual uses contraceptives or not, state is encoded, I used the following code and it did not produce the output I wanted.


                        Code:
                        twoway (connected contuse year if state == 3)
                        My goal through this is to create graphs that show trends from 1992 to 2016 for each of the 28 states, so I assume I would have to use a for loop:

                        Code:
                        foreach i in state{
                        
                        twoway (connected contuse year if state == `i')
                        
                        gr save "graph`i.pdf'", replace
                        
                        }
                        However, the code doesn't produce the graphs I required. Do I need to alter my data in anyway since its individual repeated cross sectional survey data?

                        Best,
                        Lori

                        Comment


                        • #27
                          The commands you are using are attempting to graph the individual data for each person in each state. You want to get the prevalence of contraceptive use in each state for your graphs:

                          Also the syntax -foreach I in state- is an error waiting to happen. It will initialize local macro i to the value "state", and then never change it. When you get to the -twoway- command it will try to graph those observations with state == "state", which is a type mismatch since your state variable appears to be numeric, not string. Note also that your -graph save graph`i'.pdf- command will save a file with the name graph`i'.pdf, but it will be Stata graph file, not a PDF, and other programs that process PDF files won't know what to do with it. -graph save- only creates Stata graph files, nothing else. To get your graph saved in a different file format you must use -graph export-.

                          Code:
                          collapse (mean) contuse, by(state year)
                          xtset state year
                          xtline contuse
                          This will give you a bunch of panels, one state per panel showing the trend of contraceptive use prevalence over time.

                          You don't say how many states you have. It is possible that there will be so many panels that each is too small to be useful. If that's the case, an approach a little more similar to yours is to replace the -xtline- command above with:

                          Code:
                          levelsof state, local(states)
                          foreach s of local states {
                              graph twoway connect contuse year if state == `s'
                              graph export graph`i'.pdf, replace
                          }
                          Last edited by Clyde Schechter; 04 Nov 2019, 16:14.

                          Comment


                          • #28
                            Thank you, Clyde. I was able to extract the desired graph.

                            I wanted to use sample weights to produce means so I altered the code to reflect the following changes:

                            Code:
                            collapse (mean) contuse [pweight = wt], by(state year)
                            xtset state year
                            tline contuse
                            with wt being my sample weight variable. Thank you for your help!

                            Comment

                            Working...
                            X