forked from mhahsler/Introduction_to_Data_Mining_R_Examples
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathchap2.R
333 lines (271 loc) · 10.7 KB
/
chap2.R
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
#' ---
#' title: "R Code for Chapter 2 of Introduction to Data Mining: Data (Tidyverse)"
#' author: "Michael Hahsler"
#' output:
#' html_document:
#' toc: true
#' ---
#' This code covers chapter 2 of _"Introduction to Data Mining"_
#' by Pang-Ning Tan, Michael Steinbach and Vipin Kumar.
#' __See [table of contents](https://github.com/mhahsler/Introduction_to_Data_Mining_R_Examples#readme) for code examples for other chapters.__
#'
#' ![CC](https://i.creativecommons.org/l/by/4.0/88x31.png)
#' This work is licensed under the
#' [Creative Commons Attribution 4.0 International License](http://creativecommons.org/licenses/by/4.0/). For questions please contact
#' [Michael Hahsler](http://michael.hahsler.net).
#'
#' # Tidyverse
#'
#' Some of the code uses tidyverse tibbles to replace data.frames, the pipe operator `%>%` to chain
#' functions and data transformation functions like `filter()`, `arrange()`, `select()`, and
#' `mutate()` provided by the tidyverse package `dplyr`. A good overview is given in
#' the [RStudio Data Transformation Cheat Sheet](https://github.com/rstudio/cheatsheets/raw/master/data-transformation.pdf) and an introduction can be found in the
#' [Section on Data Wrangling](https://r4ds.had.co.nz/wrangle-intro.html) the free book [R for Data Science](https://r4ds.had.co.nz).
#'
library(tidyverse)
library(ggplot2)
library(GGally) # for ggpairs
#' # Preparation
#' Load the iris data set and convert the data.frame into a tibble.
data(iris)
iris <- as_tibble(iris)
iris
#' # Data Quality
#' Inspect data (produce a scatterplot matrix using `ggpairs` from package `GGally`). Possibly
#' you can see noise and ouliers.
ggpairs(iris, aes(color = Species))
#' Get summary statistics for each column (outliers, missing values)
summary(iris)
#' just the mean
iris %>% summarize_if(is.numeric, mean)
#' Often you will do something
#' like:
clean.data <- iris %>% drop_na() %>% unique()
summary(clean.data)
#' Note that one case (non-unique) is gone. All cases with missing
#' values will also have been dropped.
#'
#' # Aggregation
#' Aggregate by species. First group the data and then summarize each group.
iris %>% group_by(Species) %>% summarize_all(mean)
iris %>% group_by(Species) %>% summarize_all(median)
#'
#' # Sampling
#' ## Random sampling
#'
#' Sample from a vector with replacement.
sample(c("A", "B", "C"), size = 10, replace = TRUE)
#' Sampling rows from a tibble.
set.seed(1000)
s <- iris %>% sample_n(15)
ggpairs(s, aes(color = Species))
#' ## Stratified sampling
#'
#' You need to install the package sampling with:
#' install.packages("sampling")
library(sampling)
id2 <- strata(iris, stratanames="Species", size=c(5,5,5), method="srswor")
id2
s2 <- iris %>% slice(id2$ID_unit)
ggpairs(s2, aes(color = Species))
#' # Features
#' ## Dimensionality reduction (Principal Components Analysis - PCA)
#'
#' Interactive 3d plots (needs package plotly)
library(plotly)
plot_ly(iris, x = ~Sepal.Length, y = ~Petal.Length, z = ~Sepal.Width,
size = ~Petal.Width, color = ~Species, type="scatter3d",
mode="markers")
#' Calculate the principal components
pc <- iris %>% select(-Species) %>% as.matrix() %>% prcomp()
#' How important is each principal component?
plot(pc)
#' Inspect the raw object (display *str*ucture)
str(pc)
ggplot(as_tibble(pc$x), aes(x = PC1, y = PC2, color = iris$Species)) + geom_point()
#' Plot the projected data and add the original dimensions as arrows (this can be done with ggplot2, but is currently painful; see https://stackoverflow.com/questions/6578355/plotting-pca-biplot-with-ggplot2).
biplot(pc, col = c("grey", "red"))
#' ## Feature selection
#'
#' We will talk about feature selection when we discuss classification models.
#'
#' ## Discretize features
ggplot(iris, aes(x = Petal.Width, y = 1:150)) + geom_point()
#' A histogram is a better visualization for the distribution of a single
#' variable.
ggplot(iris, aes(Petal.Width)) + geom_histogram()
#' Equal interval width
iris %>% pull(Sepal.Width) %>% cut(breaks=3)
#' Other methods (equal frequency, k-means clustering, etc.)
library(arules)
iris %>% pull(Petal.Width) %>% discretize(method = "interval", breaks = 3)
iris %>% pull(Petal.Width) %>% discretize(method = "frequency", breaks = 3)
iris %>% pull(Petal.Width) %>% discretize(method = "cluster", breaks = 3)
ggplot(iris, aes(Petal.Width)) + geom_histogram() +
geom_vline(xintercept =
iris %>% pull(Petal.Width) %>% discretize(method = "interval", breaks = 3, onlycuts = TRUE),
color = "blue") +
labs(title = "Discretization: interval", subtitle = "Blue lines are boundaries")
ggplot(iris, aes(Petal.Width)) + geom_histogram() +
geom_vline(xintercept =
iris %>% pull(Petal.Width) %>% discretize(method = "frequency", breaks = 3, onlycuts = TRUE),
color = "blue") +
labs(title = "Discretization: frequency", subtitle = "Blue lines are boundaries")
ggplot(iris, aes(Petal.Width)) + geom_histogram() +
geom_vline(xintercept =
iris %>% pull(Petal.Width) %>% discretize(method = "cluster", breaks = 3, onlycuts = TRUE),
color = "blue") +
labs(title = "Discretization: cluster", subtitle = "Blue lines are boundaries")
#' ## Standardize data (Z-score)
#'
#' Standardize the scale of features to make them comparable. For each
#' column the mean is subtracted (centering) and it is divided by the
#' standard deviation (scaling). Now most values should be in [-3,3].
iris.scaled <- iris %>% mutate_if(is.numeric, function(x) as.vector(scale(x)))
iris.scaled
summary(iris.scaled)
#' # Proximities: Similarities and distances
#'
#' __Note:__ R actually only uses dissimilarities/distances.
#'
#' ## Minkovsky distances
iris_sample <- iris.scaled %>% select(-Species) %>% slice(1:5)
iris_sample
#' Calculate distances matrices between the first 5 flowers (use only the 4 numeric columns).
iris_sample %>% dist(method="euclidean")
iris_sample %>% dist(method="manhattan")
iris_sample %>% dist(method="maximum")
#' __Note:__ Don't forget to scale the data if the ranges are very different!
#'
#' ## Distances for binary data (Jaccard and Hamming)
b <- rbind(
c(0,0,0,1,1,1,1,0,0,1),
c(0,0,1,1,1,0,0,1,0,0)
)
b
#' ### Jaccard index
#'
#' Jaccard index is a similarity measure so R reports 1-Jaccard
b %>% dist(method = "binary")
#' ### Hamming distance
#'
#' Hamming distance is the number of mis-matches (equivalent to
#' Manhattan distance on 0-1 data and also the squared Euclidean distance).
b %>% dist(method = "manhattan")
b %>% dist(method = "euclidean") %>% "^"(2)
#' _Note_: `"^"(2)` calculates the square.
#'
#' ## Distances for mixed data
#'
#' ### Gower's distance
#'
#' Works with mixed data
data <- tibble(
height= c( 160, 185, 170),
weight= c( 52, 90, 75),
sex= c( "female", "male", "male")
)
data
#' __Note:__ Nominal variables need to be factors!
data <- data %>% mutate_if(is.character, factor)
data
library(proxy)
d_Gower <- data %>% dist(method="Gower")
d_Gower
#' __Note:__ Gower's distance automatically scales, so no need to scale
#' the data first.
#'
#' ### Using Euclidean distance with mixed data
#'
#' Sometimes methods (e.g., k-means) only can use Euclidean distance. In this
#' case, nominal features can be converted into 0-1 dummy variables. Euclidean
#' distance on these will result in a usable distance measure.
#'
#' Create dummy variables
library(caret)
data_dummy <- dummyVars(~., data) %>% predict(data)
data_dummy
#' Since sex has now two columns, we need to weight them by 1/2 after scaling.
weight <- matrix(c(1,1,1/2,1/2), ncol = 4, nrow = nrow(data_dummy), byrow = TRUE)
data_dummy_scaled <- scale(data_dummy) * weight
d_dummy <- data_dummy_scaled %>% dist()
d_dummy
#' Distance is (mostly) consistent with Gower's distance (other than that
#' Gower's distance is scaled between 0 and 1).
ggplot(tibble(d_dummy, d_Gower), aes(x = d_dummy, y = d_Gower)) +
geom_point() +
geom_smooth(method = "lm", se = FALSE)
#' ## Additional proximity measures available in package proxy
library(proxy)
pr_DB$get_entries() %>% names()
#' # Relationship between features
#' ## Correlation (for ratio/interval scaled features)
#' Pearson correlation between features (columns)
cc <- iris %>% select(-Species) %>% cor()
ggplot(iris, aes(Petal.Length, Petal.Width)) + geom_point() +
geom_smooth(method = "lm")
with(iris, cor(Petal.Length, Petal.Width))
with(iris, cor.test(Petal.Length, Petal.Width))
ggplot(iris, aes(Sepal.Length, Sepal.Width)) + geom_point() +
geom_smooth(method = "lm")
with(iris, cor(Sepal.Length, Sepal.Width))
with(iris, cor.test(Sepal.Length, Sepal.Width))
#' ## Rank correlation (for ordinal features)
#' convert to ordinal variables with cut (see ? cut) into
#' ordered factors with three levels
iris_ord <- iris %>% mutate_if(is.numeric,
function(x) cut(x, 3, labels = c("short", "medium", "long"), ordered = TRUE))
iris_ord
summary(iris_ord)
iris_ord %>% pull(Sepal.Length)
#' Kendall's tau rank correlation coefficient
iris_ord %>% select(-Species) %>% sapply(xtfrm) %>% cor(method="kendall")
#' Spearman's rho
iris_ord %>% select(-Species) %>% sapply(xtfrm) %>% cor(method="spearman")
#' __Note:__ unfortunately we have to transform the ordered factors
#' into numbers representing the order with xtfrm first.
#'
#' Compare to the Pearson correlation on the original data
iris %>% select(-Species) %>% cor()
#' ## Relationship between nominal and ordinal features
#' Is sepal length and species related? Use cross tabulation
tbl <- iris_ord %>% select(Sepal.Length, Species) %>% table()
tbl
# this is a little more involved using tidyverse
iris_ord %>%
select(Species, Sepal.Length) %>%
pivot_longer(cols = Sepal.Length) %>%
group_by(Species, value) %>% count() %>% ungroup() %>%
pivot_wider(names_from = Species, values_from = n)
#' Test of Independence: Pearson's chi-squared test is performed with the null hypothesis that the joint distribution of the cell counts in a 2-dimensional contingency table is the product of the row and column marginals. (h0 is independence)
tbl %>% chisq.test()
#' Using xtabs instead
x <- xtabs(~Sepal.Length + Species, data = iris_ord)
x
summary(x)
#' Group-wise averages
iris %>% group_by(Species) %>% summarize_at(vars(Sepal.Length), mean)
iris %>% group_by(Species) %>% summarize_all(mean)
#' # Density estimation
#'
#' Just plotting the data is not very helpful
ggplot(iris, aes(Petal.Length, 1:150)) + geom_point()
#' Histograms work better
ggplot(iris, aes(Petal.Length)) +
geom_histogram() +
geom_rug(alpha = 1/10)
#'
#' Kernel density estimate KDE
ggplot(iris, aes(Petal.Length)) +
geom_rug(alpha = 1/10) +
geom_density()
#' Plot 2d kernel density estimate
ggplot(iris, aes(Sepal.Length, Sepal.Width)) +
geom_jitter() +
geom_density2d()
ggplot(iris, aes(Sepal.Length, Sepal.Width)) +
geom_bin2d(bins = 10) +
geom_jitter(color = "red")
ggplot(iris, aes(Sepal.Length, Sepal.Width)) +
geom_hex(bins = 10) +
geom_jitter(color = "red")