<p><div><div><div><div data-show-faces="false" data-height="21" data-width="100" data-layout="button_count" data-send="true" data-href="https://www.r-bloggers.com/simulating-water-consumption-to-develop-analysis-and-reporting/"></div></div><div></div></div><div></div></div>
<div>
(This article was first published on <strong><a href="https://r.prevos.net/simulating-water-consumption/"> The Devil is in the Data</a></strong>, and kindly contributed to <a rel="nofollow" href="https://www.r-bloggers.com/">R-bloggers)</a> </div>
<p>I am currently working on developing analytics for a digital water metering project. Over the next five years, we are enabling 70,000 customer water meters with digital readers and transmitters. The data is not yet available but we don’t want to wait to build reporting systems until after the data is live. The R language comes to the rescue as it has magnificent capabilities to simulate data. Simulating data is a useful technique to progress a project when data is being collected. Simulated data also helps because the outcomes of the analysis are known, which helps to validate the outcomes.</p>
<p>The raw data that we will eventually receive from the digital customer meters has the following basic structure:</p>
<ul>
<li><em>DevEUI</em>: Unique device identifier.</li>
<li><em>Timestamp</em>: Date and time in (UTC) of the transmission.</li>
<li><em>Cumulative count</em>: The number of revolutions the water meter makes. Each revolution is a pulse which equates to five litres of water.</li>
</ul>
<p>Every device will send an hourly data burst which contains the cumulative meter read in pulse counts. The transmitters are set at a random offset from the whole our, to minimise the risk of congestion at the receivers. The time stamp for each read is set in the <a rel="nofollow" href="https://en.wikipedia.org/wiki/Coordinated_Universal_Time" target="_blank">Coordinated Universal Time</a> (UTC). Using this time zone prevents issues with daylight savings. All analysis will be undertaken in the Australian Eastern (Daylight) Time zone.</p>
<p>This article explains how we simulated test data to assist with developing reporting and analysis. The analysis of digital metering data follows in a future post. The code and the data can be found on <a rel="nofollow" href="https://github.com/pprevos/r.prevos.net/tree/master/Hydroinformatics/DigitalMetering" target="_blank">GitHub</a>. I have recently converted to using the <a rel="nofollow" href="https://www.tidyverse.org/" target="_blank">Tidyverse</a> for all my R coding. It has made my working life much easier and I will use it for all future posts.</p>
<h2>Simulating water consumption</h2>
<p>For simplicity, this simulation assumes a standard domestic diurnal curve (average daily usage pattern) for indoor water use. Diurnal curves are an important piece of information in water management. The curve shows water consumption over the course of a day, averaged over a fixed period. The example below is sourced from a journal article. This generic diurnal curve consists of 24 data points based on measured indoor water consumption, shown in the graph below.</p>
<div class="wp-caption"><a rel="nofollow" href="http://www.sciencedirect.com/science/article/pii/S0921344914001347" target="_blank"><img alt="Simulating water consumption: diurnal curve example" width="450" src="https://i2.wp.com/ars.els-cdn.com/content/image/1-s2.0-S0921344914001347-gr8.jpg?resize=584%2C277&ssl=1" data-recalc-dims="1"></a>
<p class="wp-caption-text">Source: Gurung et al. (2014) Smart meters for enhanced water supply network modelling and infrastructure planning. Resources, Conservation and Recycling (90), 34-50.</p>
</div>
<p>This diurnal curve only includes indoor water consumption and is assumed to be independent of seasonal variation. This is not a realitic assumption, but the purpose of this simulation is not to accurately model water consumption but to provide a data set to validate the reporting and analyses.</p>
<h2>Simulating water consumption in R</h2>
<p>The first code snippet sets the parameters used in this simulation. The unique device identifiers (DevEUI) are simulated as six-digit random numbers. The timestamps vector consists of hourly date-time variables in UTC. For each individual transmitter, this timestamp is offset by a random time. Each transmitter is also associated with the number of people living in each house. This number is based on a Poisson distribution.</p>
<pre>
# Libraries
library(tidyverse)
# Boundary conditions
n &lt;- 100 # Number of simulated meters
d &lt;- 100 # Number of days to simulate
s &lt;- as.POSIXct(&quot;2020-01-01&quot;, tz = &quot;UTC&quot;) # Start of simulation

set.seed(1969) # Seed random number generator for reproducibility
rtu &lt;- sample(1E6:2E6, n, replace = FALSE) # 6-digit id
offset &lt;- sample(0:3599, n, replace = TRUE) # Unique Random offset for each RTU
# Number of occupants per connection
occupants &lt;- rpois(n, 1.5) + 1 as.data.frame(occupants) %>%
  ggplot(aes(occupants)) + geom_bar(fill = &quot;dodgerblue2&quot;, alpha = 0.5) + 
  xlab(&quot;Occupants&quot;) + ylab(&quot;Connections&quot;) + ggtitle(&quot;Occupants per connection&quot;)
</pre>
<div class="wp-caption"><a rel="nofollow" href="https://r.prevos.net/?attachment_id=1112" target="_blank"><img data-orig-file="https://i2.wp.com/r.prevos.net/wp-content/uploads/sites/11/2018/01/occupants-1.png?fit=2006%2C1290&amp;ssl=1" data-orig-size="2006,1290" data-medium-file="https://i2.wp.com/r.prevos.net/wp-content/uploads/sites/11/2018/01/occupants-1.png?fit=300%2C193&amp;ssl=1" data-attachment-id="1112" data-image-meta="{" data-image-description="" src="https://i2.wp.com/r.prevos.net/wp-content/uploads/sites/11/2018/01/occupants-1.png?resize=584%2C376&ssl=1" data-permalink="https://r.prevos.net/simulating-water-consumption/occupants-2/" data-large-file="https://i2.wp.com/r.prevos.net/wp-content/uploads/sites/11/2018/01/occupants-1.png?fit=584%2C376&amp;ssl=1" alt="Simulated number of occupants per connection." data-image-title="occupants" sizes="(max-width: 584px) 100vw, 584px" width="450" data-comments-opened="1" class="wp-image-1112" data-recalc-dims="1"></a>
<p class="wp-caption-text">Simulated number of occupants per connection.</p>
</div>
<p>The diurnal curve is based on actual data which includes leaks as the night time use shows a consistent flow of about one litre per hour. For that reason, the figures are rounded and reduced by one litre per hour, to show a zero flow when people are usually asleep. The curve is also shifted by eleven hours because the raw data is stored in UTC.</p>
<pre>
diurnal &lt;- round(c(1.36, 1.085, 0.98, 1.05, 1.58, 3.87, 9.37, 13.3, 12.1, 10.3, 8.44, 7.04, 6.11, 5.68, 5.58, 6.67, 8.32, 10.0, 9.37, 7.73, 6.59, 5.18, 3.55, 2.11)) - 1 data.frame(TimeUTC = 0:23, Flow = diurnal) %>% 
  ggplot(aes(x = TimeUTC, y = Flow)) + 
  geom_area(fill = &quot;dodgerblue2&quot;, alpha = 0.5) +
  scale_x_continuous(breaks = 0:23) + ylab(&quot;Flow [L/h/p]&quot;) + 
  ggtitle(&quot;Idealised diurnal curve for households&quot;)
ggsave(&quot;Hydroinformatics/DigitalMetering/diurnal_curve.png&quot;, dpi = 300)

tdiff &lt;- 11
diurnal &lt;- c(diurnal[(tdiff + 1): 24], diurnal[1:tdiff])
</pre>
<p>This simulation only aims to simulate a realistic data set and not to present an accurate depiction of reality. This simulation could be enhanced by using different diurnal curves for various customer segments and to include outdoor watering, temperature dependencies and so on.</p>
<h2>Simulating Water Consumption</h2>
<p>A leak is defined by a constant flow through the meter, in addition to the idealised diurnal curve. A weighted binomial distribution (θ = 0.1) models approximately one in ten properties with a leak. The size of the leak is derived from a random number between 10 and 50 litres per hour.</p>
<p>The data is stored in a matrix through a loop that cycles through each connection. The DevEUI is repeated over the simulated time period (24 times the number of days). The second variable is the time stamp plus the predetermined offset for each RTU. The meter count is defined by the cumulative sum of the diurnal flow, multiplied by the number of occupants. Each point in the diurnal deviates from the model curve by ±10%. Any predetermined leakage is added to each meter read over the whole period of 100 days. The hourly volumes are summed cumulatively to simulate meter reads. The flow is divided by five as each meter revolution indicate five litres.</p>
<p>The next code snippet simulates the digital metering data using the assumptions and parameters outlined above.</p>
<pre>
# Leak simulation
leaks &lt;- rbinom(n, 1, prob = .1) * sample(10:50, n, replace = TRUE) data.frame(DevEUI = rtu, Leak = leaks) %>%
  subset(Leak > 0)

# Digital metering data simulation
meter_reads &lt;- matrix(ncol = 3, nrow = 24 * n * d)
colnames(meter_reads) &lt;- c(&quot;DevEUI&quot;, &quot;TimeStampUTC&quot;, &quot;Count&quot;)

for (i in 1:n) {
  r &lt;- ((i - 1) * 24 * d + 1):(i * 24 * d)
  meter_reads[r, 1] &lt;- rep(rtu[i], each = (24 * d))
  meter_reads[r, 2] &lt;- seq.POSIXt(s, by = &quot;hour&quot;, length.out = 24 * d) + offset[i]
  meter_reads[r, 3] &lt;- round(cumsum((rep(diurnal * occupants[i], d) + leaks[i]) * 
                                     runif(24 * d, 0.9, 1.1))/5)
} 

meter_reads &lt;- meter_reads %>% 
  as_data_frame() %>%
  mutate(TimeStampUTC = as.POSIXct(TimeStampUTC, origin = &quot;1970-01-01&quot;, tz =&quot;UTC&quot;))
</pre>
<h2>Missing Data Points</h2>
<p>The data transmission process is not 100% reliable and the base station will not receive some reads. This simulation identifies reads to be removed from the data through the temporary variable remove. This simulation includes two types of failures:</p>
<ul>
<li>Faulty RTUs (2% of RTUs with missing 95% of data)</li>
<li>Randomly missing data points (1% of data)</li>
</ul>
<pre>
# Initialise temp variable
meter_reads &lt;- mutate(meter_reads, remove = 0)
# Define faulty RTUs (2% of fleet)
faulty &lt;- rtu[rbinom(n, 1, prob = 0.02) == 1]
meter_reads$remove[meter_reads$DevEUI %in% faulty] &lt;- rbinom(sum(meter_reads$DevEUI %in% faulty), 1, prob = .95)

# Data loss
missing &lt;- sample(1:(nrow(meter_reads) - 5), 0.005 * nrow(meter_reads))
for (m in missing){
  meter_reads[m:(m + sample(1:5, 1)), &quot;remove&quot;] &lt;- 1
}

# Remove data points
meter_reads &lt;- filter(meter_reads, remove == 0) %>%
  select(-remove)

#Visualise
filter(meter_reads, DevEUI %in% rtu[2]) %>%
  mutate(TimeStampAEST = as.POSIXct(format(TimeStampUTC, 
                                           tz = &quot;Australia/Melbourne&quot;))) %>%
  filter(TimeStampAEST >= as.POSIXct(&quot;2020-02-06&quot;) &amp; 
         TimeStampAEST &lt;= as.POSIXct(&quot;2020-02-08&quot;)) %>%
  arrange(DevEUI, TimeStampAEST) %>% 
  ggplot(aes(x = TimeStampAEST, y = Count, colour = factor(DevEUI)))  + 
    geom_line() + geom_point() 
</pre>
<p>The graph shows an example of the cumulative reads and some missing data points.</p>
<p><a rel="nofollow" href="https://r.prevos.net/?attachment_id=1117" target="_blank"><img data-orig-file="https://i1.wp.com/r.prevos.net/wp-content/uploads/sites/11/2018/01/consumption.png?fit=2006%2C1290&amp;ssl=1" data-orig-size="2006,1290" data-medium-file="https://i1.wp.com/r.prevos.net/wp-content/uploads/sites/11/2018/01/consumption.png?fit=300%2C193&amp;ssl=1" data-attachment-id="1117" data-image-meta="{" data-image-description="" src="https://i1.wp.com/r.prevos.net/wp-content/uploads/sites/11/2018/01/consumption.png?resize=584%2C376&ssl=1" data-permalink="https://r.prevos.net/simulating-water-consumption/consumption/" data-large-file="https://i1.wp.com/r.prevos.net/wp-content/uploads/sites/11/2018/01/consumption.png?fit=584%2C376&amp;ssl=1" alt="Simulated water consumption" data-image-title="consumption" sizes="(max-width: 584px) 100vw, 584px" width="450" data-comments-opened="1" class="wp-image-1117" data-recalc-dims="1"></a></p>
<h2>Analysing Digital Metering Data</h2>
<p>Data simulation is a good way to develop your analysis algorithms before you have real data. I have also used this technique when I was waiting for survey results during my dissertation. When the data finally arrived, I simply had to plug it into the code and finetune the code. R has great capabilities to simulate reality to help you understand the data.</p>
<p>In next week’s article, I will outline how I used R and the Tidyverse package to develop libraries to analyse digital metering data.</p>
<p>The post <a rel="nofollow" href="https://r.prevos.net/simulating-water-consumption/" target="_blank">Simulating Water Consumption to Develop Analysis and Reporting</a> appeared first on <a rel="nofollow" href="https://r.prevos.net/" target="_blank">The Devil is in the Data</a>.</p>
	<p>
<div>
<div>To <strong>leave a comment</strong> for the author, please follow the link and comment on their blog: <strong><a href="https://r.prevos.net/simulating-water-consumption/"> The Devil is in the Data</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 rel="nofollow" href="https://www.r-project.org/" title="The R Project for Statistical Computing">R</a> news and <a rel="nofollow" href="https://www.r-bloggers.com/search/tutorial" title="R tutorials">tutorials</a> on topics such as: <a rel="nofollow" href="https://www.r-bloggers.com/search/data%20science" title="Data science">Data science</a>, <a rel="nofollow" href="https://www.r-bloggers.com/search/Big%20Data" title="Big Data">Big Data, <a rel="nofollow" href="https://www.r-users.com/" title="R jobs">R jobs</a>, visualization (<a rel="nofollow" href="https://www.r-bloggers.com/search/ggplot2" title="ggplot and ggplot2 tutorials">ggplot2</a>, <a rel="nofollow" href="https://www.r-bloggers.com/search/boxplot" title="Boxplots using lattice and ggplot2 tutorials">Boxplots</a>, <a rel="nofollow" href="https://www.r-bloggers.com/search/map" title="Maps and gis">maps</a>, <a rel="nofollow" href="https://www.r-bloggers.com/search/animation" title="Animation in R">animation</a>), programming (<a rel="nofollow" href="https://www.r-bloggers.com/search/RStudio" title="RStudio IDE for R">RStudio</a>, <a rel="nofollow" href="https://www.r-bloggers.com/search/sweave" title="Sweave and literate programming">Sweave</a>, <a rel="nofollow" href="https://www.r-bloggers.com/search/LaTeX" title="LaTeX in R">LaTeX</a>, <a rel="nofollow" href="https://www.r-bloggers.com/search/SQL" title="SQL and databases">SQL</a>, <a rel="nofollow" href="https://www.r-bloggers.com/search/eclipse" title="Eclipse IDE for R">Eclipse</a>, <a rel="nofollow" href="https://www.r-bloggers.com/search/git" title="git and github, Version Control System">git</a>, <a rel="nofollow" href="https://www.r-bloggers.com/search/hadoop" title="Large data in R using Hadoop">hadoop</a>, <a rel="nofollow" href="https://www.r-bloggers.com/search/Web+Scraping" title="Web Scraping of google, facebook, yahoo, twitter and more using R">Web Scraping</a>) statistics (<a rel="nofollow" href="https://www.r-bloggers.com/search/regression" title="Regressions and ANOVA analysis tutorials">regression</a>, <a rel="nofollow" href="https://www.r-bloggers.com/search/PCA" title="principal component analysis tutorial">PCA</a>, <a rel="nofollow" href="https://www.r-bloggers.com/search/time+series" title="Time series">time series</a>, <a rel="nofollow" href="https://www.r-bloggers.com/search/trading" title="finance trading">trading</a>) and more...
<img alt="" width="1" src="http://feeds.feedburner.com/~r/RBloggers/~4/qua22mNhpaQ" height="1"></a></div>