DATA 621 Homework #2

Overview

In this homework assignment, you will work through various classification metrics. You will be asked to create functions in R to carry out the various calculations. You will also investigate some functions in packages that will let you obtain the equivalent results. Finally, you will create graphical output that also can be used to evaluate the output of classification models, such as binary logistic regression.

Supplemental Material:

Deliverables

  • Upon following the instructions below, use your created R functions and the other packages to generate the classification metrics for the provided data set. A write-up of your solutions submitted in PDF format.

Task 1: Download Data Set

  1. Download the classification output data set (attached in Blackboard to the assignment).

Task 2: Confusion Matrix

  1. The data set has three key columns we will use:
  • class: the actual class for the observation
  • scored.class: the predicted class for the observation (based on a threshold of 0.5)
  • scored.probability: the predicted probability of success for the observation

Use the table() function to get the raw confusion matrix for this scored dataset. Make sure you understand the output. In particular, do the rows represent the actual or predicted class? The columns?

Answer: the field class (the rows) represent the actual class, and the field scored.class (the columns) represent the predicted class.

##                  scored.class
## class             Predicted Negative Predicted Positive
##   Actual Negative                119                  5
##   Actual Positive                 30                 27

Task 3: Accuracy

  1. Write a function that takes the data set as a dataframe, with actual and predicted classifications identified, and returns the accuracy of the predictions.

\(Accuracy = \displaystyle \frac{TP+TN}{TP+FP+TN+FN}\)

Answer: Accuracy means the closeness of the measurements to a specific value.

A function named func_accuracy is created to represent the formula of Accuracy.

description of variables in the function:

  • TP: True Positive

  • TN: True Negative

## [1] 0.8066298

Task 4: Classification Error Rate

  1. Write a function that takes the data set as a dataframe, with actual and predicted classifications identified, and returns the classification error rate of the predictions.

\(Classification Error Rate = \displaystyle \frac{FP+FN}{TP+FP+TN+FN}\)

Answer: Clasification error rate means the ratio of total number of units in error to the total population, or can be calculated as 1-Accuracy

A function named func_Error_Rate is created to represent the formula of Classification Error Rate.

Descrption of variables in the function:

  • FP: False Positive

  • FN: False Negative

## [1] 0.1933702

Verify that you get an accuracy and an error rate that sums to one. Answer: verifed the output of functions func_accuracy and func_Error_Rate add up to 1.

## [1] 1

Task 5: Precision

  1. Write a function that takes the data set as a dataframe, with actual and predicted classifications identified, and returns the precision of the predictions.

\(Precision = \displaystyle \frac{TP}{TP+FP}\)

Answer: Precision means the closeness of the measurements to each other.

A function named func_precision is created to represent the formula of Precision.

Descrption of variables in the function:

  • FP: False Positive

  • TP: True Positive

## [1] 0.84375

Task 6: Sensitivity

  1. Write a function that takes the data set as a dataframe, with actual and predicted classifications identified, and returns the sensitivity of the predictions. Sensitivity is also known as recall.

\(Sensitivity = \displaystyle \frac{TP}{TP+FN}\)

Answer: Sensitivity means the proportion of actual positives that are correctly identified as such, AKA True Positive Rate

A function named func_sensitivity is created to represent the formula of Sensitivity.

Descrption of variables in the function:

  • FN: False Negative

  • TP: True Positive

## [1] 0.4736842

Task 7: Specificity

  1. Write a function that takes the data set as a dataframe, with actual and predicted classifications identified, and returns the specificity of the predictions.

\(Specificity = \displaystyle \frac{TN}{TN+FP}\)

Answer: Specificity means the proportion of actual negatives that are correctly identified as such, AKA True Negative Rate

A function named func_specificity is created to represent the formula of Specificity.

Descrption of variables in the function:

  • TN: True Negative

  • FP: False Positive

## [1] 0.9596774

Task 8: F1Score

  1. Write a function that takes the data set as a dataframe, with actual and predicted classifications identified, and returns the F1 score of the predictions.

\(F1Score = \displaystyle \frac{2 \times Precision \times Sensitivity}{Precision+Sensitivity}\)

Answer: F1Score is a measure of a test’s accuracy. It is calucalted as the harmonic mean of the precision and Sensitivity.

A function named func_f1score is created to represent the formula of F1 score. Precision and Sensitivity are used to compute F1 score, therefore the function func_precision and ‘func_sensitivity’ defined above are reused in this question.

## [1] 0.6067416

Task 9: Prove 0 < F1Score < 1

  1. Before we move on, let’s consider a question that was asked: What are the bounds on the F1 score? Show that the F1 score will always be between 0 and 1. (Hint: If \(0<a<1\) and \(0<b<1\) then \(ab<a\).)

Answer: let \(\alpha = Precision\), \(\beta = Sensitivity\), \(\gamma = F1 Score = \displaystyle \frac{2 \times \alpha \times \beta}{\alpha+\beta}\)

\(\because\) \(0<\alpha<1\) and \(0<\beta<1\)

\(\therefore\displaystyle \frac{2 \times \alpha \times \beta}{\alpha+\beta} > 0\)

and \(\because 0<\alpha<1\) and \(0<\beta<1\) then \(\alpha\beta<\alpha\)

\(\therefore \displaystyle \frac{2 \times \alpha \times \beta}{\alpha+\beta} = \displaystyle \frac{\alpha\beta}{\alpha+\beta}+\frac{\alpha\beta}{\alpha+\beta}< \displaystyle \frac{\alpha}{\alpha+\beta}+\frac{\beta}{\alpha+\beta} = \displaystyle \frac{\alpha+\beta}{\alpha+\beta} = 1\)

\(\therefore 0<\gamma<1\)

Task 10: ROC Curve

  1. Write a function that generates an ROC curve from a data set with a true classification column (class in our example) and a probability column (scored.probability in our example). Your function should return a list that includes the plot of the ROC curve and a vector that contains the calculated area under the curve (AUC). Note that I recommend using a sequence of thresholds ranging from 0 to 1 at 0.01 intervals.

Answer: ROC curve (short form of Receiver Operating Characteristic curve), is a graphical phot that illustrates the diagonostic ability of a binary classifier system as its discrimination threshold is varied (Reference: Wikipedia).

The ROC curve is created by plotting the true positive rate (TPR, or a.k.a Senstivity) against the false positive rate (FPR, can be calculated as (1-Specificity)) at various threshold settings.

Task 11: Produce All Metrics

  1. Use your created R functions and the provided classification output data set to produce all of the classification metrics discussed above.
## Warning: package 'knitr' was built under R version 3.5.3
Created Functions
Accuracy 0.8066298
Classification Error Rate 0.1933702
Precision 0.8437500
Sensitivity 0.4736842
Specificity 0.9596774
F1 Score 0.6067416

Task 12: Package: Caret

  1. Investigate the caret package. In particular, consider the functions confusionMatrix, sensitivity, and specificity. Apply the functions to the data set. How do the results compare with your own functions?
## Loading required package: lattice
## 
## Attaching package: 'caret'
## The following object is masked from 'package:purrr':
## 
##     lift
Caret Package Created Functions
Accuracy 0.8066298 0.8066298
Sensitivity 0.4736842 0.4736842
Specificity 0.9596774 0.9596774
The results fr om the caret package and the functions confusionMatrix, sensitivity, and specificity are the same.

Task 13: Package: pROC

13.Investigate the pROC package. Use it to generate an ROC curve for the data set. How do the results compare with your own functions?

FALSE Warning: package 'pROC' was built under R version 3.5.3

It appears that our results are similiar to that of the ROC curve for the data set.

Critical Thinking Group 4: Rajwant Mishra, Priya Shaji, Debabrata Kabiraj, Isabel Ramesar, Sin Ying Wong and Fan Xu

2/20/2020