Announcement

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

  • Line diagram with categorical variable on x-axis

    Is there any easy way to get line diagram like this in Stata? Revenue is continuous variable, Acquisition Channel is nominal variable, and test_coupon is dummy. The best I get is this:

    Code:
    . gen means=.
    
    . tabstat revenue_after if test_coupon == 0, by(channel_acq)
    
    . replace means = 4.92 if channel_acq == 1 & test_coupon == 0
    . replace means = 9.81 if channel_acq == 2 & test_coupon == 0
    . replace means = 9.17 if channel_acq == 3 & test_coupon == 0
    . replace means = 11.18 if channel_acq == 4 & test_coupon == 0
    . replace means = 14.86 if channel_acq == 5 & test_coupon == 0
    
    . tabstat revenue_after if test_coupon == 1, by(channel_acq)
    
    . replace means = 2.80 if channel_acq == 1 & test_coupon == 1
    . replace means = 10.90 if channel_acq == 2 & test_coupon == 1
    . replace means = 10.34 if channel_acq == 3 & test_coupon == 1
    . replace means = 11.52 if channel_acq == 4 & test_coupon == 1
    . replace means = 12.76 if channel_acq == 5 & test_coupon == 1
    
    . line means channel_acq if test_coupon == 0 || line means channel_acq if test_coupon == 1, lpattern(dash)
    Obviously, this is a lot of code for simple graph, and there is no error bars around means.
    Attached Files

  • #2
    maybe you'll find this bit of code useful,
    Code:
    clear
    sysuse auto
    
    gen test = foreign
    gen revenue = mpg
    gen acquisition = round(head)
    
    collapse (mean) mean=revenue (semean) se=revenue, by(test acquisition)
    
    gen ub = mean + 1.96*se
    gen lb = mean - 1.96*se
    
    twoway  line mean acquisition if test || ///
            line mean acquisition if !test || ///
            rcap ub lb acquisition if test || ///
            rcap ub lb acquisition if !test

    Comment


    • #3
      Originally posted by Øyvind Snilsberg View Post
      maybe you'll find this bit of code useful
      That is it. Thank you very much.

      Comment


      • #4
        Code:
        sysuse auto, clear
        statsby, by(foreign rep78) : su mpg, meanonly
        twoway connected mean rep78 if foreign || connected mean rep78 if !foreign , ytitle(Mean mpg) legend(order(1 "Foreign" 2 "Domestic"))

        Comment

        Working...
        X