In this blog series I will document my work on the Kaggle ACEA Smart Water Analytics Competition. The purpose is of these blogs is not to share perfect code or completely worked out solutions. Rather I want to take you along for the ride and share my insights as is. There will definitely be some fails and hopefully a success mixed in. In this series I will discuss 4 distinct steps in my project:

- Challenge goals and initial discussion
- Data setup for modelling
- Data modelling
- Refinements and final insights

If you want to follow along with any code in these blog series then sign up for the challenge here: https://www.kaggle.com/c/acea-water-prediction/overview. You can download the dataset for free after signing up.

## The challenge: ACEA Smart Water Analytics Competition

The goal of this challenge is to predict water levels in a collection of different water bodies based in Italy. Specifically we have to predict based on a time series model. The goal is to accurately assess the water level of tomorrow, based on data of today. This specifically is an analytics challenge, which means creating a compelling story & notebook is a very important part. My notebook is publicly available on Kaggle here. I will work through some code excerpts and interesting highlights in these blogs.

For today I want to address some of the data setup steps. This is important to prepare your data for machine learning models.

### The how-to’s on setting up your data

Data setup for machine learning models sounds a bit mystical. This is the crucial step after our initial insights. Where we shape our data to allow for answers on our main research question. Here we add features to our data, transform our variables and put in the business logic. It is the business and can make or break your findings.

There are roughly 2 ways to go about setting up your data optimally for your machine learning efforts. There is the traditional approach and the modern approach. In this blog we will compare both and show code examples for both. To start, in the traditional approach we build our feature set and then clean up our dataset before we start our modelling. An example is by removing variables that have a high correlation. In the modern approach we load all features into the model. Only after running the model we trim our data and rerun the model.

Another way of comparing a traditional approach with a modern approach is as follows. For the traditional approach we use our own reasoning and knowledge of statistics to optimize the model outcome. The modern approach replaces these steps with raw computing power.

## ACEA Smart Water Analytics Data Setup; Traditional approach

In the traditional approach for data setup we define the following steps:

– Feature addition

– Data cleaning

– Statistical preprocessing

### Feature addition

We will work with the Auser dataset from the ACEA Smart Water Analytics Competition on Kaggle. For further explanation of the dataset please refer to the introduction blog. It includes a total of 5 outcome variables. Each of these variables can and need to be used for prediction with the final notebook. For illustration purposes we will focus on the LT2 variable. By the data definition this is the only water level measurement in the south part of the aquifer. Today we will focus as predictors on the rainfall and temperature variables in the Auser dataset.

One of my main considerations were related to the temporal aspects of the dataset. Specifically the time lags associated with both the temperature and the rainfall variables in the dataset. Hence it is a logical first step to incorporate lags of these variables in our dataset. In the future I can imagine there are other parameters to consider here. One example may be averaging rainfall for multiple measuring points or using a running average of multiple days.

### Data cleaning

In this part of the process we normally remove missing values. This can be done in multiple ways which are described in depth elsewhere. In short, we can simply remove all values or In a business environment it is often a time intensive step to retain as much data as possible, for example by using imputation to fill up the missing values. This is where your business logic comes in and its an important area to focus on when your presented with real life ‘dirty’ datasets. For now we simply remove all values that are incomplete.

Another important step here is outliers, we find that some measurements in the LT2 variable are unrealistic, from one day to the next the water level moves from the average to 0 and back to the average. This is a faulty measurement and needs to be removed from the data.

### Statistical preprocessing

Now for the most important pre-processing step in the traditional approach, we clean up our variables by removing highly correlated variables (since they have a similar effect on the outcome measure of choice) and near zero variance variables (variables who are for example nearly always 0).

These steps help us gain an understanding of how the data works, and therefore help us better understand how the model gets its final outcome. Finalizing all these steps we find that 18 variables remain, using those in our initial testrun model we find an Rsquared of the model 15%. This is not the best result, but for a first try I’ve had a lot worse!

The next step in the traditional approach is to start tinkering, initially we work backwards, hence we look at our excluded variables and find different cutoff values for the different steps. This will help us expand our model, we add features and attempt to improve the model performance through understanding its inner workings. This step can take days to weeks, depending on how often we rerun our model.

## ACEA Smart Water Analytics Data Setup; The modern approach

In what I call a modern approach to data setup we skip the statistical test processing, instead we simply load the data into the model and try to optimize the predictive power. Afterwards we evaluate our model and if possible rerun a simplified version. When running the model without any statistical tests, but with a similar feature set and data cleaning we find an Rsquared of the model at 33.7%. This gives us some idea of the potential in the data.

## Code

Below you find my function for preprocessing and running my model both in the traditional and modern approach. I find that writing this in a function helps keep my code clean, and this function can be applied to all datasets in the Kaggle ACEA challenge as needed.

create_data_model <- function(data, features, outcome, lag, preprocess = NULL){ data_model <- data[,c(outcome,features)] names(data_model)[which(names(data_model)== outcome)] <- 'outcome' # Feature addition for(i in 1:length(features)){ for(j in 1:lag){ data_model$temp <- Lag(data_model[,features[i],+j]) names(data_model)[which(names(data_model)=='temp')] <- paste(features[i],j, sep = '_') } } # Data cleaning data_model <- data_model[complete.cases(data_model),] # Remove all rows with missing values data_model <- data_model[which(data_model$outcome!= 0),] # Remove all outlier measurements # Statistical preprocessing if(!is.null(preprocess)){ temp <- data_model[,-1] nzv <- nearZeroVar(data_model) # excluding variables with very low frequencies temp <- temp[, -nzv] i <- findCorrelation(cor(temp)) # excluding variables that are highly correlated with others if(length(i) > 0) temp <- temp[, -i] i <- findLinearCombos(temp) # excluding variables that are a linear combination of others if(!is.null(i$remove)) temp <- temp[, -i$remove] data_model <- data_model[,c('outcome', names(temp))] } # Modelling: fitControl <- trainControl(## 10-fold CV method = "repeatedcv", number = 10, ## repeated ten times repeats = 3, verboseIter = T) gbmGrid <- expand.grid(interaction.depth = c(1,2,4,8), n.trees = 1:2000, shrinkage = c(0.01,0.001), n.minobsinnode = c(2,5)) err <- try(load(paste(maindir,modeldir, paste('outcome =',outcome,'lag = ',lag,'preprocess = ',preprocess,'.RData', sep = ''),sep = '/'))) if(err != 'train1'){ train1 <- train(outcome ~ ., data= data_model, method = 'gbm', trControl = fitControl, tuneGrid=gbmGrid) save(train1, file = paste(maindir,modeldir, paste('outcome =',outcome,'lag = ',lag,'preprocess = ',preprocess,'.RData', sep = ''), sep = '/')) } train1 } tr_preproc <- create_data_model(data = data_auser, features = grep('Rainfall|Temperature',names(data_auser),value = T), outcome = 'Depth_to_Groundwater_LT2', lag = 15, preprocess = 'yes') tr_nopreproc <- create_data_model(data = data_auser, features = grep('Rainfall|Temperature',names(data_auser),value = T), outcome = 'Depth_to_Groundwater_LT2', lag = 15, preprocess = NULL)

## Discussion

So how to setup your data for modelling? We saw that the traditional approach leaves you with more understanding of the problem at hand. The downside is it also required more time investment and often demands more business logic to deal with the problem. The modern approach skips these steps and moves straight to optimize predictive power. It allows us to utilize machine learning techniques to optimize the data setup.

Comparing the 2, we found that simply loading all information into the model got us to 33% predictive power, not a bad score. Our first try of using some statistical concepts to preprocess the data only got us to 15%. It did take my laptop 6 hours to run the model on the ‘modern approach’, the traditional model was done after 20 mins.

By heart I am a traditionalist, having studied as an economist I strive for understanding a problem, thinking it through and finding a solution. Ultimately to use the model we often need this complex understanding that you develop through the extra time investment and hard work. How else are we going to convince other people to use a model for their business problem?

When do I prefer the modern approach? When for a particular business problem the outcome is more important than the process, or when the assumption is that the important variables in the dataset that influence the outcome can’t be influenced themselves. This Italian water level issue is actually a perfect example of a situation that tills quite heavily towards the modern approach. As we find that temperature and rainfall influence water levels, and we can’t actually influence temperature or rainfall, all we really care about is to predict the water level. It would be great if we know that the temperature on a mountain is highly predictive for this, but once we know this we can’t influence it in any way.

## ACEA Smart Water Analytics Competition next steps

I will be back soon with a more in depth look at the modelling process I’ve gone through for this challenge. Further improvements can definitely by made, such as transformations of the data and making use of lags on the outcome variable.

## 2 thoughts on “ACEA Smart Water Analytics Competition; Data Setup”