-
Notifications
You must be signed in to change notification settings - Fork 63
/
Copy pathslide_plots.Rmd
238 lines (167 loc) · 6.79 KB
/
slide_plots.Rmd
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
---
title: "Data for slides"
author: "Dr. Stephen W. Thomas, Queen's University"
date: "2017"
output:
pdf_document:
highlight: pygments
number_sections: yes
toc: no
toc_depth: '2'
---
```{r}
library(tidytext)
library(tidyr)
library(dplyr)
library(ggplot2)
library(readr)
library(tm)
```
## Crickets
```{r}
dat <- read_csv("chirps.csv")
theme_set(theme_gray(base_size = 22))
ggplot(dat, aes(x=Chirps, y=Temp)) + geom_point(size=6)
ggplot(dat, aes(x=Chirps, y=Temp)) + geom_point(size=3) + geom_smooth(method=lm)
```
```{r}
library(car)
library(stargazer)
Prestige <-read.table("Prestige.txt", header=TRUE)
head(Prestige, n=20)
Prestige[1:30, 1:2]
str(Prestige)
summary(Prestige$education)
scatterplotMatrix(~ prestige + income +education + women, span =0.7, data = Prestige)
prestige.mod1 <- lm(income ~ education, data= Prestige)
summary(prestige.mod1)
theme_set(theme_gray(base_size = 22))
ggplot(Prestige, aes(x=education, y=income)) + geom_point(size=6)
ggsave("income_1.pdf")
ggplot(Prestige, aes(x=education, y=income)) + geom_point(size=6) + geom_smooth(method = lm)
ggsave("income_2.pdf")
```
```{r}
library(ggplot2)
library(readr)
dat <- read_csv("clusters.csv")
# Temp hack: make these age and income, just from experimentation
dat$age = dat$height*10 + 15
dat$income = dat$weight*20000
iwidth = 7
iheight = 7
theme_set(theme_gray(base_size = 22))
ggplot(dat, aes(x=age, y=income)) + geom_point(aes(color=factor(truth)), size=6) + theme(legend.position="none")
ggsave(file="cluster_1.pdf", width=iwidth, height=iheight)
theme_set(theme_gray(base_size = 22))
ggplot(dat, aes(x=age, y=income)) + geom_point(color="black", size=6)
ggsave(file="cluster_2.pdf", width=iwidth, height=iheight)
```
```{r}
library(ggplot2)
library(readr)
library(extrafont)
library(ggthemes)
loadfonts(quiet = T)
dat <- read_csv("data/Mall_Customers_With_output.csv")
iwidth = 10
iheight = 7
theme_set(theme_gray(base_size = 32))
ggplot(dat, aes(x=AnnualIncome, y=SpendingScore)) +
geom_point(color="black", size=10) +
geom_rangeframe() +
theme_tufte(base_family="Calibri") +
theme(legend.position="none") +
theme(axis.title.x = element_text(colour="grey20",size=40,face="plain")) +
theme(axis.title.y = element_text(colour="grey20",size=40,face="plain")) +
theme(axis.text.x = element_text(colour="grey20",size=32,face="plain")) +
theme(axis.text.y = element_text(colour="grey20",size=32,face="plain")) +
labs(x = "Annual Income (K)", y ="Spending Score")
ggsave(file="out/cluster_1.png", width=iwidth, height=iheight)
ggplot(dat, aes(x=AnnualIncome, y=SpendingScore)) +
geom_point(aes(color=factor(ClassificationName)), size=10) +
geom_rangeframe() +
theme_tufte(base_family="Calibri") +
theme(legend.position="none") +
theme(axis.title.x = element_text(colour="grey20",size=40,face="plain")) +
theme(axis.title.y = element_text(colour="grey20",size=40,face="plain")) +
theme(axis.text.x = element_text(colour="grey20",size=32,face="plain")) +
theme(axis.text.y = element_text(colour="grey20",size=32,face="plain")) +
labs(x = "Annual Income (K)", y ="Spending Score")
ggsave(file="out/cluster_2.png", width=iwidth, height=iheight)
```
```{r}
getRandomDat = function(rho) {
n <- 50 # length of vector
#rho <- 0.8 # desired correlation = cos(angle)
theta <- acos(rho) # corresponding angle
x1 <- rnorm(n, 1, 1) # fixed given data
x2 <- rnorm(n, 2, 0.5) # new random data
X <- cbind(x1, x2) # matrix
Xctr <- scale(X, center=TRUE, scale=FALSE) # centered columns (mean 0)
Id <- diag(n) # identity matrix
Q <- qr.Q(qr(Xctr[ , 1, drop=FALSE])) # QR-decomposition, just matrix Q
P <- tcrossprod(Q) # = Q Q' # projection onto space defined by x1
x2o <- (Id-P) %*% Xctr[ , 2] # x2ctr made orthogonal to x1ctr
Xc2 <- cbind(Xctr[ , 1], x2o) # bind to matrix
Y <- Xc2 %*% diag(1/sqrt(colSums(Xc2^2))) # scale columns to length 1
x3 <- Y[ , 2] + (1 / tan(theta)) * Y[ , 1] # final new vector
as.data.frame(cbind(x1, x3))
}
iwidth = 4
iheight = 4
dat1 = getRandomDat(-0.8)
theme_set(theme_gray(base_size = 20))
ggplot(dat1, aes(x=x1, y=x3)) + labs(x = "Auto Mileage", y ="Auto Value") + geom_point(color="black", size=3) +
theme(axis.text.x=element_blank(),axis.ticks.x=element_blank(), axis.text.y=element_blank(), axis.ticks.y = element_blank())
ggsave(file="cor_neg.png", width=iwidth, height=iheight)
dat2 = getRandomDat(0.0)
theme_set(theme_gray(base_size = 20))
ggplot(dat2, aes(x=x1, y=x3)) + labs(x = "Auto Color", y ="Air Quality") + geom_point(color="black", size=3) +
theme(axis.text.x=element_blank(),axis.ticks.x=element_blank(), axis.text.y=element_blank(), axis.ticks.y = element_blank())
ggsave(file="cor_none.png", width=iwidth, height=iheight)
dat3 = getRandomDat(0.9)
theme_set(theme_gray(base_size = 20))
ggplot(dat3, aes(x=x1, y=x3)) + labs(x = "Auto Accidents", y ="Insurance Cost") + geom_point(color="black", size=3) +
theme(axis.text.x=element_blank(),axis.ticks.x=element_blank(), axis.text.y=element_blank(), axis.ticks.y = element_blank())
ggsave(file="cor_pos.png", width=iwidth, height=iheight)
```
```{r}
iwidth = 4
iheight = 4
dat1 = getRandomDat(-0.8)
theme_set(theme_gray(base_size = 20))
ggplot(dat1, aes(x=x1, y=x3)) + labs(x = "Auto Mileage", y ="Auto Value") + geom_point(color="black", size=3) +
theme(axis.text.x=element_blank(),axis.ticks.x=element_blank(), axis.text.y=element_blank(), axis.ticks.y = element_blank())
ggsave(file="cor_neg.png", width=iwidth, height=iheight)
dat2 = getRandomDat(0.0)
theme_set(theme_gray(base_size = 20))
ggplot(dat2, aes(x=x1, y=x3)) + labs(x = "Auto Color", y ="Air Quality") + geom_point(color="black", size=3) +
theme(axis.text.x=element_blank(),axis.ticks.x=element_blank(), axis.text.y=element_blank(), axis.ticks.y = element_blank())
ggsave(file="cor_none.png", width=iwidth, height=iheight)
dat3 = getRandomDat(0.9)
theme_set(theme_gray(base_size = 20))
ggplot(dat3, aes(x=x1, y=x3)) + labs(x = "Auto Accidents", y ="Insurance Cost") + geom_point(color="black", size=3) +
theme(axis.text.x=element_blank(),axis.ticks.x=element_blank(), axis.text.y=element_blank(), axis.ticks.y = element_blank())
ggsave(file="cor_pos.png", width=iwidth, height=iheight)
```
```{r}
df = data.frame(replicate(10,sample(0:1,9,rep=TRUE)))
df[1,1] = "hello this is steve"
df[2,1] = "is there anything"
df[3,1] = "how now brown cow"
df[4,1] = "the man had feet"
vs <- VectorSource(df[,1])
c <- SimpleCorpus(vs)
```
```{r}
df = data.frame(replicate(4,sample(0:1,5,rep=TRUE)))
df[1,] = c(0, 1, 1, 0)
df[2,] = c(0, 1, 0, 1)
df[3,] = c(1, 2, 1, 0)
df[4,] = c(1, 0, 1, 0)
df[5,] = c(0, 2, 1, 1)
df
pca = princomp(df, scale = TRUE)
pca$scores
```