<p><div><div><div><div data-width="100" data-show-faces="false" data-send="true" data-layout="button_count" data-height="21" data-href="http://www.r-bloggers.com/time-series-analysis-using-maxmin-and-some-neuroscience/"></div></div><div><a data-url="http://www.r-bloggers.com/time-series-analysis-using-maxmin-and-some-neuroscience/" data-counturl="http://www.r-bloggers.com/time-series-analysis-using-maxmin-and-some-neuroscience/" data-count="horizontal" data-text="Time Series Analysis Using Max/Min… and some Neuroscience." class="twitter-share-button" data-via="rbloggers" href="https://twitter.com/share"></a></div><div></div></div><div></div></div>
<div>
(This article was first published on <strong><a href="http://blog.datascienceheroes.com/time-series-analysis-profiling-using-max-min/"> R - Data Science Heroes Blog</a></strong>, and kindly contributed to <a rel="nofollow" href="http://www.r-bloggers.com/">R-bloggers)</a> </div>
<p><img alt="brain signal" src="http://i1.wp.com/datascienceheroes.com/img/blog/01_time_series_analysis.png?resize=270%2C270" data-recalc-dims="1"></p>
<h3>Introduction</h3>
<p>Time series has <strong>maximum</strong> and <strong>minimum</strong> points as general patterns. Sometimes the noise present on it causes problems to spot general behavior.</p>
<p>In this post, we will <strong>smooth</strong> time series -reducing noise- to maximize the story that data has to tell us. And then, an easy formula will be applied to find and plot max/min points thus characterize data.<br>
</p>
<h3>What we have</h3>
<pre><code># reading data sources, 2 time series
t1=read.csv(&quot;ts_1.txt&quot;)  
t2=read.csv(&quot;ts_2.txt&quot;)


# plotting...
plot(t1$ts1, type = 'l')  
plot(t2$ts2, type = 'l')
</code></pre>
<p><img alt="time series 1" src="http://i2.wp.com/datascienceheroes.com/img/blog/02_ts_c.png?w=456" data-recalc-dims="1"></p>
<p><img alt="time series 2" src="http://i2.wp.com/datascienceheroes.com/img/blog/03_time_series_2.PNG?w=456" data-recalc-dims="1"></p>
<p>As you can see there are many peaks, but intuitively you can imagine a more smoother line crossing in the middle of the points. This can achieved by applying a <strong>Seasonal Trend Decomposition</strong> (STL).</p>
<p></p>
<h3>Smoothing the series</h3>
<pre><code># first create the time series object, with frequency = 50, and then apply the stl function.
stl_1=stl(ts(t1$ts1, frequency=50), &quot;periodic&quot;)  
stl_2=stl(ts(t2$ts2, frequency=50), &quot;periodic&quot;)  
</code></pre>
<p><em>Important</em>: If you don’t know the <code>frequency</code> beforehand, play a little bit with this parameter until you find a result in which you are comfortable.</p>
<p><a target="_blank" href="https://anomaly.io/seasonal-trend-decomposition-in-r">More STL info</a> </p>
<h3>Finding max and min</h3>
<p>Creating the functions… </p>
<pre><code>ts_max&lt;-function(signal)  
{
  points_max=which(diff(sign(diff(signal)))==-2)+1
  return(points_max)
}

ts_min&lt;-function(signal)  
{
  points_min=which(diff(sign(diff(-signal)))==-2)+1
  return(points_min)
}
</code></pre>
<p></p>
<h3>Visualizing the results!</h3>
<pre><code>trend_1=as.numeric(stl_1$time.series[,2])

max_1=ts_max(trend_1)  
min_1=ts_min(trend_1)


## Plotting final results
plot(trend_1, type = 'l')  
abline(v=max_1, col=&quot;red&quot;)  
abline(v=min_1, col=&quot;blue&quot;)
</code></pre>
<p><img alt="time series 1 max min" src="http://i2.wp.com/datascienceheroes.com/img/blog/04_max_min_ts1.PNG?w=456" data-recalc-dims="1"></p>
<p>With the line: <code>stl_1$time.series[,2]</code> we are accessing to the time series <code>trend</code> component. This is the smoothing method we will use, but there are others.</p>
<p>This first series has 3 maximums <em>(red line)</em> and 2 minimum <em>(blue line)</em> in the following places:</p>
<pre><code># When occurs the max points:
max_1  
</code></pre>
<p><img alt="time series 1 max min" align="left" src="http://i1.wp.com/datascienceheroes.com/img/blog/05_res1.PNG?w=150" data-recalc-dims="1"></p>
<pre><code># When occurs the min points:
min_1  
</code></pre>
<p><img alt="time series 1 max min" align="left" src="http://i2.wp.com/datascienceheroes.com/img/blog/06_res2.PNG?resize=150%2C20" data-recalc-dims="1"></p>
<p></p>
<h3>Comparing two time series</h3>
<pre><code>trend_2=as.numeric(stl_2$time.series[,2])

max_2=ts_max(trend_2)  
min_2=ts_min(trend_2)

# create two aligned plots    
par(mfrow=c(2,1))

## Plotting series 1
plot(trend_1, type = 'l')  
abline(v=max_1, col=&quot;red&quot;)  
abline(v=min_1, col=&quot;blue&quot;)

## Plotting series 2
plot(trend_2, type = 'l')  
abline(v=max_2, col=&quot;red&quot;)  
abline(v=min_2, col=&quot;blue&quot;)
</code></pre>
<p></p>
<p><img alt="brain signal" src="http://i1.wp.com/datascienceheroes.com/img/blog/01_time_series_analysis.png?w=456" data-recalc-dims="1"></p>
<p>Some <strong>conclusions</strong> from both plots:</p>
<ul>
<li><code>Series 2</code> starts with a <code>min</code> while 1 does with a <code>max</code> </li>
<li><code>Series 1</code> has 3 <code>max</code> and 2 <code>min</code>, just the opposite to the other series</li>
</ul>
<p>Why is this important? Because of the data nature, which is in next section. </p>
<h3>What is this data about?</h3>
<p><img alt="brain signal" src="http://i2.wp.com/datascienceheroes.com/img/blog/08_brain_signal_b.PNG?w=456" data-recalc-dims="1"></p>
<p><code>ts1</code> and <code>ts2</code> are two typical responses to a brain stimulus, in other words: what happen with the brain when a person look at a picture / move a finger / think in a particular thing, etc… <a target="_blank" href="https://en.wikipedia.org/wiki/Electroencephalography">Electroencephalography</a>.</p>
<p>Some studies in <strong>neuroscience</strong> focus on averaging several responses to one stimulus -for example, to look at one particular picture. They present several times a particular image to the person. Averaging all of these signal/time series, you get the <strong>typical response</strong>.</p>
<p>Then you can <strong>predict</strong> based on the similarity between this <strong>typical response</strong> and the <strong>new image</strong> (stimulus) that the person is looking at. </p>
<p></p>
<h4>Typical response (or Event Related Potential)</h4>
<p><img alt="brain signal" src="http://i0.wp.com/datascienceheroes.com/img/blog/09_event_related_potential.PNG?w=456" data-recalc-dims="1"></p>
<p>It’s important to get the <strong>when</strong> the positive peaks occurs. In this case they are: <code>P1</code>, <code>P2</code> and <code>P3</code>. The same goes for the negative ones.</p>
<p><a target="_blank" href="https://en.wikipedia.org/wiki/Event-related_potential">Wiki: Event related potential</a>.</p>
<p><em>Note: It´s a common practise to invert negative and positive values.</em></p>
<p></p>
<h4>Finally…</h4>
<p>Typically the signal time length for this kind of studies last for <strong>400ms</strong>, thus 1 point per millisecond, just the displayed plots. And the amplitude is in <strong>volts</strong>, <em>(actually micro-volts)</em>. The same unit of measurement used by the notebook you are using now <img alt="😉" class="wp-smiley" src="http://s.w.org/images/core/emoji/72x72/1f609.png"></p>
<div>
<img alt="Brain &amp; computer" src="http://i1.wp.com/datascienceheroes.com/img/blog/10_brain_computer_b.PNG?w=456" data-recalc-dims="1">
</div>
<p></p>
<h6>that’s all!</h6>
<div>
Reproduce all the analysis with <a target="_blank" href="https://github.com/pablo14/ts_find_max_min"><br>
<img alt="Time series analysis in neuroscience Github" src="http://i2.wp.com/datascienceheroes.com/img/blog/github_logo.PNG?w=456" data-recalc-dims="1">this repository.<br>
</a><span></span>
</div>
<p> </p>
<div>
<a target="_blank" href="https://twitter.com/DataSciHeroes"><br>
<img alt="Data Science Heroes Twitter" src="http://i2.wp.com/datascienceheroes.com/img/blog/twitter_logo.PNG?w=456" data-recalc-dims="1">DSH Twitter<br>
</a>
</div>
<p> </p>
<div>
<a target="_blank" href="https://www.facebook.com/datasciheroes"><br>
<img alt="Data Science Heroes Facebook" src="http://i2.wp.com/datascienceheroes.com/img/blog/fb_logo.PNG?w=456" data-recalc-dims="1">DSH Facebook<br>
</a>
</div>
<p> </p>
<div>
<a target="_blank" href="http://blog.datascienceheroes.com/"><br>
<img alt="Data Science Heroes Blog" src="http://i2.wp.com/datascienceheroes.com/img/blog/logo_dsh.png?w=456" data-recalc-dims="1">_More DSH posts!<br>
</a>
</div>
<p><div><div><div><div data-width="55" data-show-faces="false" data-send="true" data-layout="box_count" data-height="62" data-href="http://www.r-bloggers.com/time-series-analysis-using-maxmin-and-some-neuroscience/"></div></div><div><a data-url="http://www.r-bloggers.com/time-series-analysis-using-maxmin-and-some-neuroscience/" data-counturl="http://www.r-bloggers.com/time-series-analysis-using-maxmin-and-some-neuroscience/" data-count="vertical" data-text="Time Series Analysis Using Max/Min… and some Neuroscience." class="twitter-share-button" data-via="rbloggers" href="https://twitter.com/share"></a></div><div></div></div><div></div></div>
<div>
<div>To <strong>leave a comment</strong> for the author, please follow the link and comment on their blog: <strong><a href="http://blog.datascienceheroes.com/time-series-analysis-profiling-using-max-min/"> R - Data Science Heroes Blog</a></strong>.</div>
<hr>
<a rel="nofollow" href="http://www.r-bloggers.com/">R-bloggers.com</a> offers <strong><a rel="nofollow" href="http://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="http://www.r-project.org/">R</a> news and <a title="R tutorials" rel="nofollow" href="http://www.r-bloggers.com/?s=tutorial">tutorials</a> on topics such as: <a title="Data science" rel="nofollow" href="http://www.r-bloggers.com/?s=data%20science">Data science</a>, <a title="Big Data" rel="nofollow" href="http://www.r-bloggers.com/?s=Big%20Data">Big Data, <a title="R jobs" rel="nofollow" href="http://www.r-users.com/">R jobs</a>, visualization (<a title="ggplot and ggplot2 tutorials" rel="nofollow" href="http://www.r-bloggers.com/?s=ggplot2">ggplot2</a>, <a title="Boxplots using lattice and ggplot2 tutorials" rel="nofollow" href="http://www.r-bloggers.com/?s=boxplot">Boxplots</a>, <a title="Maps and gis" rel="nofollow" href="http://www.r-bloggers.com/?s=map">maps</a>, <a title="Animation in R" rel="nofollow" href="http://www.r-bloggers.com/?s=animation">animation</a>), programming (<a title="RStudio IDE for R" rel="nofollow" href="http://www.r-bloggers.com/?s=RStudio">RStudio</a>, <a title="Sweave and literate programming" rel="nofollow" href="http://www.r-bloggers.com/?s=sweave">Sweave</a>, <a title="LaTeX in R" rel="nofollow" href="http://www.r-bloggers.com/?s=LaTeX">LaTeX</a>, <a title="SQL and databases" rel="nofollow" href="http://www.r-bloggers.com/?s=SQL">SQL</a>, <a title="Eclipse IDE for R" rel="nofollow" href="http://www.r-bloggers.com/?s=eclipse">Eclipse</a>, <a title="git and github, Version Control System" rel="nofollow" href="http://www.r-bloggers.com/?s=git">git</a>, <a title="Large data in R using Hadoop" rel="nofollow" href="http://www.r-bloggers.com/?s=hadoop">hadoop</a>, <a title="Web Scraping of google, facebook, yahoo, twitter and more using R" rel="nofollow" href="http://www.r-bloggers.com/?s=Web+Scraping">Web Scraping</a>) statistics (<a title="Regressions and ANOVA analysis tutorials" rel="nofollow" href="http://www.r-bloggers.com/?s=regression">regression</a>, <a title="principal component analysis tutorial" rel="nofollow" href="http://www.r-bloggers.com/?s=PCA">PCA</a>, <a title="Time series" rel="nofollow" href="http://www.r-bloggers.com/?s=time+series">time series</a>, <a title="finance trading" rel="nofollow" href="http://www.r-bloggers.com/?s=trading">trading</a>) and more...
<img height="1" alt="" width="1" src="http://feeds.feedburner.com/~r/RBloggers/~4/FQYSwHiyiHI"></a></div>