{"cells":[{"metadata":{"_uuid":"8f2839f25d086af736a60e9eeb907d3b93b6e0e5","_cell_guid":"b1076dfc-b9ad-4769-8c92-a6c4dae69d19","trusted":true},"cell_type":"markdown","source":"# R Quick Start and SetUp\nA quick notebook in charge of read all the data, parse the json columns and replace the missing values with NA to finally start exploring the dataset."},{"metadata":{"_cell_guid":"79c7e3d0-c299-4dcb-8224-4455121ee9b0","_uuid":"d629ff2d2480ee46fbb7e2d37f6b5fab8052498a","trusted":true,"_kg_hide-output":true},"cell_type":"code","source":"# Data Exploration\nlibrary(skimr)\n\n# Data Manipulation\nlibrary(tidyverse)\nlibrary(magrittr)\nlibrary(jsonlite)\n\n# Modeling\nlibrary(recipes)","execution_count":null,"outputs":[]},{"metadata":{"_uuid":"73138c61b7db3d8646a0337c3abade0ac4481dd5"},"cell_type":"markdown","source":"## Data loading and cleaning..."},{"metadata":{"trusted":true,"_uuid":"8dfad1aec5525b9a8832786e73e2346a23de0890","_kg_hide-output":true},"cell_type":"code","source":"#'\n#' For the given json column, parse it adding all the features into a flatten dataframe.\n#' @param df the original dataframe\n#' @param column the column to parse\n#' \njson_to_columns <- function(df, column){\n    # Intercept the column instead of evaluating it.\n    column <- enquo(column)\n\n    df %>% \n        # Extract the desired column\n        pull(!!column) %>% \n        # Create a JSON list with one entry per row.\n        # Collapse all the rows, separated by a , and a vector\n        paste(collapse = \",\") %>% \n        paste(\"[\",.,\"]\") %>% \n        # Parse the created json (one object = one row)\n        fromJSON(flatten = TRUE) %>%\n        # Replace with NA all the NA markers\n        mutate_all(funs(\n            ifelse(. %in% c('not available in demo dataset', '(not set)', '(none)', '(not provided)'), NA, .)\n        )) -> json_df\n\n    df %>% \n        # Remove the json column from the dataset\n        select(-!!column) %>% \n        # Append the new columns and return\n        bind_cols(json_df) %>%\n        # Remove the columns that only contains NA\n        select_if(~sum(!is.na(.)) > 0)\n}","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"96df83ee26993b581f6d49688f0a98c25b3b64b2","_kg_hide-output":true},"cell_type":"code","source":"# Extract all the json information to columns\nread_csv('../input/train.csv') %>%\n  json_to_columns(device) %>% \n  json_to_columns(geoNetwork) %>% \n  json_to_columns(totals) %>% \n  json_to_columns(trafficSource) -> train\n\nread_csv('../input/test.csv') %>%\n  json_to_columns(device) %>% \n  json_to_columns(geoNetwork) %>% \n  json_to_columns(totals) %>% \n  json_to_columns(trafficSource) -> test\n\n# Create all columns that were completelly empty\ntrain %<>% \n    mutate(transactionRevenue = log1p(as.numeric(transactionRevenue))) %>% \n    select(-campaignCode, -socialEngagementType)\ntest %<>%\n    mutate(transactionRevenue = NA) %>%\n    select(-socialEngagementType)\n\nwrite_csv(train, './train.min.csv')\nwrite_csv(test, './test.min.csv')","execution_count":null,"outputs":[]},{"metadata":{"_kg_hide-output":false,"trusted":true,"_uuid":"738a94f653012413ae96b72ac85246d324840493"},"cell_type":"code","source":"utils:::format.object_size(file.info(\"../input/train.csv\")$size, \"auto\")\nutils:::format.object_size(file.info(\"./train.min.csv\")$size, \"auto\")","execution_count":null,"outputs":[]},{"metadata":{"_uuid":"005220f2fc9c8e1deef309b6c67c269d99b43bb3"},"cell_type":"markdown","source":"## Data preparation using Recipes"},{"metadata":{"trusted":true,"_uuid":"51ff4295d96f6a2ac24b265b14525a5b4f5c2c25"},"cell_type":"code","source":"# Pre-processing and Transformation Recipies\ntrain %>%\n  recipe(transactionRevenue ~ .) %>%\n  # Zero Variance Filter:\n  step_zv(all_predictors()) %>%\n  # Validation:\n  check_cols(all_predictors()) %>%\n  # Prepare the recipe:\n  prep(training = train, retain = TRUE) -> the_recipe\n\ntrain  <- juice(the_recipe)\ntest   <- bake(the_recipe, newdata = test)","execution_count":null,"outputs":[]}],"metadata":{"kernelspec":{"display_name":"R","language":"R","name":"ir"},"language_info":{"mimetype":"text/x-r-source","name":"R","pygments_lexer":"r","version":"3.4.2","file_extension":".r","codemirror_mode":"r"}},"nbformat":4,"nbformat_minor":1}