-
Notifications
You must be signed in to change notification settings - Fork 0
/
example_longitudinal_eda.R
62 lines (51 loc) · 1.87 KB
/
example_longitudinal_eda.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
library(tidyverse)
library(lme4)
# a first, simple, crude plot
ggplot(sleepstudy, aes(x = Days, y = Reaction)) +
geom_point()
# a plot with lines
ggplot(sleepstudy, aes(x = Days, y = Reaction, group = Subject)) +
geom_line()
# adding geom_smooth to show models
ggplot(sleepstudy, aes(x = Days, y = Reaction, group = Subject)) +
geom_smooth(method = 'lm', alpha = 0.25, se = FALSE) +
geom_point(size = .5) +
facet_wrap(~Subject) +
scale_x_continuous(breaks = c(0, 2, 4, 6, 8)) +
theme_bw()
# let's create some example data where we might
# have multiple observations for each year
df <- tibble::tribble(
~id, ~ldl_2005, ~hdl_2005, ~ldl_2010, ~hdl_2010,
1, 150, 155, 175, 135,
2, 125, 135, 180, 146)
# we can pivot longer into a tidy format where
# each observation is in one row
df <- df %>% tidyr::pivot_longer(
cols = 2:5,
names_to = "variable",
values_to = "value")
# but we'd like to split the "hdl_" or "ldl_" prefixes
# away from the year part in the column called "variable"
df <- df %>% tidyr::separate(
col = 'variable',
into = c('type', 'year'))
# now we can pivot the hdl and ldl observations into
# their own columns
df %>% tidyr::pivot_wider(id_cols = c('id', 'year'),
values_from = 'value',
names_from = 'type')
# attempting to do this shorter -------------------------------------------
df <- tibble::tribble(
~id, ~ldl_2005, ~hdl_2005, ~ldl_2010, ~hdl_2010,
1, 150, 155, 175, 135,
2, 125, 135, 180, 146)
# we could have pivoted longer _and_ done the
# column separate step in one go using some obscure
# parameters from tidyr::pivot_longer, but
# we still would have to pivot_wider again at the end.
df %>% tidyr::pivot_longer(
cols = 2:5,
names_to = c('type', 'year'),
names_sep = '_',
values_to = 'value')