-
Notifications
You must be signed in to change notification settings - Fork 2
/
Exercise_12.R
84 lines (62 loc) · 2.51 KB
/
Exercise_12.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
# Exercise 12:
library(tidyverse)
library(NHSRdatasets)
library(glue)
library(broom)
# compose can make a new function by chaining two or more functions together.
# in exercise 1 we created a function that did 3*x + 1, this could be expressed
# as two individual functions, one that does 3*x, then one that does x+1
# note that compose is like mathematics: the functions are evaluated "backwards"
compose(~.x + 1,
~3 * .x)(3)
# the result is different from
compose(~3 * .x,
~.x + 1)(3)
# but you can "reverse" the direction like so:
compose(~3 * .x,
~.x + 1,
.dir = "forward")(3)
# (default .dir is "backward")
# composostion can be used to make useful functions, like a "not in"
`%nin%` <- compose(`!`, `%in%`)
1 %in% 1:3
1 %nin% 1:3
# it's very common to need to chain multiple functions together.
# below, let's fit a linear regression model to the ae_attendances dataset, but
# first let's group the dataframe by month and sum the numeric columns
df <- ae_attendances %>%
group_by(period) %>%
summarise_if(is.numeric, sum)
# let's try to predict the attendances column based on the other columns in the
# dateframe, so let's select all but the attendances column, get the column
# names, and then let's set the names for each item in the vector (to be the
# same as the vector item, this will become clearer later...)
df.cols <- df %>%
select(-attendances) %>%
colnames() %>%
set_names()
df.cols
# now, lets take these columns, create a string that can then be converted to a
# formula, call the lm function against these formula's (setting the data arg to
# be our dataframe), then finally use broom::tidy function to neaten the results
# (note, that by setting the .id argument with map_dfr, it will take the list
# items name (which we set above with set_names) and assign it to a column)
df.cols %>%
map(~glue::glue("attendances ~ {.x}")) %>%
map(as.formula) %>%
map(lm, data = df) %>%
map_dfr(broom::tidy, .id = "column")
# now, using multiple maps is messy. we could rewrite this using partial
# try this below
# (hint: we cannot pass data = df in the ... argument of map, as this will only
# be passed to the first function in the compose chain. we will instead need to
# use partial here: partial(lm, data = df), but .id is an argument for map_dfr
# so we don't need to use partial for this function)
my_lm <- YOUR_CODE_HERE
my_lm <- compose(
broom::tidy,
partial(lm, data = ae_attendances),
as.formula,
~glue::glue("attendances ~ {.x}")
)
map_dfr(df.cols, my_lm, .id = "column")