{"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.4.0"},"kaggle":{"accelerator":"none","dataSources":[{"sourceId":102335,"databundleVersionId":12518947,"sourceType":"competition"}],"dockerImageVersionId":30749,"isInternetEnabled":true,"language":"r","sourceType":"notebook","isGpuEnabled":false}},"nbformat_minor":4,"nbformat":4,"cells":[{"cell_type":"markdown","source":"# Exploratory Data Analysis\n\nTry to use R languagea with purpose comparison with notebook and share to everybody on Kaggle Commnunity!","metadata":{}},{"cell_type":"markdown","source":"## Read Data","metadata":{}},{"cell_type":"code","source":"# Load necessary libraries\nlibrary(data.table)\nlibrary(dplyr)\nlibrary(readr)\n\n# Load data\npath <- '/kaggle/input/cmi-detect-behavior-with-sensor-data/'\n\n# Read the first 200 rows of the train dataset\ntrain <- fread(file.path(path, 'train.csv'), nrows = 200)\n\n# Read the demographics dataset\ntrain_demo <- fread(file.path(path, 'train_demographics.csv'))\n\n# Merge demographics\ntrain <- merge(train, train_demo, by = 'subject', all.x = TRUE)\n\n# Replace ToF '-1' values with NA\ntof_cols <- grep(\"^tof_\", names(train), value = TRUE)\ntrain[, (tof_cols) := lapply(.SD, function(x) replace(x, x == -1, NA)), .SDcols = tof_cols]\n\n# Basic info\ncat(\"Train shape:\", nrow(train), \"x\", ncol(train), \"\\n\")\ncat(\"Number of sequences:\", train %>% distinct(sequence_id) %>% nrow(), \"\\n\")\ncat(\"Gesture classes:\", train %>% distinct(gesture) %>% nrow(), \"\\n\")\ncat(\"Missing values per sensor type:\\n\")\nmissing_values <- train %>%\n  select(acc_x, rot_w, tof_cols[1:5], thm_1) %>%\n  summarise_all(~ mean(is.na(.)))\nprint(missing_values)\n\n# Optional: downcast to reduce memory\nfloat_cols <- sapply(train, is.numeric)\nfloat_col_names <- names(train)[float_cols]\ntrain[, (float_col_names) := lapply(.SD, as.numeric), .SDcols = float_col_names]\n\n# Note: as.numeric in R is equivalent to np.float32 in Python for most purposes","metadata":{"_uuid":"f371fc2a-c838-4104-a760-b98feaac202b","_cell_guid":"a425e0a8-c84a-4230-bf5b-edeec76077b6","trusted":true,"collapsed":false,"execution":{"iopub.status.busy":"2025-06-02T04:18:07.125909Z","iopub.execute_input":"2025-06-02T04:18:07.127907Z","iopub.status.idle":"2025-06-02T04:18:07.800629Z","shell.execute_reply":"2025-06-02T04:18:07.798748Z"},"jupyter":{"outputs_hidden":false}},"outputs":[],"execution_count":null},{"cell_type":"markdown","source":"## 1. Dataset Overview","metadata":{}},{"cell_type":"code","source":"# Load necessary libraries\nlibrary(data.table)\nlibrary(dplyr)\nlibrary(readr)\n\n# Load data\npath <- '/kaggle/input/cmi-detect-behavior-with-sensor-data/'\n\n# Read the first 200 rows of the train dataset\ntrain <- fread(file.path(path, 'train.csv'), nrows = 200)\n\n# Read the demographics dataset\ntrain_demo <- fread(file.path(path, 'train_demographics.csv'))\n\n# Print shapes\ncat(\"Train shape:\", dim(train), \"\\n\")\ncat(\"Train demo shape:\", dim(train_demo), \"\\n\")\n\n# Print first few rows\nprint(head(train))\nprint(head(train_demo))","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-06-02T04:18:47.528360Z","iopub.execute_input":"2025-06-02T04:18:47.565772Z","iopub.status.idle":"2025-06-02T04:18:47.841217Z","shell.execute_reply":"2025-06-02T04:18:47.838596Z"}},"outputs":[],"execution_count":null},{"cell_type":"markdown","source":"##  2. Missing Values Heatmap","metadata":{}},{"cell_type":"code","source":"# Load necessary libraries\nlibrary(data.table)\nlibrary(dplyr)\nlibrary(ggplot2)\nlibrary(reshape2)\n\n# Load data\npath <- '/kaggle/input/cmi-detect-behavior-with-sensor-data/'\n\n# Read the first 200 rows of the train dataset\ntrain <- fread(file.path(path, 'train.csv'), nrows = 200)\n\n# Read the demographics dataset\ntrain_demo <- fread(file.path(path, 'train_demographics.csv'))\n\n# Function to create heatmap of missing values\ncreate_missing_values_heatmap <- function(data, title) {\n  # Add a row number column\n  data <- data.table(row = 1:nrow(data), data)\n  \n  # Convert data to long format\n  data_long <- melt(data, id.vars = \"row\")\n  \n  # Create a boolean column for missing values\n  data_long$missing <- is.na(data_long$value)\n  \n  # Create the heatmap\n  ggplot(data_long, aes(x = variable, y = row, fill = missing)) +\n    geom_tile() +\n    scale_fill_manual(values = c(`FALSE` = \"white\", `TRUE` = \"green\")) +\n    theme_minimal() +\n    theme(axis.text.x = element_text(angle = 90, vjust = 0.5, hjust=1)) +\n    labs(title = title, x = \"Variables\", y = \"Rows\", fill = \"Missing\")\n}\n\n# Create heatmap for train dataset\nplt1 <- create_missing_values_heatmap(train, \"Missing Values in `train` Dataset\")\nprint(plt1)\n\n# Create heatmap for train_demo dataset\nplt2 <- create_missing_values_heatmap(train_demo, \"Missing Values in `train_demo` Dataset\")\nprint(plt2)","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-06-02T04:19:36.799032Z","iopub.execute_input":"2025-06-02T04:19:36.800462Z","iopub.status.idle":"2025-06-02T04:19:38.194762Z","shell.execute_reply":"2025-06-02T04:19:38.192822Z"}},"outputs":[],"execution_count":null},{"cell_type":"markdown","source":"## 3. Subject Demographics (from train_demo)","metadata":{}},{"cell_type":"code","source":"# Load necessary libraries\nlibrary(data.table)\nlibrary(dplyr)\nlibrary(ggplot2)\nlibrary(reshape2)\n\n# Load data\npath <- '/kaggle/input/cmi-detect-behavior-with-sensor-data/'\n\n# Read the demographics dataset\ntrain_demo <- fread(file.path(path, 'train_demographics.csv'))\n\n# Set theme to white grid\ntheme_set(theme_minimal())\n\n# Age Distribution\nggplot(train_demo, aes(x = age)) +\n  geom_histogram(aes(y = ..density..), bins = 20, fill = \"steelblue\", color = \"white\") +\n  geom_density(alpha = 0.3, fill = \"red\") +\n  labs(title = 'Age Distribution', x = 'Age', y = 'Density') +\n  theme(axis.text.x = element_text(angle = 45, vjust = 0.5, hjust=1))\n\n# Sex Distribution\nggplot(train_demo, aes(x = sex)) +\n  geom_bar(fill = \"skyblue\") +\n  labs(title = 'Sex Distribution', x = 'Sex', y = 'Count') +\n  theme(axis.text.x = element_text(angle = 45, vjust = 0.5, hjust=1))\n\n# Handedness Distribution\nggplot(train_demo, aes(x = handedness)) +\n  geom_bar(fill = \"lightgreen\") +\n  labs(title = 'Handedness Distribution', x = 'Handedness', y = 'Count') +\n  theme(axis.text.x = element_text(angle = 45, vjust = 0.5, hjust=1))","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-06-02T04:20:08.344540Z","iopub.execute_input":"2025-06-02T04:20:08.346256Z","iopub.status.idle":"2025-06-02T04:20:09.216997Z","shell.execute_reply":"2025-06-02T04:20:09.215165Z"}},"outputs":[],"execution_count":null},{"cell_type":"markdown","source":"##  4. Behavior & Gesture Distributions","metadata":{}},{"cell_type":"code","source":"# Load necessary libraries\nlibrary(data.table)\nlibrary(dplyr)\nlibrary(ggplot2)\n\n# Load data\npath <- '/kaggle/input/cmi-detect-behavior-with-sensor-data/'\n\n# Read the first 200 rows of the train dataset\ntrain <- fread(file.path(path, 'train.csv'), nrows = 200)\n\n# Set theme to white grid\ntheme_set(theme_minimal())\n\n# Behavior Distribution\ntrain %>%\n  count(behavior, sort = TRUE) %>%\n  ggplot(aes(x = reorder(behavior, -n), y = n)) +\n  geom_bar(stat = \"identity\", fill = \"steelblue\") +\n  coord_flip() +\n  labs(title = 'Behavior Distribution', x = 'Behavior', y = 'Count') +\n  theme(axis.text.x = element_text(angle = 45, vjust = 0.5, hjust=1))\n\n# Gesture Distribution\ntrain %>%\n  count(gesture, sort = TRUE) %>%\n  ggplot(aes(x = reorder(gesture, -n), y = n)) +\n  geom_bar(stat = \"identity\", fill = \"lightgreen\") +\n  coord_flip() +\n  labs(title = 'Gesture Distribution', x = 'Gesture', y = 'Count') +\n  theme(axis.text.x = element_text(angle = 45, vjust = 0.5, hjust=1))","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-06-02T04:20:37.364148Z","iopub.execute_input":"2025-06-02T04:20:37.365847Z","iopub.status.idle":"2025-06-02T04:20:37.857454Z","shell.execute_reply":"2025-06-02T04:20:37.855490Z"}},"outputs":[],"execution_count":null},{"cell_type":"markdown","source":"## 5. Sequence Type and Orientation","metadata":{}},{"cell_type":"code","source":"# Load necessary libraries\nlibrary(data.table)\nlibrary(dplyr)\nlibrary(ggplot2)\n\n# Load data\npath <- '/kaggle/input/cmi-detect-behavior-with-sensor-data/'\n\n# Read the first 200 rows of the train dataset\ntrain <- fread(file.path(path, 'train.csv'), nrows = 200)\n\n# Set theme to white grid\ntheme_set(theme_minimal())\n\n# Sequence Type Distribution\nggplot(train, aes(x = sequence_type)) +\n  geom_bar(fill = \"steelblue\") +\n  labs(title = 'Sequence Type Distribution', x = 'Sequence Type', y = 'Count') +\n  theme(axis.text.x = element_text(angle = 45, vjust = 0.5, hjust=1))\n\n# Orientation Distribution\nggplot(train, aes(x = orientation)) +\n  geom_bar(fill = \"lightgreen\") +\n  labs(title = 'Orientation Distribution', x = 'Orientation', y = 'Count') +\n  theme(axis.text.x = element_text(angle = 45, vjust = 0.5, hjust=1))","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-06-02T04:21:02.142420Z","iopub.execute_input":"2025-06-02T04:21:02.144059Z","iopub.status.idle":"2025-06-02T04:21:02.595962Z","shell.execute_reply":"2025-06-02T04:21:02.594192Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"# Load necessary libraries\nlibrary(data.table)\nlibrary(dplyr)\nlibrary(ggplot2)\n\n# Load data\npath <- '/kaggle/input/cmi-detect-behavior-with-sensor-data/'\n\n# Read the first 200 rows of the train dataset\ntrain <- fread(file.path(path, 'train.csv'), nrows = 200)\n\n# Set theme to white grid\ntheme_set(theme_minimal())\n\n# Function to create summary plots for sensor columns\ncreate_sensor_summary_plots <- function(data, sensor_prefix, title) {\n  # Select columns starting with the sensor prefix\n  sensor_cols <- grep(paste0(\"^\", sensor_prefix), names(data), value = TRUE)\n  \n  # Melt the data to long format\n  data_long <- melt(data, measure.vars = sensor_cols)\n  \n  # Create a histogram for each sensor column\n  ggplot(data_long, aes(x = value)) +\n    geom_histogram(binwidth = 0.1, fill = \"steelblue\", color = \"white\") +\n    facet_wrap(~ variable, scales = \"free\") +\n    labs(title = title, x = sensor_prefix, y = 'Count') +\n    theme(axis.text.x = element_text(angle = 45, vjust = 0.5, hjust=1))\n}\n\n# TOF Sensor Columns Overview\ncreate_sensor_summary_plots(train, \"tof\", \"TOF Sensor Columns Overview\")\n\n# Accelerometer (Acc) Sensor Columns Overview\ncreate_sensor_summary_plots(train, \"acc\", \"Accelerometer (Acc) Sensor Columns Overview\")\n\n# Gyroscope (Rot) Sensor Columns Overview\ncreate_sensor_summary_plots(train, \"rot\", \"Gyroscope (Rot) Sensor Columns Overview\")\n\n# Thermometer (Thm) Sensor Columns Overview\ncreate_sensor_summary_plots(train, \"thm\", \"Thermometer (Thm) Sensor Columns Overview\")","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-06-02T04:21:26.797127Z","iopub.execute_input":"2025-06-02T04:21:26.798795Z","iopub.status.idle":"2025-06-02T04:24:41.104664Z","shell.execute_reply":"2025-06-02T04:24:41.102644Z"}},"outputs":[],"execution_count":null},{"cell_type":"markdown","source":"##  6. Sensor Columns Overview (TOF, Acc, etc.)","metadata":{}},{"cell_type":"code","source":"# Load necessary libraries\nlibrary(data.table)\nlibrary(dplyr)\nlibrary(ggplot2)\n\n# Load data\npath <- '/kaggle/input/cmi-detect-behavior-with-sensor-data/'\n\n# Read the first 200 rows of the train dataset\ntrain <- fread(file.path(path, 'train.csv'), nrows = 200)\n\n# Define sensor prefixes\nsensor_prefixes <- c('acc_', 'gyro_', 'mag_', 'tof_')\n\n# Calculate the number of columns for each sensor type\nsensor_counts <- sapply(sensor_prefixes, function(prefix) {\n  sum(grepl(paste0(\"^\", prefix), names(train)))\n})\n\n# Create a data frame for plotting\nsensor_df <- data.frame(\n  sensor_prefix = sensor_prefixes,\n  count = sensor_counts\n)\n\n# Set theme to white grid\ntheme_set(theme_minimal())\n\n# Create the bar plot\nggplot(sensor_df, aes(x = sensor_prefix, y = count, fill = sensor_prefix)) +\n  geom_bar(stat = \"identity\", color = \"black\") +\n  labs(title = 'Sensor Column Counts by Type', x = 'Sensor Type', y = 'Number of Features') +\n  theme(axis.text.x = element_text(angle = 45, vjust = 0.5, hjust=1),\n        legend.position = \"none\") +\n  scale_fill_manual(values = c(\"acc_\" = \"steelblue\", \"gyro_\" = \"lightgreen\", \"mag_\" = \"orange\", \"tof_\" = \"purple\"))","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-06-02T04:25:03.557744Z","iopub.execute_input":"2025-06-02T04:25:03.560508Z","iopub.status.idle":"2025-06-02T04:25:03.903548Z","shell.execute_reply":"2025-06-02T04:25:03.900594Z"}},"outputs":[],"execution_count":null}]}