Twins on the up

[This article was first published on HighlandR, and kindly contributed to R-bloggers]. (You can report issue about the content on this page here)
Want to share your content on R-bloggers? click here if you have a blog, or here if you don't.

Are multiple births on the increase?

My twin boys turned 5 years old today. Wow, time flies. Life is never dull, because twins are still seen as something of a novelty, so wherever we go, we find ourselves in conversation with strangers, who are intrigued by the whole thing.

In order to save time if we ever meet, here’s some FAQ’s:

  • No, they’re not identical
  • Yes, I’m sure
  • No, they do not have similar personalities
  • They like different things – One likes Hulk and Gekko, the other likes Iron Man and Catboy.

Recently I’ve been hearing and seeing anecdotal evidence that twins and multiple births are on the increase. I tried to find some data for Scotland, and while there is a lot of information on births in Scotland available, I couldn’t find breakdowns of multiple births.
However, I did find some information for England and Wales, so let’s look at that.

In this next bit, they key thing that may be of interest is the use of tidyr::gather.
There has been some discussion on #rstats Twitter about things people struggle with and a surprising amount of people struggle to remember the syntax for tidyr’s gather and spread.

(I can neither confirm or deny I am one of them).

The data was found here

<span class="n">library</span><span class="p">(</span><span class="n">readxl</span><span class="p">)</span><span class="w">
</span><span class="n">library</span><span class="p">(</span><span class="n">dplyr</span><span class="p">)</span><span class="w">
</span><span class="n">library</span><span class="p">(</span><span class="n">tidyr</span><span class="p">)</span><span class="w">
</span><span class="n">library</span><span class="p">(</span><span class="n">ggplot2</span><span class="p">)</span><span class="w">

</span><span class="n">data</span><span class="w"> </span><span class="o"><-</span><span class="w"> </span><span class="n">read_xls</span><span class="p">(</span><span class="s2">"birthcharacteristicsworkbook2016.xls"</span><span class="p">,</span><span class="w"> </span><span class="n">sheet</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="s2">"Table 11"</span><span class="p">,</span><span class="w">
                 </span><span class="n">range</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="s2">"A10:I87"</span><span class="p">)</span><span class="w">

</span><span class="n">data</span><span class="w"> </span><span class="o"><-</span><span class="w"> </span><span class="n">data</span><span class="w"> </span><span class="o">%>%</span><span class="w"> 
  </span><span class="n">rename</span><span class="p">(</span><span class="n">Year</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="n">X__1</span><span class="p">,</span><span class="w">
         </span><span class="n">All_ages</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="n">`All maternities with multiple births`</span><span class="p">,</span><span class="w">
         </span><span class="n">Under20</span><span class="w">  </span><span class="o">=</span><span class="w"> </span><span class="n">X__2</span><span class="p">,</span><span class="w">
         </span><span class="n">`20_to_24`</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="n">X__3</span><span class="p">,</span><span class="w">
         </span><span class="n">`25_to_29`</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="n">X__4</span><span class="p">,</span><span class="w">
         </span><span class="n">`30_to_34`</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="n">X__5</span><span class="p">,</span><span class="w">
         </span><span class="n">`35_to_39`</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="n">X__6</span><span class="p">,</span><span class="w">
         </span><span class="n">`40_to_44`</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="n">X__7</span><span class="p">,</span><span class="w">
         </span><span class="n">`45_and_over`</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="n">X__8</span><span class="p">)</span><span class="w">

</span><span class="c1"># the 1981 data is borked, so ignore that</span><span class="w">

</span>

Note use of gather to combine all the age groups into an age_group variable.

We use the Year column as an index so we have an entry for every age group, for every year, with the value represented as ‘maternities’.

Back to the code:

<span class="n">long_data</span><span class="w"> </span><span class="o"><-</span><span class="w"> </span><span class="n">data</span><span class="w"> </span><span class="o">%>%</span><span class="w"> 
  </span><span class="n">filter</span><span class="p">(</span><span class="n">Year</span><span class="w"> </span><span class="o">!=</span><span class="w"> </span><span class="s2">"1981"</span><span class="p">)</span><span class="w"> </span><span class="o">%>%</span><span class="w"> 
  </span><span class="n">gather</span><span class="p">(</span><span class="n">key</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="n">age_group</span><span class="p">,</span><span class="w"> </span><span class="n">value</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="s2">"maternities"</span><span class="p">,</span><span class="w"> </span><span class="o">-</span><span class="n">Year</span><span class="p">)</span><span class="w">

</span><span class="n">long_data</span><span class="o">$</span><span class="n">Year</span><span class="w"> </span><span class="o"><-</span><span class="w"> </span><span class="nf">as.numeric</span><span class="p">(</span><span class="n">long_data</span><span class="o">$</span><span class="n">Year</span><span class="p">)</span><span class="w">
</span><span class="n">long_data</span><span class="o">$</span><span class="n">age_group</span><span class="w"> </span><span class="o"><-</span><span class="w"> </span><span class="n">forcats</span><span class="o">::</span><span class="n">as_factor</span><span class="p">(</span><span class="n">long_data</span><span class="o">$</span><span class="n">age_group</span><span class="p">)</span><span class="w">
</span><span class="n">long_data</span><span class="o">$</span><span class="n">maternities</span><span class="w"> </span><span class="o"><-</span><span class="w"> </span><span class="nf">as.numeric</span><span class="p">(</span><span class="n">long_data</span><span class="o">$</span><span class="n">maternities</span><span class="p">)</span><span class="w">


</span><span class="n">ggplot</span><span class="p">(</span><span class="n">long_data</span><span class="p">,</span><span class="n">aes</span><span class="p">(</span><span class="n">Year</span><span class="p">,</span><span class="w"> </span><span class="n">maternities</span><span class="p">),</span><span class="w"> </span><span class="n">group</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="n">age_group</span><span class="p">)</span><span class="w"> </span><span class="o">+</span><span class="w">
  </span><span class="n">geom_line</span><span class="p">()</span><span class="w"> </span><span class="o">+</span><span class="w">
  </span><span class="n">geom_point</span><span class="p">()</span><span class="w"> </span><span class="o">+</span><span class="w">
  </span><span class="n">facet_wrap</span><span class="p">(</span><span class="n">vars</span><span class="p">(</span><span class="n">age_group</span><span class="p">),</span><span class="w"> </span><span class="n">scales</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="s2">"free_y"</span><span class="p">)</span><span class="w"> </span><span class="o">+</span><span class="w">
  </span><span class="n">ggtitle</span><span class="p">(</span><span class="n">label</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="s2">"England and Wales maternities with multiple births - numbers"</span><span class="p">,</span><span class="w">
          </span><span class="n">subtitle</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="s2">"By age of mother, 1940 to 2016"</span><span class="p">)</span><span class="w"> </span><span class="o">+</span><span class="w">
  </span><span class="n">labs</span><span class="p">(</span><span class="n">x</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="kc">NULL</span><span class="p">,</span><span class="w"> </span><span class="n">y</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="s2">"Multiple maternities"</span><span class="p">)</span><span class="w">

</span>

2018-12-13-counts-common.png

2018-12-13-counts-free.png

<span class="c1"># Let's do rates</span><span class="w">

</span><span class="n">rates</span><span class="w"> </span><span class="o"><-</span><span class="w"> </span><span class="n">read_xls</span><span class="p">(</span><span class="s2">"birthcharacteristicsworkbook2016.xls"</span><span class="p">,</span><span class="w"> </span><span class="n">sheet</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="s2">"Table 11"</span><span class="p">,</span><span class="w">
                 </span><span class="n">range</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="s2">"A89:I166"</span><span class="p">)</span><span class="w">

</span><span class="n">rates</span><span class="w"> </span><span class="o"><-</span><span class="w"> </span><span class="n">rates</span><span class="w"> </span><span class="o">%>%</span><span class="w"> 
  </span><span class="n">rename</span><span class="p">(</span><span class="n">Year</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="n">X__1</span><span class="p">,</span><span class="w">
         </span><span class="n">All_ages</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="n">`All maternities with multiple births per 1,000 all maternities`</span><span class="p">,</span><span class="w">
         </span><span class="n">Under20</span><span class="w">  </span><span class="o">=</span><span class="w"> </span><span class="n">X__2</span><span class="p">,</span><span class="w">
         </span><span class="n">`20_to_24`</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="n">X__3</span><span class="p">,</span><span class="w">
         </span><span class="n">`25_to_29`</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="n">X__4</span><span class="p">,</span><span class="w">
         </span><span class="n">`30_to_34`</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="n">X__5</span><span class="p">,</span><span class="w">
         </span><span class="n">`35_to_39`</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="n">X__6</span><span class="p">,</span><span class="w">
         </span><span class="n">`40_to_44`</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="n">X__7</span><span class="p">,</span><span class="w">
         </span><span class="n">`45_and_over`</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="n">X__8</span><span class="p">)</span><span class="w">

</span><span class="n">long_rates</span><span class="w"> </span><span class="o"><-</span><span class="w"> </span><span class="n">rates</span><span class="w"> </span><span class="o">%>%</span><span class="w"> 
  </span><span class="n">filter</span><span class="p">(</span><span class="n">Year</span><span class="w"> </span><span class="o">!=</span><span class="w"> </span><span class="m">1981</span><span class="p">)</span><span class="w"> </span><span class="o">%>%</span><span class="w"> 
  </span><span class="n">gather</span><span class="p">(</span><span class="n">key</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="n">age_group</span><span class="p">,</span><span class="w"> </span><span class="n">value</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="s2">"multiple_maternities_per_1000"</span><span class="p">,</span><span class="w"> </span><span class="o">-</span><span class="n">Year</span><span class="p">)</span><span class="w">

</span><span class="n">long_rates</span><span class="o">$</span><span class="n">Year</span><span class="w"> </span><span class="o"><-</span><span class="w"> </span><span class="nf">as.numeric</span><span class="p">(</span><span class="n">long_rates</span><span class="o">$</span><span class="n">Year</span><span class="p">)</span><span class="w">
</span><span class="n">long_rates</span><span class="o">$</span><span class="n">age_group</span><span class="w"> </span><span class="o"><-</span><span class="w"> </span><span class="n">forcats</span><span class="o">::</span><span class="n">as_factor</span><span class="p">(</span><span class="n">long_rates</span><span class="o">$</span><span class="n">age_group</span><span class="p">)</span><span class="w">
</span><span class="n">long_rates</span><span class="o">$</span><span class="n">multiple_maternities_per_1000</span><span class="w"> </span><span class="o"><-</span><span class="w"> </span><span class="nf">as.numeric</span><span class="p">(</span><span class="n">long_rates</span><span class="o">$</span><span class="n">multiple_maternities_per_1000</span><span class="p">)</span><span class="w">

</span><span class="n">ggplot</span><span class="p">(</span><span class="n">long_rates</span><span class="p">,</span><span class="n">aes</span><span class="p">(</span><span class="n">Year</span><span class="p">,</span><span class="w"> </span><span class="n">multiple_maternities_per_1000</span><span class="p">),</span><span class="w"> </span><span class="n">group</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="n">age_group</span><span class="p">)</span><span class="w"> </span><span class="o">+</span><span class="w">
  </span><span class="n">geom_line</span><span class="p">()</span><span class="w"> </span><span class="o">+</span><span class="w">
  </span><span class="n">geom_point</span><span class="p">()</span><span class="w"> </span><span class="o">+</span><span class="w">
  </span><span class="n">facet_wrap</span><span class="p">(</span><span class="n">vars</span><span class="p">(</span><span class="n">age_group</span><span class="p">))</span><span class="w"> </span><span class="o">+</span><span class="w">
  </span><span class="n">ggtitle</span><span class="p">(</span><span class="n">label</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="s2">"England and Wales Rate of maternities with multiple births  - per 1,000 all maternities "</span><span class="p">,</span><span class="w">
          </span><span class="n">subtitle</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="s2">"By age of mother, 1940 to 2016"</span><span class="p">)</span><span class="w"> </span><span class="o">+</span><span class="w">
  </span><span class="n">labs</span><span class="p">(</span><span class="n">x</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="kc">NULL</span><span class="p">,</span><span class="w"> </span><span class="n">y</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="s2">"Multiple maternities"</span><span class="p">)</span><span class="w">

</span>

When we look at maternities with multiple births as a rate per 1000 maternities, we see the increase in multiple births among older mothers, especially in the over 45 group.
2018-12-13-rates-common.png

Again, with free scales on the y axis – which helps us see almost all age groups are exhibiting an increase – compare the 20-24 age group as a rate and as count for example.

2018-12-13-rates-free.png

Looks to me that overall, the rate of multiple births is increasing.
What’s driving this?
Can it continue?
Will people ever stop asking us if the twins are identical?

arnie.jpg

To leave a comment for the author, please follow the link and comment on their blog: HighlandR.

R-bloggers.com offers daily e-mail updates about R news and tutorials about learning R and many other topics. Click here if you're looking to post or find an R/data-science job.
Want to share your content on R-bloggers? click here if you have a blog, or here if you don't.

Never miss an update!
Subscribe to R-bloggers to receive
e-mails with the latest R posts.
(You will not see this message again.)

Click here to close (This popup will not appear again)