<p><div><div><div><div data-width="100" data-show-faces="false" data-send="true" data-layout="button_count" data-height="21" data-href="https://www.r-bloggers.com/visualising-relationships-between-childrens-books/"></div></div><div></div></div><div></div></div>
<div>
(This article was first published on <strong><a href="http://ellisp.github.io/blog/2017/03/04/childrens-books"> Peter's stats stuff - R</a></strong>, and kindly contributed to <a rel="nofollow" href="https://www.r-bloggers.com/">R-bloggers)</a> </div>
<p>At the <a target="_blank" rel="nofollow" href="http://iase-web.org/documents/anzcots/OZCOTS_2016_Proceedings.pdf">OZCOTS (Australian Conference on Teaching Statistics) in late 2016</a> George Cobb gave a great talk entitled “Ask not what data science can do for the Humanities. Ask rather, what the Humanities can do for data science.”</p>
<blockquote>
<p>“George Cobb of Mt Holyoke College gave this keynote address to open the Australian Conference on Teaching Statistics. His approach was literary, throughtful, and totally out of left field. He had a paper handout! He exhorted us to read Dickens’ <em>Bleak House</em>!”</p>
</blockquote>
<p>From <a target="_blank" rel="nofollow" href="https://aliceinstatisticsland.wordpress.com/2016/12/16/ask-not-what-data-science-can-do-for-the-humanities-ask-rather-what-the-humanities-can-do-for-data-science/">ALICEINSTATISTICSLAND</a></p>
<p>One of my takeouts from that talk was that for all our modern whizz-bang tools, applied statistics is a long way from being able to analyse much of the power of literature; this doesn’t mean we shouldn’t apply statistical techniques to text, but we should do it humbly, always thinking of context, and with an eye to improving statistical understanding (which is still taking baby steps in this space) rather than an arrogant view that it is easy to add insight to traditional critical methods.</p>
<p>I found myself thinking along similar lines recently when trying to articulate common elements in literature I’d read as a child and that still stuck with me. For example, the theme of four related, self-reliant children in exciting circumstances is common to Arthur Ransome’s <em>Swallows and Amazons</em>, Enid Blyton’s <em>Famous Five</em> and C. S. Lewis’ <em>The Lion, the Witch and the Wardrobe</em>, while all of these books seem to me to be influenced by works of Edith Nesbit like <em>Five Children and It</em>. But danger and fighting bad people differentiate the Famous Five and the children in Narnia from the Swallows and Amazons’ playful exploration of nature in the Lake District, and the exploratory tone of <em>Five Children and It</em>. And Nesbit and Lewis’ children are in a magical world whereas the other two are in more mundane reality (relatively speaking – they still seem to get plenty of excitement).</p>
<p>At one point I was thinking of using statistics to try to understand the books better, then I realised it was as much a chance to understand statistics better by applying them to well known books. I frequently come across a problem of looking for pattern in high dimensional data and being unsure how successful methods like cluster analysis or principal components analysis have been. Perhaps by applying those methods to books where I felt I had an intuitive feel of the structure I might learn something about the analytical methods? Well maybe. If nothing else, it’s an excuse for drawing some charts.</p>
<h2>Defining and documenting one’s own subjective views</h2>
<p>To create a dataset for this, I constructed a set of thematic elements in which I was interested such as (incomplete list):</p>
<ul>
<li>Boarding school</li>
<li>Fantasy or magic</li>
<li>Summer holidays</li>
<li>Hidden alternative world</li>
<li>Meant to be funny</li>
</ul>
<p>I classified a bunch of my favourite children’s books (defined loosely and inarticulately) against whether they included each element. This rating process felt a bit unreliable, not least because generally I was operating on relatively distant memory. The choice of books was necessarily arbitrary and is basically those I enjoyed at a particular age (too old for <em>The Very Hungry Caterpillar</em>, too young for <em>War and Peace</em>), or in a few cases discovered recently and think I would have enjoyed if I’d known about them. They very much reflect my particular history and background culture, which is clearly to be expected.</p>
<p>You can see the full set of 23 thematic elements, 46 books, and the 1/0 assessments in this <a target="_blank" rel="nofollow" href="http://ellisp.github.io/data/childrens-books.xlsx">Excel workbook</a>.</p>
<p>The whole process, particularly the choice of thematic elements to highlight, is clearly arbitrary and subjective, but in a sense that is the whole point. In this exercise, I’m seeking to define and articulate a bunch of things of interest <em>to me</em> about books that <em>I find interesting</em>, with the intent of applying statistical exploratory techniques and seeing if they identify patterns that <em>ring true to me</em>. In doing this I’m hoping to get a visualisation of the patterns as I see them, and learn something about data exploration while I’m at it. It’s not about recovering some objective structure but about articulating my own subjective views.</p>
<h2>Cluster analysis</h2>
<p>One method I tried was the “DIvisive ANAlysis clustering” algorithm, or “DIANA”. This works by dividing the observations (in this case, books) repeatedly into smaller and smaller groups, based on their similarity as recorded in my judgements against the thematic elements. It has the advantage of coming with an intuitive (to me) display method in the form of a tree or <a target="_blank" rel="nofollow" href="https://en.wikipedia.org/wiki/Dendrogram">dendrogram</a>. <code>diana</code> is implemented in the standard <code>cluster</code> R package, and plot polish can be added with the <code>dendextend</code> package.</p>
<p><img width="100%" src="http://ellisp.github.io/img/0083-dendro1.svg"></p>
<p>Some of the clustering interpretation is straightforward and does indeed match my intuitions:</p>
<ul>
<li>At the top of the diagram we see a set of “siblings / cousins exploring stuff” – <em>Swallows and Amazons</em>, <em>Famous Five</em> and friends.</li>
<li>Below that we see a set of relatively serious and at times violent books – most of the four categories into which I divided Biggles’ adventures; <em>The Hobbit</em>; and <em>The Black Cauldron</em>. Asterix, Tintin and the Hunt Brothers’ exploits in <em>Amazon Adventure</em> and sequels are offshoots of this branch.</li>
<li>The bottom cluster – from <em>The Cat in the Hat</em> and friends to Edward Lear’s <em>A Book of Nonsense</em> is obviously the humorous group.</li>
<li><em>The Little Prince</em>, <em>Charlotte’s Web</em>, <em>The Secret Garden</em> and <em>Ann of Green Gables</em> form another relatively serious chunk, with a focus on relationships, responsibilities and friendship.</li>
</ul>
<p>And so on.</p>
<p>I was a little surprised that there wasn’t a tighter “boarding school” cluster: P. G. Wodehouse’s <em>Tales of St Austin’s</em> (in which I mentally included all his boarding school stories), Rowling’s <em>Harry Potter</em>, are distant from <em>Tom Brown’s Schooldays</em>, <em>Molesworth</em> and Blyton’s <em>Malory Towers</em> and <em>Naughtiest girl in the school</em> with a wide variety of other elements (such as humour, magic, earnest muscular Christianity, progressivism – listed in no particular order!), apparently largely orthogonal to the boarding school setting.</p>
<p>The “hidden, secret world” dimension is another that didn’t seem to feature prominently in the clustering, with books as diverse as <em>Alice in Wonderland</em>, <em>Narnia</em> and <em>The Enchanted Wood</em> including this idea but not grouping together.</p>
<p>Here are some alternative visualisations of the same DIANA clustering; two variants on the sort of trees more usually used for phylogenetics, courtesy of the <code>ape</code> package. First an “unrooted” version:</p>
<p><img width="100%" src="http://ellisp.github.io/img/0083-dendro2.svg"></p>
<p>And second a “fan” version:</p>
<p><img width="100%" src="http://ellisp.github.io/img/0083-dendro3.svg"></p>
<p>Generally, I think I prefer the very first dendrogram, which used the “triangle” method to draw the edges.</p>
<h2>Principal components</h2>
<p>A second visualisation method I tried was a biplot of the first two principal components. These plots aim to show the contribution of variables to the two aritificial dimensions that summarise the most variance possible, and allow a reasonably intuitive connection between units of observation (in this case books) and the contributing factors. Here’s the result in our case:</p>
<p><img width="100%" src="http://ellisp.github.io/img/0083-pc.svg"></p>
<p>I have a soft spot for these sorts of charts, as a valiant effort to reduce high dimensional data to the flat page. If the first two principal components explain a good chunk of the variance in the data they can be very powerful. But in this case only 25 percent of the variance is explained by the first two components, so we get a very incomplete picture; seen in the way that the clusterings visible in the dendrogram are not obvious in this chart. The graphic is an interesting one and reveals a number of the patterns I’d expected (<em>Secret Seven</em> and <em>Famous Five</em> very similar; strong “boarding school” dimension; “talking animals” and “fighting crime” pointing in different directions and essentially not appearing in the same books). But it’s definitely less comprehensive.</p>
<h2>Other</h2>
<p>I experimented with some network graphs but they didn’t work well, or add any insight to the above methods.</p>
<p>Oh, if you haven’t encountered Edith Nesbit but are interested at all in this sort of thing, check out this <a target="_blank" rel="nofollow" href="http://www.nybooks.com/articles/1964/12/03/the-writing-of-e-nesbit/">great 1964 article in the New York Review of Books by Gore Vidal</a>.</p>
<h2>Code</h2>
<p>Here’s the R code that produces the above. Pretty simple today.</p>
<figure>
<pre><code data-lang="r"><span>library</span><span>(</span><span>tidyverse</span><span>)</span><span>
</span><span>library</span><span>(</span><span>xlsx</span><span>)</span><span>
</span><span>library</span><span>(</span><span>cluster</span><span>)</span><span>
</span><span>library</span><span>(</span><span>ape</span><span>)</span><span>
</span><span>library</span><span>(</span><span>dendextend</span><span>)</span><span> </span><span>download.file</span><span>(</span><span>&quot;http://ellisp.github.io/data/childrens-books.xlsx&quot;</span><span>,</span><span> </span><span>destfile</span><span> </span><span>=</span><span> </span><span>&quot;tmp.xlsx&quot;</span><span>,</span><span> </span><span>mode</span><span> </span><span>=</span><span> </span><span>&quot;wb&quot;</span><span>)</span><span>
</span><span>books_orig</span><span> </span><span>&lt;-</span><span> </span><span>read.xlsx</span><span>(</span><span>&quot;tmp.xlsx&quot;</span><span>,</span><span> </span><span>sheetIndex</span><span> </span><span>=</span><span> </span><span>1</span><span>,</span><span> </span><span>check.names</span><span> </span><span>=</span><span> </span><span>FALSE</span><span>,</span><span> </span><span>stringsAsFactors</span><span> </span><span>=</span><span> </span><span>FALSE</span><span>)</span><span>
</span><span>unlink</span><span>(</span><span>&quot;tmp.xlsx&quot;</span><span>)</span><span>
</span><span>books_thin</span><span> </span><span>&lt;-</span><span> </span><span>books_orig</span><span> </span><span>%>%</span><span>
</span><span>gather</span><span>(</span><span>Trope</span><span>,</span><span> </span><span>Present</span><span>,</span><span> </span><span>-</span><span>Author</span><span>,</span><span> </span><span>-</span><span>Title</span><span>)</span><span> </span><span>%>%</span><span>
</span><span>filter</span><span>(</span><span>!</span><span>is.na</span><span>(</span><span>Present</span><span>))</span><span> </span><span>%>%</span><span>
</span><span>as_tibble</span><span>()</span><span> </span><span>%>%</span><span>
</span><span>mutate</span><span>(</span><span>Present</span><span> </span><span>=</span><span> </span><span>1</span><span>)</span><span>
</span><span>books_wide</span><span> </span><span>&lt;-</span><span> </span><span>books_thin</span><span> </span><span>%>%</span><span>
</span><span>select</span><span>(</span><span>-</span><span>Author</span><span>)</span><span> </span><span>%>%</span><span>
</span><span>spread</span><span>(</span><span>Trope</span><span>,</span><span> </span><span>Present</span><span>,</span><span> </span><span>fill</span><span> </span><span>=</span><span> </span><span>0</span><span>)</span><span>
</span><span>books_mat</span><span> </span><span>&lt;-</span><span> </span><span>books_wide</span><span> </span><span>%>%</span><span>
</span><span>select</span><span>(</span><span>-</span><span>Title</span><span>)</span><span> </span><span>%>%</span><span>
</span><span>as.matrix</span><span>()</span><span>
</span><span>rownames</span><span>(</span><span>books_mat</span><span>)</span><span> </span><span>&lt;-</span><span> </span><span>books_wide</span><span>$</span><span>Title</span><span>
</span><span>#---------cluster analysis-------
</span><span>books_d</span><span> </span><span>&lt;-</span><span> </span><span>daisy</span><span>(</span><span>books_mat</span><span>)</span><span>
</span><span>cld</span><span> </span><span>&lt;-</span><span> </span><span>as.dendrogram</span><span>(</span><span>diana</span><span>(</span><span>books_d</span><span>))</span><span>
</span><span># Three versions of the dendrogram:
</span><span>
</span><span># need to use hang.dendrogram to keep the hanging raggedness
</span><span>par</span><span>(</span><span>mar</span><span> </span><span>=</span><span> </span><span>c</span><span>(</span><span>5</span><span>,</span><span>4</span><span>,</span><span>4</span><span>,</span><span>7</span><span>),</span><span> </span><span>font.main</span><span> </span><span>=</span><span> </span><span>1</span><span>)</span><span>
</span><span>par</span><span>(</span><span>bg</span><span> </span><span>=</span><span> </span><span>&quot;grey99&quot;</span><span>)</span><span>
</span><span>plot</span><span>(</span><span>hang.dendrogram</span><span>(</span><span>cld</span><span>),</span><span> </span><span>horiz</span><span> </span><span>=</span><span> </span><span>TRUE</span><span>,</span><span> </span><span>yaxt</span><span> </span><span>=</span><span> </span><span>&quot;n&quot;</span><span>,</span><span> </span><span>type</span><span> </span><span>=</span><span> </span><span>&quot;triangle&quot;</span><span>,</span><span>
</span><span>xlim</span><span> </span><span>=</span><span> </span><span>c</span><span>(</span><span>4.5</span><span>,</span><span> </span><span>-1</span><span>),</span><span> </span><span>edgePar</span><span> </span><span>=</span><span> </span><span>list</span><span>(</span><span>col</span><span> </span><span>=</span><span> </span><span>&quot;steelblue&quot;</span><span>),</span><span>
</span><span>main</span><span> </span><span>=</span><span> </span><span>&quot;Selected childrens books or series, clustered by thematic elements&quot;</span><span>)</span><span>
</span><span>par</span><span>(</span><span>mar</span><span> </span><span>=</span><span> </span><span>c</span><span>(</span><span>1</span><span>,</span><span>1</span><span>,</span><span>1</span><span>,</span><span>1</span><span>))</span><span>
</span><span>par</span><span>(</span><span>bg</span><span> </span><span>=</span><span> </span><span>&quot;grey99&quot;</span><span>)</span><span>
</span><span>plot</span><span>(</span><span>as.phylo</span><span>(</span><span>cld</span><span>),</span><span> </span><span>type</span><span> </span><span>=</span><span> </span><span>&quot;unrooted&quot;</span><span>,</span><span> </span><span>cex</span><span> </span><span>=</span><span> </span><span>0.6</span><span>)</span><span> </span><span># maybe
</span><span>
</span><span>par</span><span>(</span><span>bg</span><span> </span><span>=</span><span> </span><span>&quot;grey99&quot;</span><span>)</span><span>
</span><span>plot</span><span>(</span><span>as.phylo</span><span>(</span><span>cld</span><span>),</span><span> </span><span>type</span><span> </span><span>=</span><span> </span><span>&quot;fan&quot;</span><span>,</span><span> </span><span>cex</span><span> </span><span>=</span><span> </span><span>0.6</span><span>)</span><span> </span><span># maybe
</span><span>
</span><span>#-----------principle components-------------
</span><span>books_pc</span><span> </span><span>&lt;-</span><span> </span><span>prcomp</span><span>(</span><span>books_mat</span><span>,</span><span> </span><span>scale</span><span> </span><span>=</span><span> </span><span>TRUE</span><span>)</span><span>
</span><span>par</span><span>(</span><span>family</span><span> </span><span>=</span><span> </span><span>&quot;myfont&quot;</span><span>)</span><span>
</span><span>par</span><span>(</span><span>bg</span><span> </span><span>=</span><span> </span><span>&quot;grey99&quot;</span><span>)</span><span>
</span><span>biplot</span><span>(</span><span>books_pc</span><span>,</span><span> </span><span>choices</span><span> </span><span>=</span><span> </span><span>1</span><span>:</span><span>2</span><span>,</span><span> </span><span>col</span><span> </span><span>=</span><span> </span><span>c</span><span>(</span><span>&quot;darkblue&quot;</span><span>,</span><span> </span><span>&quot;grey75&quot;</span><span>),</span><span> </span><span>pc.biplot</span><span> </span><span>=</span><span> </span><span>TRUE</span><span>)</span></code></pre>
</figure>
<p>
<div>
<div>To <strong>leave a comment</strong> for the author, please follow the link and comment on their blog: <strong><a href="http://ellisp.github.io/blog/2017/03/04/childrens-books"> Peter's stats stuff - R</a></strong>.</div>
<hr>
<a rel="nofollow" href="https://www.r-bloggers.com/">R-bloggers.com</a> offers <strong><a rel="nofollow" href="https://feedburner.google.com/fb/a/mailverify?uri=RBloggers">daily e-mail updates</a></strong> about <a title="The R Project for Statistical Computing" rel="nofollow" href="https://www.r-project.org/">R</a> news and <a title="R tutorials" rel="nofollow" href="https://www.r-bloggers.com/search/tutorial">tutorials</a> on topics such as: <a title="Data science" rel="nofollow" href="https://www.r-bloggers.com/search/data%20science">Data science</a>, <a title="Big Data" rel="nofollow" href="https://www.r-bloggers.com/search/Big%20Data">Big Data, <a title="R jobs" rel="nofollow" href="https://www.r-users.com/">R jobs</a>, visualization (<a title="ggplot and ggplot2 tutorials" rel="nofollow" href="https://www.r-bloggers.com/search/ggplot2">ggplot2</a>, <a title="Boxplots using lattice and ggplot2 tutorials" rel="nofollow" href="https://www.r-bloggers.com/search/boxplot">Boxplots</a>, <a title="Maps and gis" rel="nofollow" href="https://www.r-bloggers.com/search/map">maps</a>, <a title="Animation in R" rel="nofollow" href="https://www.r-bloggers.com/search/animation">animation</a>), programming (<a title="RStudio IDE for R" rel="nofollow" href="https://www.r-bloggers.com/search/RStudio">RStudio</a>, <a title="Sweave and literate programming" rel="nofollow" href="https://www.r-bloggers.com/search/sweave">Sweave</a>, <a title="LaTeX in R" rel="nofollow" href="https://www.r-bloggers.com/search/LaTeX">LaTeX</a>, <a title="SQL and databases" rel="nofollow" href="https://www.r-bloggers.com/search/SQL">SQL</a>, <a title="Eclipse IDE for R" rel="nofollow" href="https://www.r-bloggers.com/search/eclipse">Eclipse</a>, <a title="git and github, Version Control System" rel="nofollow" href="https://www.r-bloggers.com/search/git">git</a>, <a title="Large data in R using Hadoop" rel="nofollow" href="https://www.r-bloggers.com/search/hadoop">hadoop</a>, <a title="Web Scraping of google, facebook, yahoo, twitter and more using R" rel="nofollow" href="https://www.r-bloggers.com/search/Web+Scraping">Web Scraping</a>) statistics (<a title="Regressions and ANOVA analysis tutorials" rel="nofollow" href="https://www.r-bloggers.com/search/regression">regression</a>, <a title="principal component analysis tutorial" rel="nofollow" href="https://www.r-bloggers.com/search/PCA">PCA</a>, <a title="Time series" rel="nofollow" href="https://www.r-bloggers.com/search/time+series">time series</a>, <a title="finance trading" rel="nofollow" href="https://www.r-bloggers.com/search/trading">trading</a>) and more...
<img height="1" alt="" width="1" src="http://feeds.feedburner.com/~r/RBloggers/~4/s7zUHgJs4Ng"></a></div>