-
Notifications
You must be signed in to change notification settings - Fork 6
/
.Rhistory
512 lines (512 loc) · 9.02 KB
/
.Rhistory
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
test1
test1$score <- as.character(test1$score)
test1
str(test1)
test1_5
test1_5$score <- as.character(test1_5$score)
test1_5
test1
write.table(
test1,
file = "r/test.txt",
sep = "\t"
)
write.table(
test1,
file = "r/test.txt",
sep = "\t",
quote = FALSE
)
readr::read_tsv("r/test.txt")
write.table(
test1,
file = "r/test.txt",
sep = "\t",
row.names = FALSE,
quote = FALSE
)
readr::read_tsv("r/test.txt")
library(readr)
read_delim("r/test1.xx", delim = " ABC ")
read_lines("r/test1.xx")
x <- read_lines("r/test1.xx")
gsub(" ABC ", ",", x)
?read_tsv
default_locale()
read_file("r/test1.xx")
x2 <- read_file("r/test1.xx")
x
x2
library(tidyverse)
mtcars
## 管道 %>%
# Unix |
sum(1:10)
1:10 %>% sum
mtcars
## 取子集
mtcars %>% filer(gear == 4)
## 取子集
mtcars %>% filter(gear == 4)
mtcars %>%
filter(gear == 4) %>%
nrow()
norw(filter(mtcar, gear == 4))
nrow(filter(mtcar, gear == 4))
nrow(filter(mtcars, gear == 4))
colnames(mtcars)
mtcars %>%
select(mpg, cyl)
## 描述
summary(mtcars)
glimpse(mtcars)
?set
?setdiff()
intersect(a, b)
a <- c("A", "B", "C")
b <- c("B", "C", "D")
intersect(a, b)
union(a, b)
setdiff(a, b)
setdiff(b, a)
setequal(a, b)
data1 <- mtcars %>%
select(mpg, cyl) %>%
head(4)
data1
data2 <- mtcars %>%
select(mpg, cyl) %>%
slice(2:5)
data2
data1
data2
data1[2:4,]
intersect(data1, data2)
## 连接操作 join
# 数据库
test1
test2 <- data.frame(
stu = c("B", "C", "D"),
class = c("a", "b", "c")
)
test1
test2
full_join(
test1,
test2
)
full_join(
test1,
test2,
by = "stu"
)
left_join(
test1,
test2,
by = "stu"
)
test1
right_join(
test1,
test2,
by = "stu"
)
semi_join(
test1,
test2,
by = "stu"
)
test1
test2
anti_join(
test1,
test2,
by = "stu"
)
test1
test2
## 列修改
test1 %>%
mutate(
class = c("a", "b", "c")
)
test1 %>%
mutate(
stu = c("a", "b", "c")
)
?merge
## 排序
test1 %>%
arrange(score)
test1 %>%
arrange(desc(score))
## 计数
test1 %>%
count("stu")
## 计数
test1 %>%
count(stu)
## 记录添加
test1
## 记录添加
test1 %>%
add_row(stu = "D", score = 100)
?add_row
str(test)
str(testq1)
str(test1)
## 记录添加
test1 %>%
add_row(stu = "D", score = "100")
test1 %>%
mutate(
stu = c("a", "b", "c")
)
test1 %>%
mutate(
stu = c("a", "b", "c")
)
test1 %>%
transmute(
stu = c("a", "b", "c")
)
mtcars
mtcars %>%
group_by(gear) %>%
summarise(
wt_mean = mean(wt)
)
?summarise_each
mtcars %>%
group_by(gear) %>%
summarise_each(
list(
mean = mean,
sd = sd
)
)
mtcars %>%
group_by(gear, vs) %>%
summarise(
wt_mean = mean(wt)
)
## 数据变换
cumsum(1:10)
test1
test1 %>%
mutate(
rowid = row_number()
)
## 基因表达矩阵
expr <- matrix(
rnorm(1000),
100, 10
)
expr
## 基因表达矩阵
expr <- matrix(
rnorm(1000),
100, 10,
dimnames = list(
paste0("Gene", 1:100),
paste0("Sample", 1:10)
)
)
expr[1:5, 1:5]
apply(expr, 1, mean)
apply(expr, 2, mean)
rowMeans(expr)
identical(
apply(expr, 1, mean),
rowMeans(expr)
)
mean2 <- function(...) {
mean(...)
}
identical(
apply(expr, 1, mean2),
rowMeans(expr)
)
a_list <- list(
1:10,
2:5,
3
)
str(a_list)
lapply(a_list, mean)
sapply(a_list, mean) # simplified lapply
?tapply
file.edit("r/04-da-intro.R")
# 分布函数
# d
# p
# q
# r 生成符合某个分布的随机数据
rnorm(100)
hist(rnorm(100))
hist(rnorm(10000))
?dnorm
dnorm(0) == 1/sqrt(2*pi)
dnorm(1) == exp(-1/2)/sqrt(2*pi)
dnorm(1) == 1/sqrt(2*pi*exp(1))
## Using "log = TRUE" for an extended range :
par(mfrow = c(2,1))
plot(function(x) dnorm(x, log = TRUE), -60, 50,
main = "log { Normal density }")
curve(log(dnorm(x)), add = TRUE, col = "red", lwd = 2)
mtext("dnorm(x, log=TRUE)", adj = 0)
mtext("log(dnorm(x))", col = "red", adj = 1)
plot(function(x) pnorm(x, log.p = TRUE), -50, 10,
main = "log { Normal Cumulative }")
curve(log(pnorm(x)), add = TRUE, col = "red", lwd = 2)
mtext("pnorm(x, log=TRUE)", adj = 0)
mtext("log(pnorm(x))", col = "red", adj = 1)
# 线性回归
library(tidyverse)
re <- read_csv("r/data/real-estate.csv")
lmfit <- lm(Price ~ Size, data = re)
lmfit
# Price = -113509 + 283 * Size
coef(lmfit)
# Price = -113509 + 283 * Size
coef(lmfit)[1]
coef(lmfit)[2]
confint(lmfit)
AIC(lmfit)
BIC(lmfit)
?AIC
?BIC
residuals(lmfit)
shapiro.test(residuals(lmfit))
?shapiro.test
shapiro.test(rnorm(100, mean = 5, sd = 3
shapiro.test(rnorm(100, mean = 5, sd = 3))
# 检查数据分布是否服从正态分布
shapiro.test(residuals(lmfit))
par(mfrow = c(2, 2))
plot(lmfit)
plot(re$Size, re$Price)
layout(c(1))
plot(re$Size, re$Price)
abline(lmfit)
test_size = tibble(
Size = c(2000, 4000, 8000)
)
test_size
predict(lmfit, test_size)
predict(lmfit)
lmfit$fitted.values
as.numeric(predict(lmfit))
as.numeric(lmfit$fitted.values)
all.equal(as.numeric(predict(lmfit)),
as.numeric(lmfit$fitted.values))
print(p)
p <- ggplot(real_estate, aes(Size, Price)) +
xlab('Size (SQ. ft)') +
ylab('Price (U.S. dollar)') +
geom_point(color='black') +
geom_smooth(method='lm', se=F) +
theme(text=element_text(size=20))
print(p)
p <- ggplot(re, aes(Size, Price)) +
xlab('Size (SQ. ft)') +
ylab('Price (U.S. dollar)') +
geom_point(color='black') +
geom_smooth(method='lm', se=F) +
theme(text=element_text(size=20))
print(p)
glimpse(re)
summary(lmfit)
# 多元线性回归
lm(Price ~ Size + Bedrooms, data = re)
# 多元线性回归
summary(lm(Price ~ Size + Bedrooms, data = re))
summary(lm(Price ~ Size:Bedrooms, data = re))
summary(lm(Price ~ Size + Bedrooms + Size:Bedrooms, data = re))
summary(lm(Price ~ Size*Bedrooms, data = re))
summary(lm(Price ~ I(Size^2), data = re))
summary(lm(Price ~ Size))
summary(lm(Price ~ Size, data = re))
# y = beta1 * x + (error)
summary(lm(Price ~ Size - 1, data = re))
# .
str(re)
summary(lm(Price ~ ., data = re))
vcov(lmfit)
# modeling
nrow(re)
library(ggplot2)
?geom_xspline()
install.packages("ggalt")
?geom_xspline()
library(ggalt)
?geom_xspline()
set.seed(1492)
dat <- data.frame(x=c(1:10, 1:10, 1:10),
y=c(sample(15:30, 10), 2*sample(15:30, 10),
3*sample(15:30, 10)),
group=factor(c(rep(1, 10), rep(2, 10), rep(3, 10)))
)
ggplot(dat, aes(x, y, group=group, color=group)) +
geom_point() +
geom_line()
ggplot(dat, aes(x, y, group=group, color=factor(group))) +
geom_point(color="black") +
geom_smooth(se=FALSE, linetype="dashed", size=0.5) +
geom_xspline(size=0.5)
ggplot(dat, aes(x, y, color=factor(group))) +
geom_xspline(size=0.5)
ggplot(dat, aes(x, y, color=factor(group))) +
geom_xspline()
ggplot(dat, aes(x, y, shape=factor(group))) +
geom_xspline()
ggplot(dat, aes(x, y, group=factor(group))) +
geom_xspline()
file.edit("r/05-feature.R")
stus <- data.frame(
id = c(1, 2, 3, 3, 4, 4),
gender = c("F", "M", "F", "F", "M", "F")
)
stu
stus
dim(stus)
length(unique(stus$id))
length(unique(stus))
unique(stus)
length(unique(stus))
dim(unique(stus))
# 02.特征去重
stus <- data.frame(
id = 1:3,
gender = c("F", "M", "M"),
grade = c(6, 6, 6)
height = c(1.7, 1.9, 1.6)
)
# 02.特征去重
stus <- data.frame(
id = 1:3,
gender = c("F", "M", "M"),
grade = c(6, 6, 6),
height = c(1.7, 1.9, 1.6)
)
stus
# 02.特征去重
stus <- data.frame(
id = 1:3,
gender = c("F", "M", "M"),
grade = c(6, 6, 6),
height = c(1.7, 1.9, 1.6)
)
stus
std(c(1, 1, 1, 1))
var(c(1, 1, 1, 1))
magrittr::`%>%`
sum(1:10)
1:10 %>% sum
sapply(stus,
function(x) length(unique(x)))
library(caret)
nearZeroVar(stus)
stus
nearZeroVar(stus, saveMetrics = TRUE)
?nearZeroVar
stus <- data.frame(
id = 1:3,
gender = c("F", "M", "M"),
grade = c(6.0002, 6.0001, 6.00012),
height = c(1.7, 1.9, 1.6)
)
nearZeroVar(stus)
nearZeroVar(stus, saveMetrics = TRUE)
var(stus$grade)
stus <- data.frame(
id = 1:3,
gender = c("F", "M", "M"),
grade = c(6+1e-30, 6+1e-32, 6+1e-35),
height = c(1.7, 1.9, 1.6)
)
nearZeroVar(stus, saveMetrics = TRUE)
nearZeroVar(stus)
summary(airquality[, 1:4])
sum(is.na(airquality$Ozone))
library(mice)
md.pattern(airquality)
install.packages("VIM")
aggr(airquality)
library(VIM)
aggr(airquality)
aggr(airquality)
aggr(airquality)
aggr(airquality)
library(VIM)
aggr(airquality)
aggr(airquality)
matrixplot(airquality)
na.omit()
na.omit(c(1, NA, 3))
# 删除缺失值仅适合用于完全随机缺失的数据
na.fail(c(1, NA, 3))
na.exclude(c(1, NA, 3))
na.pass(c(1, NA, 3))
sample(1:10, 5, replace = TRUE)
sample(1:10, 5, replace = FALSE)
# error
sample(1:4, 5, replace = FALSE)
airquality
dplyr::sample_n(airquality, 10)
# 分层采样
iris_ <- iris[c(1:60, 101:130),]
iris_
ct <- table(iris$Species)
ct
ct <- table(iris_$Species)
ct
n <- round(as.numeric(ct) * 0.8)
sampling::strata(
iris_,
stratanames = "Species",
size = n,
method = "srswor"
)
s_i <- sampling::strata(
iris_,
stratanames = "Species",
size = n,
method = "srswor"
)
table(s_i$Species)
# 无量纲化
normalize <- function(x) {
(x - min(x)) / (max(x) - min(x))
}
normalize(1:10)
sd
scale2 <- function(x) {
(x - mean(x)) / sd(x)
}
scale2(1:10)
sd(scale2(1:10))
?scale
scale(1:10)
# 离散化
price <- c(4, 8, 15, 21, 21, 24, 25, 28, 34)
cut(price, breaks = 3)
Hmisc::cut2(price, g = 3)
Hmisc::cut2(c(4, 8, 15, 21, 21, 24, 25, 28, 34, 30), g = 3)
length(price)
# 等深分箱
Hmisc::cut2(price, g = 3)
# 哑变量化
# 也叫热编码 one-hot encoding
factor(c("F", "M", "F"))
# 哑变量化
# 也叫热编码 one-hot encoding
str(factor(c("F", "M", "F")))
# PCA
library(graphics)
pca <- prcomp(USArrests, scale. = TRUE)
summary(pca)
USArrests
head(factor(c("F", "M", "F")))
head(USArrests)
install.packages("mlr3")