# %% [code]
---
title: 'EDA with R'
author: 'Peter Hahn'
date: '`r Sys.Date()`'
output:
  html_document:
    number_sections: true
    toc: true
---

## Importing packages

This R environment comes with many helpful analytics packages installed
It is defined by the kaggle/rstats Docker image: https://github.com/kaggle/docker-rstats
For example, heres a helpful package to load

## This notebook is inspired by the work of @Marilia Prata
https://www.kaggle.com/code/mpwolke/zzzs-wake-up-kagglers


## Load libraries
The library arrow is required for reading .parquet files. 
```{r message = FALSE}
library(tidyverse) # metapackage of all tidyverse packages
library(arrow)
library(pryr)
library(skimr)
```

```{r}
list.files(path = "../input")
```

### Read train_events and modify timestamp with lubridate
Be careful with timestamps: I substracted 4 hours to get the right time and days.
See: https://www.kaggle.com/competitions/child-mind-institute-detect-sleep-states/discussion/440868

## Read events
```{r}
events <- read_csv("../input/child-mind-institute-detect-sleep-states/train_events.csv")
events <- events %>% mutate(dt = as_datetime(timestamp)) %>% 
        mutate(dt = dt - hours(4))  %>% mutate(hr = hour(dt))

```

```{r}
head(events)
```


### Event counts
```{r}
events %>% count(event)
```

### Distibution of wakeup 
```{r}
events %>% filter(event == "wakeup") %>% 
        ggplot(aes(hr)) + geom_density()
```
### Distibution of onset
```{r}
events %>% filter(event == "onset") %>% 
        ggplot(aes(hr)) + geom_density()
```


## Read training data
This will take some time
```{r}
train <- read_parquet("../input/child-mind-institute-detect-sleep-states/train_series.parquet") %>% 
        mutate(dt = as_datetime(timestamp)) %>%
        mutate(dt = dt - hours(4)) %>% 
        mutate(hr = hour(dt))
```
### Object size:
```{r}
object_size(train)
```

```{r}
head(train)
```


### Anglez- Z Angels
```{r}
summary(train$anglez)
```

```{r}
train %>% ggplot(aes(anglez)) + geom_density()
```


### Enmo
```{r}
summary(train$enmo)
```

```{r}
train %>% ggplot(aes(enmo)) + geom_density()
```








```