Slide 1

Slide 1 text

Sendai.R #1 @kilometer BeginneR Session 2019.05.18 at DATACOM CORP.

Slide 2

Slide 2 text

Who!?

Slide 3

Slide 3 text

Who!? 名前: 三村 @kilometer 職業: ポスドク (こうがくはくし) 専⾨: ⾏動神経科学(霊⻑類) 脳イメージング 医療システム⼯学 R歴: ~ 10年ぐらい 流⾏: 出張

Slide 4

Slide 4 text

BeginneR Session

Slide 5

Slide 5 text

2018.04.21 Tokyo.R #69 BeginneR Session – Data import / Export 2018.06.09 Tokyo.R #70 BeginneR Session – Bayesian Modeling 2018.07.15 Tokyo.R #71 Landscape with R – the Japanese R community 2018.10.20 Tokyo.R #73 BeginneR Session – Visualization & Plot 2019.01.19 Tokyo.R #75 BeginneR Session – Data pipeline 2019.03.02 Tokyo.R #76 BeginneR Session – Data pipeline 2019.04.13 Tokyo.R #77 BeginneR Session – Data analysis

Slide 6

Slide 6 text

BeginneR

Slide 7

Slide 7 text

BeginneR Advanced Hoxo_m If I have seen further it is by standing on the shoulders of Giants. -- Sir Isaac Newton, 1676

Slide 8

Slide 8 text

Before After BeginneR Session BeginneR BeginneR

Slide 9

Slide 9 text

What is Data?

Slide 10

Slide 10 text

Input Output Data Pipeline

Slide 11

Slide 11 text

packages you

Slide 12

Slide 12 text

Input Output packages Data Pipeline

Slide 13

Slide 13 text

Output Input Input Data Pipeline

Slide 14

Slide 14 text

Output Input Input Data Pipeline

Slide 15

Slide 15 text

Output Input Input Data Pipeline

Slide 16

Slide 16 text

Data Pipeline

Slide 17

Slide 17 text

Data Pipeline readable coding

Slide 18

Slide 18 text

Programing Write Run Read Think

Slide 19

Slide 19 text

Run!!! https://www.amazon.co.jp/dp/B00Y0UI990/

Slide 20

Slide 20 text

Programing Write Run Read Think

Slide 21

Slide 21 text

Programing Write Run Read Think coding style

Slide 22

Slide 22 text

The tidyverse style guide https://style.tidyverse.org/ "Good coding style is like correct punctuation: you can manage without it, butitsuremakesthingseasiertoread." Google's R Style Guide https://style.tidyverse.org/ "The goal of the R Programming Style Guide is to make our R code easier to read, share, and verify." R coding style guides

Slide 23

Slide 23 text

The tidyverse style guide https://style.tidyverse.org/ "Good coding style is like correct punctuation: you can manage without it, butitsuremakesthingseasiertoread." Google's R Style Guide https://style.tidyverse.org/ "The goal of the R Programming Style Guide is to make our R code easier to read, share, and verify." R coding style guides

Slide 24

Slide 24 text

Programing Write Run Read Think Write Run Read Think Share

Slide 25

Slide 25 text

ブール演算⼦ Boolean Algebra A == B A != B A | B A & B A %in% B # equal to # not equal to # or # and # is A in B?

Slide 26

Slide 26 text

George Boole 1815 - 1864 A Class-Room Introduction to Logic https://niyamaklogic.wordpress.co m/category/laws-of-thoughts/ Mathematician Philosopher &

Slide 27

Slide 27 text

Programing

Slide 28

Slide 28 text

Programing

Slide 29

Slide 29 text

No content

Slide 30

Slide 30 text

No content

Slide 31

Slide 31 text

1JQFBMHFCSB X %>% f X %>% f(y) X %>% f %>% g X %>% f(y, .) f(X) f(X, y) g(f(X)) f(y, X) %>% {magrittr} 「dplyr再⼊⾨(基本編)」yutanihilation https://speakerdeck.com/yutannihilation/dplyrzai-ru-men-ji-ben-bian

Slide 32

Slide 32 text

① ② ③ ④ lift take pour put Bring milk from the kitchen!

Slide 33

Slide 33 text

① lift Bring milk from the kitchen! lift(Robot, glass, table) -> Robot' take ② take(Robot', fridge, milk) -> Robot''

Slide 34

Slide 34 text

Bring milk from the kitchen! Robot' <- lift(Robot, glass, table) Robot'' <- take(Robot', fridge, milk) Robot''' <- pour(Robot'', milk, glass) result <- put(Robot''', glass, table) result <- Robot %>% lift(glass, table) %>% take(fridge, milk) %>% pour(milk, glass) %>% put(glass, table) by using pipe, # ① # ② # ③ # ④ # ① # ② # ③ # ④

Slide 35

Slide 35 text

{magrittr} # こう書きますか? 536&*/ dat1 <- f1(dat0, var1-1, var1-2) dat2 <- f2(dat1, var2) dat3 <- f3(dat2, var3) dat4 <- f4(var4-1, dat3, var4-2) dat5 <- f5(dat4, var5) dat6 <- f6(dat5, var6) 536&065 5IJOLJOH 3FBEJOH 1JQFBMHFCSB %>%

Slide 36

Slide 36 text

{magrittr} # こうやって書く事もできます。 dat <- dat0 %>% f1(var1-1, var1-2) %>% f2(var2) %>% f3(var3) %>% f4(var4-1, ., var4-2) %>% f5(var5) %>% f6(var6) */ 065 1JQFBMHFCSB %>%

Slide 37

Slide 37 text

Text Figure Information Intention Data decode encode feedback Programing

Slide 38

Slide 38 text

Ageanda 0. Introduction 1. Data manipulation 2. Visualization in

Slide 39

Slide 39 text

data.frame (復習)

Slide 40

Slide 40 text

vector in Excel

Slide 41

Slide 41 text

vector in R in Excel pre <- c(1, 2, 3, 4, 5) post <- pre * 5 > pre [1] 1 2 3 4 5 > post [1] 5 10 15 20 25

Slide 42

Slide 42 text

No content

Slide 43

Slide 43 text

No content

Slide 44

Slide 44 text

No content

Slide 45

Slide 45 text

No content

Slide 46

Slide 46 text

It (dplyr) provides simple “verbs” to help you translate your thoughts into code. functions that correspond to the most common data manipulation tasks Introduction to dplyr https://cran.r-project.org/web/packages/dplyr/vignettes/dplyr.html WFSCT {dplyr}

Slide 47

Slide 47 text

dplyrは、あなたの考えをコードに翻訳 するための【動詞】を提供する。 データ操作における基本のキを、 シンプルに実⾏できる関数 (群) Introduction to dplyr https://cran.r-project.org/web/packages/dplyr/vignettes/dplyr.html WFSCT {dplyr} ※ かなり意訳

Slide 48

Slide 48 text

mutate select filter arrange summaries # add column # select column # select row # arrange row # summary of vars {dplyr} WFSCT WFSCGVODUJPOT

Slide 49

Slide 49 text

WFSCT {dplyr} mutate # カラムの追加 + mutate(dat, C = fun(A, B))

Slide 50

Slide 50 text

WFSCT {dplyr} mutate # カラムの追加 + dat %>% mutate(C = fun(A, B))

Slide 51

Slide 51 text

> df1 A B 1 1 11 2 2 12 3 3 13 df1 <- data.frame(A = 1:3, B = 11:13) WFSCT {dplyr} mutate # カラムの追加 > df2 A B C 1 1 11 11 2 2 12 24 3 3 13 39 df2 <- df1 %>% mutate(C = A * B)

Slide 52

Slide 52 text

WFSCT {dplyr} select # カラムの選択 dat %>% select(tag, B)

Slide 53

Slide 53 text

library(dplyr) iris %>% select(Sepal.Length, Sepal.Width) 'data.frame': 150 obs. of 6 variables: $ Sepal.Length: num 5.1 4.9 4.7 4.6 5 5.4 ... $ Sepal.Width : num 3.5 3 3.2 3.1 3.6 3.9 ... WFSCT {dplyr}

Slide 54

Slide 54 text

library(dplyr) iris %>% select(contains(“Width”)) 'data.frame': 150 obs. of 6 variables: $ Sepal.Width : num 3.5 3 3.2 3.1 3.6 3.9 ... $ Petal.Width : num 0.2 0.2 0.2 0.2 0.2 0.4 ... WFSCT {dplyr} Select help functions

Slide 55

Slide 55 text

WFSCT {dplyr} # Select help functions starts_with("s") ends_with("s") contains("se") matches("^.e") one_of(c("Sepal.Length", "Species")) everything() https://kazutan.github.io/blog/2017/04/dplyr-select-memo/ 「dplyr::selectの活⽤例メモ」kazutan

Slide 56

Slide 56 text

WFSCT {dplyr} filter # ⾏の絞り込み dat %>% filter(tag %in% c(1, 3, 5))

Slide 57

Slide 57 text

library(dplyr) iris %>% filter(Species == "versicolor") WFSCT {dplyr} 'data.frame': 50 obs. of 5 variables: $ Sepal.Length: num 7 6.4 6.9 5.5 6.5 5.7 6.3 ... $ Sepal.Width : num 3.2 3.2 3.1 2.3 2.8 2.8 ... $ Petal.Length: num 4.7 4.5 4.9 4 4.6 4.5 4.7 ... $ Petal.Width : num 1.4 1.5 1.5 1.3 1.5 1.3 ... $ Species : Factor w/ 3 levels "setosa","versicolor",..: 2 2 2 2 2 2 2 2 2 2 ...

Slide 58

Slide 58 text

library(dplyr) iris %>% filter(Species == "versicolor") WFSCT {dplyr} NSE (Non-Standard Evaluation) 'data.frame': 50 obs. of 5 variables: $ Sepal.Length: num 7 6.4 6.9 5.5 6.5 5.7 6.3 ... $ Sepal.Width : num 3.2 3.2 3.1 2.3 2.8 2.8 ... $ Petal.Length: num 4.7 4.5 4.9 4 4.6 4.5 4.7 ... $ Petal.Width : num 1.4 1.5 1.5 1.3 1.5 1.3 ... $ Species : Factor w/ 3 levels "setosa","versicolor",..: 2 2 2 2 2 2 2 2 2 2 ...

Slide 59

Slide 59 text

mutate select filter arrange summaries # カラムの追加 # カラムの選択 # ⾏の絞り込み # ⾏の並び替え # 値の集約 {dplyr} WFSCT WFSCؔ਺܈

Slide 60

Slide 60 text

(SBNNBSPGEBUBNBOJQVMBUJPO By constraining your options, it helps you think about your data manipulation challenges. Introduction to dplyr https://cran.r-project.org/web/packages/dplyr/vignettes/dplyr.html

Slide 61

Slide 61 text

選択肢を制限することで、 データ解析のステップを シンプルに考えられますヨ。 (めっちゃ意訳) Introduction to dplyr https://cran.r-project.org/web/packages/dplyr/vignettes/dplyr.html ※ まさに意訳 (SBNNBSPGEBUBNBOJQVMBUJPO

Slide 62

Slide 62 text

より多くの制約を課す事で、 魂の⾜枷から、より⾃由になる。 Igor Stravinsky И ́ горь Ф Страви́нский The more constraints one imposes, the more one frees one's self of the chains that shackle the spirit. 1882 - 1971 ※ 割と意訳

Slide 63

Slide 63 text

Ageanda 0. Introduction 1. Data manipulation 2. Visualization in

Slide 64

Slide 64 text

Text Image Information Intention Data decode encode Public writing feedback ≠

Slide 65

Slide 65 text

Text Image First, A. Next, B. Then C. Finally D. time Intention encode "Frozen" structure A B C D time value α β

Slide 66

Slide 66 text

How do we "read" figures?

Slide 67

Slide 67 text

Human brain

Slide 68

Slide 68 text

What Yamis 2014 PNAS Visual pathway of the brain Convolutional Feature map VC

Slide 69

Slide 69 text

Saur 2008 PNAS Auditory pathway of the brain VC AC (repetition) (comprehension)

Slide 70

Slide 70 text

AC VC Language Mishkin 1983 TINS, Saur 2008 PNAS Language pathway of the brain

Slide 71

Slide 71 text

Real world Figure Language Entropy "Ramen" How do we "read" figures?

Slide 72

Slide 72 text

Real world Figure Language Entropy "Ramen" Loss of Information How do we "read" figures?

Slide 73

Slide 73 text

Real world Figure Language Entropy "Ramen" Loss of Information Model selection How do we "read" figures?

Slide 74

Slide 74 text

Basic plotting functions in {graphics} ‧High-level: create a new plot ‧Low-level: add more information ‧Interactive: interactively add info.

Slide 75

Slide 75 text

Basic plotting functions in {graphics} ‧High-level: create a new plot ‧Low-level: add more information ‧Interactive: interactively add info.

Slide 76

Slide 76 text

demo..

Slide 77

Slide 77 text

‧High-level: create a new plot Basic plotting functions in {graphics} x <- c(1:10) y <- 0.5 * x + 3 plot(x, y) Scatterplot of two vectors

Slide 78

Slide 78 text

‧High-level: create a new plot x <- c(1:10) y <- 0.5 * x + 3 plot(x, y, type = "b") Scatterplot of two vectors Basic plotting functions in {graphics}

Slide 79

Slide 79 text

‧High-level: create a new plot x <- c(1:10) y <- 0.5 * x + 3 plot(x, y, type = "o") Scatterplot of two vectors Basic plotting functions in {graphics}

Slide 80

Slide 80 text

‧High-level: create a new plot x <- c(1:10) y <- 0.5 * x + 3 plot(x, y, type = "o", col = "red") Scatterplot of two vectors Basic plotting functions in {graphics}

Slide 81

Slide 81 text

‧High-level: create a new plot x <- c(1:10) y <- 0.5 * x + 3 plot(x, y, type = "o", col = "red", pch = 2) Scatterplot of two vectors Basic plotting functions in {graphics}

Slide 82

Slide 82 text

‧High-level: create a new plot Scatterplot of two vectors R-Tips, http://cse.naro.affrc.go.jp/takezawa/r-tips/r/53.html Basic plotting functions in {graphics}

Slide 83

Slide 83 text

‧High-level: create a new plot x <- c(1:10) y <- 0.5 * x + 3 plot(x, y, type = "o", col = "red", pch = 2, lwd = 5) Scatterplot of two vectors Basic plotting functions in {graphics}

Slide 84

Slide 84 text

How to plot multiple data series + x <- 1:10 y <- 0.5 * x + 3 x <- 1:10 y2 <- 0.25 * x + 0.1 * x^2 + 1 Basic plotting functions in {graphics}

Slide 85

Slide 85 text

How to plot multiple data series Basic plotting functions in {graphics}

Slide 86

Slide 86 text

How to plot multiple data series x <- c(1:10) y1 <- 0.5 * x + 3 y2 <- 0.25 * x + 0.1 * x^2 + 1 # create & set a new plot: high level plot(0, 0, type = "n", xlim = c(min(x), max(x)), ylim = c(min(y1, y2), max(y1, y2)), xlab = "x", ylab = "y") # add elements: low level funcs. lines(x, y1, col = "red") lines(x, y2, col = "blue") points(x, y1, col = "red") points(x, y2, col = "blue", pch = 2) Basic plotting functions in {graphics}

Slide 87

Slide 87 text

How to plot multiple data series x <- c(1:10) y1 <- 0.5 * x + 3 y2 <- 0.25 * x + 0.1 * x^2 + 1 # create & set a new plot: high level plot(0, 0, type = "n", xlim = c(min(x), max(x)), ylim = c(min(y1, y2), max(y1, y2)), xlab = "x", ylab = "y") # add elements: low level funcs lines(x, y1, col = "red") lines(x, y2, col = "blue") points(x, y1, col = "red") points(x, y2, col = "blue", pch = 2) Basic plotting functions in {graphics}

Slide 88

Slide 88 text

How to custom graphic parameters Basic plotting functions in {graphics}

Slide 89

Slide 89 text

# set graphic params: par par(mar = c(3, 3, 0.5, 0.5), tcl = -0.2, mgp = c(1.5, 0.3, 0), bty = "l") # create & set a new plot: high level plot(0, 0, type = "n", xlab = "x", ylab = "y", xlim = c(min(x), max(x)), ylim = c(min(y1, y2), max(y1, y2))) # add elements: low level funcs. lines(x, y1, col = "red") lines(x, y2, col = "blue") points(x, y1, col = "red") points(x, y2, col = "blue", pch = 2) Basic plotting functions in {graphics}

Slide 90

Slide 90 text

"declarative" graphic package {ggplot2} Basic plotting functions in {graphics} ⇔ ふつ〜に思ったことを書けば描けるというニュアンス (直訳すると"平叙⽂の"ぐらいの意味)

Slide 91

Slide 91 text

"declarative" graphic package {ggplot2} Basic plotting functions in {graphics} ⇔ ggplot2 is a system for declaratively creating graphics, based on The Grammar of Graphics. You provide the data, tell ggplot2 how to map variables to aesthetics, what graphical primitives to use, and it takes care of the details. https://ggplot2.tidyverse.org/index.html ふつ〜に思ったことを書けば描ける

Slide 92

Slide 92 text

# set graphic params par(...) # set a new plot plot(0, 0, type = "n", ...) # add elements lines(x, y1,..) lines(x, y2, ...) points(x, y1, ...) points(x, y2, ...) ggplot(data, aes(x, y, ...))+ geom_path()+ geom_point()+ scale_... theme_... in {graphics} in {ggplot2}

Slide 93

Slide 93 text

# set graphic params par(...) # set a new plot plot(0, 0, type = "n", ...) # add elements lines(x, y1,..) lines(x, y2, ...) points(x, y1, ...) points(x, y2, ...) ggplot(data, aes(x, y, ...))+ geom_path()+ geom_point()+ scale_...()+ theme_...() in {graphics} in {ggplot2} ONLY data.frame

Slide 94

Slide 94 text

# set graphic params par(...) # set a new plot plot(0, 0, type = "n", ...) # add elements lines(x, y1,..) lines(x, y2, ...) points(x, y1, ...) points(x, y2, ...) ggplot(data, aes(x, y, ...))+ geom_path()+ geom_points()+ scale_...()+ theme_...() in {graphics} in {ggplot2} ONLY data.frame ① data ② encode ③ style (optional)

Slide 95

Slide 95 text

Text Image Information Intention Data decode encode Public writing feedback ≠ ① ②

Slide 96

Slide 96 text

demo..

Slide 97

Slide 97 text

"declarative" graphic package {ggplot2} ggplot(iris, aes(x = Sepal.Length, y = Petal.Width, color = Species))+ geom_point()

Slide 98

Slide 98 text

ggplot(iris, aes(x = Sepal.Length, y = Petal.Width, color = Species))+ geom_point() factor class "declarative" graphic package {ggplot2}

Slide 99

Slide 99 text

Sample data iris %>% str row column 'data.frame': 150 obs. of 5 variables: $ Sepal.Length: num 5.1 4.9 4.7 4.6 5 ... $ Sepal.Width : num 3.5 3 3.2 3.1 3.6 ... $ Petal.Length: num 1.4 1.4 1.3 1.5 1.4 ... $ Petal.Width : num 0.2 0.2 0.2 0.2 0.2 ... $ Species : Factor w/ 3 levels "setosa", ... class names mode value

Slide 100

Slide 100 text

ggplot(iris, aes(x = Sepal.Length, y = Petal.Width, color = Petal.Length))+ geom_point() continuous volume "declarative" graphic package {ggplot2}

Slide 101

Slide 101 text

ggplot(iris, aes(x = Sepal.Length, y = Petal.Width, color = Species))+ geom_line() "declarative" graphic package {ggplot2}

Slide 102

Slide 102 text

ggplot(iris, aes(x = Sepal.Length, y = Petal.Width, color = Species))+ geom_smooth() "declarative" graphic package {ggplot2}

Slide 103

Slide 103 text

ggplot(iris, aes(x = Sepal.Length, y = Petal.Width, color = Species))+ geom_smooth()+ geom_point() "declarative" graphic package {ggplot2}

Slide 104

Slide 104 text

ggplot(iris, aes(x = Sepal.Length, y = Petal.Width, color = Species))+ geom_smooth()+ geom_point(aes(shape=Species), size = 4, alpha = 0.5) "declarative" graphic package {ggplot2}

Slide 105

Slide 105 text

ggplot(iris, aes(x = Sepal.Length, y = Petal.Width, color = Species))+ geom_smooth()+ geom_point()+ scale_color_manual(values = c("Blue", "Red", "Black")) "declarative" graphic package {ggplot2}

Slide 106

Slide 106 text

ggplot(iris, aes(x = Sepal.Length, y = Petal.Width, color = Species))+ geom_smooth()+ geom_point()+ theme_classic()+ "declarative" graphic package {ggplot2}

Slide 107

Slide 107 text

ggplot(iris, aes(x = Sepal.Length, y = Petal.Width, color = Species))+ geom_smooth()+ geom_points()+ theme_classic()+ theme(legend.position = "bottom") "declarative" graphic package {ggplot2}

Slide 108

Slide 108 text

# set graphic params par(...) # set a new plot plot(0, 0, type = "n", ...) # add elements lines(x, y1,..) lines(x, y2, ...) points(x, y1, ...) points(x, y2, ...) ggplot(data, aes(x, y, ...))+ geom_path()+ geom_points()+ scale_...()+ theme_...() in {graphics} in {ggplot2} ONLY data.frame ① data ② encode ③ style (optional)

Slide 109

Slide 109 text

# set graphic params par(...) # set a new plot plot(0, 0, type = "n", ...) # add elements lines(x, y1,..) lines(x, y2, ...) points(x, y1, ...) points(x, y2, ...) ggplot(data, aes(x, y, ...))+ geom_path()+ geom_points()+ scale_...()+ theme_...() in {graphics} in {ggplot2} # open graphic device png("img.png", ...) # colse graphic device dev.off() ggsave("img.png", width = 5, height = 4)

Slide 110

Slide 110 text

# set graphic params par(...) # set a new plot plot(0, 0, type = "n", ...) # add elements lines(x, y1,..) lines(x, y2, ...) points(x, y1, ...) points(x, y2, ...) ggplot(data, aes(x, y, ...))+ geom_path()+ geom_points()+ scale_...()+ theme_...() in {graphics} in {ggplot2} # open graphic device png("img.png", ...) # colse graphic device dev.off() ggsave("img.png", width = 5, height = 4) 1 2 3 4 5 1 2 3 4 5

Slide 111

Slide 111 text

"declarative" graphic package {ggplot2} Basic plotting functions in {graphics} ⇔ ggplot2 is a system for declaratively creating graphics, based on The Grammar of Graphics. You provide the data, tell ggplot2 how to map variables to aesthetics, what graphical primitives to use, and it takes care of the details. https://ggplot2.tidyverse.org/index.html ふつ〜に思ったことを書けば描ける

Slide 112

Slide 112 text

Summary...

Slide 113

Slide 113 text

Real world Figure Language Entropy "Ramen" Loss of Information Model selection How do we "read" figures?

Slide 114

Slide 114 text

Text Image Information Intention Data decode encode Public writing feedback ≠

Slide 115

Slide 115 text

Social communication output input/feedback decode encode internal status A B estimated B status A status Emotion, Intention, ... = Language, Gesture, Prosody, Face expression...

Slide 116

Slide 116 text

Before After BeginneR Session BeginneR BeginneR

Slide 117

Slide 117 text

BeginneR Advanced Hoxo_m If I have seen further it is by standing on the sholders of Giants. -- Sir Isaac Newton, 1676

Slide 118

Slide 118 text

Enjoy!! KMT©