Skip to content
ImagineWorld

The Modeling Process

node_idnode-book_practical-predictive-analytics_chapter-02-the-modeling-process
node_typechapter-node
schema_profileworld.chapter_node.v1
statusactive
entropyhigh
manifoldworld
domainimagineworld.net
authorityinforms
layercorpus
chapter_num2
chapter_titleThe Modeling Process
governed_byCORPUS
constitutional_citationCONSTITUTION
local graph

Remember that all models are wrong; the practical question is how wrong do they have to be to not be useful.

-George Edward Pelham Box

Today, we are at a juncture in which many different types of skill sets are needed to participate in predictive analytics projects. Once, this was the pure domain of statisticians, programmers, and business analysts. Now, the roles have expanded to include visualization experts, data storage experts, and other types of specialists. Yet, so many are unfamiliar with an understanding of how predictive analytics projects can be structured. This lack of structure can be inhibited by several factors. Often there is a lack of understanding of the critical parts of a business problem, and a model is developed much too early. Alternatively, a formal methodology may be put off to the future, in favor of a quick solution.

In this chapter, we will start by discussing the advantages of using structured analytics methodologies. Methodologies are a great way to demonstrate a value to the upper management, and groups or individuals advocating for a structured approach is one way to obtain buy-in from the management. If the project gains acceptance using a methodology, there is a strong possibility that future projects will have support.

We will cover the following points in this chapter:

  • Advantages of a structured approach
  • Analytic process methodologies
  • An outline of the specific steps used within an analytics process methodology
  • Short descriptions of specific analytic techniques, with code examples

Analytic projects have many components. That is where a structured methodology can help. Many benefits can be gained if there is a structure which is placed upon discovery and analysis, rather than only on pure model building. The discovery and insight gained will certainly be utilized past the original intent of the problem.

We assume that the quick-thinking “hare brain” will beat out the slower Intuition of the “tortoise mind.” However, now research in cognitive science is changing this understanding of the human mind. It suggests that patience and confusion—rather than rigor and certainty—are the essential precursors of wisdom.

-Guy Claxton

Here are several points to bear in mind concerning the advantages of structured methodologies:

  • Data is coming at us fast and furious. We need to keep track of the many data sources, evaluate which ones are the best ones to use at any given time and continually monitor them for data accuracy. Expect changes to come quicker than expected. Predictive modelers need a structured methodology to be able to keep track of things; changes can be disruptive at whatever stage of the modeling process they are in.
  • The difference between useful data and data masquerading as useful data is increasing. Structured methodologies help with maintaining metadata repositories for information, which can help in determining what data is useful and what is not.
  • The number of data analysis techniques are increasing. Knowing which analytical techniques to choose can be a daunting task. Dedicating projects purely for evaluating which data techniques are more useful than others for a particular business problem is a laudable goal.
  • Structured methodologies help with objectivity. Everyone has their own subjective technical biases that they bring to the table. Creating a structured way for sharing code and results can encourage out-of-the-box thinking.
  • Incremental Improvement: Often projects are too large or ambitious. Projects can be organized in a way which offers small ways to provide value. This is easier to attain when projects are encapsulated within a structured methodology.
  • Iterative analytics development uses structured techniques that enforce good data analytics practice such as being able to iterate in small steps. If any discrepancies are found later on, it is relatively easy to backtrack through the incremental updates.
  • Divide and conquer helps to organize projects involving multiple team members who work on different parts of project
  • Reproducibility helps analytic teams reproduce the same results again and again. This has always been important in research, but also has implications for any large-scale data project, in which you can be dealing with a multitude of data raw sources. Often, one needs help with understanding transformed data sources in which business rule transformations are unclear and can be changed without your knowledge. Certainly, this is also important when implementing version control, but this is also important when you are upgrading packages and need to recreate results. When data sampling is involved, it might also be possible that the original selection criteria which produced the sample is no longer available, and that reproducibility may be lost. Therefore, it is important to develop sample strategies in a structured way, which are robust and can be reproduced with future analyses.

There are several analytic process methodologies which are currently practiced; however, I will be discussing only two longstanding methodologies that have been in existence for a while, CRISP-DM and SEMMA, which can help you organize your journey from problem definition to insight.

Cross-Industry Standard process for Data Mining (CRISP-DM) and Sample, Explore, Modify, Model, and Assess (SEMMA) are two standard data mining methodologies that have been utilized for many years and describe a general methodology for implementing analytical projects. There is a good deal of overlap between the methodologies, even though the names for each step are different. All of the listed steps are important to the success of a predictive analytics project. However, it is not necessary that these steps be followed exactly in order. The concepts outlined are more or less an outline of best practices. It helps to be aware of the importance of each of these steps, and understand how each step is built upon the knowledge of the previous ones.

Although these steps are listed in sequence for reference, you will discover that in practice, they are more iterative and that you will often be cycling back to a previous step. This often happens when you discover information, which is in conflict with what you have previously discovered.

As an example, many times you believe that you are finished with the data preparation stage, only to find during the modeling stage that you have discovered a glitch in the data collection, and that you need to perform more data prep to accommodate certain conditions. One solution might be to cycle back, try to remedy the data situation, while at the same time see how you can continue with your modeling. This often entails ‘coding around’ the problem by setting flags, or maintaining different include files, versions, and so on. It always pays to code defensively when dealing with data.

If you examine the differences between SEMMA and CRISP-DM in the following chart, you will notice that steps 2-5 are similar in approach.

Note that SEMMA adds sampling as an initial phase and CRISP-DM begins with business understanding and ends with model deployment.

Step #CRISP-DMSEMMA
1Business understandingSample
2Data understandingExplore
3Data preparationModify
4ModelingModel
5EvaluationAssess
6Deployment

Critical to both processes is the concept of visualizing and communicating results to stakeholders. Note that visualization is not considered a separate step. Always try to include a presentation layer (plots, charts, and visualizations) within each of the steps, so that communications between all of the predictive analytical stakeholders are facilitated.

Since developing a predictive model is iterative, agile approaches such as Scrum and Kanban work well within a structured framework, especially when they are tied to product delivery. However, knowledge discovery, proof of concept, and model incremental change improvements also benefit from an agile approach. When used as part of the business understanding process, these techniques can also be useful in grouping lists of questions into an agile backlog, which can then be incorporated in sprints.

Six sigma has been around for a long time and is associated with process improvement. As a result, it has developed its own set of methodologies and techniques, which deal with examining business problems and their solutions. Knowledge of basic statistical techniques is useful. Often a root cause analysis step (which is an important part of six sigma) is followed in order to understand why a data quality issue is occurring. For example, data can be passed along through multiple internal and external systems and be subject to many manual updates and system glitches and failures. In complex systems, it is often unclear where the errors occur. Six sigma can also be used to examine some causal relationships using some very simple techniques such as the 5 Whys (Determine the Root Cause: 5 Whys.)

Sampling is specified as step 1 of the SEMMA process (but not specific to CRISP-DM), so I will cover this separately.

Traditionally, predictive analytics have started with sampling. Sampling is particularly important in certain industries (such as pharmaceutical and healthcare), which begin with experimental studies. Sampling is also important in studies which you follow groups of people over a long period of time (cohorts). However, other kinds of data projects are not research type projects, and they are more machine learning oriented. Given that, I hold the belief that many algorithms are easier to work with (and are more powerful) if the data follows certain statistical properties, such as transforming raw data into data which follows normal distributions, or constructing training data so that there are an equal number of observations for various sub segments of the population. Sampling can be critical in preparing the data for these algorithms, so that data is more representative of the larger dataset.

Here are some other advantages to sampling:

  • With a smaller sample, you will have the ability to get to know all of your data. If necessary this means going down to the record level. This is something that would not be practical with very large datasets. This also enables you to ask more specific detailed level follow up questions, in addition to larger group level questions.
  • Sampling allows you to contemplate potential interesting subsets of the population. You can first random sample a smaller subset of something that looks interesting, and then test to see if that group behaves heterogeneously.
  • Sampling also speeds up algorithmic development. Spending a lot of time running algorithms on very large datasets is often not productive, since you can spend a lot of processing time running memory-consuming algorithms. If you anticipate a lot of future tweaking for the algorithm, I suggest testing your algorithms first on a smaller representative sample, and then scaling up to the much larger dataset.
  • With sampling, you are more in control of the ultimate data quality of your samples. I say this since you would have already done your legwork by looking at the characteristics of your population first, and then designing an appropriate sampling strategy which minimizes biases.
  • Performing Bootstrap (repeated) sampling can help illustrate any biases in large data samples. If you see some strange stuff appear via sampling, there is a good chance that there is a much larger problem in the larger data.

However, many data scientists will choose to use the entire population as the basis of analysis, instead of a sample. I suspect one reason is that often it is difficult to obtain a reliable sample, especially when the data has many different sources, and some are of unknown data quality. Good reliable data costs money to collect. Another reason is that the data scientists may not be familiar with sampling techniques, or believe that more is always better. Therefore, it is important to understand some important points about using all of the data:

  • You can use all of the data if it accurately represents the underlying population. If all the data is the underlying population, you can’t do better than that, but proceed with caution. What you think is a population may just be a very large sample. Future data may reflect a reality that is completely different. For very large data sets, you can never be quite sure if what we are looking at IS representative, since the amount of data can simply be overwhelming, and the underlying data collection methods may be unknown or unreliable. Be particular careful for data which is collected over long periods of time. Often data collection methods change, or the way calculations are performed are altered.
  • When you use an incredibly large amount of data, what you are measuring is not necessarily representative of the factors which motivate the response. For example, clickstream data and online survey data is not always representative of the why? Deeper inspection (through smaller samples) is always advisable to examine motivation.
  • As mentioned earlier, processing huge amounts of data consumes a lot of computational resources. Processing accurate samples can take a fraction of that time
  • With large datasets, you will just about always find correlations somewhere. Related to this is the concept of significant correlation versus effect size. This means that even if you find a statistically significant correlation, the differences can be so slight, so as to render any association meaningless or nonactionable.

So the first line of attack for this is to try to understand how the data was collected. It may be biased towards certain age groups, genders, technology users, and so on. There may be ways to fix this using a technique known as oversampling in which you weigh certain under-represented groups in a way which more realistically represents their frequency.

To illustrate some of the benefits of sampling, and to see how you can often get close to the same results with a sample as with a larger population, copy the following code and run it within an R script. This script will generate a 15,000,000 row population and then extract a 100-row random sample. Then we will compare the results:

large.df <- data.frame(
gender = c(rep(c("Male", "Female", "Female"), each = 5000000)),
purchases = c(0:9, 0:5, 0:7)
)
#take a small sample
y <- large.df[sample(nrow(large.df), 100), ]
mean(large.df$purchases)
mean(y$purchases)
#Render 2 plots side-by-side by setting the plot frame to 1 by 2
par(mfrow=c(1,2))
barplot(table(y$gender)/sum(table(y$gender)))
)barplot(table(large.df$gender)/sum(table(large.df$gender))
#Return the plot window to a 1 by 1 plot frame
par(mfrow = c(1, 1))

Switch over to the console and note that the sample mean is close to the population mean:

> mean(large.df$purchases)
[1] 3.666667
> mean(y$purchases)
[1] 3.64

Now, switch over to the plot area and note that the distribution of gender is also similar when comparing the sample (left plot), and the population (right plot):

I encourage you to take some large datasets on your own, practice taking some random samples and seeing how close you get to population results. Possibly that will enable you to work faster and in a more productive manner.

This section will look at each of the analytics methodology steps individually. I will use CRISP-DM as the template, because it covers model deployment, and we have already mentioned the benefits of sampling (which is the first step in SEMMA).

Many predictive modelers assume that the actual modeling phase is where the most insightful model development takes place. However, much of the groundwork and insight can be discovered early on, and a good understanding of business objectives can avoid pitfalls later on.

I must admit, business people and technical people can be better at communicating with each other. How business goals are communicated can run the gamut. It can be anything from a business partner stating, “Tell me how sales need to be increased” or “Tell me something I don’t know.”

So, it really starts with understanding what the specific business objectives are. During this step, you will also get involved in breaking down the babble and buzzwords into specifics so that communication can begin using data as a common language. As exchanges begin between the domain experts and data scientists, it will become clear that business people often do know what they are looking for in terms of outcomes, but do not understand the analytic modeling process well enough to verbalize their goals. This is often the case in reverse for technical people who do not understand the nuances of the business.

The understanding phase is characterized by people asking many questions, and establishing an analytic dialogue between all parties. So, even with a more precise question like, “Who are our best customers?”, the dialog can go as follows:

  1. What do you mean by best?
  2. Do you mean best now or best compared to what it was a year ago?
  3. What metric are you using to define best? Do you mean best in terms of revenue or in terms of frequency of purchase?
  4. How will we measure success? What are the key metrics? What are the possible outcomes?

The process of business understanding also involves an understanding of where, how, and when you obtain your data.

Your own internal data is the first and best place to start. Many projects start with examining data from business operational systems, such as sales systems. You can start by analyzing transaction data, such as looking at all orders for a customer and analyzing the number of units sold, total sales price, and so on. However, transaction data by itself does not tell you anything about the customer. You need to also enrich your basic data by augmenting it with other attributes which can be obtained by matching common elements of your database transactions (such as customer ID, product keys) with valuable data which exists in other related databases. For example, a rich source of data is demographic and attitudinal data which may be stored in company customer surveys.

In addition to customer surveys, there may be other internal repositories that you can use to derive insight. Web logs may reveal more information about customer clickstream buying patterns. Data can be captured from customer call center data, HR and other administrative systems.

Finding other data sources often involves a lot of digging, since often internal data is siloed in large organizations.

Data can also be augmented by integrating a company’s internal data combined with data obtained from a variety of sources, including government data, social media feeds, and data purchased from vendors. Often demographic, behavioral, and risk data is purchased separately, and then merged within the data warehouse.

Beware of the daunting task of data integration. A key problem encountered in this task is being able to associate data from a variety of disparate sources with each other. This is less of a problem when dealing with internal data, but when dealing with external data you may have to use alternative methods available to perform matching, such as using fuzzy match (similarity) algorithms or performing entity extraction methods (which, for example, can extract a customer name). These are just two ways which can help you with associating data with external sources.

Defining the objectives can be tough at the beginning, and it is not advisable to start using advanced analytic tools at the business understanding stage. The best tools to use at this stage are somewhat low-tech and include important concepts such as process understanding, data dictionaries, and data linkage, and Structured Query Language (SQL).

An understanding of how data is generated in your domain as well as how it has been generated in the past is important. This means understanding how data has been transformed from its original raw state to how it is consumed by the ultimate user of the data. Understanding the imperfections of the current process will enable you to get a sense of which data can be consistently relied upon. Additionally, try to gather a history of what data changes and methodologies have been attempted in the past. This is important since the different ways that data used might have been quite different, and the goals might have been different. Knowing what has succeeded and what has failed in the past will prevent you from reinventing the wheel, keep you from replicating the very same mistakes, and help you build upon what has been attempted before.

Data lineage tools are helpful for documentation, and for following the changes of the nuanced meaning of data as it progresses from raw data form, to data storage, and then to the final definition. The general term for the type of data that lineage tools deal with is known as metadata (data about data).

Data dictionaries can be valuable as a source for understanding the types of variables under analysis and how they are measured. Here are some useful metadata items to keep in a data dictionary:

  • Name of the variable: Consistency in naming conventions helps in understanding and readability. Some analysts like to use CamelCase, others like to use punctuation for an object, as in Variable.data.frame, and others will insist on only having lowercase letters.
  • Measurement data: This answers questions such as, “Is the data numeric or categorical?”, “How many levels are contained in each category?”, and “What is the length of each variable?”
  • Sources of the data: This covers, “Where did the data originally came from?”
  • Transformations: This answers, “How was the data manipulated from its original form to what it is now?”
  • Data quality items: Attaching frequency distributions and summary statistics to each variable will be helpful, along with any comments regarding any questionable data quality.

It is important to keep a data dictionary up to date, because variables or their values can change meaning over time. For example, a marketing offer with the value of A100 could mean we give a 15% discount to the customer now, but upon closer inspection, the same A100 code happened to be used five years ago in a different marketing application where it meant 10% discount to the customer.

Here is an example of a simple data dictionary:

Variable nameThis is the name of the data element used in your program.
Business source elementThis is the mapping of the variable to a business element. If the variable is a new or transformed variable, list the logic that is used to create or transform the variable.
Type of variableThis indicates the type of variable: nominal, interval, binary, continuous, and so on.
Variable roleThis indicates whether it is a potentially dependent, independent, or informational variable.
Additional metadataThe total sales include Widget1 up until 2016. 10% missing values in sales for Northeast due to system glitch in February 2016.

As you begin to explore your dataset, you can also add additional columns, such as the percentage of missing values, variable means, extreme values, and so on.

SQL has been referred to as a universal query language. One of the reasons to learn SQL is that it has a relatively common syntax that runs on a variety of operating systems. Most importantly, knowledge of SQL is paramount for communicating ideas with managers who are familiar with it. While queries can also be done natively in R via native tools and packages, I prefer SQL as a first query language since work performed using SQL in R can then be modified to run natively within other systems, where the code can be optimized in its native environment, in order to make it run faster.

In this example, we will use the R package sqldf to show the difference in sales and units in four sales regions. Note that this is an artificial example since we will first generate separate data for the West region with sales figures that are higher than the other three regions:

install.packages("sqldf")
library(sqldf)
set.seed(10)
rows=100
y <- rbind(
data.frame(indv=factor(paste("TransId-", 1:100, sep = "")),
Sales=rnorm(rows, mean=1500000,sd=100000),
Units=round(rnorm(rows,mean=10, sd=3)),
Region=sample(c("North","East","South"),rows, replace=TRUE)),
data.frame(indv=factor(paste("TransId-", 101:200, sep = "")),
Sales=rnorm(rows, mean=2000000,sd=100000),
Units=round(rnorm(rows,mean=10, sd=3)),
Region=sample(c("West"),rows, replace=TRUE))
)
query <- "select Region,avg(Sales),avg(Units) from y group by Region"
results <- sqldf(query,stringsAsFactors = FALSE)
results

The results will appear in the console window:

This is randomly generated data. The functions rnorm() and sample() in the written code are giveaways. Generating random data is a great way to begin to test code and algorithms since you will always have a better idea of what kind of results to expect, given the assumptions.

At the business understanding phase, it is not necessary to get too complex with visualizations. The reason I say this is that in some instances complex visualization can influence interpretation, rather than provide objective data. Basic charts and plots, which indicate the relationship between the variables are more than adequate for communicating ideas.

When passing charts and plots back and forth during this phase, it is a good idea to include basic statistical measures of distribution and association this will give management a rough idea of how significant the relationships are, rather than having them rely purely on the visual representation.

Sending the data to spreadsheets and creating pivot tables is another way to get management involved in the process. Spreadsheets have definite disadvantages, but they are heavily used in the industry and knowing how to manipulate spreadsheets will be another way that you can communicate with managers.

Simulation is a tool which can demonstrate what-ifs. This technique is very useful if there is not a lot of historical data to go by, and you need to make assumptions about the behavior of some of your variables. Typically, you do not have a lot of data at this point, so you are really constructing your own data, based upon how you think it will behave.

In the later sections of the book, I will use simulation techniques heavily to illustrate this.

Here is an example that is based upon a business estimate that one of three customer contacts will result in a sale. Another assumption is that if a sale is made, it will result in an average of 100eachwithastandarddeviationof100 each with a standard deviation of 5.

ExpectedPayoff either produces 0 revenue or a figure that hovers around $100, as specified in line 5 of the following code:

library(ggplot2)
set.seed(123)
CustomerAcquired.Flag <- sample(c(0,0,1), 100, replace = TRUE)
Revenue <- sample(rnorm(100,100,5))
ExpectedPayoff <- CustomerAcquired.Flag*Revenue
head(ExpectedPayoff)
PayoffCompare = ggplot(data.frame(ExpectedPayoff), aes(x=ExpectedPayoff)) + stat_bin(binwidth=5, position="identity")
PayoffCompare

We can view this conditional customer acquisition problem as two separate charts contained within one visualization. The left side of the visualization is the count of customer contacts which result in no revenue, and the right side shows the histogram of binned sales when they occur:

In this example, we can simulate calls to the customer service which might occur at the beginning and at the end of the week. Management projects that weekend volume will be heavy, with an average of 1500000 calls and an average turnaround time of 4 minutes. Customer service call volume for Monday-Thursday is estimated as handling 1000000 calls with an average turnaround time of 1 minute. We could use this simulation to build a model to include weekends versus nonweekends as new variables in a predictive model:

library(ggplot2)
library(grid)
library(gridExtra)
set.seed(123)
MonTuesWedThurs=rnorm(1000000,1,1)
FriSatSun=rnorm(1500000,4,1)
weekly = c(MonTuesWedThurs,FriSatSun)

If we were interested in looking at the difference in call volumes, we could look at them individually (as shown in the top row of the following plot) or as a combined distribution for weekends and nonweekends. The combined plot is one way of illustrating the difference between weekends and nonweekends, and shows that they have similar shapes, but different mean values:

p1 = ggplot(data.frame(FriSatSun), aes(x=FriSatSun)) + stat_bin(binwidth=0.25, position="identity")
p2 = ggplot(data.frame(MonTuesWedThurs), aes(x=MonTuesWedThurs)) + stat_bin(binwidth=0.25, position="identity")
p3 = ggplot(data.frame(weekly), aes(x=weekly)) + stat_bin(binwidth=0.25, position="identity")
grid.arrange(p1, p2, p3, ncol = 2)

Once an objective is established and data sources have been identified, you can begin looking at the data in order to understand how each data element behaves individually, as well as how it interacts in combination with other variables. But even before you start looking at the values of variables, it is important to understand the different types of data levels of measurement and the kind of analyses you can perform with them.

Levels of measurement is a classification system for classifying data into 4 different categories which is discussed as follows (ratio, ordinal, interval, and nominal). It is an important aspect of the project or studies metadata.

Levels of measurement is important in the world of predictive analytics since the specific measurements will often dictate which algorithm or techniques can be applied. For example k-means clustering does work if you want to incorporate nominal data, and logistic regression can not use ratio data as a dependent variable (although you can transform variables to a lower scale).

Nominal data, sometimes referred to as categorical data, simply defines a class or group without any natural order. Nominal variables are usually character data, but not always. Your gender, the cell phone brand you prefer, and the type of book you are reading now are all examples of nominal variables. Sometimes it can be tricky, since the number 1 and 2 written on a sign are also nominal variables when they are used to designate two possible waiting lines you may need to visit at the motor vehicle bureau. Nominal data only designates classes and you cannot perform any mathematical operations on them, such as addition or subtraction.

When characterizing nominal data, the only measure of central tendency is the mode, which is the most frequently occurring value. Computing means or medians cannot be performed.

In the R language, we can convert character data to factors so that we can analyze them as nominal data. That will be an important part of the cleaning process, which we will learn about later on. When analyzing nominal data, frequencies of occurrence (counts) are a good starting point. Alternatively, you can begin by grouping calculations with other nominal variables, in order to determine counts, or averages of other numeric variables.

Ordinal data only has rank order. Statements can be made such as A>B and B < C, but you cannot compute the numeric differences between any of the groups. A top ten list is an example of the presentation of ordinal data. Often categorical data can be classified as ordinal data if they have an implied order, such as two categories labeled Better, and Best. One problem with ordinal data is we never know what is the exact difference between each of the ranked data points.

This is numeric data in which you can measure distances but cannot take ratios. Temperature data and calendar data are two examples of interval data. All of the arithmetic differences between two consecutive data points on the numeric scale are measured exactly the same way, so you can take meaningful differences.

Often, interval data is treated as ratio data. This is often seen when performing calculations such as averages on likert scales (even though this is not technically correct). With ordinal data, you can compute frequencies, calculate the median, and compute percentiles but you cannot do any arithmetic or calculate a mean.

The highest level of numeric data is ratio scale. With ratio data, you can make meaningful comparisons using division and subtraction. It is meaningful to say that you have acquired twice as many customers when you went from 50 to 100 customers in one month. Ratio data also adds 0 to the domain of numbers (which interval data does not contain), so it is meaningful to say that you have 0 customers (although not desireable). Weight and income are other examples of ratio data. however, the fahrenheit and celsius temperature scale do not have a 0 meaning no temperature. That creates some interesting comparisons if you would try to make temperature comparisons. For example, in fahrenheit, a temperature of 36/18 equals 2, but the equivalent celsius is 2.22/-7.78 which equals 0.28 which suggests that ration comparisons are meaningless, and that both sides should be treated as interval.

To learn more about the theory of scales and measurement refer to On the Theory of Scales and of Measurement (Stevens, 1946).

As a general rule, you can always transform a higher level of measurement to a lower level of measurement, but not the reverse. Often this is done via a technique known as binning in which you place all values with a certain range into a bin. You may want to take various ranges of height and bin them into tall, short, or medium based upon predefined ranges. However, you would generally not take a categorical variable and map it to a numeric variable, unless you knew a lot about how that category was measured and you were willing to make a lot of assumptions.

The dependent variable is the variable that you are predicting. It can also be referred to as a target variable, response variable, or outcome variable. One goal of predictive modeling is to derive a prediction for the dependent variable based upon some function of the independent variables. A dependent variable is typically fixed and you cannot manipulate it. Independent variables are variables that you choose in the belief that they are important in determining the outcome of the dependent variable, also based upon some function. This function is typically applied using an algorithm.

Transformed variables are variables that you create which did not exist in the raw data. Here are some examples of how you can create transformed variables:

  • You can bin a numeric variable into several distinct categories such as High (all numeric values greater than 10) or low or any value less than 10. Notice that binning results in losing information, but allows you some flexibility by being able to name things and promote understandability
  • Transforming count data into percentages by dividing a cell count by the total population.
  • Standardizing the data in the modeling phase; it is often useful to work with standardized variables, as opposed to using the raw data itself. A standardized variable is a transformation which forces a mean of 0, and a standard deviation of 1. This transformation preserves the distribution and structure of the original values, but makes it easier to compare one set of variables with another variable with a different scale.
  • In regression modeling, it is common to replace or add another variable with a transformation such as log() or exp() so that the resultant model has a better linear fit.

After taking inventory of all your potential candidate variables, it makes sense to start with single variable analysis. Why complicate things by looking all at multiple variables at once when you can start looking at them one at a time? Often, the results of modeling will suggest immediate elimination of a variable for inclusion in a model, such as one with a high percentage of missing values, or one with data quality issues. Eliminating variables early is often the best course, rather than carrying them through an analysis only to discard them later on. This is especially true if you find two variables which measure the same thing.

It is usually a good idea to begin analysis with look at summary statistics for every candidate variable which might appear in a model. Means, standard deviations, frequencies, and skewness allow quick generalization about how you expect a variable to behave. Special consideration should also be given when examining any variable deemed to be a target variable.

To display basic summary statistics about a single variable, use the R Summary() function. This can be issued as part of an R scripts or directly from the command line. The general form of the Summary() function is:

#for a single variable
Summary(dataframe$variable)
#for all of the variables
Summary(dataframe)

Two other typical single variable analysis summary techniques to use are:

  • Distributions: A distribution plot (histogram, probability density plot, and so on) will give you an idea of what a variable looks like versus a theoretical statistical distribution. Some modeling techniques will require some assumptions regarding distributions and others will not. If you find that a variable follows a particular distribution, you will be lucky. That will make modeling easier later on. In R, you can use the hist() function.
  • Boxplots: A boxplot is a simple graphic representation of a distribution, which always includes five key elements of a distribution minimum, first quartile, median, third quartile, and maximum. (Refer to Comparing treatments in the next section for an example the R function boxplot(), which produces these five key elements.)
    • The box has the upper and lower lines of the box representing the third and first quartile, with the median line in between.
      • The whiskers has the top line representing the maximum and the bottom line representing the minimum.

Bivariate analysis is typically the next step you would take after single variable analysis. Bivariate analysis is used to show the relationship between any two variables. You are performing bivariate analysis for two reasons:

  • In a modeling framework, one key outcome we are looking for would be any association between the target variable and any one of the independent variables.
  • Another, equally important outcome we are looking for is any possible associations between any two of the independent variables. That will help us understand which of the independent variables are correlated with each other, or help us understand how one variable changes the behavior of another.

As you examine the relationship between any two variables, here are some key points to keep in mind:

  • Do the two variables move in the same direction as one changes, or do they move in opposite directions? Is there one critical value in either of the variables that alters the relationship?
  • Are the changes in the values dramatic, or are they slow and steady?
  • Is there strength in the relationship, and is the relationship linear, or is there curvature?
  • Are there specific subsets of data that have correlations that are more interesting than others?

The kinds of plots and charts one uses to display these relationships are dependent upon the variable data types. For interval and ratio scale data, we will group them together into one category and refer to them as quantitative data.

Therefore, when we look at bivariate relations between quantitative and nominal data, we can have three basic combinations:

  • Quantitative with quantitative
  • Nominal with nominal
  • Nominal with quantitative

Scatterplots are often used to show relationships between two quantitative variables. In the following code, we will use the R pairs() function to plot some associations. As you can see, Petal.Width has a strong relationship with Petal.Length (row 3, column 4), while the relationship between Sepal.Width and Sepal.Length (row 1, column 2) is not as strong.

pairs(iris[1:4], bg=c("green","blue","brown","yellow","black","orange"), pch=21)

We will look at versus nominal versus nominal in two different ways. One using the table style and the other using the graphic method.

Cross-tabulations are a good starting point for examining the relationships between nominal variables. There are many ways to do this in R. I like to start off by using the CrossTable() function from the gmodels package, since it will give not only the cell counts but also the frequencies for the rows and columns totals, and supply a chi-square statistic to measure the statistical significance. It may not be pretty but it does the job well.

See the following code:

install.packages("gmodels")
library(gmodels)
CrossTable(mtcars$cyl, mtcars$gear, prop.t=TRUE, prop.r=TRUE, prop.c=TRUE)
> CrossTable(mtcars$cyl, mtcars$gear, prop.t=TRUE, prop.r=TRUE, prop.c=TRUE)

Mosaic plots will also display cross-tabulations graphically. You can visually see that eight cylinders and three gears represent the largest car cylinder/gear offering (37.5% of cars shaded in the preceding contingency table), while the eight cylinder and four gear combinations seem to not exist. This is also shown in the boxed value of the preceding contingency table, along with the dotted line in the following mosaic plot:

These comparisons are often examined using basic bar charts or, in the following example, using side-by-side box-plots. These boxplots give very clear comparisons.

The following code simulates two ad campaigns A, and B with two different outcomes:

set.seed(123)
rows=100
a <- data.frame(Sales=rnorm(rows,mean=75,sd=5),
Treatment=factor(c("Campaign A")))
b <- data.frame(Sales=rnorm(rows,mean=80,sd=5),
Treatment=factor(c("Campaign B")))
combined=rbind(a,b)
#Boxplots which compare treatments
boxplot(Sales~Treatment,data=combined, main="Comparing Treatments",
xlab="Treatment", ylab="Sales")

Another basic technique that you could use if one of the categories is a nominal variable with only two classes and the other variable is quantitative would be point biserial correlation. However, since this technique uses Pearsons correlation coefficient, you need to make certain assumptions about the distributions of the data. For example, the data needs to be normally distributed and have equal variance. This assumption holds for our simulated sales example, since each category is generated using the rnorm() function which simulates 2 randomly distributed data frames with different means, but identical standard deviations (variances)

To show point biserial correlation, we can use our previous sales treatment. First, map each of the sales treatment to numeric variables:

Treatment_num <- as.integer(combined$Treatment)

Next, we can use the R table function simply to see how the two marketing campaigns have been mapped to numeric. Campaign A maps to number 1 and campaign B maps to number 2.

table(combined$Treatment,Treatment_num)

Now, run a biserial plot. You can restrict the x axis to the values 1 or 2, since you know in advance that there are only two values:

ggplot(combined,aes(x=as.integer(Treatment_num), y=Sales))
+ geom_point(size=1.5,shape=1)
+ geom_smooth(method=lm)
+ scale_x_continuous(breaks=c(1,2))
+ ggtitle("Point BiSerial Correlation of Campaign with Sales")
+ labs(x="Campaign Number",y="Sales")

Having numeric data has its advantages. Since the data is now in numeric form, we can also run a correlation test. The output shows correlation between sales for the two campaigns is 39:

cor.test(Treatment_num,combined$Sales)

As was mentioned in Chapter 1, Getting Started with Predictive Analysis, one purpose of data preparation is preparing an input data modeling file, which can go directly into an algorithm. In theory, the input file will encompass all of the knowledge gained in steps 1 and 2. Ideally, this file will consist of a target variable, all meaningful predictor variables and other identification variables to aid in the modeling process, and any additional variables which would have been created based on the raw data sources. Data preparation, such as the previous steps outlined is an iterative process. Here are some typical steps you might follow when preparing the data:

  • Identifying the data sources: These are the critical data inputs that you will need to read in and manipulate. They can be sourced from various data formats such as CSV files, databases, or XML or JSON files. They can be in structured format or unstructured format.
  • Identify the expected input: Read in some test samples and closely examine the input to see if it is what you expect. More often than not, there will be some formatting problems that can be easily fixed, and you will probably want to rename variables and change some data types from character to numeric and vice versa.
  • Perform further data quality and reasonability checks: After you have performed your own internal checks, cross-reference the data you observe with existing metadata information (such as data dictionaries, if available), domain experts, and other data artifacts which already exist to see if what you are reading is what you expected. Sometimes you will need to join the data with other lookup or reference tables in order to obtain what you are really looking for.
  • Expanding the number of input records: After reading in and getting a test sample, you will probably want to read in a much larger sample of records and variables beyond what you read in the initial stage. Determining how much data to read in at this stage is a decision you will need to make. If you read in all of the variables, you may encounter memory problems later on, and if you read in just what you need, that may force you to go back later on and incorporate additional data or variables. Try to get a good representative sample of data at the beginning. Later on, it may make sense to rewrite your code to read in only a subset of the variables that you really need.
  • Perform some basic data cleaning: Although cleaning the data is an important part of the modeling process, it is important not to over clean. An example of over cleaning would be attempting to fix every missing value via imputation. Models can co-exist with a reasonable amount of bad data and will perform better in the long run if some variation is included.
  • Try some aggregation: Aggregate some of the data by some basic categories and observe the results. Consider whether using aggregated data instead of individual data will help speed model development while achieve models results than are similar to those achieved from using the individual observations.
  • Create new variables or transformations: This is where binning and standardizing variables come into play.
  • Identify key variables: Even though modeling has begun, you should be able to preliminarily identify which variables are important using a combination of bivariate analysis, SQL, correlations and other exploratory tools. It is not critical to identify the most important variables, but only the ones that may have some predictive power, or are already known to be relevant.
  • Join any of the input files you have examined into one single analytics file. Now you are ready for the modeling phase.

In the modeling stage, you will pick an appropriate predictive modeling technique that fits your problem and apply it to your data. There are several factors which influence the selection of a model:

  1. Who will use the model?
  2. How will the model be used?
  3. What are the assumptions of the model?
  4. How much data do I have?
  5. How many variables do I need to use?
  6. What is the accuracy level needed by the model?
  7. Am I willing to trade some accuracy for interpretability?

Particularly related to the last point is the concept of bias and variance.

Bias is related to the ability of a model to approximate the data. Low bias algorithms are able to fit the data with little error. While this may seem to an advantage all of the time, it can result in a complex model which is unstable, and difficult to explain. On the other hand, a high bias model is relatively simple to explain (like linear regression), but may sacrifice some accuracy for explanability, and stability. You will usually start by looking at your data and matching it up with a choice of potential algorithms. A linear regression, for example, which tries to fit data which forms a U-shaped distribution would not be an appropriate algorithmic choice for that problem, regardless of the number of parameters selected or how the coefficients were adjusted. For example, 5-star ranking reviews are often skewed toward very high or very low ratings and linear regression would not be an appropriate choice.

Here is an example showing how linear regression would be a ppor choice:

set.seed(1010)
x <- sample(c(1,2,3,4,5),100,replace=TRUE,prob=(c(.25,.05,.05,.3,.35)))
y <- data.frame(rating=x,custid=seq(1:100))
hist(y$rating, prob=TRUE, main="Customer Ratings")
lines(density(y$rating))

It would be a much better fit if a regression problem involved sales prediction from inventory. Low bias models tend to result in over fitting and high bias models have lower accuracy but are simpler to explain.

Variance is related to how a model would change when supplied with different data. Low variance is also desirable.

Decision trees are an example of an algorithm, which tends to have low bias (over fits) but can come up with completely different results when given a new training sample (high variance). Even adding one additional observation to an existing decision tree model can result in a totally different tree.

To develop a good predictive model, you acknowledge that there ultimately will be a compromise or tradeoff between bias and variance. To learn more about the bias-variance trade-off, check out some of the external references in Wikipedia.

Here are some short descriptions of some of the models that we will be covering, with some short code examples.

A Poisson model is used to model counts of things. That could be the number of insurance claims filed in a given month, the number of calls which are received in a call center in a given minute, or the number of orders which are sold for a particular item. The Poisson distribution is the appropriate way for modeling count data since all data is positive and the range of the distribution is bound by 0 and infinity. The classic way of modeling a Poisson model is through the R glm() function using a poisson link function:

model.poisson <- glm(count ~ v1+v2+v3, data=inputdata, family=poisson())

Note that the preceding model specified merely shows the model in a generalized form. Do not try to run it since there are no variables other than the existing ones v1, v2, v3 or count. However, what the model specification says is that you will run a Poisson model via the following general steps:

  • The model will be run via the glm() function with some dependent variable to the left of the ~
  • The independent variable will be supplied to the right of the ~
  • The Data= parameter will supply an input dataset
  • The family= parameter will specify the type of general linear model that you will be running, and, in this case, it will be a Poisson model

To try a Poisson model on real data, we can use the warpsbreaks data, which is included with R.

  • First at the console, enter help (warpbreaks) to get a description of the dataset:
[,1]breaksnumericThis is the number of breaks
[,2]woolfactorThis is the type of wool (A or B)
[,3]tensionfactorThis is the level of tension (L, M, or H)

Then, set up and execute the model using the glm() function. We are predicting the number of breaks, using the type of wool and level of tension. Note that we need to add a summary() function after the glm() function in order to see the output, since running the glm() function just assigns the output to an object named the model.poisson model.

model.poisson <-glm(breaks~wool+tension, data=warpbreaks, family=poisson)
summary(model.poisson)

Logistic regression is one of the oldest and stable techniques that one can use for classification. Logistic regression, linear regression, and Poisson regression are all considered General Linear Models (GLM). However, in the case of logistic regression, the predicted value can only be 0, or 1. Fortunately, this corresponds with many use cases, such as whether or not a customer will leave, or whether or not a hurricane will appear. If you are already familiar with multiple linear regressions, logistic regression should be easier to understand, since you should already be familiar with concepts such as specifying multiple independent variables, and the use of mathematical functions, such as log and exp, which can smooth the variables in the model and force it to be more linear.

Logistic regression is also useful, in that it produces an odd ratio. An odd ratio is the probability that an event will occur divided by the probability that the event will not occur.

A standard logistic regression is called in R via the glm() function.

The call to glm() contains a specification for a link function. The link function specifies which kind of model or distribution will be used for the linear model. For logistic regression, use family=binomial() to specify logistic regression.

The simplest general form of the glm() function is:

Model.logistic <- glm(Target~v1+v2+v3,data=sourcedata,family=binomial())

Support vector machines (SVM) can also be used to predict a binary class. SVM projects the data into a higher dimensional space so that hyperplanes can be used to separate the classifiers. SVMs can be very accurate but difficult to interpret and computationally expensive. They are a classic example of a low bias algorithm.

Here is a simple example of using an SVM to predict whether a person is satisfied based upon the day of the week and whether or not it is a payday. (The vector element is marked as 1 in the payday vector, which can be interpreted as Friday if you start counting from Sunday.)

library(e1071)
satisfied = factor(c(F,F,F,F,F,T,F))
day = c(1,2,3,4,5,6,7)
payday = c(0,0,0,0,0,1,0)
satisfaction.df = data.frame(day=day, payday=payday, satisfied=satisfied)
model.svm = svm(satisfied ~ day + payday, data = satisfaction.df)
plot(model.svm,satisfaction.df,main="",col=c('grey','black'))

As you can see from the following plot, the decision line is a curve near the upper-right part of the quadrant, and the area where payday is close to 1 (yes), and day of week is close to 4, 5, 6, 7 (later part of the week). So we can interpret that as saying people are satisfied on payday and at the end of the week. However, this is a very low dimension example (rendered in two axes) that we used to illustrate the concept. Higher dimensional examples are not that easy to interpret:

Decision trees are popular since they can roughly be equated with If/Then/Else rules used in some business contexts and are relatively easy to explain to managers. Decision trees are not only used for classification. When they are used to predict numeric outcomes, they are referred to as regression trees. The basic concept that decision trees use is that each node of the tree is split into two parts based upon an optimal split point. The tree continues to grow by adding more leafs until it is not able to make any additional splits, which improve the ability to distinguish between any of the decisions.

Note that Random Forests (tm) is a trademark of Leo Breiman and Adele Cutler and is licensed exclusively to Salford Systems for the commercial release of the software.

One problem with decision trees is that they are highly dependent on the specific variables that are initially chosen. One could generate a slightly different set of initial variables and end up with an equally valid solution, although different.

Random forest algorithms (which are known by several different names refer to the note at end of the chapter) are an attempt to improve decision trees in that random forests randomize the selection of variables and subsamples in order to generate multiple (even thousands of) separate decision trees. After all the decision trees are generated, a consensus prediction is determined which averages the effects over all of the different trees that have been generated.

In order to determine consensus, you can specify that the algorithm implements a simple majority vote scheme. We will see how this works by running two simple decision trees on the titanic dataset first, and then comparing the results to that which is obtained using a random forest.

First, install the titanic package and assign the training dataset to a dataframe:

install.packages("titanic")
library(titanic)
titanic <- titanic_train[complete.cases(titanic_train), ]

Grow a simple decision tree to predict whether a passenger survived using Age as an independent variable and then plot the tree:

library(rpart)
library(rpart.plot)
set.seed(123)
fit <- rpart(as.factor(Survived) ~ Age, data=titanic, method="class")
rpart.plot(fit,extra=102)

How accurate was this simple tree? Just to flex our brain, we will compute this manually.

At the console, compute the correct classification percentages. The correct number of predictions for each class is shown as the first number in each node in the preceding plot. The second number is the total number assigned to that class.

By adding up the total of the correct predictions and dividing it by the total number of predictions, the math computed via the console shows that this simple model is correct 62% of the time. however, you may ask what is the 93 % given in the lefthand node of the tree. This would be the correct classification for that node given that the passengers age was less than 6.5:

> (410+33) / nrow(titanic)

This is the following output:

[1] 0.6204482

Let’s say we arbitrarily selected another simple decision tree model; this time we will predict whether or not a passenger survived based upon the passenger class variable:

This is a slightly more complex tree, since it has four branches and three terminal nodes. Now, let’s calculate the correct classification rate for the model by adding up the correct classification numbers for the three nodes and dividing this by the total number of rows, as we did before:

> (270+90+122) / nrow(titanic)

This is the following output:

[1] 0.67507
>

This simple model is even better, with a 67% correct classification.

Now, let’s run a random forest model. We will grow 2,000 trees and just for illustration include Age, Pclass (Passenger Class), and Fare as the independent variables. Random forest randomizes both the observations selected as well as a sample number of observations selected, so you are never certain which specific trees you will get. You might even get one of the trees just generated in the previous example!

library(randomForest)
set.seed(123)
fit <- randomForest(as.factor(Survived) ~ Age + Pclass + Fare,
data=titanic,
importance=TRUE,
ntree=2000)

Random forest also has a predict function, with a similar syntax to the predict function we used in Chapter 1, Getting Started with Predictive Analytics. We will use this function to generate the predictions:

prediction.rf <- predict(fit, titanic)

Once the predictions are generated, we can construct a dataframe consisting of the predictions along with the actual survival outcomes obtained from the raw data:

x<-data.frame(predict.rf=as.factor(prediction.rf),survived=titanic$Survived)

Now we can run a simple table() function which will count the number of actual outcomes classified by their predicted values.

table(x$predict.rf,x$survived)

This is the following output:

0 1
0 384 118
1 40 172

The numbers in the table reflect the following predictions:

(Row 1,Column 1) Passenger predicted NOT to survive & DID NOT survive
(Row 1,Column 2) Passenger predicted NOT to survive DID survive
(Row 2,Column 1) Passenger predicted to survive and DID NOT survive
(Row 2,Column 2) Passenger predicted to survive and DID survive

Based on that, we can see that we have made correct predictions for the counts contained in (Row 1,Column 1) and (Row 2,Column 2), since our predictions agree with the outcomes.

To get the total number of predictions, we will add up the total number of correct counts, and then divide it by the number of rows. We can do the math at the console:

(384+172) / nrow(titanic)

This is the following output:

[1] 0.7787115

Using a random forest, the correct predictions rate has been raised to 77%.

Even though random forests can be better predictors than singly partitioned trees, the issue of interpretability comes into play. Random forests do not generate visual decision trees, and computationally random forests can take quite a long time to run as they work to grow and optimize multiple trees from many variables. Some feel that they act as a black box since there are so many ways to optimize and the underlying methods are not readily transparent. However, it is an optimization method and can be very accurate, if extreme accuracy is your goal.

Aside from the prediction accuracy, another popular use for random forests is variable selection, using the varImp() function. A variable importance plot can be useful in situations in which there are many input variables, but I have found it to be of limited value for a manageable number of variables.

Just to illustrate on our example data, here is the varImp() function showing Fare, Age, and Pclass in the order of importance. There are a couple of ways it can do this, but I will be showing it as determined via statistics referred to as MeanDecreaseGini. It is not critical to understand how this statistic is computed, but it is sufficient to say at this point that the importance is related to how many different trees the variable appears in, and the part it played in deciding how well it was able to discern one outcome of a tree branch from another. We will discuss decision trees and Gini in the later chapters.

varImpPlot(fit,type=2)

You will often be examining hundreds or even thousands of variables, and dimension reduction is a technique that you can use to drastically reduce the number of rows or variables that you need to examine. The premise behind dimension reduction is duplication, that is, many variables which are measuring the same thing. For example, reading, writing, math, and musical aptitude scores are all important in predicting a college GPA, but it is possible that if you only used a musical aptitude score in combination with a writing score, you could achieve the same prediction accuracy as compared to using all four measures. It might also be easier to explain as well, that is one example of why you would use a dimension reduction technique.

When you are looking at reducing the actual number of variables, consider principal components. You will end up with the same number of observations but will limit the number of variables you look at (the Principal Components section).

When you are considering reducing the dimensionality of rows, use clustering methods.

Principal Component Analysis (PCA) is useful in cases in which you have too many variables and you wish to capture all of the variation of data into one or two variables. Principal component does this using matrix algebra to create linear combinations of all of the variables.

PCA is also used to see which variables have the most influence within each of the principal components, so often it is a way to discard certain variables when a PCA analysis indicates certain variables have little effect on the outcome.

An example using the variables of height and weight is a good one. Since height and weight are positively correlated, choosing to use only height or only weight as an independent variable to predict body mass might not make much of a difference. So, we can create a new variable which is a linear combination of height and weight and use that as a variable instead.

Clustering is a method which groups data into different classes, so that each class is similar to each other. There are various methods that can be used to define similarity. K-Means clustering is probably the most popular method of clustering. This method uses distances measured to assign data observations to the closest class. Clustering is often used in marketing in order to develop different customer segments.

Clustering is an unsupervised algorithm and is subjective. You can specify beforehand how many groups you wish to cluster into. This number is somewhat arbitrary, and if the goal is interpretability, it can yield to different interpretations.

Scatterplots are often used to show data clusters using only two variables (one is on the x axis and the other on the y axis).

Here is an example of some scatterplots which can be used to pick out clusters by seeing which data points congregate together.

The plot on the right shows five distinct clusters. You can see that very low values of variable x tend to go with very low values of variable y. However, it is a bit unclear what the clusters should be for the plot on the left. Sometimes low values of x go with low values of y, but sometimes they are associated with high values of y. There is no apparent distinct grouping. What do you see? Do you see two clusters, three or more, or no clustering? It is really impossible to tell and that is where subjectivity creeps in.

Problems containing data observations which are not independent of one another, such as time-based data, deserve special treatment since events which occur in time exhibit a property known as autocorrelation. What that means is that the independent variables are all dependent upon the previous values of that variable. Many data techniques assume that all values occur independently of each other, so special techniques were developed to deal with time-based data.

Exponential smoothing is based upon the simple concept that the most recent data is the best predictor of the future. Exponential smoothing techniques allow you to tweak the parameters so that you can decide how much weight is given to the recent data versus the older data. It is suitable for many time-series problems.

Time-series models can also be based upon cyclical, trend, seasonal, one-time occurrence, or causal factors. One of the more advanced time-series methods is known as ARIMA or Box-Jenkins models which incorporate elements of time-series and regression models.

Naive Bayes originally became popular as a method for spam detection. It is quick and fast. Naive Bayes assumes that the variables are all independent and not related to each other (a bad assumption, but that is what makes it Naive!). It also has the advantage that it does not need to be retrained when adding new data. Naive Bayes has its roots in Bayes theorem.

This simple example shows Naive Bayes in action. Using the Iris dataset, Naive Bayes will make a prediction for the fifth column using the first four columns as independent variables:

#use 1st 4 columns to predict the fifth
library(e1071)
iris.nb<-naiveBayes(iris[,1:4], iris[,5])
table(predict(iris.nb, iris[,1:4]), iris[,5])

The results of the table() function will go to the console. This output is the confusion matrix which is as follows:

setosa versicolor virginica
setosa 50 0 0
versicolor 0 47 3
virginica 0 3 47

The correct classification rate for the problem is 96%. This can be easily verified at the console by summing up the values corresponding with the correct classification counts (that is, row 1/column 1, row 2/column 2, and row 3/column 3) and then dividing by the total number of rows.

Here is how you would do it in the console using the R calculator:

The confusion matrix also tells you which classifications did not perform well. For example, if you sum the values of column 2, you can see that there is a total of 50 versicolor species. However, there were a total of three misclassifications for the versicolor / virginia combination (following bold underlined):

setosa versicolor virginica
setosa 50 0 0
versicolor 0 47 3
virginica 0 3
47

To identify which combinations were misclassified, we can write a little bit of code and examine the incorrect classification rows using a DataTable object. Using a DataTable object allows you to sort, search, and filter the data.

Merge the predictions with the original data. Then, assign a Correct or Wrong flag to the dataframe to designate whether the prediction was correct or not.

mrg <- cbind(pred,iris)
mrg$correct <- ifelse(mrg$pred==mrg$Species,"Correct","Wrong")

Load the DT library and specify that you want an interactive datatable on the merged data. You will also want some interactive filtering capabilities, so specify filter=‘top’ as a parameter.

library(DT)
datatable(mrg,filter='top')

The interactive data table will open in the RStudio viewer:

To find the misclassified species, type Wrong, in the search box. The display will automatically update to show the incorrect predictions:

Most legacy predictive analytics techniques were originally designed to work with structured data. However, that has changed with the advent of text mining techniques, which are able to process and visualize unstructured data. Some of the more popular tools are:

  • Word clouds: Word clouds are graphical representations of word frequencies or concepts and would be a simple tool to present important words in a corpus (collection of text documents) to management.

Here is an example of a word cloud produced from Lincoln’s Gettysburg address (Bliss Copy) (Gettysburg Address, 1863). To limit the number of words displayed, set min.freq=2, so that only words which appear at least two times will be displayed. Notice that the output is also eliminating some common words (stopwords), which do not usually add to the interpretation (a, and, it, and so on).

install.packages("wordcloud")
install.packages("RColorBrewer")
library("wordcloud")
lincoln <- "Four score and seven years ago our fathers brought forth on this continent, a new nation, conceived in Liberty, and dedicated to the proposition that all men are created equal. Now we are engaged in a great civil war, testing whether that nation, or any nation so conceived and so dedicated, can long endure. We are met on a great battle-field of that war. We have come to dedicate a portion of that field, as a final resting place for those who here gave their lives that that nation might live. It is altogether fitting and proper that we should do this. But, in a larger sense, we can not dedicate -- we can not consecrate -- we can not hallow -- this ground. The brave men, living and dead, who struggled here, have consecrated it, far above our poor power to add or detract. The world will little note, nor long remember what we say here, but it can never forget what they did here. It is for us the living, rather, to be dedicated here to the unfinished work which they who fought here have thus far so nobly advanced. It is rather for us to be here dedicated to the great task remaining before us -- that from these honored dead we take increased devotion to that cause for which they gave the last full measure of devotion -- that we here highly resolve that these dead shall not have died in vain -- that this nation, under God, shall have a new birth of freedom -- and that government of the people, by the people, for the people, shall not perish from the earth."
set.seed(123)
wordcloud(lincoln, min.freq=2,scale=c(3,.5),random.order = FALSE)

The output of the wordcloud uses the size of the word to represent its frequency in the text:

  • Bigram analysis: Word clouds usually show the frequencies of single words in a paragraph or document. They do not usually analyze the surrounding words. Bigram, on the other hand, looks at the frequency of two consecutive words as a single pair. Often, additional insight can be derived by seeing how many times these consecutive words appear together in the various parts of the document being analyzed. This bigram concept can also be extended to three words at a time, four words at a time, and so on. In this case, they are called ngrams, meaning analysis of any number of consecutive words which appear together.

For example, the following table shows four bigrams which appear twice in the preceding paragraph and one that appears three times (consecutive words). Note that this includes punctuation which is usually removed before frequency counts are determined. Punctuation, numbers, and small parts of speech such as the, them, a, or an are usually performed prior to performing text analysis. These are collectively known as stopwords.

  • Here is an example which uses the get.phrasetable for the ngram package to extract all possible ngrams. To specify a bigram we will use n=2.
install.packages("ngram")
library(ngram)
df <-get.phrasetable ( ngram(lincoln, n=2) )
View(df)

From the output of the View command, we can see that the people occurs three times:

  • Topic definitions: With topic definition, your goal is to classify documents into a set of categories, just as we would with a nontextual problem. For example, if a customer calls and then starts complaining about the reliability of a product, we might want to direct the customer to a different call center area to that if he was calling simply to get information about a product. A key component of text mining is the ability to be able to parse unstructured data and learning how to mine this data for the important words.
  • Clustering: Other traditional techniques, such as clustering, can also be used with free form text. However, in many cases, the text data will need to be retrofitted and will need to be preprocessed into a structured format first, before traditional predictive analytics techniques such as clustering are applied. You will learn how to convert unstructured text into a structured form called a term frequency matrix, which will allow you to perform traditional predictive analytics techniques.
  • Sentiment analysis is the ability to gauge an individual’s or group’s opinions (whether positive or negative) about a product, service, or policy.

Here is an example which will produce a sentiment analysis prediction for various short comments made to a call center. A Naive Bayes model is used to predict the last five comments, based on the first 10 comments, which have been manually preclassified as positive or negative.

install.packages("RTextTools")
library(RTextTools)
library(e1071)
verbatim =
rbind(
c('Agent was very helpful', 'positive'),
c('Would buy the product again', 'positive'),
c('Satisfied with response', 'positive'),
c('Helped me to choose between the two offers', 'positive'),
c('Defintely would recommend to a friend', 'positive'),
c('Terrible customer service', 'negative'),
c('not recommended', 'negative'),
c('Agent took too much time', 'negative'),
c('Waiting a long time for customer service', 'negative'),
c('Not satisfied with response', 'negative'),
c('Not satisfied at all', 'negative'),
c('Would not recommend', 'negative'),
c('Helpful customer support', 'positive'),
c('Great product support', 'positive'),
c('excellent product', 'positive')
)
## build Term document matrix
TDM.mat= as.matrix(create_matrix(verbatim[,1],language="english",removeStopwords=TRUE, removeNumbers=TRUE,stemWords=FALSE))
classifier = naiveBayes(TDM.mat[1:10,], as.factor(verbatim[1:10,2]) )
# test the model
predicted = predict(classifier, TDM.mat[11:15,]);

The table() function will tell you how the predictions did for the last five customer comments. Three of the five comments were classified correctly (60%). Is 60% considered good? That depends upon what you will be doing with your model. If you are able to identify negative comments, you might want to reach out to the customer via various channels and try to win the customer back. On the other hand, positive comments imply that you are doing right by the customer and maybe you should think about offering good customer discounts.

table(verbatim[11:15, 2], predicted)

Model evaluation deals with how accurate or useful the model you have just developed is or will be in the future. Model evaluation can take different forms. Some are more subjective and are domain oriented, such as placing it under the scrutiny of experts in your field, and some are more technically oriented. There are many metrics and procedures available to assess a model. At the basic level, you have many statistics (some of them with acronyms known as AIC, BIC, and AUC) which purport to convey the goodness of a model in a single metric. However, these metrics by themselves are unable to convey the purpose and application of a predictive model to a larger audience and often these metrics are in conflict. Some context is needed. Some would argue that one could also develop a perfectly good predictive model and then be unable to convey its purpose and application to a larger audience. In my opinion, that is a bad model, regardless of how well an evaluation metric fits. And then there is the performance factor. A model may work well on sample data but be too slow to become actionable in the real world. In short, there is no single metric that you should use for model evaluation. The best course is to look at it from all angles and then present the objective results.

The basic premise of predictive analytics model validation is that you develop your model on one subset of the data (called the training dataset), and then demonstrate that your model has the capability to successfully predict similar resuls on a different set of data. Of primary importance the data set known generically as test. The test dataset is also known as a holdout sample. The training and test datasets are randomly determined before any model building begins, and the data is never changed. The validation data is a third dataset that is sometimes used to further test the validity of the data. It typically contains data the modeler has never seen before and is introduced after one model has been developed and determined to be the champion model.

The following code will create a simple Naive Bayes model and create a confusionMatrix (or classification error table), which shows how many predictions were classified correctly. For the iris dataset, 92% of the predictions were correct. Note that, for this example, we are using the confusion matrix, which is part of the caret package, rather than computing the accuracy manually as we did in the previous examples:

require(caret)
require(e1071)
data(iris)
set.seed(123)
partition.index <- createDataPartition(iris$Species, p=.5, list=FALSE)
Training <- iris[ partition.index,]
test <- iris[-partition.index,]
model <- naiveBayes(Species~.,data = Training)
predictions <- predict(model, test[,1:4])
# summarize results
% confusionMatrix(predictions, test[,5])

Area under the curve (AUC) is another popular measure to assess the goodness of your model. Historically, this measurement was developed during World War II. The original terminology was Receiver Operating Characteristic (ROC) and its original purpose was to determine whether or not a blip on the radar screen was an enemy ship or just random noise.

One of the things that the AUC tells us is the ratio of the true positives to the false positives. The AUC is determined via a mathematic formula, which will be a number between 0 and 1. An AUC of 0.5 is considered a random classification. Look for points that hover near the upper-left quadrant. This would be the area where advantageous conditions converge: high true positives with low false positives.

AUC is a good measure of the tradeoffs involved in classification errors. However, it should not be considered as absolute. Costs of misclassification should be taken into account when using the AUC.

Here is an example of plotting an ROC curve on the titanic dataset, using a simple logistic regression model to predict survival:

install.packages("titanic")
install.packages("ROCR")
library(titanic)
library(ROCR)
titanic <- titanic_train[complete.cases(titanic_train), ]
model <- glm(as.factor(Survived) ~ Sex+Age+Pclass, data=titanic, family="binomial")
pred <- prediction(predict(model), titanic$Survived)
perf <- performance(pred,"tpr","fpr")
plot(perf)
abline(a=0,b=1)

The AUC curve is plotted along with the various cutoff values for the probability of the predicted outcome. The diagonal reference line represents a random model. The area under the logistic model curve looks to be about 75% of the total area:

As mentioned earlier, the AUC is used for comparing models. Here is the AUC for the logistic model using only Age as a factor:

model2 <- glm(as.factor(Survived) ~ Age, data=titanic, family="binomial")
pred <- prediction(predict(model2), titanic$Survived)
perf <- performance(pred,"tpr","fpr")
plot(perf,main="AUC for Logistic Regression Titanic Model - Age Only")
abline(a=0,b=1)

We can see immediately that this single variable model has little predictive power. The AUC curve is similar to the preceding random diagonal reference line, which indicates no predictive power.

After a model has been developed, it is considered best practice to validate the results against other data.

Training data is the data that is used to build the predictive data. Test data, also known as a hold-out sample, is the data which was not used in the training process. Test data is used to see if a trained model is able to generalize its results to another dataset which was not part of the model training.

However, even though the test dataset is not used in the training of the model, the contents are usually viewable by the modeler during model development.

A validation set is a third dataset which is sometimes used to further benchmark the model results. Validation data is also data that has never been seen before, which is incorporated after the model has passed validation against the test dataset and prior to deployment. Validation datasets are often brought from a completely different source from the original training/test data source, the goal being to provide a second corroboration, or refutation, of the model results.

Along with training, test, and validation data, you will also hear some other terms which are related:

  • In-sample data refers to the data which was used to fit (model) the data and is used on the training dataset
  • Out-of-sample data refers to data not used to fit the data

This means that out-of-sample data is typically used only on the test and validation datasets. Another technique to validate the data is known as k-fold cross-validation. This technique will use multiple folds to define different subsets of in-sample and out-of-sample datasets in order to perform multiple iterations of model testing in order to randomize the selection of training and test data.

Of course time series models also use this concept; however, time periods alone can be used to partition test and training data, for example, a model might be built on the previous five years of historical data (training) and then forecast on the current sixth year of data (testing or validation data).

Variations of this are common. A walk forward testing modeler will develop a time-series model on years 1-5, test the results on year 6, and then develop a similar model on years 2-6, and then test the results on year 7. This procedure would be repeated until there is no more data to test. In all these cases, the model is built on the in-sample data, and tested on out-of-sample data which is similar to k-fold validation.

In champion/challenger model development, you currently have a deployed stable champion model in which you can benchmark any future results. This allows you to discard any model results which fall far below this benchmark. If you do not have a current champion, you can develop a theoretical best model using an algorithm such as random forests or SVM, and use that as a benchmark for what is achievable but (assume) is not actionable. Look at the results of the model you have just developed and see if it is close to these kinds of results. If so, the results are probably not attainable and you might want to look elsewhere.

This is often done with classification problems and it essentially pits man against machine. If an expert agrees with a lot of what your model spits out, that is another reason to deem it valid.

Much has been written about junk science and how there is always another study which produces the opposite results from a previous one. p-value hacking is one way to do this. We have already mentioned metadata which is data about data.

Meta-analysis is the study of studies in which a research will look at all of the previous analyses and report what they all have to say about outcomes in totality. If your analysis agrees with other similar analyses that have been performed in the past, that would be more evidence to support your model.

In some instances, you need not have only one model to predict the outcome. If you accept the view that All models are useful, why not use some of them at the same time? This is used extensively in marketing campaign management and financial portfolio management. Sometimes it pays not to put all of your eggs in one basket. One model can fail to work during one month but is offset with positive results from another model. Ensemble modeling, which is similar, can also incorporate several diverse models at the same time, but usually there is a single outcome and may be subject to overfitting.

Deployment of a model is the process by which you put your models into a real-world production setting. This can depend on many factors, such as the environment in which it was developed, the algorithm that was chosen, assumptions concerning the data that was made when the model was developed, and of course, the level of the developer. Often a model is unable to scale up to the demands of a production environment and knowing your possible production environment in advance will dictate what problems or techniques are feasible.

Model scoring makes the model actionable. If you develop a model and you are unable to apply the results to new data, then you will be unable to do any prediction on an ongoing basis. New model scoring often involves outputing the development model outputs to a real-time scoring engine. That engine is often Java or C++. How that is performed varies vastly depending upon the modeling technique. Sometimes the scoring is performed separately, so it can be optimized for efficiency.

Regression type models are relatively easy to score, since all that is needed are the model’s coefficients and intercepts. Other types of models need a little more work. For example, decision tree software should be able to output a set of decision rules which would enable production language to recode it using a series of if/then/else rules. Software packages often output the decision rules which were determined for each node.

Certain production packages will accept PMML input. PMML is a common language which is used to facilitate translation of one language’s model specification into a common format.

For more complicated models, it may be necessary to have production versions of the model on a separate production machine and run the model using similar code to that used for development.

You can refer to the following articles:

Random Forests (tm) is a trademark of Leo Breiman and Adele Cutler and is licensed exclusively to Salford Systems for the commercial release of the software.

In this chapter, we learned about the various structured approaches to predictive analytics and how implementing an analytics project in a methodical way can enhance the success of an analytics project through collaboration and communication. We went through the various steps of the CRISP-DM methodology and demonstrated tools that you could use to help you progress along these steps.

We discussed the benefits of sampling and how it could speed up your project. SQL was demonstrated to illustrate basic charts and plots, so that you can begin to develop insight even before you create a first model. We showed that data simulation could also be used at the data understanding phase as a preliminary modeling tool to do “what ifing”, even before actual company data is obtained.

We learned about the various types of data that you will encounter, and showed some examples of independent and dependent variables and the importance of doing preliminary 1-way and 2-way variable analysis as a precursor to modeling.

Finally, we learned about some of the basic predictive models that we will be using later on, and discussed the importance of the bias/variance tradeoff when starting to examine which types of predictive models you might want to employ.

We ended with discussing some best practices for evaluating a model, and the importance of planning for the eventual deployment of models into production and how that could alter your development process.

In the next chapter, we will focus more upon getting the data into a form suitable for actual predictive modeling with a focus on data cleaning, outliers, missing values, and some data reduction and transformation techniques.