Announcement

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

  • Is it possible to apply sample weights when generating a line graph?

    Greetings,

    I'm running Stata 15.1 on OSX. I'd like to graph the % of respondents who use social media by political ideology and survey year. That is, I'd like to be able to illustrate what percent of liberals, moderates and conservatives, respectively, use social media overtime. I can obtain the weighted proportions in a bar graph, but am unable to reproduce them in a two-way line graph. I tried browsing the archives here, but didn't find any help. My example data below consists of 5 variables: sns_use (the social media variable), 3 dummy-coded ideology variables, a year variable (year_sns) and the sample weights.

    This is the code I've been using:
    Code:
    egen libsns_mean=mean(sns_use) if ideo3==1, by(year_sns)
    egen modsns_mean=mean(sns_use) if ideo3==2, by(year_sns)
    egen consns_mean=mean(sns_use) if ideo3==3, by(year_sns)
    egen allsns_mean=mean(sns_use), by(year_sns)
    Code:
    twoway (line allsns_mean libsns_mean modsns_mean consns_mean year_sns [pweight=weight]), xlabel(2010 2012 2014 2015 2016) plotregion(fcolor(white)) graphregion(fcolor(white)) xtitle("Year") ytitle("Percent") legend(order(1 "Overall" 2 "Liberal" 3 "Moderate" 4 "Conservative"))
    And here is some example data:

    Code:
    * Example generated by -dataex-. To install: ssc install dataex
    clear
    input double sns_use float year_sns double(ideo3 weight)
    0 2010 3  1.08
    0 2010 2  1.92
    0 2010 2  4.32
    0 2010 3  1.96
    1 2010 2  4.24
    0 2010 2  4.24
    1 2010 3  3.24
    0 2010 .  1.12
    0 2010 .  3.96
    0 2010 1  5.72
    0 2010 3   2.4
    0 2010 1  8.08
    0 2010 3  8.92
    1 2010 3  5.04
    0 2010 1  5.16
    1 2010 2  4.44
    0 2010 1     1
    1 2010 3  3.44
    1 2010 3   4.4
    0 2010 2     3
    0 2010 3  1.72
    0 2010 2     6
    0 2010 1  5.96
    0 2010 2  5.96
    0 2010 3     1
    1 2010 2  4.72
    0 2010 3     2
    1 2010 3  3.64
    0 2010 2   2.8
    0 2010 3  2.36
    0 2010 3  5.64
    0 2010 3   3.8
    0 2010 3  1.96
    0 2010 2     3
    1 2010 3  3.24
    0 2010 2  2.88
    0 2010 3  1.24
    1 2010 3  2.88
    0 2010 2  4.76
    0 2010 2  2.88
    1 2010 1     3
    1 2010 .  3.36
    0 2010 1  2.48
    0 2010 .  1.72
    0 2010 1  9.84
    0 2010 2   2.8
    1 2010 1 10.72
    1 2010 1  3.76
    0 2010 3  5.96
    1 2010 3  3.88
    1 2010 3  1.72
    0 2010 2  1.72
    1 2010 1  5.96
    0 2010 3  2.36
    0 2010 3  2.68
    1 2010 2  2.64
    1 2010 3   4.8
    1 2010 3  5.96
    0 2010 2  3.04
    1 2010 3  1.72
    0 2010 2  1.08
    0 2010 1     1
    1 2010 3  2.52
    0 2010 1  2.28
    0 2010 2  1.72
    0 2010 1  5.96
    0 2010 3  4.44
    0 2010 3     1
    1 2010 2  6.48
    0 2010 2  1.72
    0 2010 3  5.96
    1 2010 1  2.36
    0 2010 1  3.24
    0 2010 2  1.72
    0 2010 3  2.08
    0 2010 2  1.72
    0 2010 3  3.56
    0 2010 3  1.76
    0 2010 3  2.36
    0 2010 3  1.72
    0 2010 3  1.24
    1 2010 2  5.96
    0 2010 .  2.16
    0 2010 2  3.28
    0 2010 3   3.4
    1 2010 3  5.96
    0 2010 .  1.04
    1 2010 2     1
    0 2010 3  6.84
    0 2010 .     2
    0 2010 3  1.72
    0 2010 2  2.56
    1 2010 3   2.2
    0 2010 3  2.48
    0 2010 1  2.08
    1 2010 2  4.96
    0 2010 2  5.96
    1 2010 2  2.56
    1 2010 2  3.44
    0 2010 3     2
    end
    label values ideo3 ideo3
    label def ideo3 1 "Liberal", modify
    label def ideo3 2 "Moderate", modify
    label def ideo3 3 "Conservative", modify
    Any help will be much appreciated. Thanks!

  • #2
    You need to apply the weights in calculating what you want to plot. Giving line some variables and some weights won't change what the line graph shows. In that respect graph bar is quite different as it does do calculations first.

    Comment


    • #3
      Hey Nick,

      You need to apply the weights in calculating what you want to plot
      How can I do this?

      Thanks!

      Comment


      • #4
        Did you notice that there is only one year in your sample data set? In what follows I've divided 2010 into three years. I'll leave the titles and legend to you.
        Code:
        clear
        input double sns_use float year double(ideo3 weight)
        0 2010 3  1.08
        0 2010 2  1.92
        0 2010 2  4.32
        0 2010 3  1.96
        1 2010 2  4.24
        0 2010 2  4.24
        1 2010 3  3.24
        0 2010 .  1.12
        0 2010 .  3.96
        0 2010 1  5.72
        0 2010 3   2.4
        0 2010 1  8.08
        0 2010 3  8.92
        1 2010 3  5.04
        0 2010 1  5.16
        1 2010 2  4.44
        0 2010 1     1
        1 2010 3  3.44
        1 2010 3   4.4
        0 2010 2     3
        0 2010 3  1.72
        0 2010 2     6
        0 2010 1  5.96
        0 2010 2  5.96
        0 2010 3     1
        1 2010 2  4.72
        0 2011 3     2
        1 2011 3  3.64
        0 2011 2   2.8
        0 2011 3  2.36
        0 2011 3  5.64
        0 2011 3   3.8
        0 2011 3  1.96
        0 2011 2     3
        1 2011 3  3.24
        0 2011 2  2.88
        0 2011 3  1.24
        1 2011 3  2.88
        0 2011 2  4.76
        0 2011 2  2.88
        1 2011 1     3
        1 2011 .  3.36
        0 2011 1  2.48
        0 2011 .  1.72
        0 2011 1  9.84
        0 2011 2   2.8
        1 2011 1 10.72
        1 2011 1  3.76
        0 2011 3  5.96
        1 2011 3  3.88
        1 2011 3  1.72
        0 2011 2  1.72
        1 2011 1  5.96
        0 2011 3  2.36
        0 2011 3  2.68
        1 2011 2  2.64
        1 2011 3   4.8
        1 2011 3  5.96
        0 2011 2  3.04
        1 2012 3  1.72
        0 2012 2  1.08
        0 2012 1     1
        1 2012 3  2.52
        0 2012 1  2.28
        0 2012 2  1.72
        0 2012 1  5.96
        0 2012 3  4.44
        0 2012 3     1
        1 2012 2  6.48
        0 2012 2  1.72
        0 2012 3  5.96
        1 2012 1  2.36
        0 2012 1  3.24
        0 2012 2  1.72
        0 2012 3  2.08
        0 2012 2  1.72
        0 2012 3  3.56
        0 2012 3  1.76
        0 2012 3  2.36
        0 2012 3  1.72
        0 2012 3  1.24
        1 2012 2  5.96
        0 2012 .  2.16
        0 2012 2  3.28
        0 2012 3   3.4
        1 2012 3  5.96
        0 2012 .  1.04
        1 2012 2     1
        0 2012 3  6.84
        0 2012 .     2
        0 2012 3  1.72
        0 2012 2  2.56
        1 2012 3   2.2
        0 2010 3  2.48
        0 2010 1  2.08
        1 2010 2  4.96
        0 2010 2  5.96
        1 2010 2  2.56
        1 2010 2  3.44
        0 2010 3     2
        end
        table year ideo3 sns_use
        replace sns_use=1 if year==2010 & ideo3==1
        table year ideo3 sns_use
        drop if ideo3==.
        
        tempfile t1
        save `t1'
        collapse  (mean) sns_use [pw = weight], by(ideo3 year)
        list
        tempfile t2
        save `t2'
        
        use `t1', clear
        collapse (mean)  sns_use [pw = weight], by(year)
        gen ideo3 =4
        append using `t2'
        sort year ideo3
        list
        
        separate sns_use, by(ideo3)  gen(sns)
        
        sort year ideo3
        twoway connect sns? year, xlabel(2010 2011 2012)
        graph export trial.png
        Click image for larger version

Name:	trial.png
Views:	1
Size:	63.9 KB
ID:	1449575

        Last edited by Steve Samuels; 18 Jun 2018, 17:34.
        Steve Samuels
        Statistical Consulting
        [email protected]

        Stata 14.2

        Comment

        Working...
        X