{"id":5945,"date":"2020-05-19T20:59:58","date_gmt":"2020-05-19T18:59:58","guid":{"rendered":"https:\/\/www.aaa-riskfinance.nl\/publicaties\/chaining-in-python-and-r\/"},"modified":"2020-05-19T23:01:45","modified_gmt":"2020-05-19T21:01:45","slug":"chaining-in-python-and-r","status":"publish","type":"post","link":"https:\/\/www.aaa-riskfinance.nl\/en\/publicaties\/chaining-in-python-and-r\/","title":{"rendered":"Chaining in Python and R"},"content":{"rendered":"<p>We used to think that the main objective of programming is to communicate our ideas to the machine. Nowadays, we realize that programming also has to do with communicating ideas to other developers. That\u2019s where clean code comes into\u00a0play.<\/p>\n<p>In actuality, the vast majority for the cost of code isn\u2019t in initial development, it is maintenance over the long-term. In software engineering there is the concept of \u201cmodifiability\u201d which encompasses the idea that quality code is easily modified and\u00a0extended.<\/p>\n<p>Both Python and R support syntax constructs for writing code that can be easily understood by other developers, including using good variable names and coding styles to achieve \u201cself documenting code\u201d like the chaining construct we will discuss in this post. Self documenting code is essential as sooner or later will come the day other developers will either decide to abandon or maintain your\u00a0code.<\/p>\n<h2>Example<\/h2>\n<p>Let\u2019s assume you work for an insurance company selling its products through agents. Each agent is paid a commission, calculated as an agent specific percentage of written\u00a0premiums.<\/p>\n<p>You are asked to produce a quarterly report containing this bar chart summarizing the commission in dollars your insurance company has to pay each agent for their online\u00a0sales.<\/p>\n<p><img decoding=\"async\" class=\"img-responsive lazyload\" data-src=\"http:\/\/actuarialdatascience.com\/images\/art23-commission-per-agent-R.jpeg\" alt=\"provision-per-agent\" src=\"data:image\/svg+xml;base64,PHN2ZyB3aWR0aD0iMSIgaGVpZ2h0PSIxIiB4bWxucz0iaHR0cDovL3d3dy53My5vcmcvMjAwMC9zdmciPjwvc3ZnPg==\" \/><\/p>\n<p>What do the data look like? You have two data tables at your disposal. In the premium table (25,184 rows), each record represents a premium booking classified by channel and agent. The commission table (12 rows) features the brokerage \u2013 expressed as a percentage of premiums &#8211; for each\u00a0agent.<\/p>\n<p><img decoding=\"async\" class=\"img-responsive lazyload\" data-src=\"http:\/\/actuarialdatascience.com\/images\/art23-premium-and-comission-table.jpg\" alt=\"premium-and-commission-table\" src=\"data:image\/svg+xml;base64,PHN2ZyB3aWR0aD0iMSIgaGVpZ2h0PSIxIiB4bWxucz0iaHR0cDovL3d3dy53My5vcmcvMjAwMC9zdmciPjwvc3ZnPg==\" \/><\/p>\n<h2>Chain of data\u00a0transformations<\/h2>\n<p>Taking one step back, we realize that producing the bar chart involves a chain of subsequent data transformations, where the output of each step is the input for the\u00a0next:<\/p>\n<ol>\n<li><strong>filter<\/strong>\u00a0the premium table on sales channel\u00a0\u201conline\u201d;<\/li>\n<li><strong>merge<\/strong>\u00a0the premium table with the commission table on\u00a0agent;<\/li>\n<li>for each premium,\u00a0<strong>calculate<\/strong>\u00a0the agent\u2019s\u00a0commission;<\/li>\n<li>in the combined table,\u00a0<strong>group<\/strong>\u00a0the commissions by\u00a0agent;<\/li>\n<li>for each group,\u00a0<strong>sum<\/strong>\u00a0the\u00a0commissions;<\/li>\n<li><strong>sort<\/strong>\u00a0the commission paid; (yes, we want the agent receiving the highest commission on top of the bar\u00a0chart)<\/li>\n<li><strong>plot<\/strong>\u00a0the results as a bar\u00a0chart.<\/li>\n<\/ol>\n<p>Once we have this chain of data transformations in place, we can produce the bar chart our boss asked for with just one line (or \u201cchain\u201d) of\u00a0code.<\/p>\n<h2>Chaining in\u00a0Python<\/h2>\n<p>In Python, the data transformations described above are chained through the dot operator, using the pandas\u00a0library:<\/p>\n<p><img decoding=\"async\" class=\"img-responsive lazyload\" data-src=\"http:\/\/actuarialdatascience.com\/images\/art23Pythonchain.jpg\" alt=\"Pythonchain\" src=\"data:image\/svg+xml;base64,PHN2ZyB3aWR0aD0iMSIgaGVpZ2h0PSIxIiB4bWxucz0iaHR0cDovL3d3dy53My5vcmcvMjAwMC9zdmciPjwvc3ZnPg==\" \/><\/p>\n<h2>Chaining in\u00a0R<\/h2>\n<p>In R, the data transformations described above are chained through the forward pipe operator %&gt;%, using the tidyverse\u00a0package:<\/p>\n<p><img decoding=\"async\" class=\"img-responsive lazyload\" data-src=\"http:\/\/actuarialdatascience.com\/images\/art23Rchain.jpg\" alt=\"Rchain\" src=\"data:image\/svg+xml;base64,PHN2ZyB3aWR0aD0iMSIgaGVpZ2h0PSIxIiB4bWxucz0iaHR0cDovL3d3dy53My5vcmcvMjAwMC9zdmciPjwvc3ZnPg==\" \/><\/p>\n<h2>Conclusion<\/h2>\n<p>Chaining is a powerful construct supported both by Python and R. Chaining allows you to express the idea of data flowing through a pipeline almost 1-to-1 into your code, making your code more readable both for yourself and for other developers. Feel free to try it yourself, code and data can be found\u00a0<a href=\"https:\/\/github.com\/Oxylo\/actuarialdatascience\/tree\/master\/art23\" target=\"_blank\" rel=\"noopener noreferrer\">here<\/a>.<\/p>\n<p><em>Acknowledgements<\/em>: I would like to express my gratitude to\u00a0<a href=\"https:\/\/www.linkedin.com\/in\/davelanger\/\" target=\"_blank\" rel=\"noopener noreferrer\">David Langer<\/a>, hands-on writer and instructor on data analytics, for his valuable comments on this\u00a0post.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Chaining is a powerful construct supported both by Python and R. Feel free to try it yourself, code and data can be found\u00a0here.<\/p>\n","protected":false},"author":2,"featured_media":2426,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"_acf_changed":false,"footnotes":""},"categories":[26,6],"tags":[65,66,67,68],"class_list":["post-5945","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-news","category-publications","tag-data-en","tag-data-analytics-en","tag-python-en","tag-r-en"],"acf":[],"yoast_head":"<!-- This site is optimized with the Yoast SEO plugin v28.0 - https:\/\/yoast.com\/product\/yoast-seo-wordpress\/ -->\n<title>Chaining in Python and R | Triple A - Risk Finance<\/title>\n<meta name=\"description\" content=\"Chaining is a powerful construct supported both by Python and R. Feel free to try it yourself, code and data can be found\u00a0here.\" \/>\n<meta name=\"robots\" content=\"index, follow, max-snippet:-1, max-image-preview:large, max-video-preview:-1\" \/>\n<link rel=\"canonical\" href=\"https:\/\/www.aaa-riskfinance.nl\/en\/publicaties\/chaining-in-python-and-r\/\" \/>\n<meta property=\"og:locale\" content=\"en_US\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"Chaining in Python and R | Triple A - Risk Finance\" \/>\n<meta property=\"og:description\" content=\"Chaining is a powerful construct supported both by Python and R. Feel free to try it yourself, code and data can be found\u00a0here.\" \/>\n<meta property=\"og:url\" content=\"https:\/\/www.aaa-riskfinance.nl\/en\/publicaties\/chaining-in-python-and-r\/\" \/>\n<meta property=\"og:site_name\" content=\"AAA Riskfinance\" \/>\n<meta property=\"article:published_time\" content=\"2020-05-19T18:59:58+00:00\" \/>\n<meta property=\"article:modified_time\" content=\"2020-05-19T21:01:45+00:00\" \/>\n<meta property=\"og:image\" content=\"https:\/\/www.aaa-riskfinance.nl\/app\/uploads\/2018\/02\/oplossing5.jpg\" \/>\n\t<meta property=\"og:image:width\" content=\"604\" \/>\n\t<meta property=\"og:image:height\" content=\"428\" \/>\n\t<meta property=\"og:image:type\" content=\"image\/jpeg\" \/>\n<meta name=\"author\" content=\"Sturdy Digital\" \/>\n<meta name=\"twitter:card\" content=\"summary_large_image\" \/>\n<meta name=\"twitter:label1\" content=\"Written by\" \/>\n\t<meta name=\"twitter:data1\" content=\"Sturdy Digital\" \/>\n\t<meta name=\"twitter:label2\" content=\"Est. reading time\" \/>\n\t<meta name=\"twitter:data2\" content=\"3 minutes\" \/>\n<script type=\"application\/ld+json\" class=\"yoast-schema-graph\">{\"@context\":\"https:\\\/\\\/schema.org\",\"@graph\":[{\"@type\":\"Article\",\"@id\":\"https:\\\/\\\/www.aaa-riskfinance.nl\\\/en\\\/publicaties\\\/chaining-in-python-and-r\\\/#article\",\"isPartOf\":{\"@id\":\"https:\\\/\\\/www.aaa-riskfinance.nl\\\/en\\\/publicaties\\\/chaining-in-python-and-r\\\/\"},\"author\":{\"name\":\"Sturdy Digital\",\"@id\":\"https:\\\/\\\/www.aaa-riskfinance.nl\\\/#\\\/schema\\\/person\\\/13216ef8a37612e89cb6b22f6e14e4ed\"},\"headline\":\"Chaining in Python and R\",\"datePublished\":\"2020-05-19T18:59:58+00:00\",\"dateModified\":\"2020-05-19T21:01:45+00:00\",\"mainEntityOfPage\":{\"@id\":\"https:\\\/\\\/www.aaa-riskfinance.nl\\\/en\\\/publicaties\\\/chaining-in-python-and-r\\\/\"},\"wordCount\":510,\"commentCount\":0,\"image\":{\"@id\":\"https:\\\/\\\/www.aaa-riskfinance.nl\\\/en\\\/publicaties\\\/chaining-in-python-and-r\\\/#primaryimage\"},\"thumbnailUrl\":\"https:\\\/\\\/www.aaa-riskfinance.nl\\\/app\\\/uploads\\\/2018\\\/02\\\/oplossing5.jpg\",\"keywords\":[\"data\",\"data analytics\",\"Python\",\"R\"],\"articleSection\":[\"News\",\"Publications\"],\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"CommentAction\",\"name\":\"Comment\",\"target\":[\"https:\\\/\\\/www.aaa-riskfinance.nl\\\/en\\\/publicaties\\\/chaining-in-python-and-r\\\/#respond\"]}]},{\"@type\":\"WebPage\",\"@id\":\"https:\\\/\\\/www.aaa-riskfinance.nl\\\/en\\\/publicaties\\\/chaining-in-python-and-r\\\/\",\"url\":\"https:\\\/\\\/www.aaa-riskfinance.nl\\\/en\\\/publicaties\\\/chaining-in-python-and-r\\\/\",\"name\":\"Chaining in Python and R | Triple A - Risk Finance\",\"isPartOf\":{\"@id\":\"https:\\\/\\\/www.aaa-riskfinance.nl\\\/#website\"},\"primaryImageOfPage\":{\"@id\":\"https:\\\/\\\/www.aaa-riskfinance.nl\\\/en\\\/publicaties\\\/chaining-in-python-and-r\\\/#primaryimage\"},\"image\":{\"@id\":\"https:\\\/\\\/www.aaa-riskfinance.nl\\\/en\\\/publicaties\\\/chaining-in-python-and-r\\\/#primaryimage\"},\"thumbnailUrl\":\"https:\\\/\\\/www.aaa-riskfinance.nl\\\/app\\\/uploads\\\/2018\\\/02\\\/oplossing5.jpg\",\"datePublished\":\"2020-05-19T18:59:58+00:00\",\"dateModified\":\"2020-05-19T21:01:45+00:00\",\"author\":{\"@id\":\"https:\\\/\\\/www.aaa-riskfinance.nl\\\/#\\\/schema\\\/person\\\/13216ef8a37612e89cb6b22f6e14e4ed\"},\"description\":\"Chaining is a powerful construct supported both by Python and R. Feel free to try it yourself, code and data can be found\u00a0here.\",\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"ReadAction\",\"target\":[\"https:\\\/\\\/www.aaa-riskfinance.nl\\\/en\\\/publicaties\\\/chaining-in-python-and-r\\\/\"]}]},{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\\\/\\\/www.aaa-riskfinance.nl\\\/en\\\/publicaties\\\/chaining-in-python-and-r\\\/#primaryimage\",\"url\":\"https:\\\/\\\/www.aaa-riskfinance.nl\\\/app\\\/uploads\\\/2018\\\/02\\\/oplossing5.jpg\",\"contentUrl\":\"https:\\\/\\\/www.aaa-riskfinance.nl\\\/app\\\/uploads\\\/2018\\\/02\\\/oplossing5.jpg\",\"width\":604,\"height\":428,\"caption\":\"ORSA-proces\"},{\"@type\":\"WebSite\",\"@id\":\"https:\\\/\\\/www.aaa-riskfinance.nl\\\/#website\",\"url\":\"https:\\\/\\\/www.aaa-riskfinance.nl\\\/\",\"name\":\"AAA Riskfinance\",\"description\":\"Risk Finance &amp; Data Science\",\"potentialAction\":[{\"@type\":\"SearchAction\",\"target\":{\"@type\":\"EntryPoint\",\"urlTemplate\":\"https:\\\/\\\/www.aaa-riskfinance.nl\\\/?s={search_term_string}\"},\"query-input\":{\"@type\":\"PropertyValueSpecification\",\"valueRequired\":true,\"valueName\":\"search_term_string\"}}],\"inLanguage\":\"en-US\"},{\"@type\":\"Person\",\"@id\":\"https:\\\/\\\/www.aaa-riskfinance.nl\\\/#\\\/schema\\\/person\\\/13216ef8a37612e89cb6b22f6e14e4ed\",\"name\":\"Sturdy Digital\",\"image\":{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\\\/\\\/secure.gravatar.com\\\/avatar\\\/5a763d68a1ba206cda90a2028e0997849ac4094fde5d8bc397263744b09b3919?s=96&d=mm&r=g\",\"url\":\"https:\\\/\\\/secure.gravatar.com\\\/avatar\\\/5a763d68a1ba206cda90a2028e0997849ac4094fde5d8bc397263744b09b3919?s=96&d=mm&r=g\",\"contentUrl\":\"https:\\\/\\\/secure.gravatar.com\\\/avatar\\\/5a763d68a1ba206cda90a2028e0997849ac4094fde5d8bc397263744b09b3919?s=96&d=mm&r=g\",\"caption\":\"Sturdy Digital\"},\"url\":\"https:\\\/\\\/www.aaa-riskfinance.nl\\\/en\\\/publicaties\\\/author\\\/newfountain\\\/\"}]}<\/script>\n<!-- \/ Yoast SEO plugin. -->","yoast_head_json":{"title":"Chaining in Python and R | Triple A - Risk Finance","description":"Chaining is a powerful construct supported both by Python and R. Feel free to try it yourself, code and data can be found\u00a0here.","robots":{"index":"index","follow":"follow","max-snippet":"max-snippet:-1","max-image-preview":"max-image-preview:large","max-video-preview":"max-video-preview:-1"},"canonical":"https:\/\/www.aaa-riskfinance.nl\/en\/publicaties\/chaining-in-python-and-r\/","og_locale":"en_US","og_type":"article","og_title":"Chaining in Python and R | Triple A - Risk Finance","og_description":"Chaining is a powerful construct supported both by Python and R. Feel free to try it yourself, code and data can be found\u00a0here.","og_url":"https:\/\/www.aaa-riskfinance.nl\/en\/publicaties\/chaining-in-python-and-r\/","og_site_name":"AAA Riskfinance","article_published_time":"2020-05-19T18:59:58+00:00","article_modified_time":"2020-05-19T21:01:45+00:00","og_image":[{"width":604,"height":428,"url":"https:\/\/www.aaa-riskfinance.nl\/app\/uploads\/2018\/02\/oplossing5.jpg","type":"image\/jpeg"}],"author":"Sturdy Digital","twitter_card":"summary_large_image","twitter_misc":{"Written by":"Sturdy Digital","Est. reading time":"3 minutes"},"schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":"Article","@id":"https:\/\/www.aaa-riskfinance.nl\/en\/publicaties\/chaining-in-python-and-r\/#article","isPartOf":{"@id":"https:\/\/www.aaa-riskfinance.nl\/en\/publicaties\/chaining-in-python-and-r\/"},"author":{"name":"Sturdy Digital","@id":"https:\/\/www.aaa-riskfinance.nl\/#\/schema\/person\/13216ef8a37612e89cb6b22f6e14e4ed"},"headline":"Chaining in Python and R","datePublished":"2020-05-19T18:59:58+00:00","dateModified":"2020-05-19T21:01:45+00:00","mainEntityOfPage":{"@id":"https:\/\/www.aaa-riskfinance.nl\/en\/publicaties\/chaining-in-python-and-r\/"},"wordCount":510,"commentCount":0,"image":{"@id":"https:\/\/www.aaa-riskfinance.nl\/en\/publicaties\/chaining-in-python-and-r\/#primaryimage"},"thumbnailUrl":"https:\/\/www.aaa-riskfinance.nl\/app\/uploads\/2018\/02\/oplossing5.jpg","keywords":["data","data analytics","Python","R"],"articleSection":["News","Publications"],"inLanguage":"en-US","potentialAction":[{"@type":"CommentAction","name":"Comment","target":["https:\/\/www.aaa-riskfinance.nl\/en\/publicaties\/chaining-in-python-and-r\/#respond"]}]},{"@type":"WebPage","@id":"https:\/\/www.aaa-riskfinance.nl\/en\/publicaties\/chaining-in-python-and-r\/","url":"https:\/\/www.aaa-riskfinance.nl\/en\/publicaties\/chaining-in-python-and-r\/","name":"Chaining in Python and R | Triple A - Risk Finance","isPartOf":{"@id":"https:\/\/www.aaa-riskfinance.nl\/#website"},"primaryImageOfPage":{"@id":"https:\/\/www.aaa-riskfinance.nl\/en\/publicaties\/chaining-in-python-and-r\/#primaryimage"},"image":{"@id":"https:\/\/www.aaa-riskfinance.nl\/en\/publicaties\/chaining-in-python-and-r\/#primaryimage"},"thumbnailUrl":"https:\/\/www.aaa-riskfinance.nl\/app\/uploads\/2018\/02\/oplossing5.jpg","datePublished":"2020-05-19T18:59:58+00:00","dateModified":"2020-05-19T21:01:45+00:00","author":{"@id":"https:\/\/www.aaa-riskfinance.nl\/#\/schema\/person\/13216ef8a37612e89cb6b22f6e14e4ed"},"description":"Chaining is a powerful construct supported both by Python and R. Feel free to try it yourself, code and data can be found\u00a0here.","inLanguage":"en-US","potentialAction":[{"@type":"ReadAction","target":["https:\/\/www.aaa-riskfinance.nl\/en\/publicaties\/chaining-in-python-and-r\/"]}]},{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/www.aaa-riskfinance.nl\/en\/publicaties\/chaining-in-python-and-r\/#primaryimage","url":"https:\/\/www.aaa-riskfinance.nl\/app\/uploads\/2018\/02\/oplossing5.jpg","contentUrl":"https:\/\/www.aaa-riskfinance.nl\/app\/uploads\/2018\/02\/oplossing5.jpg","width":604,"height":428,"caption":"ORSA-proces"},{"@type":"WebSite","@id":"https:\/\/www.aaa-riskfinance.nl\/#website","url":"https:\/\/www.aaa-riskfinance.nl\/","name":"AAA Riskfinance","description":"Risk Finance &amp; Data Science","potentialAction":[{"@type":"SearchAction","target":{"@type":"EntryPoint","urlTemplate":"https:\/\/www.aaa-riskfinance.nl\/?s={search_term_string}"},"query-input":{"@type":"PropertyValueSpecification","valueRequired":true,"valueName":"search_term_string"}}],"inLanguage":"en-US"},{"@type":"Person","@id":"https:\/\/www.aaa-riskfinance.nl\/#\/schema\/person\/13216ef8a37612e89cb6b22f6e14e4ed","name":"Sturdy Digital","image":{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/secure.gravatar.com\/avatar\/5a763d68a1ba206cda90a2028e0997849ac4094fde5d8bc397263744b09b3919?s=96&d=mm&r=g","url":"https:\/\/secure.gravatar.com\/avatar\/5a763d68a1ba206cda90a2028e0997849ac4094fde5d8bc397263744b09b3919?s=96&d=mm&r=g","contentUrl":"https:\/\/secure.gravatar.com\/avatar\/5a763d68a1ba206cda90a2028e0997849ac4094fde5d8bc397263744b09b3919?s=96&d=mm&r=g","caption":"Sturdy Digital"},"url":"https:\/\/www.aaa-riskfinance.nl\/en\/publicaties\/author\/newfountain\/"}]}},"_links":{"self":[{"href":"https:\/\/www.aaa-riskfinance.nl\/en\/wp-json\/wp\/v2\/posts\/5945","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.aaa-riskfinance.nl\/en\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.aaa-riskfinance.nl\/en\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.aaa-riskfinance.nl\/en\/wp-json\/wp\/v2\/users\/2"}],"replies":[{"embeddable":true,"href":"https:\/\/www.aaa-riskfinance.nl\/en\/wp-json\/wp\/v2\/comments?post=5945"}],"version-history":[{"count":1,"href":"https:\/\/www.aaa-riskfinance.nl\/en\/wp-json\/wp\/v2\/posts\/5945\/revisions"}],"predecessor-version":[{"id":5946,"href":"https:\/\/www.aaa-riskfinance.nl\/en\/wp-json\/wp\/v2\/posts\/5945\/revisions\/5946"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/www.aaa-riskfinance.nl\/en\/wp-json\/wp\/v2\/media\/2426"}],"wp:attachment":[{"href":"https:\/\/www.aaa-riskfinance.nl\/en\/wp-json\/wp\/v2\/media?parent=5945"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.aaa-riskfinance.nl\/en\/wp-json\/wp\/v2\/categories?post=5945"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.aaa-riskfinance.nl\/en\/wp-json\/wp\/v2\/tags?post=5945"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}