SendaiR#1-BeginneRSession

 SendaiR#1-BeginneRSession

Sendai.R#1の初心者セッションで喋ったスライドです。

8284465a94bbdf1ea82cf1a67d55f447?s=128

kilometer

May 18, 2019
Tweet

Transcript

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

  2. Who!?

  3. Who!? 名前: 三村 @kilometer 職業: ポスドク (こうがくはくし) 専⾨: ⾏動神経科学(霊⻑類) 脳イメージング

    医療システム⼯学 R歴: ~ 10年ぐらい 流⾏: 出張
  4. BeginneR Session

  5. 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
  6. BeginneR

  7. BeginneR Advanced Hoxo_m If I have seen further it is

    by standing on the shoulders of Giants. -- Sir Isaac Newton, 1676
  8. Before After BeginneR Session BeginneR BeginneR

  9. What is Data?

  10. Input Output Data Pipeline

  11. packages you

  12. Input Output packages Data Pipeline

  13. Output Input Input Data Pipeline

  14. Output Input Input Data Pipeline

  15. Output Input Input Data Pipeline

  16. Data Pipeline

  17. Data Pipeline readable coding

  18. Programing Write Run Read Think

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

  20. Programing Write Run Read Think

  21. Programing Write Run Read Think coding style

  22. 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
  23. 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
  24. Programing Write Run Read Think Write Run Read Think Share

  25. ブール演算⼦ Boolean Algebra A == B A != B A

    | B A & B A %in% B # equal to # not equal to # or # and # is A in B?
  26. George Boole 1815 - 1864 A Class-Room Introduction to Logic

    https://niyamaklogic.wordpress.co m/category/laws-of-thoughts/ Mathematician Philosopher &
  27. Programing

  28. Programing

  29. None
  30. None
  31. 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
  32. ① ② ③ ④ lift take pour put Bring milk

    from the kitchen!
  33. ① lift Bring milk from the kitchen! lift(Robot, glass, table)

    -> Robot' take ② take(Robot', fridge, milk) -> Robot''
  34. 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, # ① # ② # ③ # ④ # ① # ② # ③ # ④
  35. {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 %>%
  36. {magrittr} # こうやって書く事もできます。 dat <- dat0 %>% f1(var1-1, var1-2) %>%

    f2(var2) %>% f3(var3) %>% f4(var4-1, ., var4-2) %>% f5(var5) %>% f6(var6) */ 065 1JQFBMHFCSB %>%
  37. Text Figure Information Intention Data decode encode feedback Programing

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

  39. data.frame (復習)

  40. vector in Excel

  41. 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
  42. None
  43. None
  44. None
  45. None
  46. 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}
  47. dplyrは、あなたの考えをコードに翻訳 するための【動詞】を提供する。 データ操作における基本のキを、 シンプルに実⾏できる関数 (群) Introduction to dplyr https://cran.r-project.org/web/packages/dplyr/vignettes/dplyr.html WFSCT

    {dplyr} ※ かなり意訳
  48. mutate select filter arrange summaries # add column # select

    column # select row # arrange row # summary of vars {dplyr} WFSCT WFSCGVODUJPOT
  49. WFSCT {dplyr} mutate # カラムの追加 + mutate(dat, C = fun(A,

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

    fun(A, B))
  51. > 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)
  52. WFSCT {dplyr} select # カラムの選択 dat %>% select(tag, B)

  53. 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}
  54. 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
  55. 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
  56. WFSCT {dplyr} filter # ⾏の絞り込み dat %>% filter(tag %in% c(1,

    3, 5))
  57. 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 ...
  58. 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 ...
  59. mutate select filter arrange summaries # カラムの追加 # カラムの選択 #

    ⾏の絞り込み # ⾏の並び替え # 値の集約 {dplyr} WFSCT WFSCؔ਺܈
  60. (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
  61. 選択肢を制限することで、 データ解析のステップを シンプルに考えられますヨ。 (めっちゃ意訳) Introduction to dplyr https://cran.r-project.org/web/packages/dplyr/vignettes/dplyr.html ※ まさに意訳

    (SBNNBSPGEBUBNBOJQVMBUJPO
  62. より多くの制約を課す事で、 魂の⾜枷から、より⾃由になる。 Igor Stravinsky И ́ горь Ф Страви́нский The

    more constraints one imposes, the more one frees one's self of the chains that shackle the spirit. 1882 - 1971 ※ 割と意訳
  63. Ageanda 0. Introduction 1. Data manipulation 2. Visualization in

  64. Text Image Information Intention Data decode encode Public writing feedback

  65. Text Image First, A. Next, B. Then C. Finally D.

    time Intention encode "Frozen" structure A B C D time value α β
  66. How do we "read" figures?

  67. Human brain

  68. What Yamis 2014 PNAS Visual pathway of the brain Convolutional

    Feature map VC
  69. Saur 2008 PNAS Auditory pathway of the brain VC AC

    (repetition) (comprehension)
  70. AC VC Language Mishkin 1983 TINS, Saur 2008 PNAS Language

    pathway of the brain
  71. Real world Figure Language Entropy "Ramen" How do we "read"

    figures?
  72. Real world Figure Language Entropy "Ramen" Loss of Information How

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

    selection How do we "read" figures?
  74. Basic plotting functions in {graphics} ‧High-level: create a new plot

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

    ‧Low-level: add more information ‧Interactive: interactively add info.
  76. demo..

  77. ‧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
  78. ‧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}
  79. ‧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}
  80. ‧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}
  81. ‧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}
  82. ‧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}
  83. ‧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}
  84. 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}
  85. How to plot multiple data series Basic plotting functions in

    {graphics}
  86. 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}
  87. 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}
  88. How to custom graphic parameters Basic plotting functions in {graphics}

  89. # 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}
  90. "declarative" graphic package {ggplot2} Basic plotting functions in {graphics} ⇔

    ふつ〜に思ったことを書けば描けるというニュアンス (直訳すると"平叙⽂の"ぐらいの意味)
  91. "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 ふつ〜に思ったことを書けば描ける
  92. # 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}
  93. # 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
  94. # 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)
  95. Text Image Information Intention Data decode encode Public writing feedback

    ≠ ① ②
  96. demo..

  97. "declarative" graphic package {ggplot2} ggplot(iris, aes(x = Sepal.Length, y =

    Petal.Width, color = Species))+ geom_point()
  98. ggplot(iris, aes(x = Sepal.Length, y = Petal.Width, color = Species))+

    geom_point() factor class "declarative" graphic package {ggplot2}
  99. 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
  100. ggplot(iris, aes(x = Sepal.Length, y = Petal.Width, color = Petal.Length))+

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

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

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

    geom_smooth()+ geom_point() "declarative" graphic package {ggplot2}
  104. 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}
  105. 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}
  106. ggplot(iris, aes(x = Sepal.Length, y = Petal.Width, color = Species))+

    geom_smooth()+ geom_point()+ theme_classic()+ "declarative" graphic package {ggplot2}
  107. 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}
  108. # 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)
  109. # 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)
  110. # 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
  111. "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 ふつ〜に思ったことを書けば描ける
  112. Summary...

  113. Real world Figure Language Entropy "Ramen" Loss of Information Model

    selection How do we "read" figures?
  114. Text Image Information Intention Data decode encode Public writing feedback

  115. Social communication output input/feedback decode encode internal status A B

    estimated B status A status Emotion, Intention, ... = Language, Gesture, Prosody, Face expression...
  116. Before After BeginneR Session BeginneR BeginneR

  117. BeginneR Advanced Hoxo_m If I have seen further it is

    by standing on the sholders of Giants. -- Sir Isaac Newton, 1676
  118. Enjoy!! KMT©