<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/personal-data-collection-and-analysis/"></div></div><div></div></div><div></div></div>
<div>
(This article was first published on <strong><a href="https://randomjohn.github.io/personal-data-collection/"> Upstate Data Analysis</a></strong>, and kindly contributed to <a rel="nofollow" href="https://www.r-bloggers.com/">R-bloggers)</a> </div>
<h2>Motivation behind this example</h2>
<p>I was diagnosed with sleep apnea last year, and have to use a continuous positive airway pressure (CPAP) machine to sleep well enough to feel alert during the day. The machine uploads data (via cellular connection) to a website that will give me results for the last two weeks. This data includes both usage (time of usage, air leakage, number of times mask was put on/taken off), and results (apnea-hypopnea index, which is an average of the number of times per hour that slow or no breathing occurred for at least 10 seconds). The website only displays results from the last two weeks, and I’d like to eventually do a long-term analysis. I’d also like to have things displayed my own way, because, well, I’m like that.</p>
<p>I could enter this information in a spreadsheet, and for import into R or other statistical software that might be the sensible thing to do. However, by having this data in context of other diary entries and text surround it I get to see this data in context of other things going on in my life. This information does not exist in a vacuum, and is important context for other things. For instance, if I’m dealing with a particularly stressful situation, it would be nice to go back and see how I dealt with that in the context of how my sleeping is going (and vice versa – does the apnea get better or worse during that time?). Another issue is that I’m dealing with migraines, and I’d like to know something about the frequency and severity in the context of sleep.</p>
<h2>Methodology for data collection</h2>
<p>This personal data collection exercise uses an excellent piece of software specifically for journaling called <a target="_blank" rel="nofollow" href="http://www.davidrm.com/">The Journal</a>. I’ve been using The Journal since 2007 to record events and just simply jog my memory of goings on in my life. The software has a few nifty features that dovetail nicely with data collection.</p>
<h3>Daily entries</h3>
<p>The Journal splits writing up into categories. Categories can be either loose-leaf (where entries can be organized hierarchically any way you want) and daily (where entries are organized by the date of entry). If you set it up a certain way, you can have the Journal lock entries on every day except for the day you are working on. It can also automatically create an entry for the day you are working on. Very handy for just daily jouraling in general.</p>
<h3>Topics</h3>
<p>Topics are tags for specific pieces of text or entries. If you select a piece of text and tag it with a topic (say, CPAP), you can extract that piece of text later. Couple this with the Search by Topic command, and you can extract all text tagged with a certain topic into one document and save a single document with all text from that topic. So, for example, I will tag all my CPAP writings with the CPAP topic, and later on save a text file with what I have written about CPAP therapy (in this case, the data I collected).</p>
<h3>Templates</h3>
<p>The Journal has a sophisticated template system that can insert not only the same text over and over, but tag it automatically with a certain topic and even fill in certain data such as the current date and time. I use the template feature to create some structured text (a data entry form of sorts) and tag the whole piece of inserted text with the CPAP topic. That way, I don’t have to bother with selecting and tagging manually. I can simply insert the text and fill in the numbers when I read the website.</p>
<p>The template looks like this:</p>
<div>
<pre><code>Sleep numbers for &lt;ENTRYDATE format=“mm/dd/yyyy”/>
* Usage: 
* Leakage: L/min
* AHI: events/h
* Mask on/off: 
* MyAir score: 
* Comments: 
</code></pre>
</div>
<p>Because the text follows the same structure for all such entries, it is easy to write R code to pull out the data and make a <code>data.frame</code>.</p>
<p>What you don’t see (and is hard to show here) is that in the template itself I selected all of the text and tagged it CPAP. That way, my CPAP entries will always be tagged, and I can easily extract them later.</p>
<h2>Methodology for analysis</h2>
<h3>Data extraction</h3>
<p>The first part of data extraction is in The Journal. I use a saved search from the Search Entries by Topic function, then click View All Result Entries to see the text I had entered. The result is a screen showing the last 100 pieces of text I tagged CPAP (which may include other pieces of text if I felt the need to write on the topic). I can change this with an option. Clicking Save to File will allow me to save to a Journal file, and RTF, or a TXT file. I save the result to a TXT file so that I can easily read it in R. The text file contains only the data I entered for the CPAP machine, as well as any other text I tagged (which is fairly uncommon).</p>
<h3>Data import</h3>
<p>This is where I pay the price for putting the data in a diary rather than a tabular format. I use <code>readlines</code>.</p>
<figure>
<pre><code data-lang="r"><span>library</span><span>(</span><span>readr</span><span>)</span><span>
</span><span>library</span><span>(</span><span>dplyr</span><span>)</span><span>
</span><span>library</span><span>(</span><span>stringr</span><span>)</span><span>
</span><span>library</span><span>(</span><span>ggplot2</span><span>)</span><span>
</span><span>raw_file</span><span> </span><span>&lt;-</span><span> </span><span>&quot;_Rmd/cpap.txt&quot;</span><span>
</span><span>if</span><span> </span><span>(</span><span>file.exists</span><span>(</span><span>raw_file</span><span>))</span><span> </span><span>{</span><span>
</span><span># file.copy(raw_file,backup_file,copy.date = TRUE)
</span><span> </span><span>raw_lines</span><span> </span><span>&lt;-</span><span> </span><span>read_lines</span><span>(</span><span>raw_file</span><span>)</span><span>
</span><span>data_row</span><span> </span><span>&lt;-</span><span> </span><span>0</span><span>
</span><span>cpap_df</span><span> </span><span>&lt;-</span><span> </span><span>data.frame</span><span>(</span><span>date</span><span>=</span><span>c</span><span>(),</span><span>usage</span><span>=</span><span>c</span><span>(),</span><span>leakage</span><span>=</span><span>c</span><span>(),</span><span>events</span><span>=</span><span>c</span><span>(),</span><span>mask</span><span>=</span><span>c</span><span>(),</span><span>score</span><span>=</span><span>c</span><span>())</span><span>
</span><span>for</span><span> </span><span>(</span><span>this_line</span><span> </span><span>in</span><span> </span><span>raw_lines</span><span>)</span><span> </span><span>{</span><span>
</span><span>if</span><span> </span><span>(</span><span>str_sub</span><span>(</span><span>this_line</span><span>,</span><span>1</span><span>,</span><span>18</span><span>)</span><span> </span><span>==</span><span> </span><span>&quot;Sleep numbers for &quot;</span><span>)</span><span> </span><span>{</span><span>
</span><span>data_row</span><span> </span><span>&lt;-</span><span> </span><span>data_row</span><span>+1</span><span>
</span><span>cpap_df</span><span>[</span><span>data_row</span><span>,</span><span>&quot;date&quot;</span><span>]</span><span> </span><span>&lt;-</span><span> </span><span>as.Date</span><span>(</span><span>str_sub</span><span>(</span><span>this_line</span><span>,</span><span>19</span><span>),</span><span>&quot;%m/%d/%Y&quot;</span><span>)</span><span>
</span><span>}</span><span> </span><span>else</span><span> </span><span>if</span><span> </span><span>(</span><span>str_sub</span><span>(</span><span>this_line</span><span>,</span><span>1</span><span>,</span><span>9</span><span>)</span><span>==</span><span>&quot;* Usage: &quot;</span><span>)</span><span> </span><span>{</span><span>
</span><span>tm</span><span> </span><span>&lt;-</span><span> </span><span>str_extract</span><span>(</span><span>this_line</span><span>,</span><span>&quot;1?[0-9]{1}:[0-9]{2}&quot;</span><span>)</span><span>
</span><span>tm</span><span> </span><span>&lt;-</span><span> </span><span>as.numeric</span><span>(</span><span>str_split</span><span>(</span><span>tm</span><span>,</span><span>&quot;:&quot;</span><span>)[[</span><span>1</span><span>]])</span><span> </span><span>%>%</span><span> </span><span>(</span><span>function</span><span>(</span><span>x</span><span>)</span><span> </span><span>x</span><span>[</span><span>1</span><span>]</span><span>*</span><span>60</span><span>+</span><span>x</span><span>[</span><span>2</span><span>])</span><span>
</span><span>cpap_df</span><span>[</span><span>data_row</span><span>,</span><span>&quot;usage&quot;</span><span>]</span><span> </span><span>&lt;-</span><span> </span><span>tm</span><span>
</span><span>}</span><span> </span><span>else</span><span> </span><span>if</span><span> </span><span>(</span><span>str_sub</span><span>(</span><span>this_line</span><span>,</span><span>1</span><span>,</span><span>11</span><span>)</span><span>==</span><span>&quot;* Leakage: &quot;</span><span>)</span><span> </span><span>{</span><span>
</span><span>cpap_df</span><span>[</span><span>data_row</span><span>,</span><span>&quot;leakage&quot;</span><span>]</span><span> </span><span>&lt;-</span><span> </span><span>as.numeric</span><span>(</span><span>str_extract</span><span>(</span><span>this_line</span><span>,</span><span>&quot;[0-9]+&quot;</span><span>))</span><span>
</span><span>}</span><span> </span><span>else</span><span> </span><span>if</span><span> </span><span>(</span><span>str_sub</span><span>(</span><span>this_line</span><span>,</span><span>1</span><span>,</span><span>15</span><span>)</span><span>==</span><span>&quot;* Mask on/off: &quot;</span><span>)</span><span> </span><span>{</span><span>
</span><span>cpap_df</span><span>[</span><span>data_row</span><span>,</span><span>&quot;mask&quot;</span><span>]</span><span> </span><span>&lt;-</span><span> </span><span>as.numeric</span><span>(</span><span>str_extract</span><span>(</span><span>this_line</span><span>,</span><span>&quot;[0-9]+&quot;</span><span>))</span><span>
</span><span>}</span><span> </span><span>else</span><span> </span><span>if</span><span> </span><span>(</span><span>str_sub</span><span>(</span><span>this_line</span><span>,</span><span>1</span><span>,</span><span>15</span><span>)</span><span>==</span><span>&quot;* MyAir score: &quot;</span><span>)</span><span> </span><span>{</span><span>
</span><span>cpap_df</span><span>[</span><span>data_row</span><span>,</span><span>&quot;score&quot;</span><span>]</span><span> </span><span>&lt;-</span><span> </span><span>as.numeric</span><span>(</span><span>str_extract</span><span>(</span><span>this_line</span><span>,</span><span>&quot;[0-9]+&quot;</span><span>))</span><span>
</span><span>}</span><span> </span><span>else</span><span> </span><span>if</span><span> </span><span>(</span><span>str_sub</span><span>(</span><span>this_line</span><span>,</span><span>1</span><span>,</span><span>7</span><span>)</span><span>==</span><span>&quot;* AHI: &quot;</span><span>)</span><span> </span><span>{</span><span>
</span><span>cpap_df</span><span>[</span><span>data_row</span><span>,</span><span>&quot;events&quot;</span><span>]</span><span> </span><span>&lt;-</span><span> </span><span>as.numeric</span><span>(</span><span>str_extract</span><span>(</span><span>this_line</span><span>,</span><span>&quot;[0-9\\.]+&quot;</span><span>))</span><span>
</span><span>}</span><span>
</span><span>}</span><span>
</span><span>for</span><span> </span><span>(</span><span>data_row</span><span> </span><span>in</span><span> </span><span>1</span><span>:</span><span>nrow</span><span>(</span><span>cpap_df</span><span>))</span><span> </span><span>{</span><span>
</span><span>if</span><span> </span><span>(</span><span>all</span><span>(</span><span>is.na</span><span>(</span><span>cpap_df</span><span>[</span><span>data_row</span><span>,])))</span><span> </span><span>{</span><span>
</span><span>cpap_df</span><span> </span><span>&lt;-</span><span> </span><span>cpap_df</span><span>[</span><span>1</span><span>:</span><span>(</span><span>data_row</span><span>-1</span><span>),]</span><span>
</span><span>data_row</span><span> </span><span>&lt;-</span><span> </span><span>data_row</span><span>-1</span><span>
</span><span>}</span><span>
</span><span>}</span><span>
</span><span># write_csv(cpap_df,csv_file)
</span><span>}</span><span> </span><span>else</span><span> </span><span>{</span><span>
</span><span>cat</span><span>(</span><span>&quot;Oops! &quot;</span><span>,</span><span>raw_file</span><span>,</span><span>&quot; does not exist!\n&quot;</span><span>)</span><span>
</span><span>}</span></code></pre>
</figure>
<p>The commented out code above basically writes out the file to a CSV (for easier processing in the future if I need) and backs up the CSV file each time I run the analysis. (I run this analysis in an <a target="_blank" rel="nofollow" href="http://www.rstudio.com/">R Studio</a> R notebook.)</p>
<p>This code is basically a brute force conversion of the structured text of the template into a data frame – the text indicates the variable (column), and the row is given by a counter that is incremented every time a new entry, as indicated by the text “Sleep numbers for”, is read. The decisions on the variables are made in a rather old-fashioned way, with long <code>if</code> … <code>else if</code> … <code>else</code> block. The <code>str_sub</code> commands from the <code>stringr</code> package (you could also use base, if you wish) look for the substrings that I know will be present due to the template function in The Journal (and the hope that I don’t overwrite them when I record data), and the <code>str_extract</code> function will look for the numerical digits for most lines, two numbers separated by a colon (i.e. a time) for the usage line, and digits or a decimal point for the AHI line. These are converted to appropriate dates and numeric values, with the exception of the usage, which is converted to minutes.</p>
<p>The code above is slightly flawed in that there is the possibility for records that are all missing, so the last <code>for</code> block steps through and eliminates those records.</p>
<p>This is the most complicated part of the analysis! Once this data is in a <code>data.frame</code>, you can treat it as any other data analysis.</p>
<h3>Data analysis</h3>
<p>I won’t focus too heavily on the data analysis here, but just to demonstrate here is a usage graph:</p>
<figure>
<pre><code data-lang="r"><span>cpap_df</span><span> </span><span>%>%</span><span> </span><span>ggplot</span><span>(</span><span>aes</span><span>(</span><span>as.Date</span><span>(</span><span>date</span><span>,</span><span>origin</span><span>=</span><span>&quot;1970-1-1&quot;</span><span>),</span><span>usage</span><span>))</span><span> </span><span>+</span><span>
</span><span>geom_hline</span><span>(</span><span>aes</span><span>(</span><span>yintercept</span><span>=</span><span>480</span><span>),</span><span>color</span><span>=</span><span>&quot;red&quot;</span><span>,</span><span>lty</span><span>=</span><span>2</span><span>)</span><span> </span><span>+</span><span>
</span><span>geom_line</span><span>()</span><span> </span><span>+</span><span>
</span><span>xlab</span><span>(</span><span>&quot;Date&quot;</span><span>)</span><span> </span><span>+</span><span>
</span><span>ylab</span><span>(</span><span>&quot;Usage (minutes)&quot;</span><span>)</span><span> </span><span>+</span><span>
</span><span>scale_y_continuous</span><span>(</span><span>limits</span><span>=</span><span>c</span><span>(</span><span>0</span><span>,</span><span>NA</span><span>))</span><span> </span><span>+</span><span>
</span><span>scale_x_date</span><span>(</span><span>date_labels</span><span> </span><span>=</span><span> </span><span>&quot;%b %d&quot;</span><span>)</span></code></pre>
</figure>
<p><img alt="plot of chunk unnamed-chunk-1" src="https://i1.wp.com/randomjohn.github.io/figures//2017-04-17-personal-data-collection.Rmdunnamed-chunk-1-1.png?w=456&ssl=1" data-recalc-dims="1"></p>
<p>No, you don’t get to see the other stuff, how much I sleep is enough!</p>
<h2>Discussion</h2>
<p>While it might be easier in some ways to enter this data into a spreadsheet daily, I chose this method of personal data collection for several reasons:</p>
<ul>
<li>It allows me to put each day’s data into context, for example recording in prose whether that day was stressful, was a holiday, or had work pressures</li>
<li>It enables me to enter several kinds of data into a diary (e.g. migraine data, dietary data, exercise data), and use multiple extractions to correlate data</li>
</ul>
<p>Because I use The Journal almost daily, and because it has these sophisticated features, I use that as a central location for all sorts of personal data, and so it became the natural place to record my sleep habits, and it is also a good piece of software to use to record other habits. Perhaps it can also supplement Fitbits, Garmins, and other kinds of personal habit data collection workflows.</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://randomjohn.github.io/personal-data-collection/"> Upstate Data Analysis</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/DYrIWdDgDho"></a></div>