Performing out of sample validation to test predictive quality of the model Developing applications of multinomial logistic regression and ordinal.

Lesson 42/77 | Study Time: Min


Performing out of sample validation to test predictive quality of the model Developing applications of multinomial logistic regression and ordinal 


Performing out of sample validation is a crucial step in assessing the predictive quality of a model. It involves evaluating the model's performance on data that it has not been trained on, thereby simulating its ability to make accurate predictions on unseen data.


The purpose of out of sample validation is to test the generalizability of the model. A model that performs well on the data it was trained on may not necessarily perform well on new, unseen data. By using out of sample validation, we can determine if the model has learned meaningful patterns that can be applied to other similar datasets.

Here is a step-by-step process for performing out of sample validation:

  1. Split the data: The first step is to divide the dataset into two parts - a training set and a test set. The training set is used to build the model, while the test set is used to evaluate its performance. The recommended split is usually around 70-80% for training and 20-30% for testing.

  2. Train the model: Using the training set, develop the model using binary logistic regression, multinomial logistic regression, or ordinal logistic regression, depending on the nature of the dependent variable. Use functions in R or Python to build the model and estimate the coefficients.

  3. Make predictions: Once the model is trained, use it to make predictions on the test set. This involves applying the model's equations or algorithms to the independent variables in the test set and obtaining predicted probabilities or predicted categories for the dependent variable.

  4. Assess model performance: Compare the predicted values with the actual values in the test set to assess the model's performance. This can be done using various evaluation metrics such as accuracy, precision, recall, and F1 score for binary logistic regression. For multinomial logistic regression, metrics such as overall accuracy and the confusion matrix can be used. For ordinal logistic regression, metrics such as the proportional odds assumption and the concordance index can be employed.

  5. Repeat the process: To ensure the reliability of the results, it is advisable to repeat steps 1-4 multiple times using different train-test splits or employing cross-validation techniques. This helps to account for any potential variations in model performance due to the specific subsets of data used.


By performing sample validation, we can identify any potential issues with overfitting or underfitting of the model. Overfitting occurs when the model has learned the noise or random variations in the training data, leading to poor performance on new data. Underfitting, on the other hand, occurs when the model is too simplistic and fails to capture the underlying patterns in the data.

Overall, out of sample validation is an essential step in the predictive modeling process as it provides an unbiased evaluation of the model's performance on unseen data. It helps to ensure that the model is reliable and can be used for making accurate predictions in real-world scenarios.


Understanding Out-of-Sample Validation

  • Definition of out-of-sample validation

  • Importance of out-of-sample validation in assessing model performance

  • Differences between in-sample and out-of-sample validation


Understanding Out-of-Sample Validation


Once upon a time, statisticians were faced with a tricky problem: how do we know a statistical model we've developed will be effective in predicting new data? From this problem, the concept of out-of-sample validation was born.


What is Out-of-Sample Validation? 🧪


Out-of-sample validation is a model validation technique where the validity of a model is measured on a test data set. This test data set is separate from the data set used to create the model, hence 'out-of-sample'.


In simpler terms, you can consider it as a practice exam before the final test. You don't see the exact questions that will be on your final exam (test data set), but you practice with similar ones (training data set).


# an example in R

# assume we have a data set 'data' and we are trying to predict 'y' using 'x'


# split the data into a training set and a test set

set.seed(123)

training_samples <- data$x %>% 

  createDataPartition(p = 0.8, list = FALSE)

train_data <- data[training_samples, ]

test_data  <- data[-training_samples, ]


# fit the model on the training data

model <- lm(y ~ x, data = train_data)


# use the model to predict the test data

predictions <- model %>% predict(test_data)


In this example, the model is built using train_data and then the model's predictive power is tested using test_data.

Why is Out-of-Sample Validation Important? 💡


The main goal of any statistical model is to make accurate predictions. For a model to be considered valid, it must maintain its accuracy not just on the data it was trained on, but also on new, unseen data - the ultimate test of a model's predictive power.


Without out-of-sample validation, we run the risk of overfitting our model. Overfitting occurs when a model is too closely fitted to the training data, to the point where it starts to 'memorize' the noise and outliers in the data rather than the underlying pattern to be modeled. Thus, while it may have high accuracy on the training data, it performs poorly on new data.


In-Sample Vs Out-of-Sample Validation 🔄


The twin sibling of out-of-sample validation is in-sample validation. In-sample validation involves testing the model on the same data that was used to create it. While this can give an indication of how well the model fits the data it was trained on, it doesn't provide any information about how the model will perform on new data.


To illustrate the difference, consider a student who is studying for an exam using past papers. If the student only studies these past papers and then takes a test composed of questions from these papers (in-sample validation), they'll likely do very well.


But if the actual exam contains completely new questions, the student might not perform as well. Similarly, a model might perform well on the training data but fail to generalize to new data if it's not tested with out-of-sample validation.


In conclusion, out-of-sample validation is a critically important step in assessing the predictive quality of statistical models. It's a vital tool in a statistician's toolbox to prevent overfitting and ensure that a model has robust predictive power



Splitting the Data for Validation

  • Randomly splitting the dataset into training and testing sets

  • Determining the appropriate ratio for splitting the data

  • Ensuring that the split is representative of the original dataset


The Art of Splitting the Data for Validation


Let's dive into the world of data science where splitting data is an art. One can't help but compare it to a chef delicately slicing ingredients to whip up a culinary masterpiece. In the realm of statistics and predictive modeling, this process is critical to ensure that the predictive model is neither overfitted nor under-fitted to the data.


Random Splitting of Data into Training and Testing Sets


The first step is akin to cutting the ingredients into sizable chunks. In this case, your entire dataset is randomly split into two sets: the training set and the testing set. The training set is like the main ingredient of your dish, it forms the bulk of the data and is used to train the predictive model. The testing set, on the other hand, is used to validate the model's performance.


from sklearn.model_selection import train_test_split


# Split the data into training and testing sets

X_train, X_test, Y_train, Y_test = train_test_split(X, Y, test_size = 0.2, random_state = 42)


In this example, train_test_split function from sklearn.model_selection module is used to split the data. The test_size parameter is set to 0.2, meaning 20% of the data is reserved for the testing set and 80% for the training set.


Determining the Appropriate Ratio for Splitting Data


The next step in this culinary journey is deciding how much of each ingredient to use. Determining the appropriate ratio to split your data is crucial. A common practice is the 80-20 rule where 80% of data is used for training and 20% for testing, but this can vary. It’s about finding the right balance. Too much training data might lead to overfitting, where the model performs exceptionally well on training data but poorly on new, unseen data. On the other hand, too little training data might lead to underfitting, where the model's predictive power is low even on the training data.


Take for example the case of medical research. When predicting the incidence of a rare disease, one might choose a 90-10 split or even 95-5, given the scarcity of positive cases.


Ensuring the Split is Representative of the Original Dataset


The final step is ensuring that the split is representative of the whole data, much like how a good dish combines flavors from all its ingredients. Stratified sampling is a popular method used to accomplish this. It involves splitting the data in a way that maintains the same proportions of classes in both training and testing sets as in the original dataset.


from sklearn.model_selection import train_test_split


# Split the data into training and testing sets using stratified sampling

X_train, X_test, Y_train, Y_test = train_test_split(X, Y, stratify=Y, test_size=0.2, random_state=42)


In the case of a marketing campaign dataset, the stratify parameter ensures that the proportion of customers who responded to the campaign is the same in both the training and testing sets, preserving the original distribution and enhancing the model's predictive quality.


In the end, splitting data for validation is a necessary step to ensure the predictive quality of the model, much like how a chef's delicate preparation ensures a delightful dish.


Training the Model

  • Developing the model using the training set

  • Choosing the appropriate algorithm for the model

  • Tuning the model parameters for optimal performance

The Art of Model Training


The core of any predictive model lies in its training process. Model training is essentially about teaching the model to understand patterns within the data and learn from them. You may consider it similar to how a student learns from a textbook.


Choosing the Right Algorithm


Every predictive model starts with an algorithm. This is the mathematical function that the model uses to map input data to output predictions. Different algorithms work better for different types of problems, so choosing the right one is critical.

Let's consider an example in basketball analytics. A data scientist might want to predict the winner of a game based on different stats like shooting percentage, turnovers and rebounds. A linear regression algorithm might not provide the best results here, as the relationship between these stats and winning is not perfectly linear.


On the other hand, a decision tree or random forest algorithm, which can capture more nuanced relationships, might be more suitable. The algorithm choice heavily depends on the nature of the data and the prediction task at hand.

# Example of algorithm selection in Python

from sklearn.ensemble import RandomForestClassifier


# Initialize the algorithm

rf = RandomForestClassifier(n_estimators=100)


Training the Model with the Training Set

Once you have the right algorithm, it's time to feed it some data. The training set is a subset of your total data that you use to train your model. During training, the model analyses the training data and adjusts its internal parameters to better predict the outcomes.


For instance, let's consider a popular online shopping platform. They may want to predict which customers will make a purchase in the next month based on their browsing history. In this case, the training set might include data like pages visited, time spent on the site, previous purchases, etc., along with a binary outcome indicating whether the customer made a purchase.

# Example of model training in Python

# Suppose X_train is the feature matrix and y_train is the target vector

rf.fit(X_train, y_train)


Tuning the Model Parameters for Optimal Performance

Just as a musician tweaks the tuning of their instrument to produce the best sound, a data scientist tweaks the parameters of their model to produce the best predictions. This process, often referred to as hyperparameter tuning, involves selecting the set of parameters that minimizes the model's error on the validation set.


For example, in a random forest classifier, one important parameter is the number of trees in the forest (n_estimators). If the number of trees is too small, the model may not capture all the patterns in the data, but if it's too large, the model may overfit to the training data and perform poorly on new data.

Hyperparameter tuning is often done through a process called grid search, which involves testing different combinations of parameters and selecting the one that results in the lowest validation error.

# Example of hyperparameter tuning in Python

from sklearn.model_selection import GridSearchCV


# Define the parameter grid

param_grid = {

    'n_estimators': [50, 100, 200],

    'max_depth': [5, 10, 20]

}


# Initialize the grid search

grid_search = GridSearchCV(rf, param_grid, cv=5)


# Fit the grid search to the data

grid_search.fit(X_train, y_train)


# Get the best parameters

best_params = grid_search.best_params_


Training a model is a delicate process that requires a deep understanding of the task at hand, the data, and the strengths and weaknesses of different algorithms. It's like teaching a student - some methods work better than others, and there's always room for improvement.


Testing the Model

  • Applying the trained model to the testing set

  • Evaluating the model's predictions against the actual outcomes

  • Calculating performance metrics such as accuracy, precision, recall, and F1 score


When the Training is Over, The Real Test Begins


Famous statistician George Box once said, "All models are wrong, but some are useful." The crucial step in finding out the 'usefulness' of a created model is Testing the Model. After our predictive model has been trained using a training dataset, it's time to expose it to new data, see how it performs and evaluate its predictions against actual outcomes.


Applying the Trained Model to the Testing Set


The first step in testing the model is to apply it to the testing set. In practice, this often involves using the model's predict() function. For example, in Python's Scikit-learn library, a trained model—let's say a Multinomial Logistic Regression—can be applied to the testing set as follows:

predictions = trained_model.predict(X_test)


In this code, X_test represents the testing set, and trained_model is the model that has been trained on the training set. The predict() function generates predictions for each example in the testing set, and these predictions are stored in the predictions variable.

Evaluating the Model's Predictions

Once we have the model's predictions, we compare them against the actual outcomes to see how well the model performed. Essentially, we're asking, "How often did the model make the correct prediction?"

For instance, if we're predicting whether a customer will churn or not, we compare the model's predictions (who it thinks will churn) with the actual churned customers. If the model predicted a customer will churn and they did, it's a success. But if the model predicted a customer won't churn, and they did, it's a failure.


Performance Metrics: Accuracy, Precision, Recall, and F1 Score


The moment of truth for any predictive model comes when its performance is measured. There are several metrics used to evaluate a model's performance:

  • 🎯 Accuracy: The proportion of total predictions that are completely correct. It's suitable when target classes are well balanced. But beware! It can be misleading when classes are imbalanced.

  • 🔬 Precision: The proportion of positive predictions that are actually correct. It's important when the cost of False Positives is high.

  • 🔭 Recall (Sensitivity): The proportion of actual positive cases which are correctly identified. It's crucial when the cost of False Negatives is high.

  • 🏆 F1 Score: The harmonic mean of Precision and Recall. It tries to find the balance between precision and recall. It's useful when you need to take both precision and recall into account.

Here's how you can calculate these metrics in Python using Scikit-learn:

from sklearn.metrics import accuracy_score, precision_score, recall_score, f1_score


accuracy = accuracy_score(y_test, predictions)

precision = precision_score(y_test, predictions)

recall = recall_score(y_test, predictions)

f1 = f1_score(y_test, predictions)


In this code, y_test are the actual outcomes, and predictions are the predictions made by the model. Each metric function computes the respective metric.


The Story of Titanic: An Example

An interesting real example is the prediction of survivors in the Titanic disaster. A model could be trained using passenger features like class, age, sex, etc. After the model is trained, it's then tested with a subset of passengers not used in training. The model's predictions of who survived and who didn't are compared with the actual outcomes. Finally, metrics like accuracy, precision, recall, and F1 score are calculated to evaluate the model's performance in predicting survival in the Titanic disaster.


The process of Testing the Model is a critical step in the life cycle of model development. It reveals the truth about the model's performance and the areas where it needs improvement. It's the final checkpoint that tells us whether our model is ready for deployment or needs more fine-tuning.


Assessing Predictive Quality

  • Interpreting the performance metrics to assess the predictive quality of the model

  • Comparing the model's performance to baseline or benchmark models

  • Identifying potential issues or limitations of the model based on the validation result


The Importance of Assessing Predictive Quality


A compelling fact is that as of 2020, only around 15% of data science projects make it into production. A significant reason for such a high 'failure' rate is the lack of predictive quality in many models. A model might perform excellently during initial testing with training data, but fail miserably when predicting unseen, real-world data. This highlights the importance of out of sample validation and careful assessment of predictive quality.


Interpreting the Performance Metrics


Performance metrics 📊 are the backbone of any model validation. They help quantify the model's predictive power and identify areas where it's performing well or poorly.


In a multinomial logistic regression model, commonly used metrics are accuracy, precision, recall, F1 score, and area under the ROC curve(AUC-ROC).


For instance, accuracy measures the proportion of correct predictions made by the model, including both true positives and true negatives. Precision observes the proportion of positive identifications that were indeed correct, while recall measures the proportion of actual positives that were identified correctly. F1 score is the harmonic mean of precision and recall, and AUC-ROC reflects the model's ability to distinguish between classes.


from sklearn.metrics import accuracy_score, precision_score, recall_score, f1_score, roc_auc_score


# assuming y_test are the actual values and y_pred are the model's predictions

accuracy = accuracy_score(y_test, y_pred)

precision = precision_score(y_test, y_pred)

recall = recall_score(y_test, y_pred)

f1 = f1_score(y_test, y_pred)

roc_auc = roc_auc_score(y_test, y_pred)


Comparing the Model's Performance to Baseline or Benchmark Models


Baseline or Benchmark models 🎯 serve as a reference point against which all models are compared. They are simple, well-understood models that are easy to implement and interpret.


For instance, in binary classification problems, a common baseline model is one that always predicts the most frequent class. If your complex, heavy-duty model can't outperform this simple model, it's a strong indicator that something is wrong.


Comparisons against benchmark models provide invaluable insights. They help gauge the added value of our model and validate whether the additional complexity is justified.


from sklearn.dummy import DummyClassifier


# creating a dummy classifier

dummy = DummyClassifier(strategy='most_frequent')


# fit the dummy model and make predictions

dummy.fit(X_train, y_train)

dummy_pred = dummy.predict(X_test)


# calculate performance metrics for the dummy model

dummy_accuracy = accuracy_score(y_test, dummy_pred)


Identifying Potential Issues or Limitations of the Model Based on the Validation Result


Model limitations and issues 🚧 often surface when the model is evaluated on out of sample data. These can stem from overfitting, underfitting, incorrect model assumptions, or biases in the data.


Overfitting occurs when a model performs exceptionally well on the training data but poorly on unseen data. This often means the model has learnt the noise or randomness in the data rather than the underlying pattern.


Underfitting, on the other hand, means the model is too simple to capture the complexity of the data. It performs poorly both on the training and unseen data.

To avoid these issues, aim for a balance between bias and variance, ensuring your model is complex enough to capture important patterns, but not so complex that it overfits to the training data. Regularization techniques can help achieve this balance.


from sklearn.linear_model import LogisticRegression


# Using logistic regression with l2 regularization

lr = LogisticRegression(penalty='l2', C=1.0, solver='lbfgs')


# fit the model and make predictions

lr.fit(X_train, y_train)

lr_pred = lr.predict(X_test)


# calculate performance metrics for the logistic regression model

lr_accuracy = accuracy_score(y_test, lr_pred)


Remember, no model is perfect, and every model makes assumptions. The key is to understand these assumptions, validate them with your data, and interpret the results accordingly.

UE Campus

UE Campus

Product Designer
Profile

Class Sessions

1- Introduction 2- Import and export data sets and create data frames within R and Python 3- Sort, merge, aggregate and append data sets. 4- Use measures of central tendency to summarize data and assess symmetry and variation. 5- Differentiate between variable types and measurement scales. 6- Calculate appropriate measures of central tendency based on variable type. 7- Compare variation in two datasets using coefficient of variation. 8- Assess symmetry of data using measures of skewness. 9- Present and summarize distributions of data and relationships between variables graphically. 10- Select appropriate graph to present data 11- Assess distribution using Box-Plot and Histogram. 12- Visualize bivariate relationships using scatter-plots. 13- Present time-series data using motion charts. 14- Introduction 15- Statistical Distributions: Evaluate and analyze standard discrete and continuous distributions, calculate probabilities, and fit distributions to observed. 16- Hypothesis Testing: Formulate research hypotheses, assess appropriate statistical tests, and perform hypothesis testing using R and Python programs. 17- ANOVA/ANCOVA: Analyze the concept of variance, define variables and factors, evaluate sources of variation, and perform analysis using R and Python. 18- Introduction 19- Fundamentals of Predictive Modelling. 20- Carry out parameter testing and evaluation. 21- Validate assumptions in multiple linear regression. 22- Validate models via data partitioning and cross-validation. 23- Introduction 24- Time Series Analysis: Learn concepts, stationarity, ARIMA models, and panel data regression. 25- Introduction 26- Unsupervised Multivariate Methods. 27- Principal Component Analysis (PCA) and its derivations. 28- Hierarchical and non-hierarchical cluster analysis. 29- Panel data regression. 30- Data reduction. 31- Scoring models 32- Multi-collinearity resolution 33- Brand perception mapping 34- Cluster solution interpretation 35- Use of clusters for business strategies 36- Introduction 37- Advance Predictive Modeling 38- Evaluating when to use binary logistic regression correctly. 39- Developing realistic models using functions in R and Python. 40- Interpreting output of global testing using linear regression testing to assess results. 41- Performing out of sample validation to test predictive quality of the model Developing applications of multinomial logistic regression and ordinal. 42- Selecting the appropriate method for modeling categorical variables. 43- Developing models for nominal and ordinal scaled dependent variables in R and Python correctly Developing generalized linear models . 44- Evaluating the concept of generalized linear models. 45- Applying the Poisson regression model and negative binomial regression to count data correctly. 46- Modeling 'time to event' variables using Cox regression. 47- Introduction 48- Classification methods: Evaluate different methods of classification and their performance in order to design optimum classification rules. 49- Naïve Bayes: Understand and appraise the Naïve Bayes classification method. 50- Support Vector Machine algorithm: Understand and appraise the Support Vector Machine algorithm for classification. 51- Decision tree and random forest algorithms: Apply decision trees and random forest algorithms to classification and regression problems. 52- Bootstrapping and bagging: Analyze the concepts of bootstrapping and bagging in the context of decision trees and random forest algorithms. 53- Market Baskets: Analyze transaction data to identify possible associations and derive baskets of associated products. 54- Neural networks: Apply neural networks to classification problems in domains such as speech recognition, image recognition, and document categorization. 55- Introduction 56- Text mining: Concepts and techniques used in analyzing unstructured data. 57- Sentiment analysis: Identifying positive, negative, or neutral tone in Twitter data. 58- SHINY package: Building interpretable dashboards and hosting standalone applications for data analysis. 59- Hadoop framework: Core concepts and applications in Big Data Analytics. 60- Artificial intelligence: Building simple AI models using machine learning algorithms for business analysis. 61- SQL programming: Core SQL for data analytics and uncovering insights in underutilized data. 62- Introduction 63- Transformation and key technologies: Analyze technologies driving digital transformation and assess the challenges of implementing it successfully. 64- Strategic impact of Big Data and Artificial Intelligence: Evaluate theories of strategy and their application to the digital economy, and analyze. 65- Theories of innovation: Appraise theories of disruptive and incremental change and evaluate the challenges of promoting and implementing innovation. 66- Ethics practices and Data Science: Assess the role of codes of ethics in organizations and evaluate the importance of reporting. 67- Introduction 68- Introduction and Background: Provide an overview of the situation, identify the organization, core business, and initial problem/opportunity. 69- Consultancy Process: Describe the process of consultancy development, including literature review, contracting with the client, research methods. 70- Literature Review: Define key concepts and theories, present models/frameworks, and critically analyze and evaluate literature. 71- Contracting with the Client: Identify client wants/needs, define consultant-client relationship, and articulate value exchange principles. 72- Research Methods: Identify and evaluate selected research methods for investigating problems/opportunity and collecting data. 73- Planning and Implementation: Demonstrate skills as a designer and implementer of an effective consulting initiative, provide evidence of ability. 74- Principal Findings and Recommendations: Critically analyze data collected from consultancy process, translate into compact and informative package. 75- Understand how to apply solutions to organisational change. 76- Conclusion and Reflection: Provide overall conclusion to consultancy project, reflect on what was learned about consultancy, managing the consulting. 77- Handle and manage multiple datasets within R and Python environments.
noreply@uecampus.com
-->