Announcement

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

  • Help generating this type of plot

    Hey all, I need some assistance regarding the appropriate data shaping and plot type to generate this simple plot (see attachment).

    Basically all I'm trying to do is plot a change in a continuous outcome (6MWD) pre and post treatment for two different groups. The continuous scale is on the y axis and x axis only has two data points - pre-treatment and post-treatment (averages). I would like to have the lines connected for each treatment.

    I've been fiddling with stata for awhile trying to figure this out but without avail. Anyone have any suggestions regarding the type of plot and how I need my data laid out? Thank you!
    Attached Files

  • #2
    The actual -graph- command to do this, once the data are appropriately arranged, is a short one-liner. So the task will be to prepare the data for it. But nobody can help you with that unless you show an example of your data. So please post back with a data example, and be sure to use the -dataex- command to do that. If you are running version 17, 16 or a fully updated version 15.1 or 14.2, -dataex- is already part of your official Stata installation. If not, run -ssc install dataex- to get it. Either way, run -help dataex- to read the simple instructions for using it. -dataex- will save you time; it is easier and quicker than typing out tables. It includes complete information about aspects of the data that are often critical to answering your question but cannot be seen from tabular displays or screenshots. It also makes it possible for those who want to help you to create a faithful representation of your example to try out their code, which in turn makes it more likely that their answer will actually work in your data.

    Comment


    • #3
      Oh sorry about that. Here's the data that I'm working with:

      Code:
      * Example generated by -dataex-. For more info, type help dataex
      clear
      input float(walk_pre walk_post treatment)
      407 400.2024 1
      397      382 0
      392      320 0
      328      277 0
      405      452 0
      422      398 1
      439      501 1
      437      450 1
      449      560 1
      448      473 0
      439      465 1
      308      425 1
      365      463 1
      372      434 1
      440      564 1
      331  347.472 0
      414      438 1
      379      442 0
      360      425 1
      366      382 0
      425      440 1
      389      366 1
      379      358 0
      425      425 0
      397      398 1
      350        0 1
      422      457 1
      407      437 1
      388      435 1
      430   426.72 1
      235 237.1344 1
      393      402 0
      394      423 1
      390      520 0
      432      511 1
      420      380 1
      271      247 1
      402      405 0
      336      336 1
      358      379 0
      291        . 0
      413      445 1
      377      437 1
      377      349 0
      444      434 0
      411      437 0
      344      400 1
      362      354 0
      282  262.128 1
      364      391 0
      448      511 1
      376      414 1
      252      305 0
      412      390 1
      434      409 0
      436      480 1
      324      280 0
      360      371 1
      406      444 0
      414      389 0
      430      350 1
      430      420 0
      291      329 0
      407      362 1
      446      440 1
      280      372 1
      434      460 1
      427      446 1
      398      423 1
      372      263 1
      383      417 0
      278      455 1
      420        . 1
      418      442 0
      359      341 0
      395      550 1
      387        . 1
      408      465 1
      345      369 1
      268      338 1
      439      448 0
      417      411 1
      351      402 1
      324      320 1
      342      360 1
      425        . 1
      431      353 0
      440      460 1
      320      335 1
      415      440 0
      322      231 0
      404      425 1
      423      536 0
      277      362 1
      380   388.62 1
      376      385 1
      328      338 1
      285      260 0
      347      451 1
      419      490 1
      end
      label values treatment treatment
      label def treatment 0 "Placebo", modify
      label def treatment 1 "Active treatment", modify
      walk_pre is the pre-treatment walk distance and walk_post is the post-treatment walk distance. treatment is a binary treatment indicator.

      Comment


      • #4
        It's a little bit complicated. You need to, in effect, transpose the roles of pre vs post and placebo vs active treatment in your data layout. This is done by two -reshape-s, and a bunch of renaming and labeling of variables.
        Code:
        collapse (mean) walk_pre walk_post, by(treatment)
        reshape long walk_, i(treatment) j(pre_post_) string
        label define pre_post 0 "pre" 1 "post"
        encode pre_post_, gen(pre_post) label(pre_post)
        drop pre_post_
        reshape wide walk_, i(pre_post) j(treatment)
        label var walk_0 "Placebo"
        label var walk_1 "Active Treatment"
        graph twoway connect walk* pre_post, sort xlabel(0 1, valuelabel)
        (Note: the graph of the data is not quite like your drawing, because the values of walk_pre are not the same in the two treatment groups.

        Comment


        • #5
          See also https://www.stata-journal.com/articl...article=gr0041

          Comment


          • #6
            Originally posted by Clyde Schechter View Post
            It's a little bit complicated. You need to, in effect, transpose the roles of pre vs post and placebo vs active treatment in your data layout. This is done by two -reshape-s, and a bunch of renaming and labeling of variables.
            Code:
            collapse (mean) walk_pre walk_post, by(treatment)
            reshape long walk_, i(treatment) j(pre_post_) string
            label define pre_post 0 "pre" 1 "post"
            encode pre_post_, gen(pre_post) label(pre_post)
            drop pre_post_
            reshape wide walk_, i(pre_post) j(treatment)
            label var walk_0 "Placebo"
            label var walk_1 "Active Treatment"
            graph twoway connect walk* pre_post, sort xlabel(0 1, valuelabel)
            (Note: the graph of the data is not quite like your drawing, because the values of walk_pre are not the same in the two treatment groups.
            Thank you! This is so helpful

            Comment

            Working...
            X