<h2>Seasonal air pollution in India</h2>
<p>The motivation for this blog post was a conference paper I recently heard that analysed five years of daily pollution data in an India city with a non-seasonal auto-regressive integrated moving average (ARIMA) model. In discussion after the presentation, there were differing views on whether such data should be modelled with a seasonal approach, and I wanted to look at some data to see for myself (disclaimer - I was one of the pro-seasonal camp).</p>
<p>To cut to the chase, ultimately I was looking at this sort of obviously seasonal result:</p>
<p></p>
<h2>Open Air Quality Data</h2>
<p>But first thing was to get some data. I didn’t have access to the original data but was aware of other sources, with shorter time spans, but hopefully still enough for my purposes. My first call was the <a href="https://openaq.org/#/locations?page=3&countries=AU&_k=iz4lwf">openaq Air Quality Data</a> site and in particular the excellent <code>ropenaq</code> R package which speaks to the openaq API and makes it easy to access their data. This looked very promising, but the data available are for far too short a period for me. Here is the six months or so of data which is all that is available for Ahmedabad.</p>
<p></p>
<p>For the record, here’s the R code that downloaded that data (nearly 23,000 observations - many observations per day) and drew the graph, and also set up the session for everything else done in the rest of this post</p>
<pre><code><span># Might need to uncomment out and run these next two lines if not already installed:
# devtools::install_github(&quot;Ather-Energy/ggTimeSeries&quot;)
# devtools::install_github(&quot;masalmon/usaqmindia&quot;)
</span><span>
</span><span># load up functionality
</span><span>library</span><span>(</span><span>ropenaq</span><span>)</span><span> </span><span># sourced from OpenAQ, see https://github.com/ropensci/ropenaq
</span><span>library</span><span>(</span><span>ggplot2</span><span>)</span><span>
</span><span>library</span><span>(</span><span>scales</span><span>)</span><span>
</span><span>library</span><span>(</span><span>tidyverse</span><span>)</span><span>
</span><span>library</span><span>(</span><span>usaqmindia</span><span>)</span><span> </span><span># sourced from US Air Quality Monitoring, see https://github.com/masalmon/usaqmindia
</span><span>library</span><span>(</span><span>forecastHybrid</span><span>)</span><span>
</span><span>library</span><span>(</span><span>seasonal</span><span>)</span><span> </span><span># for Diwali dates
</span><span>library</span><span>(</span><span>ggseas</span><span>)</span><span>
</span><span>library</span><span>(</span><span>ggmap</span><span>)</span><span>
</span><span>library</span><span>(</span><span>lubridate</span><span>)</span><span>
</span><span>library</span><span>(</span><span>RColorBrewer</span><span>)</span><span>
</span><span>data</span><span>(</span><span>holiday</span><span>)</span><span> </span><span># will be using this for &quot;diwali&quot;
</span><span>
</span><span>#============openaq experiment================
# based on the example on the package page on GitHub
</span><span>how_many</span><span> </span><span>&lt;-</span><span> </span><span>attr</span><span>(</span><span>aq_measurements</span><span>(</span><span>city</span><span> </span><span>=</span><span> </span><span>&quot;Ahmedabad&quot;</span><span>),</span><span> </span><span>&quot;meta&quot;</span><span>)</span><span>
</span><span>n</span><span> </span><span>&lt;-</span><span> </span><span>how_many</span><span>$</span><span>found</span><span> </span><span># unfortunately only goes back to mid 2015
</span><span>results_ls</span><span> </span><span>&lt;-</span><span> </span><span>list</span><span>()</span><span>
</span><span># Limit is 1000 observations at once, so we bring them all in 1000 at a time:
</span><span>for</span><span>(</span><span>i</span><span> </span><span>in</span><span> </span><span>1</span><span>:</span><span>ceiling</span><span>(</span><span>n</span><span> </span><span>/</span><span> </span><span>1000</span><span>)){</span><span>
</span><span>results_ls</span><span>[[</span><span>i</span><span>]]</span><span> </span><span>&lt;-</span><span> </span><span>aq_measurements</span><span>(</span><span>country</span><span> </span><span>=</span><span> </span><span>&quot;IN&quot;</span><span>,</span><span> </span><span>city</span><span> </span><span>=</span><span> </span><span>&quot;Ahmedabad&quot;</span><span>,</span><span> </span><span>date_from</span><span> </span><span>=</span><span> </span><span>&quot;2010-01-01&quot;</span><span>,</span><span> </span><span>limit</span><span> </span><span>=</span><span> </span><span>1000</span><span>,</span><span> </span><span>page</span><span> </span><span>=</span><span> </span><span>i</span><span>)</span><span> </span><span>cat</span><span>(</span><span>i</span><span>)</span><span>
</span><span>}</span><span>
</span><span># convert into a data frame:
</span><span>results_df</span><span> </span><span>&lt;-</span><span> </span><span>do.call</span><span>(</span><span>&quot;rbind&quot;</span><span>,</span><span> </span><span>results_ls</span><span>)</span><span> </span><span>%>%</span><span>
</span><span>arrange</span><span>(</span><span>dateLocal</span><span>)</span><span>
</span><span># draw exploratory graph
</span><span>results_df</span><span> </span><span>%>%</span><span>
</span><span>ggplot</span><span>(</span><span>aes</span><span>(</span><span>x</span><span> </span><span>=</span><span> </span><span>dateLocal</span><span>,</span><span> </span><span>y</span><span> </span><span>=</span><span> </span><span>value</span><span>))</span><span> </span><span>+</span><span> </span><span>facet_wrap</span><span>(</span><span>~</span><span>parameter</span><span>,</span><span> </span><span>ncol</span><span> </span><span>=</span><span> </span><span>1</span><span>,</span><span> </span><span>scales</span><span> </span><span>=</span><span> </span><span>&quot;free_y&quot;</span><span>)</span><span> </span><span>+</span><span>
</span><span>geom_line</span><span>()</span><span> </span><span>+</span><span>
</span><span>ggtitle</span><span>(</span><span>&quot;Air pollutants in Ahmedabad, 2016&quot;</span><span>)</span><span> </span><span>+</span><span>
</span><span>scale_y_continuous</span><span>(</span><span>&quot;Value&quot;</span><span>,</span><span> </span><span>label</span><span> </span><span>=</span><span> </span><span>comma</span><span>)</span><span> </span><span>+</span><span>
</span><span>labs</span><span>(</span><span>x</span><span> </span><span>=</span><span> </span><span>&quot;&quot;</span><span>,</span><span> </span><span>caption</span><span> </span><span>=</span><span> </span><span>&quot;Source: OpenAQ&quot;</span><span>)</span></code></pre><h2>US Embassy and Consulate India Air Quality Monitoring</h2>
<p>Luckily, the <a href="https://github.com/ropensci/ropenaq">GitHub page for the <code>ropenaq</code> R package</a> referenced a second R package, <a href="https://github.com/masalmon/usaqmindia"><code>usaqmindia</code></a>. This package has tidied and bundled up various source CSV and PDF files <a href="https://in.usembassy.gov/embassy-consulates/new-delhi/air-quality-data/">published by the US State Department</a> on particulate matter concentrations in five Indian cities. The R package might not be completely current, but it looks pretty good. The particulate matter data are “commonly referred to as PM 2.5 because they are less than or equal to 2.5 microns in diameter”.</p>
<p>If your Indian geography is as rusty as mine you may appreciate this map of the five cities in question:</p>
<p></p>
<p>… which was made with this code using the <code>ggmap</code> R package (yes, I looked up those latitude and longitudes and copied and pasted them by hand - there are better ways of doing this in general, but for just five cities this was simplest):</p>
<pre><code><span>#=========map of india========
</span><span>cities</span><span> </span><span>&lt;-</span><span> </span><span>data_frame</span><span>(</span><span>
</span><span>city</span><span> </span><span>=</span><span> </span><span>c</span><span>(</span><span>&quot;Chennai&quot;</span><span>,</span><span> </span><span>&quot;Delhi&quot;</span><span>,</span><span> </span><span>&quot;Hyderabad&quot;</span><span>,</span><span> </span><span>&quot;Kolkata&quot;</span><span>,</span><span> </span><span>&quot;Mumbai&quot;</span><span>),</span><span>
</span><span>lat</span><span> </span><span>=</span><span> </span><span>c</span><span>(</span><span>13.067439</span><span>,</span><span> </span><span>28.644800</span><span>,</span><span> </span><span>17.387140</span><span>,</span><span> </span><span>22.572645</span><span>,</span><span> </span><span>19.0759837</span><span>),</span><span>
</span><span>long</span><span> </span><span>=</span><span> </span><span>c</span><span>(</span><span>80.2784700</span><span>,</span><span> </span><span>77.216721</span><span>,</span><span> </span><span>78.491684</span><span>	</span><span>,</span><span> </span><span>88.363892</span><span>,</span><span>	</span><span>72.8776559</span><span>)</span><span>
</span><span>)</span><span>
</span><span>india</span><span> </span><span>&lt;-</span><span> </span><span>get_map</span><span>(</span><span>&quot;India&quot;</span><span>,</span><span> </span><span>zoom</span><span> </span><span>=</span><span> </span><span>5</span><span>,</span><span> </span><span>maptype</span><span> </span><span>=</span><span> </span><span>&quot;satellite&quot;</span><span>)</span><span>
</span><span>ggmap</span><span>(</span><span>india</span><span>)</span><span> </span><span>+</span><span>
</span><span>geom_text</span><span>(</span><span>data</span><span> </span><span>=</span><span> </span><span>cities</span><span>,</span><span> </span><span>aes</span><span>(</span><span>x</span><span> </span><span>=</span><span> </span><span>long</span><span>,</span><span> </span><span>y</span><span> </span><span>=</span><span> </span><span>lat</span><span>,</span><span> </span><span>label</span><span> </span><span>=</span><span> </span><span>city</span><span>),</span><span> </span><span>colour</span><span> </span><span>=</span><span> </span><span>&quot;white&quot;</span><span>)</span><span> </span><span>+</span><span>
</span><span>theme_nothing</span><span>()</span></code></pre><p>The original data published by the US has multiple observations for the five cities for most days, and some days missing observations altogether. It’s not easy to spot patterns with this irregular data. Here’s how it looks out of the box:</p>
<p></p>
<p>… which was created with this:</p>
<pre><code><span>#=========US Air Quality Monitoring===========
</span><span>data</span><span>(</span><span>&quot;pm25_india&quot;</span><span>)</span><span>
</span><span>pm25_india</span><span> </span><span>%>%</span><span>
</span><span>ggplot</span><span>(</span><span>aes</span><span>(</span><span>x</span><span> </span><span>=</span><span> </span><span>datetime</span><span>,</span><span> </span><span>y</span><span> </span><span>=</span><span> </span><span>conc</span><span>))</span><span> </span><span>+</span><span>
</span><span>facet_wrap</span><span>(</span><span>~</span><span>city</span><span>,</span><span> </span><span>ncol</span><span> </span><span>=</span><span> </span><span>1</span><span>)</span><span> </span><span>+</span><span>
</span><span>geom_line</span><span>()</span></code></pre><p>Those observations at concentration levels of 2000 looks suspicious for two reasons:</p>
<ul><li>they are so high compared to other levels. It’s plausible there are massive spikes some days, but it’s also plausible there’s something over-sensitive about the measuring device. I lack the subject matter expertise and resources to decide, so simply note that I might want to carefully use robust methods.</li>
<li>they are exactly 2000, which looks like some kind of truncation (ie the real values may be more than 2000, and 2000 is just the maximum value of the censors).</li>
</ul><p>To see patterns I want more regularised data. As a first go, I tried taking the trimmed mean average observation for each day. This gave me this graphic:</p>
<p></p>
<p>This is getting me somewhere. The patterns in Kolkata and Mumbai are looking plausibly regular now. This daily aggregation was made on the fly with this code:</p>
<pre><code><span># daily aggregation
</span><span>pm25_india</span><span> </span><span>%>%</span><span>
</span><span>mutate</span><span>(</span><span>day</span><span> </span><span>=</span><span> </span><span>as.Date</span><span>(</span><span>datetime</span><span>))</span><span> </span><span>%>%</span><span>
</span><span>group_by</span><span>(</span><span>day</span><span>,</span><span> </span><span>city</span><span>)</span><span> </span><span>%>%</span><span>
</span><span>summarise</span><span>(</span><span>avvalue</span><span> </span><span>=</span><span> </span><span>mean</span><span>(</span><span>conc</span><span>,</span><span> </span><span>tr</span><span> </span><span>=</span><span> </span><span>0.1</span><span>,</span><span> </span><span>na.rm</span><span> </span><span>=</span><span> </span><span>TRUE</span><span>))</span><span> </span><span>%>%</span><span>
</span><span>ggplot</span><span>(</span><span>aes</span><span>(</span><span>x</span><span> </span><span>=</span><span> </span><span>day</span><span>,</span><span> </span><span>y</span><span> </span><span>=</span><span> </span><span>avvalue</span><span>))</span><span> </span><span>+</span><span>
</span><span>facet_wrap</span><span>(</span><span>~</span><span>city</span><span>,</span><span> </span><span>ncol</span><span> </span><span>=</span><span> </span><span>1</span><span>,</span><span> </span><span>scales</span><span> </span><span>=</span><span> </span><span>&quot;free_y&quot;</span><span>)</span><span> </span><span>+</span><span>
</span><span>geom_line</span><span>()</span></code></pre><h2>Aggregate to monthly data</h2>
<p>There are still a fair few days missing data altogether (apparent in the plot above by breaks in the lines). So I decided that for my purposes it made sense to aggregate the data by month. There’s all sorts of information that’s chucked out by this - variation on days within the month, and hours within the day; subtle treatment of the different number of days by month etc. But it’s good enough for my purposes of examining seasonality, and in fact this summarising/aggregation process is probably the best way to investigate a broad annual seasonal pattern. Just because we have large amount of irregular, granular data doesn’t mean we’re compelled to leave it in that state for analysis. Here’s how it looks when aggregated at a monthly level:</p>
<p></p>
<p>Looking at the pattern for each month, the seasonality is very apparent; there is much more particulate matter in the air in the winter-centred November to February period, most noticeably in the four northern cities (ie less so in Chennai). I don’t know enough about the subject matter to speculate if this is related purely to weather (the dominant factor being the <a href="https://en.wikipedia.org/wiki/Monsoon_of_South_Asia">May to November monsoon</a>), or to human response to seasons.</p>
<p></p>
<p>We can decompose the monthly series into the trend, seasonal and random elements using the <code>ggseas</code> package to get a simultaneous view of scale and seasonality. The GitHub page of the <code>usaqmindia</code> R package draws attention to the importance of the Hindu <a href="https://en.wikipedia.org/wiki/Diwali">Diwali ‘festival of lights’</a> - which involves fireworks, burning effigies, lamps, and general celebrations - for the particulate matter concentrations, so I’m interested in annotating graphics with those months. This gives us the graphic I started the post with:</p>
<p></p>
<p>Here’s the code that did the above aggregation and analysis.</p>
<pre><code><span># monthly aggregation graphic:
</span><span>pm25_india</span><span> </span><span>%>%</span><span>
</span><span>mutate</span><span>(</span><span>mon</span><span> </span><span>=</span><span> </span><span>substring</span><span>(</span><span>datetime</span><span>,</span><span> </span><span>1</span><span>,</span><span> </span><span>7</span><span>))</span><span> </span><span>%>%</span><span>
</span><span>group_by</span><span>(</span><span>mon</span><span>,</span><span> </span><span>city</span><span>)</span><span> </span><span>%>%</span><span>
</span><span>summarise</span><span>(</span><span>avvalue</span><span> </span><span>=</span><span> </span><span>mean</span><span>(</span><span>conc</span><span>,</span><span> </span><span>tr</span><span> </span><span>=</span><span> </span><span>0.1</span><span>,</span><span> </span><span>na.rm</span><span> </span><span>=</span><span> </span><span>TRUE</span><span>))</span><span> </span><span>%>%</span><span> </span><span>ungroup</span><span>()</span><span> </span><span>%>%</span><span>
</span><span>mutate</span><span>(</span><span>mon</span><span> </span><span>=</span><span> </span><span>as.Date</span><span>(</span><span>paste0</span><span>(</span><span>mon</span><span>,</span><span> </span><span>&quot;-15&quot;</span><span>)))</span><span> </span><span>%>%</span><span>
</span><span>ggplot</span><span>(</span><span>aes</span><span>(</span><span>x</span><span> </span><span>=</span><span> </span><span>mon</span><span>,</span><span> </span><span>y</span><span> </span><span>=</span><span> </span><span>avvalue</span><span>))</span><span> </span><span>+</span><span>
</span><span>facet_wrap</span><span>(</span><span>~</span><span>city</span><span>,</span><span> </span><span>ncol</span><span> </span><span>=</span><span> </span><span>1</span><span>,</span><span> </span><span>scales</span><span> </span><span>=</span><span> </span><span>&quot;free_y&quot;</span><span>)</span><span> </span><span>+</span><span>
</span><span>geom_line</span><span>(</span><span>colour</span><span> </span><span>=</span><span> </span><span>&quot;grey50&quot;</span><span>,</span><span> </span><span>linetype</span><span> </span><span>=</span><span> </span><span>2</span><span>)</span><span> </span><span>+</span><span>
</span><span>stat_stl</span><span>(</span><span>s.window</span><span> </span><span>=</span><span> </span><span>7</span><span>,</span><span> </span><span>frequency</span><span> </span><span>=</span><span> </span><span>12</span><span>,</span><span> </span><span>colour</span><span> </span><span>=</span><span> </span><span>&quot;steelblue&quot;</span><span>,</span><span> </span><span>size</span><span> </span><span>=</span><span> </span><span>1.2</span><span>)</span><span> </span><span>+</span><span>
</span><span>ggtitle</span><span>(</span><span>&quot;Airborne fine particulate matter in Indian cities (PM2.5)&quot;</span><span>,</span><span>
</span><span>subtitle</span><span> </span><span>=</span><span> </span><span>&quot;Showing original and seasonally adjusted&quot;</span><span>)</span><span> </span><span>+</span><span>
</span><span>labs</span><span>(</span><span>x</span><span> </span><span>=</span><span> </span><span>&quot;Seasonal adjustment does not take Diwali into account&quot;</span><span>,</span><span> </span><span>y</span><span> </span><span>=</span><span> </span><span>&quot;Trimmed mean monthly PM2.5 concentration&quot;</span><span>,</span><span>
</span><span>caption</span><span> </span><span>=</span><span> </span><span>&quot;Data: U.S. Embassy and Consulates in India&quot;</span><span>)</span><span>
</span><span># Create time series objects
</span><span>pm25_monthly_df</span><span> </span><span>&lt;-</span><span> </span><span>pm25_india</span><span> </span><span>%>%</span><span>
</span><span>mutate</span><span>(</span><span>mon</span><span> </span><span>=</span><span> </span><span>substring</span><span>(</span><span>datetime</span><span>,</span><span> </span><span>1</span><span>,</span><span> </span><span>7</span><span>))</span><span> </span><span>%>%</span><span>
</span><span>group_by</span><span>(</span><span>mon</span><span>,</span><span> </span><span>city</span><span>)</span><span> </span><span>%>%</span><span>
</span><span>summarise</span><span>(</span><span>avvalue</span><span> </span><span>=</span><span> </span><span>mean</span><span>(</span><span>conc</span><span>,</span><span> </span><span>tr</span><span> </span><span>=</span><span> </span><span>0.1</span><span>,</span><span> </span><span>na.rm</span><span> </span><span>=</span><span> </span><span>TRUE</span><span>))</span><span> </span><span>%>%</span><span> </span><span>ungroup</span><span>()</span><span> </span><span>%>%</span><span>
</span><span>spread</span><span>(</span><span>city</span><span>,</span><span> </span><span>avvalue</span><span>)</span><span> </span><span>pm25_monthly_ts</span><span> </span><span>&lt;-</span><span> </span><span>pm25_monthly_df</span><span> </span><span>%>%</span><span>
</span><span>select</span><span>(</span><span>-</span><span>mon</span><span>)</span><span> </span><span>%>%</span><span>
</span><span>map</span><span>(</span><span>function</span><span>(</span><span>x</span><span>){</span><span>ts</span><span>(</span><span>x</span><span>,</span><span> </span><span>start</span><span> </span><span>=</span><span> </span><span>c</span><span>(</span><span>2013</span><span>,</span><span> </span><span>1</span><span>),</span><span> </span><span>frequency</span><span> </span><span>=</span><span> </span><span>12</span><span>)})</span><span> </span><span>%>%</span><span>
</span><span>do.call</span><span>(</span><span>cbind</span><span>,</span><span> </span><span>.</span><span>)</span><span>
</span><span># Month-plots
</span><span>par</span><span>(</span><span>mfrow</span><span> </span><span>=</span><span> </span><span>c</span><span>(</span><span>2</span><span>,</span><span> </span><span>3</span><span>),</span><span> </span><span>font.main</span><span> </span><span>=</span><span> </span><span>1</span><span>)</span><span>
</span><span>for</span><span>(</span><span>i</span><span> </span><span>in</span><span> </span><span>1</span><span>:</span><span>5</span><span>){</span><span>
</span><span>monthplot</span><span>(</span><span>pm25_monthly_ts</span><span>[</span><span> </span><span>,</span><span> </span><span>i</span><span>],</span><span> </span><span>bty</span><span> </span><span>=</span><span> </span><span>&quot;l&quot;</span><span>,</span><span>
</span><span>ylab</span><span> </span><span>=</span><span> </span><span>&quot;&quot;</span><span>,</span><span>
</span><span>main</span><span> </span><span>=</span><span> </span><span>colnames</span><span>(</span><span>pm25_monthly_ts</span><span>)[</span><span>i</span><span>])</span><span> </span><span>grid</span><span>()</span><span>
</span><span>}</span><span>
</span><span>#-------------Decomposition graphics---------------------- # named palette assigning a colour to each city
</span><span>palette</span><span> </span><span>&lt;-</span><span> </span><span>brewer.pal</span><span>(</span><span>5</span><span>,</span><span> </span><span>&quot;Set1&quot;</span><span>)</span><span>
</span><span>names</span><span>(</span><span>palette</span><span>)</span><span> </span><span>&lt;-</span><span> </span><span>c</span><span>(</span><span>&quot;Delhi&quot;</span><span>,</span><span> </span><span>&quot;Kolkata&quot;</span><span>,</span><span> </span><span>&quot;Mumbai&quot;</span><span>,</span><span> </span><span>&quot;Hyderabad&quot;</span><span>,</span><span> </span><span>&quot;Chennai&quot;</span><span>)</span><span>
</span><span># Note that Diwali was in November 2013, October 2014, November 2015 in the relevant years
</span><span>diwali</span><span>[</span><span>114</span><span>:</span><span>116</span><span>]</span><span>
</span><span># Decomposition graphic:
</span><span>pm25_monthly_ts</span><span> </span><span>%>%</span><span>
</span><span>as.data.frame</span><span>()</span><span> </span><span>%>%</span><span>
</span><span>mutate</span><span>(</span><span>time</span><span> </span><span>=</span><span> </span><span>time</span><span>(</span><span>pm25_monthly_ts</span><span>),</span><span>
</span><span>month</span><span> </span><span>=</span><span> </span><span>time</span><span> </span><span>-</span><span> </span><span>floor</span><span>(</span><span>time</span><span>))</span><span> </span><span>%>%</span><span>
</span><span>gather</span><span>(</span><span>city</span><span>,</span><span> </span><span>value</span><span>,</span><span> </span><span>-</span><span>time</span><span>,</span><span> </span><span>-</span><span>month</span><span>)</span><span> </span><span>%>%</span><span> </span><span># imputation: if value is missing, give it mean for that month and city:
</span><span> </span><span>group_by</span><span>(</span><span>city</span><span>,</span><span> </span><span>month</span><span>)</span><span> </span><span>%>%</span><span>
</span><span>mutate</span><span>(</span><span>value</span><span> </span><span>=</span><span> </span><span>ifelse</span><span>(</span><span>is.na</span><span>(</span><span>value</span><span>),</span><span> </span><span>mean</span><span>(</span><span>value</span><span>,</span><span> </span><span>na.rm</span><span> </span><span>=</span><span> </span><span>TRUE</span><span>),</span><span> </span><span>value</span><span>))</span><span> </span><span>%>%</span><span>
</span><span>ungroup</span><span>()</span><span> </span><span>%>%</span><span>
</span><span># order city levels for drawing legend (there are better ways for doing this programmatically):
</span><span> </span><span>mutate</span><span>(</span><span>city</span><span> </span><span>=</span><span> </span><span>factor</span><span>(</span><span>city</span><span>,</span><span> </span><span>levels</span><span> </span><span>=</span><span> </span><span>c</span><span>(</span><span>&quot;Delhi&quot;</span><span>,</span><span> </span><span>&quot;Kolkata&quot;</span><span>,</span><span> </span><span>&quot;Mumbai&quot;</span><span>,</span><span> </span><span>&quot;Hyderabad&quot;</span><span>,</span><span> </span><span>&quot;Chennai&quot;</span><span>)))</span><span> </span><span>%>%</span><span>
</span><span>ggsdc</span><span>(</span><span>aes</span><span>(</span><span>x</span><span> </span><span>=</span><span> </span><span>time</span><span>,</span><span> </span><span>y</span><span> </span><span>=</span><span> </span><span>value</span><span>,</span><span> </span><span>colour</span><span> </span><span>=</span><span> </span><span>city</span><span>),</span><span> </span><span>s.window</span><span> </span><span>=</span><span> </span><span>7</span><span>)</span><span> </span><span>+</span><span>
</span><span>geom_line</span><span>(</span><span>size</span><span> </span><span>=</span><span> </span><span>1</span><span>)</span><span> </span><span>+</span><span>
</span><span>theme</span><span>(</span><span>legend.position</span><span> </span><span>=</span><span> </span><span>&quot;right&quot;</span><span>)</span><span>+</span><span>
</span><span>labs</span><span>(</span><span>colour</span><span> </span><span>=</span><span> </span><span>&quot;&quot;</span><span>,</span><span> </span><span>x</span><span> </span><span>=</span><span> </span><span>&quot;Vertical lines show the month of Diwali, and any impact is seen in the 'irregular' series.&quot;</span><span>,</span><span> </span><span>y</span><span> </span><span>=</span><span> </span><span>&quot;Trimmed mean monthly PM2.5 concentration&quot;</span><span>,</span><span>
</span><span>caption</span><span> </span><span>=</span><span> </span><span>&quot;Data: U.S. Embassy and Consulates in India&quot;</span><span>)</span><span> </span><span>+</span><span>
</span><span>scale_colour_manual</span><span>(</span><span>values</span><span> </span><span>=</span><span> </span><span>palette</span><span>)</span><span> </span><span>+</span><span>
</span><span>ggtitle</span><span>(</span><span>&quot;Airborne fine particulate matter in Indian cities (PM2.5)&quot;</span><span>,</span><span>
</span><span>subtitle</span><span> </span><span>=</span><span> </span><span>&quot;Diwali makes an impact but is part of a broader seasonality&quot;</span><span>)</span><span> </span><span>+</span><span>
</span><span>geom_vline</span><span>(</span><span>xintercept</span><span> </span><span>=</span><span> </span><span>((</span><span>month</span><span>(</span><span>diwali</span><span>)</span><span> </span><span>-</span><span> </span><span>1</span><span>)</span><span> </span><span>/</span><span> </span><span>12</span><span> </span><span>+</span><span> </span><span>year</span><span>(</span><span>diwali</span><span>))[</span><span>114</span><span>:</span><span>116</span><span>],</span><span> </span><span>colour</span><span> </span><span>=</span><span> </span><span>&quot;grey50&quot;</span><span>)</span></code></pre><h2>Delhi time series analysis</h2>
<p>Diwali’s date is in a different month from year to year, which means we have a naturally occuring experiment that lets us distinguish between the underlying seasonal impact of any month, and the particular effect of Diwali. To investigate this further, I had a look at just the Delihi data, which is more complete than that of the other four cities even when aggregated to monthly level.</p>
<p>Here are graphics of the auto-correlation and partial auto-correlation functions of the Delhi monthly time series. These show the strength of relationship between any particular observation and lagged versions of the series. A lag of 1.0 means 12 months in this case. The patterns we see are typical of a series with a noticeable seasonal impact and a strong relationship to the observation of the previous month:</p>
<p></p>
<pre><code><span>delhi</span><span> </span><span>&lt;-</span><span> </span><span>pm25_monthly_ts</span><span>[</span><span> </span><span>,</span><span> </span><span>&quot;Delhi&quot;</span><span>]</span><span>
</span><span>par</span><span>(</span><span>mfrow</span><span> </span><span>=</span><span> </span><span>c</span><span>(</span><span>1</span><span>,</span><span> </span><span>2</span><span>),</span><span> </span><span>bty</span><span> </span><span>=</span><span> </span><span>&quot;l&quot;</span><span>,</span><span> </span><span>font.main</span><span> </span><span>=</span><span> </span><span>1</span><span>)</span><span>
</span><span>acf</span><span>(</span><span>delhi</span><span>)</span><span>
</span><span>pacf</span><span>(</span><span>delhi</span><span>)</span></code></pre><p>For analysis I was a little worried about second order stationarity, in particular whether the variance of monthly observations is higher when the mean observations are higher. I also had in mind the possibility of using a model for forecasting. While not shown in this already-too-long post, forecasting based on the original data hand a tendency for the lower range of the prediction interval to be negative, which isn’t possible and suggested the need for a transformation. All these problems can be fixed by a <a href="https://en.wikipedia.org/wiki/Power_transform">Box-Cox power transform</a>. The <code>BoxCox.lambda</code> function used below uses Guerrero’s 1993 method for choosing a value of that minimises the coefficient of variation of the time series. With an estimated value of 0.21 for Delhi’s data (somewhere between a logarithm value of 0 and a square-root-like value of 0.5) this is well in the range of rule of thumb transformation commonly used for this sort of time series.</p>
<p>I use for fitting an ARIMA model with a dummy variable for whether Diwali occurred in the month:</p>
<pre><code><span>BoxCox.lambda</span><span>(</span><span>delhi</span><span>)</span><span> </span><span># about 0.21. Note that if we don't use this in forecasting there would be a tendency to forecast &lt; 0
</span><span>
</span><span>diwalix</span><span> </span><span>&lt;-</span><span> </span><span>window</span><span>(</span><span>genhol</span><span>(</span><span>diwali</span><span>),</span><span> </span><span>start</span><span> </span><span>=</span><span> </span><span>start</span><span>(</span><span>delhi</span><span>),</span><span> </span><span>end</span><span> </span><span>=</span><span> </span><span>end</span><span>(</span><span>delhi</span><span>))</span><span>
</span><span>mod0</span><span> </span><span>&lt;-</span><span> </span><span>auto.arima</span><span>(</span><span>delhi</span><span>,</span><span> </span><span>xreg</span><span> </span><span>=</span><span> </span><span>diwalix</span><span>,</span><span> </span><span>lambda</span><span> </span><span>=</span><span> </span><span>0.21</span><span>)</span><span>
</span><span>mod0</span></code></pre><p>That gives us these results, showing some modest evidence of a gentle drift downwards as well as a Diwali impact (0.46, with a standard error of 0.29 - well after all, it is only three years) within a seasonal ARIMA context</p>
<pre><code><span>Series</span><span>:</span><span> </span><span>delhi</span><span> </span><span>ARIMA</span><span>(</span><span>1</span><span>,</span><span>0</span><span>,</span><span>0</span><span>)(</span><span>1</span><span>,</span><span>1</span><span>,</span><span>0</span><span>)[</span><span>12</span><span>]</span><span> </span><span>with</span><span> </span><span>drift</span><span> </span><span>Box</span><span> </span><span>Cox</span><span> </span><span>transformation</span><span>:</span><span> </span><span>lambda</span><span>=</span><span> </span><span>0.21</span><span> </span><span>Coefficients</span><span>:</span><span>
</span><span>ar1</span><span> </span><span>sar1</span><span> </span><span>drift</span><span> </span><span>diwalix</span><span>
</span><span>0.5400</span><span> </span><span>-0.7709</span><span> </span><span>-0.0164</span><span> </span><span>0.4626</span><span>
</span><span>s.e.</span><span> </span><span>0.1561</span><span> </span><span>0.0990</span><span> </span><span>0.0082</span><span> </span><span>0.2969</span><span>
</span><span>sigma</span><span>^</span><span>2</span><span> </span><span>estimated</span><span> </span><span>as</span><span> </span><span>0.1691</span><span>:</span><span> </span><span>log</span><span> </span><span>likelihood</span><span>=</span><span>-20.41</span><span>
</span><span>AIC</span><span>=</span><span>50.83</span><span> </span><span>AICc</span><span>=</span><span>53.14</span><span> </span><span>BIC</span><span>=</span><span>58.16</span></code></pre><p>A 95% confidence interval <code>confint(mod0)</code> for the Diwali impact is between -0.12 and +1.04 on the transformed scale. This range includes zero so this analysis isn’t sufficient by itself to conclude it has an impact. However, the power of this test would be pretty low (one of the prices paid for by the aggregation to the monthly level) and my intuitive Bayesian inside would be happy to say it is more likely than not that there is some impact of Diwali on particulate matter in the atmosphere in Delhi.</p>