Announcement

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

  • Creating a bar chart with error when all variables are categorical

    Hi everyone,

    I am trying to create bar graphs with error bars. My data consists of an individual id called id, place they are moving called destination, and an individual's race called race. I also have dummy variables for each of the possible destinations. My data is below.

    Code:
    * Example generated by -dataex-. To install: ssc install dataex
    clear
    input float(id destination race) byte(east west south north)
     1 2 4 0 1 0 0
     2 3 1 0 0 1 0
     3 4 1 0 0 0 1
     4 1 4 1 0 0 0
     5 2 5 0 1 0 0
     6 3 1 0 0 1 0
     7 3 4 0 0 1 0
     8 3 1 0 0 1 0
     9 2 5 0 1 0 0
    10 3 5 0 0 1 0
    11 4 2 0 0 0 1
    12 2 1 0 1 0 0
    13 1 4 1 0 0 0
    14 1 1 1 0 0 0
    15 1 1 1 0 0 0
    16 1 5 1 0 0 0
    17 2 2 0 1 0 0
    18 3 4 0 0 1 0
    19 1 5 1 0 0 0
    20 1 5 1 0 0 0
    21 4 3 0 0 0 1
    22 1 5 1 0 0 0
    23 3 1 0 0 1 0
    24 2 4 0 1 0 0
    25 3 2 0 0 1 0
    26 1 2 1 0 0 0
    27 3 1 0 0 1 0
    28 1 4 1 0 0 0
    29 3 3 0 0 1 0
    30 3 4 0 0 1 0
    31 2 4 0 1 0 0
    32 2 4 0 1 0 0
    33 2 2 0 1 0 0
    34 2 4 0 1 0 0
    35 1 4 1 0 0 0
    36 2 1 0 1 0 0
    37 3 2 0 0 1 0
    38 1 2 1 0 0 0
    39 3 1 0 0 1 0
    40 3 5 0 0 1 0
    end
    
    label values destination area
    label def area 1 "east", modify
    label def area 2 "west", modify
    label def area 3 "south", modify
    label def area 4 "north", modify
    label values race Race
    label def Race 1 "African American", modify
    label def Race 2 "Asian", modify
    label def Race 3 "Latino", modify
    label def Race 4 "White", modify
    label def Race 5 "Other", modify

    I subsetted each racial group and used the graph command to chart where individuals are planning to move. After subsetting for the five racial groups, I used the user package grc1leg2 to combine them but there isn't an option to add confidence intervals.

    Code:
    *Subset for African American
    preserve
    keep if race==1
    
    **Original destination for African American
    graph hbar east west south north, over(race, label(nolabel)) ///
    legend(size(small) cols(5) region(lstyle(none)) ///
    label(1 "east") label(2 "west") label(3 "south") label(4 "north")) ///
    title("African American") ///
    ylabel(0 .1 .2 .3 .4 .5 .6 .7, angle(horizontal)) ///
    ytitle("Percentage") ///
    blabel(bar, format(%4.2f)) ///
    saving("AA", replace)
    restore
    
    *Subset for Asian
    preserve
    keep if race==2
    
    *Transfer to segment disaggregated by race
    graph hbar east west south north, over(race, label(nolabel)) ///
    legend(size(small) cols(5) region(lstyle(none)) ///
    label(1 "east") label(2 "west") label(3 "south") label(4 "north")) ///
    title("Asian") ///
    ylabel(0 .1 .2 .3 .4 .5 .6 .7, angle(horizontal)) ///
    ytitle("Percentage") ///
    blabel(bar, format(%4.2f)) ///
    saving("Asian", replace)
    restore
    
    
    *Subset for Latino
    preserve
    keep if race==3
    
    *Original destination for Latio
    graph hbar east west south north, over(race, label(nolabel)) ///
    legend(size(small) cols(5) region(lstyle(none)) ///
    label(1 "east") label(2 "west") label(3 "south") label(4 "north")) ///
    title("Latino") ///
    ylabel(0 .1 .2 .3 .4 .5 .6 .7, angle(horizontal)) ///
    ytitle("Percentage") ///
    blabel(bar, format(%4.2f)) ///
    saving("Latino", replace)
    restore
    
    
    *Subset for White
    preserve
    keep if race==4
    
    *Original destination for White
    graph hbar east west south north, over(race, label(nolabel)) ///
    legend(size(small) cols(5) region(lstyle(none)) ///
    label(1 "east") label(2 "west") label(3 "south") label(4 "north")) ///
    title("White") ///
    ylabel(0 .1 .2 .3 .4 .5 .6 .7, angle(horizontal)) ///
    ytitle("Percentage") ///
    blabel(bar, format(%4.2f)) ///
    saving("White", replace)
    restore
    
    
    *Subset for Other
    preserve
    keep if race==1
    
    *Original destination for Other
    graph hbar east west south north, over(race, label(nolabel)) ///
    legend(size(small) cols(5) region(lstyle(none)) ///
    label(1 "east") label(2 "west") label(3 "south") label(4 "north")) ///
    title("Other") ///
    ylabel(0 .1 .2 .3 .4 .5 .6 .7, angle(horizontal)) ///
    ytitle("Percentage") ///
    blabel(bar, format(%4.2f)) ///
    saving("Other", replace)
    restore
    
    *Combine all the charts together
    grc1leg2 AA.gph Asian.gph Latino.gph White.gph Other.gph,  ycommon

    As a result, I am using the following website to help me estimate the error bars.

    https://stats.oarc.ucla.edu/stata/fa...th-error-bars/

    I tried the following code but this is not the same as the website.

    Code:
    collapse (mean) meandestination= destination (sd) sddestination=destination (count) n=destination, by(race)
    I am not sure how to collapse the data the way the website suggests. Any suggestions would be greatly appreciated. Thanks.
    Last edited by Jack Prado; 18 Feb 2022, 15:05.
Working...
X