{"metadata":{"kernelspec":{"name":"ir","display_name":"R","language":"R"},"language_info":{"name":"R","codemirror_mode":"r","pygments_lexer":"r","mimetype":"text/x-r-source","file_extension":".r","version":"4.0.5"}},"nbformat_minor":4,"nbformat":4,"cells":[{"cell_type":"code","source":"# This R environment comes with many helpful analytics packages installed\n# It is defined by the kaggle/rstats Docker image: https://github.com/kaggle/docker-rstats\n# For example, here's a helpful package to load\n\nlibrary(tidyverse) # metapackage of all tidyverse packages\n\n# Input data files are available in the read-only \"../input/\" directory\n# For example, running this (by clicking run or pressing Shift+Enter) will list all files under the input directory\n\nlist.files(path = \"../input\")\n\n# You can write up to 20GB to the current directory (/kaggle/working/) that gets preserved as output when you create a version using \"Save & Run All\" \n# You can also write temporary files to /kaggle/temp/, but they won't be saved outside of the current session","metadata":{"_uuid":"051d70d956493feee0c6d64651c6a088724dca2a","_execution_state":"idle","execution":{"iopub.status.busy":"2023-09-13T12:21:56.890095Z","iopub.execute_input":"2023-09-13T12:21:56.891670Z","iopub.status.idle":"2023-09-13T12:21:56.913048Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"df <- read.csv(\"/kaggle/input/child-mind-institute-detect-sleep-states/train_events.csv\")\n\n# Create a new column called \"sleep_state\"\ndf$sleep_state <- NA\n\n# Loop through the dataframe\nfor (i in 1:nrow(df)) {\n  # If the event is \"sleep\" and the step count is 0, then the sleep state is \"asleep\"\n  if (df$event[i] == \"sleep\" && df$step[i] == 0) {\n    df$sleep_state[i] <- \"asleep\"\n  } else if (df$event[i] == \"wake\" && df$step[i] == 0) {\n    df$sleep_state[i] <- \"awake\"\n  } else {\n    df$sleep_state[i] <- \"unknown\"\n  }\n}\n\n# Print the dataframe\nhead(df)","metadata":{"execution":{"iopub.status.busy":"2023-09-13T12:21:56.916192Z","iopub.execute_input":"2023-09-13T12:21:56.917750Z","iopub.status.idle":"2023-09-13T12:21:57.506325Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# Convert the timestamp column to a standard format\ndf$timestamp <- strptime(df$timestamp, \"%Y-%m-%d %H:%M:%S\")\n\n# Convert the timestamp column to POSIXct format\ndf$timestamp <- as.POSIXct(df$timestamp)\n\n# Create a new column for the sleep state\ndf$sleep_state <- NA\n\nfor (i in 1:nrow(df)) {\n  # Calculate the hour of the day\n    \n  hour <- hour(df$timestamp[i])\n\n  # If the hour is between 22 and 7, set the sleep state to \"asleep\"\n  if (!is.na(hour) & hour >= 22 & hour <= 7) {\n  df$sleep_state[i] <- \"asleep\"\n} else {\n  df$sleep_state[i] <- \"awake\"\n}\n}\n\nhead(df)","metadata":{"execution":{"iopub.status.busy":"2023-09-13T12:21:57.508893Z","iopub.execute_input":"2023-09-13T12:21:57.510259Z","iopub.status.idle":"2023-09-13T12:21:58.348937Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# Calculate the sleep state count for each person\nsleep_state_count <- df %>%\n  group_by(series_id, night) %>%\n  count(event) %>%\n  select(event, n) %>%\n  rename(sleep_state_count = n)\n\nprint(sleep_state_count)","metadata":{"execution":{"iopub.status.busy":"2023-09-13T12:21:58.351508Z","iopub.execute_input":"2023-09-13T12:21:58.352889Z","iopub.status.idle":"2023-09-13T12:21:58.588286Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# Scatter plot of the sleep state count vs. night\nggplot(sleep_state_count, aes(x = night, y = sleep_state_count)) +\n  geom_point() +\n  theme_minimal()","metadata":{"execution":{"iopub.status.busy":"2023-09-13T12:21:58.590830Z","iopub.execute_input":"2023-09-13T12:21:58.592217Z","iopub.status.idle":"2023-09-13T12:21:59.431814Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"library(arrow)\n\ntrain_series <- read_parquet(\"/kaggle/input/child-mind-institute-detect-sleep-states/test_series.parquet\")\nhead(train_series)","metadata":{"execution":{"iopub.status.busy":"2023-09-13T12:21:59.434484Z","iopub.execute_input":"2023-09-13T12:21:59.435952Z","iopub.status.idle":"2023-09-13T12:21:59.479988Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"plot(train_series)","metadata":{"execution":{"iopub.status.busy":"2023-09-13T12:21:59.484555Z","iopub.execute_input":"2023-09-13T12:21:59.486354Z","iopub.status.idle":"2023-09-13T12:22:00.006223Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"for (series_id in unique(train_series$series_id)) {\n  # Filter the data for the current series_id\n  df_filtered <- train_series[train_series$series_id == series_id, ]\n\n  # Calculate the mean anglez\n  mean_anglez <- mean(df_filtered$anglez)\n\n  # Calculate the standard deviation of anglez\n  std_anglez <- sd(df_filtered$anglez)\n\n  # Plot the anglez vs timestamp\n  ggplot(df_filtered, aes(x = timestamp, y = anglez, group = series_id)) +\n    geom_line() +\n    ylab(\"Anglez\") +\n    xlab(\"Timestamp\")\n\n  ggsave(\"anglez.png\")\n}","metadata":{"execution":{"iopub.status.busy":"2023-09-13T12:22:00.011858Z","iopub.execute_input":"2023-09-13T12:22:00.015014Z","iopub.status.idle":"2023-09-13T12:22:01.882660Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train_df <- merge(df, train_series, by = \"series_id\")\nhead(train_df)","metadata":{"execution":{"iopub.status.busy":"2023-09-13T12:23:00.601614Z","iopub.execute_input":"2023-09-13T12:23:00.603257Z","iopub.status.idle":"2023-09-13T12:23:00.723945Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"library(patchwork)\n# Plot the distribution of the anglez column\np1 <- ggplot(train_df, aes(x = anglez)) +\n  geom_histogram(bins = 30)\n\n# Plot the distribution of the enmo column\np2 <- ggplot(train_df, aes(x = enmo)) +\n  geom_histogram(bins = 30)\n\np1 | p2","metadata":{"execution":{"iopub.status.busy":"2023-09-13T12:26:13.231962Z","iopub.execute_input":"2023-09-13T12:26:13.233537Z","iopub.status.idle":"2023-09-13T12:26:13.689852Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"- Sleep onset labels, where 1 indicates sleep onset and 0 indicates wakefulness. The second output shows the wake state labels, where 1 indicates wakefulness and 0 indicates sleep onset.\n- The first three observations are classified as wakefulness, the fourth observation is classified as sleep onset, and the fifth observation is classified as wakefulness.","metadata":{}},{"cell_type":"code","source":"# Thresholds for sleep onset and wake state\nsleep_onset_threshold <- 0.5\nwake_state_threshold <- 1.0\n\n# Calculate the anglez and enmo z-scores\nanglez_zscores <- (train_df$anglez - mean(train_df$anglez)) / sd(train_df$anglez)\nenmo_zscores <- (train_df$enmo - mean(train_df$enmo)) / sd(train_df$enmo)\n\n# Sleep onset and wake state labels\nsleep_onset_labels <- ifelse(anglez_zscores > sleep_onset_threshold, 1, 0)\nwake_state_labels <- ifelse(enmo_zscores > wake_state_threshold, 1, 0)\n\nhead(sleep_onset_labels)\nhead(wake_state_labels)","metadata":{"execution":{"iopub.status.busy":"2023-09-13T12:38:58.213790Z","iopub.execute_input":"2023-09-13T12:38:58.215909Z","iopub.status.idle":"2023-09-13T12:38:58.256689Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{},"execution_count":null,"outputs":[]}],"kernelspec":{"name":"ir","display_name":"R","language":"R"},"language_info":{"name":"R","codemirror_mode":"r","pygments_lexer":"r","mimetype":"text/x-r-source","file_extension":".r","version":"4.0.5"}}