forked from XSLiuLab/Workshop
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathshowcase.R
205 lines (161 loc) · 4.67 KB
/
showcase.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
grep("at", x = c("cat", "at", "df"))
grepl("at", x = c("cat", "at", "df"))
grep("^at", x = c("cat", "at", "df"))
grep("at$", x = c("cat", "at", "df", "caty"))
grep("at.", x = c("cat", "at", "df", "caty"))
grep("at.*", x = c("cat", "at", "df", "caty"))
grep("at.+", x = c("cat", "at", "df", "caty"))
# ? 0 or 1
# * >= 0
# + >= 1
grep("(at){2}", x = c("at", "atat", "atatat"))
# () used to group
# {}
grep("(at){1,2}", x = c("at", "atat", "atatat"))
## how to exclude three times?
grep("(at){1,2}", x = c("at", "atat", "atatat"))
grep("^at", x = c("cat", "at", "df"), value = TRUE)
sub(pattern = "ab", "", "abababc")
gsub(pattern = "ab", "", "abababc")
sub(pattern = "abab", "", "abababc")
sub(pattern = "abab(.+)", "\\1", x = c("abababc", "abababd"))
emals <- c("a@xx.com", "b@yy.com", "c@shanghaitech", "zzzz@yahoo.com")
# \\.
sub("(.+)@.+", "\\1", emals)
sub(".com", "", emals, fixed = TRUE) %>%
sub("^([^@]+)@(.+)$", "\\1-\\2", .)
#sub("(.+)@([^(\\.com)]+)(\\.com)?", "\\1-\\2", emals, fixed = TRUE)
lm(mpg ~ wt + cyl, data = mtcars)
## y = a0 + a1*x1 + a2*x2
lm(mpg ~ wt + cyl - 1, data = mtcars)
lm(mpg ~ wt:cyl, data = mtcars)
lm(mpg ~ wt*cyl, data = mtcars)
# wt*cyl => wt + cyl + wt:cyl
fit <- lm(mpg ~ wt + cyl, data = mtcars)
summary(fit)
coefficients(fit)
predict(fit, newdata = head(mtcars))
wt
a = mpg ~ wt + cyl
b = "mpg ~ wt + cyl"
typeof(a)
class(a)
as.list(a)
as.formula("mpg ~ wt + cyl")
x = c("mpg", "wt", "cyl")
paste0(x[1], "~", paste0(x[2:3], collapse = "+"))
as.formula(paste0(x[1], "~", paste0(x[2:3], collapse = "+")))
library(ggplot2)
?mtcars
# mpg Miles/(US) gallon
# Weight (1000 lbs)
# Number of forward gears
p <- ggplot(mtcars, aes(wt, mpg)) + geom_point()
coef(lm(mpg ~ wt, data = mtcars))
p + geom_abline(intercept = 37, slope = -5)
p + facet_grid(rows = vars(gear), scales = "free") + geom_smooth(method = "lm", se = FALSE)
# Construct mpg ~ wt for each gear ---------------------------------------
library(dplyr)
library(tidyr)
library(purrr)
mtcars %>%
group_by(gear)
mtcars %>%
group_by(gear) %>%
nest()
mtcars %>%
group_by(gear) %>%
nest() -> data
class(data$data)
sapply(data$data, class)
#lapply
#map
mtcars %>%
group_by(gear) %>%
nest() %>%
mutate(model = map(data, function(x) {
lm(mpg ~ wt, data = x)
}))
model_df <- mtcars %>%
group_by(gear) %>%
nest() %>%
mutate(model = map(data, function(x) {
lm(mpg ~ wt, data = x)
}))
model_df$data[[1]]
model_df$model[[1]]
model_df2 <- model_df %>%
mutate(gg = map2(data, model, function(x, y) {
# x represents data subset
# y represents a model based on the data subset
coefs = coef(y)
ggplot(x, aes(wt, mpg)) + geom_point() +
geom_abline(intercept = coefs[1], slope = coefs[2])
}))
model_df2
model_df2$gg[[1]]
# Comparison
ggplot(subset(mtcars, gear == 4), aes(wt, mpg)) + geom_point() +
geom_smooth(method = "lm", se = FALSE)
## Plot a list of ggplot2 objects
cowplot::plot_grid(plotlist = model_df2$gg)
glist = model_df2$gg
glist = lapply(glist, function(x) {
x + cowplot::theme_cowplot()
})
glist[[1]]
## This can also be implemented by for loop
gglist = list()
for (i in unique(mtcars$gear)) {
df_subset <- subset(mtcars, gear == i)
coefs = coef(lm(mpg ~ wt, data = df_subset))
gglist[[as.character(i)]] <- ggplot(df_subset, aes(wt, mpg)) + geom_point() +
geom_abline(intercept = coefs[1], slope = coefs[2])
}
gglist$`3`
cowplot::plot_grid(plotlist = gglist)
data.frame(
g = c(rep("a", 2), rep("b", 2)),
v = c(1, 1, 1, 2)
) %>%
group_by(g) %>%
summarise(v_unique = list(unique(v)))
data.frame(
g = c(rep("a", 2), rep("b", 2)),
v = c(1, 1, 1, 2)
) %>%
group_by(g) %>%
summarise(v_unique = list(unique(v))) %>%
unnest("v_unique")
## What if mpg, wt and gear are cheanged?
## Could you implement a function to do this?
colnames(mtcars)
plotGroupLM <- function(g, p, r) {
gglist = list()
for (i in unique(mtcars[[g]])) {
df_subset <- subset(mtcars, mtcars[[g]] == i)
coefs = coef(lm(as.formula(
paste0(r, "~", p)
), data = df_subset))
print(coefs)
gglist[[as.character(i)]] <- ggplot(df_subset, aes_string(p, r)) +
geom_point() +
geom_abline(intercept = coefs[1], slope = coefs[2])
}
cowplot::plot_grid(plotlist = gglist)
}
plotGroupLM("vs", "wt", "mpg")
mtcars %>%
group_by(gear) %>%
nest() %>%
mutate(model = map(data, function(x) {
lm(mpg ~ wt, data = x)
})) %>% mutate(gg = map2(data, model, function(x, y) {
# x represents data subset
# y represents a model based on the data subset
coefs = coef(y)
ggplot(x, aes(wt, mpg)) + geom_point() +
geom_abline(intercept = coefs[1], slope = coefs[2])
})) %>%
pull(gg) %>%
cowplot::plot_grid(plotlist = .)