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
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
names(new.theta) <- names(features)
alpha <- 0.01
# Utility functon that calculates the prediction for an observation
# given the current state of the hypothesis function.
#h.theta <- function(theta, observation) {
# return(sum(theta * observation))
# prediction <- 0.0
# for(i in 1:length(theta)) {
# prediction <- prediction + (theta[i] * observation[i])
# }
# return(prediction)
#}
# for(k in 1:2){
# m <- ncol(features)
#
# for(j in 1:m) {
# n <- nrow(features)
# summation <- 0.0
#
# for(i in 1:n) {
# # prediction <- h.theta(theta, features[i,])
# prediction <- sum(theta * features[i,])
# residual <- prediction - y[i]
# update.value <- residual * features[i, j]
# summation <- summation + update.value
# }
#
# new.theta[j] <- theta[j] + (alpha * summation)
# }
#
# theta <- new.theta
#
# # print(theta)
# }
#
# print(theta)
iterations <- 250
X <- features
for(k in 1:iterations) {
for(j in 1:ncol(X)) {
summation <- 0
for(i in 1:nrow(X)) {
residual <- sum(X[i,] * theta[j]) - y[i]
summation <- summation + (residual * X[i, j])
}
new.theta[j] <- theta[j] - (alpha / nrow(X) * summation)
}
theta <- new.theta
}
lm.model
set.seed(1234)
x <- runif(1000, -5, 5)
y <- x + rnorm(1000) + 3
intercept <- rep(1, length(x))
lm.model <- lm(y ~ x)
summary(lm.model)
features <- data.frame(intercept = intercept, x = x)
theta <- rep(0, ncol(features))
names(theta) <- names(features)
theta
new.theta <- rep(0, ncol(features))
names(new.theta) <- names(features)
alpha <- 0.025
# Utility functon that calculates the prediction for an observation
# given the current state of the hypothesis function.
#h.theta <- function(theta, observation) {
# return(sum(theta * observation))
# prediction <- 0.0
# for(i in 1:length(theta)) {
# prediction <- prediction + (theta[i] * observation[i])
# }
# return(prediction)
#}
# for(k in 1:2){
# m <- ncol(features)
#
# for(j in 1:m) {
# n <- nrow(features)
# summation <- 0.0
#
# for(i in 1:n) {
# # prediction <- h.theta(theta, features[i,])
# prediction <- sum(theta * features[i,])
# residual <- prediction - y[i]
# update.value <- residual * features[i, j]
# summation <- summation + update.value
# }
#
# new.theta[j] <- theta[j] + (alpha * summation)
# }
#
# theta <- new.theta
#
# # print(theta)
# }
#
# print(theta)
iterations <- 250
X <- features
for(k in 1:iterations) {
for(j in 1:ncol(X)) {
summation <- 0
for(i in 1:nrow(X)) {
residual <- sum(X[i,] * theta[j]) - y[i]
summation <- summation + (residual * X[i, j])
}
new.theta[j] <- theta[j] - (alpha / nrow(X) * summation)
}
theta <- new.theta
}
set.seed(1234)
x <- runif(1000, -5, 5)
y <- x + rnorm(1000) + 3
intercept <- rep(1, length(x))
lm.model <- lm(y ~ x)
summary(lm.model)
features <- data.frame(intercept = intercept, x = x)
theta <- rep(0, ncol(features))
names(theta) <- names(features)
theta
new.theta <- rep(0, ncol(features))
names(new.theta) <- names(features)
alpha <- 0.05
# Utility functon that calculates the prediction for an observation
# given the current state of the hypothesis function.
#h.theta <- function(theta, observation) {
# return(sum(theta * observation))
# prediction <- 0.0
# for(i in 1:length(theta)) {
# prediction <- prediction + (theta[i] * observation[i])
# }
# return(prediction)
#}
# for(k in 1:2){
# m <- ncol(features)
#
# for(j in 1:m) {
# n <- nrow(features)
# summation <- 0.0
#
# for(i in 1:n) {
# # prediction <- h.theta(theta, features[i,])
# prediction <- sum(theta * features[i,])
# residual <- prediction - y[i]
# update.value <- residual * features[i, j]
# summation <- summation + update.value
# }
#
# new.theta[j] <- theta[j] + (alpha * summation)
# }
#
# theta <- new.theta
#
# # print(theta)
# }
#
# print(theta)
iterations <- 250
X <- features
for(k in 1:iterations) {
for(j in 1:ncol(X)) {
summation <- 0
for(i in 1:nrow(X)) {
residual <- sum(X[i,] * theta[j]) - y[i]
summation <- summation + (residual * X[i, j])
}
new.theta[j] <- theta[j] - (alpha / nrow(X) * summation)
}
theta <- new.theta
}
set.seed(1234)
x <- runif(1000, -5, 5)
y <- x + rnorm(1000) + 3
intercept <- rep(1, length(x))
lm.model <- lm(y ~ x)
summary(lm.model)
features <- data.frame(intercept = intercept, x = x)
theta <- rep(0, ncol(features))
names(theta) <- names(features)
theta
new.theta <- rep(0, ncol(features))
names(new.theta) <- names(features)
alpha <- 0.05
# Utility functon that calculates the prediction for an observation
# given the current state of the hypothesis function.
#h.theta <- function(theta, observation) {
# return(sum(theta * observation))
# prediction <- 0.0
# for(i in 1:length(theta)) {
# prediction <- prediction + (theta[i] * observation[i])
# }
# return(prediction)
#}
# for(k in 1:2){
# m <- ncol(features)
#
# for(j in 1:m) {
# n <- nrow(features)
# summation <- 0.0
#
# for(i in 1:n) {
# # prediction <- h.theta(theta, features[i,])
# prediction <- sum(theta * features[i,])
# residual <- prediction - y[i]
# update.value <- residual * features[i, j]
# summation <- summation + update.value
# }
#
# new.theta[j] <- theta[j] + (alpha * summation)
# }
#
# theta <- new.theta
#
# # print(theta)
# }
#
# print(theta)
iterations <- 300
X <- features
for(k in 1:iterations) {
for(j in 1:ncol(X)) {
summation <- 0
for(i in 1:nrow(X)) {
residual <- sum(X[i,] * theta[j]) - y[i]
summation <- summation + (residual * X[i, j])
}
new.theta[j] <- theta[j] - (alpha / nrow(X) * summation)
}
theta <- new.theta
}
set.seed(1234)
x <- runif(1000, -5, 5)
y <- x + rnorm(1000) + 3
intercept <- rep(1, length(x))
lm.model <- lm(y ~ x)
summary(lm.model)
features <- data.frame(intercept = intercept, x = x)
theta <- rep(0, ncol(features))
names(theta) <- names(features)
theta
new.theta <- rep(0, ncol(features))
names(new.theta) <- names(features)
alpha <- 0.05
# Utility functon that calculates the prediction for an observation
# given the current state of the hypothesis function.
#h.theta <- function(theta, observation) {
# return(sum(theta * observation))
# prediction <- 0.0
# for(i in 1:length(theta)) {
# prediction <- prediction + (theta[i] * observation[i])
# }
# return(prediction)
#}
# for(k in 1:2){
# m <- ncol(features)
#
# for(j in 1:m) {
# n <- nrow(features)
# summation <- 0.0
#
# for(i in 1:n) {
# # prediction <- h.theta(theta, features[i,])
# prediction <- sum(theta * features[i,])
# residual <- prediction - y[i]
# update.value <- residual * features[i, j]
# summation <- summation + update.value
# }
#
# new.theta[j] <- theta[j] + (alpha * summation)
# }
#
# theta <- new.theta
#
# # print(theta)
# }
#
# print(theta)
iterations <- 500
X <- features
for(k in 1:iterations) {
for(j in 1:ncol(X)) {
summation <- 0
for(i in 1:nrow(X)) {
residual <- sum(X[i,] * theta[j]) - y[i]
summation <- summation + (residual * X[i, j])
}
new.theta[j] <- theta[j] - (alpha / nrow(X) * summation)
}
theta <- new.theta
}
data(iria)
data(iris)
install.packages(c("lmtest", "mgcv", "nlme"))
data("iris")
library(GGally)
ggpairs(iris)
remove.packages("tibble")
library(GGally)
remove.packages("GGally")
remove.packages("plotly")
install.pacakges("GGally")
install.packages("GGally")
library(GGally)
install.packages(tibble)
install.packages("tibble")
library(GGally)
data("iris")
ggpairs(iris)
debugSource('~/Dropbox/AmsterdamBootcamp/GradientDescentExample.R', echo=TRUE)
debugSource('~/Dropbox/AmsterdamBootcamp/GradientDescentExample.R', echo=TRUE)
#
# Copyright 2017 Dave Langer
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
#
#
# This R source code file corresponds to video 10 of the YouTube series
# "R Programming for Excel Users" located at the following URL:
# https://youtu.be/gYt05xI2Fm8
#
#===========================================================================
# Numeric Vectors
#
# Create a vector of integer values
my_vector <- 1:10
my_vector
# Inspect the vector more closely
class(my_vector)
str(my_vector)
summary(my_vector)
# Add 1 to each value of the vector
my_vector_plus1 <- my_vector + 1
my_vector_plus1
# Divide each value of the vector by 2
half_my_vector <- my_vector / 2
half_my_vector
# Make the vector whole again
whole_my_vector <- half_my_vector + half_my_vector
whole_my_vector
# Square the value of each vector
my_vector_squared1 <- my_vector * my_vector
my_vector_squared1
# Square the value of each vector
my_vector_squared2 <- my_vector ^ 2
my_vector_squared2
# Take the square root of each value
sqrt_my_vector <- sqrt(my_vector)
sqrt_my_vector
# More vectorized functions
sum(my_vector)
mean(my_vector)
sd(my_vector)
#===========================================================================
# Logical Vectors
#
# Which values are greater than 3.5?
larger_than_3.5 <- my_vector > 3.5
larger_than_3.5
# Inspect vector more closely
class(larger_than_3.5)
str(larger_than_3.5)
summary(larger_than_3.5)
# Grab only the values larger than 3.5
my_vector2 <- my_vector[larger_than_3.5]
my_vector2
# Grab only the values larger than 3.5
my_vector3 <- my_vector[my_vector > 3.5]
my_vector3
# Grow the vector
my_bigger_vector <- c(my_vector, 11:15, 16, 17, 18, 19, 20)
my_bigger_vector
# How big is it now?
length(my_bigger_vector)
dim(my_bigger_vector)
#===========================================================================
# String Vectors
#
# Create a vector of strings
force_users <- c("Yoda", "Darth Vader", "Obi Wan", "Mace Windu",
"Darth Maul", "Luke Skywalker", "Darth Sidious")
# Inspect vector more closely
class(force_users)
str(force_users)
summary(force_users)
# Add 1 to string vector
force_users + 1
# Add another force user
force_users <- force_users + "Kylo Ren"
# Add more force users
more_force_users <- c(force_users, "Qui-Gon Jinn", "Darth Tyranus")
more_force_users
# How big is the vector?
length(more_force_users)
# How long is each string in the vector?
name_lengths <- nchar(more_force_users)
name_lengths
#===========================================================================
# Missing Values
#
# Build a vector with missing values
birthplaces <- c(NA, "Tatooine", "Stewjon", "Haruun Kal", "Dathomir",
"Polis Massa", "Naboo", "Coruscant", "Serenno")
birthplaces
# Inspect closer
class(birthplaces)
str(birthplaces)
summary(birthplaces)
# Vectorized operation
is.na(birthplaces)
nchar(birthplaces)
nchar("")
# Logical operations
birthplaces[!is.na(birthplaces)]
#===========================================================================
# Factor Vectors
#
# Create factor (categorical) vector
affiliation <- as.factor(c("Jedi", "Sith", "Rogue"))
affiliation
# Inspect
class(affiliation)
str(affiliation)
summary(affiliation)
levels(affiliation)
# Explore representations
as.numeric(affiliation)
as.character(affiliation)
#===========================================================================
# Data Frames
#
star_wars <- data.frame(id = 1:length(more_force_users),
more_force_users,
birthplaces = as.factor(birthplaces),
affiliation = c("Jedi", "Sith",
"Jedi", "Jedi",
"Sith", "Jedi",
"Sith", "Jedi",
"Sith"),
stringsAsFactors = FALSE)
# Inspect
View(star_wars)
head(star_wars)
summary(star_wars)
str(star_wars)
# Set up factors
star_wars$affiliation <- as.factor(star_wars$affiliation)
# Reinspect
str(star_wars)
# Additional slicing syntax
star_wars$more_force_users[3]
star_wars$more_force_users[star_wars$affiliation == "Sith"]
# Load-up some built in data
data(iris)
data(mtcars)
# Get help on built-in data
?mtcars
# Understand the shape of a data frame
nrow(mtcars)
ncol(mtcars)
dim(mtcars)
# Understand the metadata of a data frame
names(mtcars)
names(mtcars)[3]
colnames(mtcars)
colnames(mtcars)[3:5]
rownames(mtcars)
rownames(mtcars)[c(3, 4, 5)]
# Cool RStudio feature - spreadsheet view of a data frame
View(mtcars)
# See a few rows at the top and bottom of a data frame
head(mtcars)
tail(mtcars)
# All-up view of a data frame
summary(mtcars)
# Understand the data type of a data frame
class(mtcars)
str(mtcars)
setwd("~/Dropbox/DataScienceDojo/IntroToTextAnalyticsWithR")
spam.raw <- read.csv("spam.csv", stringsAsFactors = FALSE, fileEncoding = "UTF-16")
View(spam.raw)
# Clean up the data frame and view our handiwork.
spam.raw <- spam.raw[, 1:2]
names(spam.raw) <- c("Label", "Text")
View(spam.raw)
# Check data to see if there are missing values.
length(which(!complete.cases(spam.raw)))
# Convert our class label into a factor.
spam.raw$Label <- as.factor(spam.raw$Label)
# The first step, as always, is to explore the data.
# First, let's take a look at distibution of the class labels (i.e., ham vs. spam).
prop.table(table(spam.raw$Label))
# Next up, let's get a feel for the distribution of text lengths of the SMS
# messages by adding a new feature for the length of each message.
spam.raw$TextLength <- nchar(spam.raw$Text)
summary(spam.raw$TextLength)
# Visualize distribution with ggplot2, adding segmentation for ham/spam.
library(ggplot2)
ggplot(spam.raw, aes(x = TextLength, fill = Label)) +
theme_bw() +
geom_histogram(binwidth = 5) +
labs(y = "Text Count", x = "Length of Text",
title = "Distribution of Text Lengths with Class Labels")