How Indonesians are moving around differently due to COVID-19 Pandemic? How their mobility trends changed for trip to workplaces, groceries and pharmacy, restaurants, parks, transit stations, or just staying at home?
Well, under the #covid19 pandemic, it’s time for us to work from home, study from home and worship at home. That advice was announced by Pak Jokowi for Indonesians (esp. Jabodetabek) on March 15, 2020. Look at this ‘spaghetti’ graph with the data sourced from Google Community Mobility Report below. Start from 15 March 2020, around +15% mobility trend changes for places of residences. (Note: mobility changes means two parameters how visits and length of stay at the places.) Around -15% mobility trend changes for places of work.
Yesterday I continued a Datacamp online course named Introduction to Machine Learning. Frankly, this course is very useful to strengthen my understanding in machine learning! Plus, I am a big fan of R! The more you repeat the course, the more you understand the meaning of it. Well, the topic was about “comparing the methods”. It is part of chapter 3 – Classification topic, precisely at the end of the chapter. It says that the powerful tool to compare the machine learning methods, especially classification, is ROC Curve. FYI, out of the record, this ROC curve analysis was also requested by the one of the client. 😉
What is ROC?
ROC stands for Receiver Operating Characteristic. In statistics, it is a graphical plot that illustrates the performance of a binary classifier system as its discrimination threshold is varied. Electrical engineers and radar engineers during World War II firstly developed the ROC curve for detecting objects of enemy, then soon used by psychologist to account for perceptual detection of stimuli. At this point, ROC analysis has been used in medicine, radiology, biometrics, machine learning, and data mining research. (Source: here).
The sample of ROC curve is illustrated in the Figure 1. The horizontal axis represents the false-positive rate (FPR), while vertical axis represents the true-positive rate (TPR). The true-positive rate is also known as sensitivity, recall or probability of detectionin machine learning. The false-positive rate is also known as the fall-out or probability of false alarmand can be calculated as (1 −specificity).
Suppose that you have a data set called adult that can be downloaded here from UCIMLR. It is a medium sized dataset about the income of people given a set of features like education, race, sex, and so on. Each observation is labeled with 1 or 0: 1 means the observation has annual income equal or above $50,000, 0 means the observation has an annual income lower than $50,000. This label information is stored in the income variable. Then data split into train and test. Upon splitting, you can train the data using a method e.g. decision tree (rpart), predict the test data with “predict” function and argument type=”prob”, and aha… see the complete R code below.
# Build a model using decision tree: tree
# Predict probability values using the model: all_probs
# Print out all_probs
# Select second column of all_probs: probs
# Load the ROCR library
# Make a prediction object: pred
# Make a performance object: perf
# Plot this curve
The plot result is as follow:
How to interpret the result of ROC?
Basically, the closer the curve to the upper left corner, the better the classifier. In other words, the “area under curve” should be closed to maximum value, which is 1. We can do comparison of performance based on ROC curve of two methods which are Decision Tree (DT) and K-Nearest Neighbor K-NN as seen in Figure 3. It shows that the DT method that represented by red line outperforms K-NN that represented by green line.
The R Code to draw Figure 3 is represented by the following code:
# Load the ROCR library
# Make the prediction objects for both models: pred_t, pred_k
# probs_t is the result of positive prediction of Decision Tree Model
# probs_k is the result of positive prediction of K-Nearest Neighbor
# Make the performance objects for both models: perf_t, perf_k
# Draw the ROC lines using draw_roc_lines()
Area under curve (AUC) parameter can also be calculated by running this command below. It shows that the AUC of DT is 5% greater than K-NN.
# Make the performance object of pred_t and pred_k: auc_t and auc_k
# Print the AUC value
firstname.lastname@example.org[]# AUC result is 0.9504336
email@example.com[]# AUC result is 0.9076067
ROC (Receiver Operator Characteristic) Curve is a very powerful performance measure.
It is used for binomial classification.
ROCR is a great package to be used in R for drawing ROC curve
The closer the curve to the upper left of area, the better the classifier.