Showing posts with label sleep project. Show all posts
Showing posts with label sleep project. Show all posts

Monday, March 19, 2018

Spreadsheet Tasker Plugin

I collect various types of data on my phone such as sleep data using Tasker. Traditionally, I've saved the data to a local file on my phone (typically a csv) and then used an app to transfer that data file to Google Drive.

I recently found a Tasker plugin app called Spreadsheet Tasker Plugin that will communicate directly with a Google spreadsheet. This approach is much easier.

In the case of my sleep data, each time I complete a sleep instance, it adds a row to a Google sheet with the data.

I have my phone set up via Tasker to start a sleep instance when the phone is oriented face-down and end the sleep instance when it no longer orients face down. I have a timer built in so that it only records the data after 30 minutes to avoid accidental downward orientations or instances where I haven't fallen asleep yet.

The paid version offers off-line buffering which stores the data if you aren't connected until a connection is made.

Google Play Store: Spreadsheet Tasker Plugin

Friday, December 25, 2015

Fitbit Data to Google Spreadsheet Via IFTTT

In a previous post, I described a method I've been using to collect Fitbit data into a Google spreadsheet for use in things like R.

Since then, I've found two things that make that process even easier.

One is an R package, googlesheets, that easily loads data from a Google spreadsheet into an R dataframe. More on that in another post.

The other is the use of IFTTT to load connect to my Fitbit account and send the data to a Google spreadsheet. IFTTT has connections to both Fitbit and Google Drive. It makes it easy to set up a connection for both activity data and sleep data. I currently have two IFTTT recipes, one for activity data and one for sleep data.



Saturday, December 28, 2013

FitBit Data to Google Spreasheet

I received a FitBit for Christmas and while the website provides access to a lot of data a graphs, I wanted a way to access the data in a format to process with R (statistics).

I found the following blog post which points to the use of a Google script that accesses the data via the developer API and populates a Google spreadsheet.

http://citizen-statistician.org/2012/09/30/getting-data-from-fitbit/

It was pretty straight forward since I've worked a bit with Google scripts before.

It provides daily summary data across a dozen or so categories. What would be nice is more granular data.

Monday, December 9, 2013

Sleep Segments

First graph of the number of sleep segments per night. This, combined with the duration of sleep would be the two most significant measurements of the quality of sleep. So far, it appears I am improving in my sleep by waking less during the night.

(click to view)

R - Converting Column to Date

In my sleep data, I have a date column that was originally a character type. When plotted in ggplot(), it was listing every date in the x axis, making the labels very crowded.

Then I added the following code

dailytotals$EndDate <- as.Date(dailytotals$EndDate , "%m-%d-%Y")

which converted it to a date type. Then ggplot automatically created tick marks and labels every 15 days (for data that spans two months.

Note that the %Y (capitalized) recognizes a four digit year where %y (lower case) recognizes a two digit year.

Saturday, December 7, 2013

Initial Sleep Analysis

This is my first post sharing my initial analysis of my sleep data. I've collected data for two months now using Tasker on my Android phone. It collects the start and end date/time of each sleep period as well as why I woke up.

I've just started using R and love it. Here is the first graph showing the total number of hours slept each night. Weekends are colored red because I wanted to know if I slept better on the weekends (it doesn't seem to matter).

Here is the graph:
(click to view)
I had a few days in November where my tasker action didn't save all the sleep intervals. On those days, I added estimate data. I may remove those dates altogether. There may be other dates that are incomplete. I have a could of days with less than four hours and I can't remember if that is correct or not. That probably has an impact on the dip in sleep duration in November.

Generally, I am getting more sleep that I expected, averaging around 6.5 to 7 hours per night.

Next I need to factor in the number of sleep segments to come up with a measurement of sleep quality (greater duration and fewer segments represents better sleep). I also need to consider why I woke up. If I was interrupted by an alarm, for example, and that cut my sleep shorter than it would have been naturally, it's not necessarily fair to treat that as a dependent variable when considering factors that affect sleep quality.

Here is the R Script I used (via RStudio):
library(methods) 
library(lubridate)

sleep <- read.csv("C:/Users/xxxxxxx/Documents/R/Sleep/sleepdata.csv", header=T)

sleep["EndDate"] <- NA

sleep$EndDate <- ifelse(sleep$EndTime > 12, format(mdy(as.character(sleep$Date)) + days(1), format="%m-%d-%Y"), format(mdy(as.character(sleep$Date)), format="%m-%d-%Y"))  #fill column with date of the morning of each sleep period

sleep["Weekday"] <- NA

sleep$Weekday <- weekdays(mdy(sleep$EndDate))  #fill column with weekday of the sleep period

sleep  #display data in case I want to review

dailytotals <-unique(within(sleep, {
  Duration <- ave(Duration, EndDate, FUN=sum)rm(Date,StartTime,EndTime,Status)
}))  #create frame, one record per day with total number of hours slept

dailytotals  #display in case I want to review

ggplot(dailytotals, aes(x=EndDate, y=Duration, group=1, colour=Weekday)) +
  geom_point() +  #plot points on graph
  stat_smooth(level=.99) + #regression/curve line with 99% certainty range
  theme(axis.text.x = element_text(angle = 90), axis.title.x = element_text(angle = 0), axis.title.y = element_text(angle = 0)) +  #labels, turn x axis vertically
  scale_color_manual(values=c(Saturday="red", Sunday="red", Monday="blue", Tuesday="blue", Wednesday="blue", Thursday="blue", Friday="blue"))  #color code weekend days

Friday, November 1, 2013

Sleep Data - Getting Started

Occasionally, I have trouble sleeping. The most common issue is waking up in the middle of the night, typically around 2am, and having trouble getting back to sleep.

There are lots of variables that could impact my sleep and rather than just guessing, I wanted to start collecting my actual sleep times and other events that could impact sleep. Then I can look for potential causal relationships.

I've been using Tasker on my Android phone to collect sleep data. When I turn the phone face down, it sets that time to a variable. Then when the phone is picked up (and it's been more than 1/2 hour), a dialog asked why I woke up. Then it records the date, time, duration, and reason to a new line in a csv file on the phone. That file is synced once a day to my Google Drive so I can work further with it.

I also have a Tasker dialog that allows me to collect events that could affect sleep such as caffine, stress, nap, etc. That records the date, time, and reason to another csv file.

I've started to learn how to use R, a statistics programming language/environment, to analyze the data but still very early in that process. Specifically, I've been using R Studio.