DSCI 100

source: R for Data Science by Grolemund & Wickham
Suppose we have past data of cancer tumour cell diagnosis labelled “benign” and “malignant”. Do you think a new cell with Concavity = 3.3 and Perimeter = 0.2 would be malignant? How did you decide?
Descriptive, exploratory, predictive, inferential, causal, or mechanistic?
Notes:
Predict the label / class for a new observation using the K closest points from our dataset.
Step 1: Compute the distance between the new observation and each observation in our training set
\[ \text{Distance} = \sqrt{ {({\text{new}_x} - {\text{train}_x})^2 + ({\text{new}_y} - {\text{train}_y})^2}} \]
Notes:
Predict the label / class for a new observation using the K closest points from our dataset.
Step 2: Sort the data in ascending order according to the distances
Step 3: Choose the top K rows as “neighbours”
## # A tibble: 5 x 5
## ID Perimeter Concavity Class dist_from_new
## <dbl> <dbl> <dbl> <fct> <dbl>
## 1 86409 0.241 2.65 B 0.881
## 2 887181 0.750 2.87 M 0.980
## 3 899667 0.623 2.54 M 1.14
## 4 907914 0.417 2.31 M 1.26
## 5 8710441 -1.16 4.04 B 1.28
Predict the label / class for a new observation using the K closest points from our dataset.
Step 4: Classify the new observation based on majority vote.

So… what would the predicted class be?
Notes:
For two observations \(u, v\), each with \(m\) variables (columns) labelled \(1, \dots, m\),
Aside from that, it’s the same algorithm!
Notes:
What if one variable is much larger than the other?
Which observations are closest? What is your initial guess at a classification using K = 3?
Notes:
What if one variable is much larger than the other?
Notes:
What if one variable is much larger than the other?
We need to Standardize: shift and scale so that the average is 0 and the standard deviation (a measure of the spread/variation of the data) is 1.
Notes:
tidymodels package in Rtidymodels is a collection of packages and handles computing distances, standardization, balancing, and prediction for us!
Source: https://www.r-bloggers.com/2019/06/a-gentle-introduction-to-tidymodels/
tidymodels package in RStep 0a: Load the libraries
Not coding in these slides? You can find clean-wbc.data.csv here
Notes:
tidymodels package in RStep 0(b): Read in and inspect data we need
Not coding in these slides? You can find clean-wbc.data.csv here
tidymodels package in RIn tidymodels, the recipes package is named after cooking terms.
Step 1: Make a recipe to specify the predictors/response and preprocess the data
recipe(): Main function in the formula. Arguments:- formula
- data
You can also prep() and bake() a recipe to see what the preprocessing does! Visit https://recipes.tidymodels.org/reference/index.html to see all the preprocessing steps
Notes:
Notes:
tidymodels package in RStep 2: Build a model specification (model_spec) to specify the model and training algorithm
model type: kind of model you want to fit
arguments: model parameter values
engine: underlying package the model should come from
mode: type of prediction (some packages can do both classification and regression)
Notes:
tidymodels package in RStep 3: Put them together in a workflow and then fit it
Notes:
tidymodels package in RStep 4: Predict a new class label using predict

Notes:
Let’s finish our remaining iClicker questions and begin to work through this week’s worksheet.