Instructor: Dr. Aric LaBarr


What is/are Data?

Data is defined by Merriam Webster as:

Data

Factual information used as a basis for reasoning, discussion, or calculation.

Inference is derived from data. Inference is using information (data) to come to some conclusion. We want to use the information to draw conclusions and make better decisions in the context of our problem. However, the quality of our data drives the quality of our results / inference that arise from any analytics problem. The old saying is that “rotten eggs make a rotten soufflé.” The same applies to any data driven analytics problem. Amazing data professionals with the fanciest machine learning techniques cannot make up for horrible data quality.

First, we need to look at our data before building any models to try and explain/predict our categorical target variable. For this analysis we are going to the popular Ames housing dataset. This dataset contains information on home values for a sample of nearly 1,500 houses in Ames, Iowa in the early 2000s.

Let’s see how to do this in each of our software!

Here are some common considerations when it comes to data before we really get started with heavy analysis:

  • What to do with missing values?

  • Do we have the right variables?

  • How do we reduce the variables we have to a reasonable amount?

Each of those above questions should be thought about and addressed before any real data analysis begins. Let’s explore each of those in more detail.

Missing Values

Missing values are one of the most common problems in real world datasets. However, in their natural form, most (not all) machine learning algorithms cannot handle missing values. If you were to give a typical machine learning algorithm data with missing values it would perform complete case analysis. Complete case analysis is the process of only looking at observations (rows) in your dataset that contain no missing values. This could pose problems. The example below has 80 data points and only 8 missing values scattered throughout it:

Example Data with Missing Values

Although only 10% of all of the data points are missing, the example below shows that only 3 of the 8 observations have no missing values:

Complete Case Analysis

Therefore, even though only 10% of the data points are missing, only 37.5% of all of the data rows would be used to build a model if you wanted to do complete case analysis in the above example. Even if you have millions of observations and had plenty of complete cases, not addressing missing values poses a problem for scoring / predicting new observations that have missing data points.

To address these problems, there are 3 possible solutions:

  1. Delete

  2. Keep

  3. Replace

Deleting Solution

One possible solution to missing values is to delete entire variables that have too many missing values. A common threshold is 50%. If more than half of the observations in a variable are missing, then you should consider removing that variable all together.

Missing Observation Solution - Delete

The business impact of deleting a variable from an analysis should be considered before any variable is deleted. However, if more than half of the observations in a variable are missing, then you might not even know what a majority of that variable truly looks like. This would make imputation (the replace solution to missing values) very questionable as over half of your variable would be generated data and not real.

Keeping Solution

One of the biggest misconceptions around missing values is that they are bad. Missing values are not necessarily bad as they might even be predictive of the outcome. For example, if we were predicting fraudulent claims, missing a home address in demographic information might be a strong predictive signal of fraud.

Categorical variables with missing values are the easiest variables to correct and solve the missing observation problem. If you have a categorical variable with missing values, you should just convert the missing values into their own separate category.

Missing Observations Solution - Keep

By creating a new category in the categorical variable, you do not lose any information. Another possible solution is to replace all missing values in a categorical variable with the most common category. However, this is completely unnecessary as it results in a loss of information.

Replacing Solution

The last common solution to missing observations is replacing these missing values - a process called imputation. As mentioned above, imputation should never be the first option with categorical variables. However, with continuous variables, we cannot just add a new category. With continuous variables we can replace the missing values using a couple of different approaches:

  1. Simple mean / median

  2. Predictive model using other variables

The most common imputation would be replacing the missing values in a variable with a common value of that variable such as the mean or the median of the variable. In a lot of instances this approach is the best approach to imputation.

More recently another approach to imputing missing observations is to use predictive models and other variables to impute the observations. For example, you could use the other predictor variables in your dataset to predict a realistic value for the variable you are imputing. There are some immediate issues that arise from this approach. Multicollinearity (which will be discussed in more detail in the following sections) is a problem with machine learning models where predictor variables are too correlated with each other. By using other variables you to impute values, you are inherently creating a relationship between variables. Unfortunately, only having one variable with missing values in a dataset is quite rare, so a more realistic situation is that many predictor variables are all being used to impute values for each other which further creates high correlation among them. Lastly, a lot of empirical studies have shown that predictive modeling approaches to imputation are no better than the more simplistic mean / median imputation at making machine learning models better at identifying signals in datasets.

In either approach, one additional step is needed - creating a missing flag variable.

Missing Observation Solution - Replace

When imputing continuous variables, we must always create a missing value flag to go along with our newly imputed variable. In the example above, the median of the observations is 32. The two missing observations are imputed with this value. However, we do not want the model to think these are real values of 32 that could also appear in the dataset. We want the model to potentially treat these observations differently and not just think of them as real observations with the value of 32.

Feature Engineering

In the world of machine learning, the importance of good variables cannot be understated. Better variables will always beat fancier modeling techniques. No amount of complicated algorithms can make up for variables that are not predictive. We can help make our variables better through the process of feature engineering.

Feature Engineering

Process of transforming raw data into variables (called features) that are suitable for machine learning models.

How we engineer features in the data completely drives the success of any modeling process that comes afterwards. Good feature engineering involves understanding your data. There are a lot of feature creation programs that will just generate high quantities of features based on common transformations and combinations of your existing variables. However, most of these don’t provide a lot of valuable information since those programs do not understand your data and the context of your business problem.

Let’s work through an example. The following is a common problem in machine learning scenarios to argue for complicated algorithms:

Classic Argument for Fancy Models

In the problem, we have two predictor variables called \(x_1\) and \(x_2\). We are trying to predict which observations are X’s and which are O’s. Some people would argue for fancier machine learning algorithms that can better discover these complicated relationships to identify the difference between the X’s and the O’s. Let’s examine this problem though after some easy feature engineering:

Feature Engineered Variables

The above plot is the same as the previous one, just with new variables. Instead of the original \(x_1\) and \(x_2\) variables, we have used those variables to generate new variables called distance and direction. These variables are the distance from the center of the first plot along with the direction away from the center. Notice in the second plot that it is easy to isolate the X’s from the O’s based solely on an easy cut-off on the distance axis.

Transactional Data

Transactional data contains many different rows of data for each individual in the dataset, typically gathered over time. These types of datasets lend themselves very well to feature engineering.

Model Developed Data vs. Transactional Form

These transactions are rich with information about the individuals in our dataset. There are many different industries where transactional data plays an important role. Some examples include credit card purchasing data, medical and insurance claims data, retail purchasing data, etc. There are some great advantages to transactional data as the data is highly detailed and captures individual behaviors of the customers. This leads to strong correlations in predicting the target variable.

There are some challenges to transactional data due to the complexity and detail they contain. Transactional data can be hard to obtain, process, and store. It also poses problems when it comes to modeling. Most modeling approaches need one line per unique observation, not multiple lines of data. We must aggregate our transactional data for modeling. To not lose the valuable information inside the data, we must creatively engineer features that capture the important information from our transactions.

Summarize categorical variables with counts of transactions in each category, or proportion of observations in each category, as well as the total unique number of categories in a time frame. Of course, all of the continuous variable aggregations could also be calculated for each category as well.

Categorical Feature Engineering

For continuous variables you can think about simple metrics like the average or sum across all transactions. Don’t limit yourself to these basic summaries though. The entire distribution of values provides potentially important information. Perhaps the maximum amount ever spent on a credit card helps, or the interquartile range of transaction amounts.

Continuous Feature Engineering

Those distributional features that are created might also be valuable if broken up into different time ranges for stratification. Instead of just look at all transactions in the dataset, maybe examining the most recent transactions (most recent 30 days for example) would be worthwhile. In fact, a comparison of the most recent transactions compared to the overall trend might prove valuable.

Stratification of Last 30 Days for Transactions

These stratifications can also be done by categories of a variable instead of by time. For example, if a categorical predictor variable has 3 categories (A, B, C) then we can calculate features by each category.

Stratification by Category

Features that summarize changes across time provide value as well. The slope of a trend line through the data describes if the variable is trending up or down. Compare these across different time periods to really see impact. For example, the overall trend may be slightly increasing, but the trend over the past week is actually decreasing.

Features Summarizing Over Time

The best engineered features are ones derived by the creativity of the analytics team working with the data. The value of the modeling comes from the features just as much as the algorithms. Simple aggregations will not suffice. Analytics professionals in this field must think about the business problem at hand and try to engineer features that would describe this. For example, in life insurance fraud, the length of time before a claim is made in which an increase in coverage limit occurs might be important. If a fraudulent claim is about to be made, the fraudster might try to increase the policy coverage limit right before.

Variable Reduction

One of the key struggles of business analytics problems is to reduce the typically immense amounts of variables to a workable list. There are a variety of techniques ranging from rather simple to much more complex:

  • Common, Basic Techniques:

    • Business logic / context

    • Too much missingness

    • Low (or no) variability

    • Univariate statistical testing (Section on Model Building)

  • More Advanced Techniques:

    • Automatic feature reduction (Section on Model Building)

    • Regularization (Section on Regularized Regression)

    • Variable Clustering (Section on Unsupervised Learning)

Business Logic

Even though we are in the world of data analysis and analytics, it doesn’t mean that business logic and knowledge should be left out of the variable reduction process. Good business logic is a quick and easy way to help reduce the number of variables that you have. Common things to remove would be any individual identifiers, information you are not allowed to use, and information you wouldn’t know at decision point, but do after the fact.

Individual identifiers are rarely useful in a modeling context. This is because a new individual in a dataset would never be able to be modeled. In our Ames, Iowa housing dataset, this kind of information would be things like street address. Other examples outside of our dataset would be customer ID’s, social security numbers, phone numbers, etc.

We have to be careful to use information in models that would not be allowed to use. This information would be considered private or protected information that might be illegal to use. For example, gender or race would be protected and not allowed to be used to model whether someone should be given a loan.

Lastly, we should remove any information that would not be known at the point of our business decision making process. For example, if we are trying to model the sale price of a home, we probably will not know what type of sale it would be. Another example would be hoe much money was lost on a defaulted loan when we are modeling if someone should get a loan.

Let’s remove some variables in each of software!

Missingness

As mentioned above in the section titled Deleting Solution, one possible variable reduction is to remove variables with too much missingness. For our data we will check the count of missing values for each variable. Any variable with more than 730 observations missing will be removed.

Let’s see how to do this in each of our software!

Now that we have removed the variables with too much variability, we can go ahead and perform the “keep” solution to missing values. For all of our categorical variables we can impute missing values with the value of “Missing” as its own, new category.

Let’s see how to do this in each of our software!

We can not yet impute continuous variables because that would involve calculating means and medians on the whole dataset, which is not allowed until we split our data into training and testing subsets.

Low Variability

Another common reason to remove a variable is low or no variability. If a variable has the same value for every observation (no variability) then it cannot be predictive of the target. If a variable never changes, we cannot answer the question, “what happens to our target if our predictor changes?”

The same logic applies to variables with really low variability. For continuous variables, if the variable has a variance less than 0.01 we should consider it removal. For categorical variables, if a variable has 1 category for more than 95% of the data it should be considered for removal. Both of those thresholds are just common thresholds and not set in stone. Your data might require different thresholds. Lastly, business logic around the importance of a variable should always override these thresholds and a variable’s removal.

Let’s see how to do this in each of our software!