Showing posts with label Identify last observation. Show all posts
Showing posts with label Identify last observation. Show all posts

Aug 20, 2015

Random graphs (51): Bar graph


// Data from 
// de Mouzon, Jacques, Veerle Goossens, Siladitya Bhattacharya, José Antonio 
//     Castilla, Anna Pia Ferraretti, V. Korsak, Markus Kupka, Karl-Gösta 
//     Nygren, Anders Nyboe Andersen, and the European IVF Monitoring
//     Consortium for the European Society of Human Reproduction and Embryology.
//     2012. "Assisted Reproductive Technology in Europe, 2007. Results
//     Generated from European Registers by ESHRE." Human Reproduction 27(4):
//     954-966. doi: 10.1093/humrep/des023
// Ferraretti, Anna Pia, Veerle Goossens, Jacques de Mouzon, Siladitya 
//     Bhattacharya, José Antonio Castilla, Vladislav Korsak, Markus Kupka, Karl
//     G. Nygren, Anders Nyboe Andersen, and the European IVF Monitoring
//     Consortium for the European Society of Human Reproduction and Embryology.
//     2012. "Assisted Reproductive Technology in Europe, 2008. Results
//     Generated from European Registers by ESHRE." Human Reproduction 27(9):
//     2571-2584. doi: 10.1093/humrep/des255
// Ferraretti, Anna Pia, Veerle Goossens, Markus Kupka, Siladitya Bhattacharya,
//     Jacques De Mouzon, José Antonio Castilla, Karin Erb, Vladislav Korsak,
//     Anders Nyboe Andersen, and the European IVF Monitoring Consortium for the
//     European Society of Human Reproduction and Embryology. 2013. "Assisted
//     Reproductive Technology in Europe, 2009. Results Generated from European
//     Registers by ESHRE." Human Reproduction 28(9):2318-2331. doi:
//     10.1093/humrep/det278
// Kupka, Markus S., Anna Pia Ferraretti, Jacques De Mouzon, Karin Erb, Thomas
//     D'Hooghe, José Antonio Castilla, Carlos Calhaz-Jorge, Christian De
//     Geyter, Veerle Goossens, and the European IVF Monitoring Consortium for
//     the European Society of Human Reproduction and Embryology. 2014.
//     "Assisted Reproductive Technology in Europe, 2010. Results Generated from
//     European Registers by ESHRE." Human Reproduction 29(10):2099-2113. doi:
//     10.1093/humrep/deu175
// Always the two right-hands columns of Table 1
// "Cycles/million women 15–45 y" and "Cycles/million population"--Cycles of 
// IVF, ICSI, and FER


// Fix country variable
kountry cntry, from(other) stuck marker
ren  _ISO3N_ country
kountry country, from(iso3n) to(iso2c)
ren _ISO2C_ geo
drop country
ren cntry country
ren geo cntry
order cntry country
drop MARKER

// Fix string numbers
replace cycmio1545x2010 = subinstr(cycmio1545x2010, " ", "",.)  // Remove blank (thousands separator)
replace cycmio1545x2009 = subinstr(cycmio1545x2009, " ", "",.)  
replace cycmio1545x2008 = subinstr(cycmio1545x2008, " ", "",.)  
replace cycmio1545x2007 = subinstr(cycmio1545x2007, " ", "",.)  

destring cycmio1545x2010 cycmio1545x2009 cycmio1545x2008 cycmio1545x2007, replace
*list

// Turn data set around
reshape long cycmio1545x cycmio, i(cntry) j(year)
list, sepby(cntry)

// Cross-sectional bar graph at latest point in time
replace cycmio1545x = . if cntry == "DK" & year == 2010 // Get rid of dodgy DK 2010 value

by cntry: egen lastobs = max(cond(!missing(cycmio1545x), year, .))  // Identify year of latest observation
gen byte last = year == lastobs                            // ID variable of latest observation
// According to http://www.stata.com/support/faqs/data-management/first-and-last-occurrences/

graph hbar cycmio1545x if last == 1, over(country, sort(cycmio1545x) descending) nofill ///
      ytitle("ART cycles per million women age 15{c 150}45", size(medsmall)) ylabel(0(2500)15000, ang(45)) ///
      /*note("{it:Note:} Data refer to 2010 or latest available (no earlier than 2007)", span) */ ///
      name(figure1, replace) ysize(6.5) xsize(5.5) scheme(lean1)

// Figure 1: ART cycles per million women age 15–45 per country, 2010
// Sources: Ferraretti et al. (2012; 2013) and Kupka et al. (2014).
// Notes: Values for Albania, Estonia, Ireland, Lithuania, Poland, Spain, 
// Serbia, Switzerland, and Ukraine refer to 2008; for Croatia, Cyprus, France, 
// and Denmark to 2009.  ART cycles refer to IVF, ICSI, frozen embryo 
// replacement (thawings), preimplantation genetic diagnosis and screening, egg
// donation (donation cycles), in vitro maturation, and frozen oocyte
// replacement (thawings).

Nov 2, 2014

Random graphs (35): Line plots

clear
import excel "art births across countries.xlsx", sheet("Data") firstrow case(lower) clear

ren a geo // Rename country ID

// Rename variables: perc_YEAR format
foreach v of varlist b-o {
   local x : variable label `v'
   rename `v' perc`x'
}

// Reshape data set to long format
reshape long perc, i(geo) j(year)

// Cross-sectional bar graph at latest point in time
by geo: egen lastobs = max(cond(!missing(perc), year, .))  // Identify year of latest observation
gen byte last = year == lastobs                            // ID variable of latest observation
// According to http://www.stata.com/support/faqs/data-management/first-and-last-occurrences/

graph hbar perc if year >= 2007 & last == 1, over(geo, sort(perc) descending) nofill /// ytitle("% ART live births among all births") ylabel(0(1)6) /// note("{it:Note:} Data refer to 2010 or latest available (no earlier than 2007)", span) /// /*title("Country differences in ART birth rates")*/ /// name(crosssec, replace)
// Create classical twoway line plot twoway (line perc year if geo == "DK", cmissing(no)) /// (line perc year if geo == "SI", cmissing(no)) /// (line perc year if geo == "BE", cmissing(no)) /// (line perc year if geo == "SE", cmissing(no)) /// (line perc year if geo == "DE", cmissing(no)) /// (line perc year if geo == "FR", cmissing(no)) /// (line perc year if geo == "IT", cmissing(no)) /// (line perc year if geo == "UK", cmissing(no)) /// (line perc year if geo == "US", cmissing(no)) /// , xlabel(1997(1)2010, ang(h) labsize(small)) xtitle("") /// ylabel(0(1)6) ytitle("% ART live births among all births") /// legend(label(1 "DK") label(2 "SI") label(3 "BE") /// label(4 "SE") label(5 "DE") label(6 "FR") /// label(7 "IT") label(8 "UK") label(9 "US") /// pos(2))
//cmissing(no) makes sure that gaps are not filled
 
// Use panel line plot functionality keep if inlist(geo, "DK", "SI", "BE", "SE", "DE", "FR", "IT", "UK", "US") encode geo, gen(country) xtset country year, yearly xtline perc, overlay xlabel(1997(1)2010, ang(h) labsize(small)) xtitle("") /// ylabel(0(1)6) ytitle("% ART live births among all births") ///
             legend(pos(2))
// Note that the gaps are much harder to spot in this plot
 

// Small multiples version
twoway (line perc year, by(geo, note("") /*title("Development of ART birth rate, selected countries")*/) cmissing(no)), ///
        xlabel(1998 (2) 2010, ang(h) labsize(small) alternate) xtitle("") ///
        ylabel(0(1)6) yscale(r(0)) ytitle("% ART live births among all births") ///
        name(longit, replace)
        // yscale(r(0)) adds missing gridline according to
        // http://www.stata.com/statalist/archive/2013-04/msg00904.html