R入門5

We flip the coin 30 times and observe 10 head. We can use R to tell us the probability of getting 10 or fewer heads using the pbinom function. pbinom(10, size=30, prob=0.5) 0.0493685733526945 The way we determined the probability of getting exactly 10 heads is by using the probability formula for Bernoulli trials. The probability of getting k successes in n trials is equal to: $$\binom{n}{k}p^k(1-p)^{n-k}$$ rnorm is the R function that simulates random variates having a specified normal distribution.

R入門4

women is a built-in data set in R, which holds the height and weight of 15 American women from ages 30 to 39. print(head(women)) height weight 1 58 115 2 59 117 3 60 120 4 61 123 5 62 126 6 63 129 str(women) 'data.frame': 15 obs. of 2 variables: $ height: num 58 59 60 61 62 63 64 65 66 67 ... $ weight: num 115 117 120 123 126 129 132 135 139 142 .

R入門3

ggplot2()の基礎 install.packages("tidyverse") install.packages("dplyr") Updating HTML index of packages in '.Library' Making 'packages.html' ... done Updating HTML index of packages in '.Library' Making 'packages.html' ... done ggplot2 is one of the core members of the tidyverse package. library(tidyverse) ── Attaching packages ─────────────────────────────────────── tidyverse 1.2.1 ── ✔ ggplot2 3.0.0 ✔ purrr 0.2.5 ✔ tibble 1.4.2 ✔ dplyr 0.7.7 ✔ tidyr 0.8.1 ✔ stringr 1.3.1 ✔ readr 1.1.1 ✔ forcats 0.3.0 ── Conflicts ────────────────────────────────────────── tidyverse_conflicts() ── ✖ dplyr::filter() masks stats::filter() ✖ dplyr::lag() masks stats::lag() mpgデータセットの説明

R入門2

Rの入門2, 簡単にdata frameを操作する mtcarsデータセット 特にロードする必要もなく、デフォルトで入っている。 mtcars mpg cyl disp hp drat wt qsec vs am gear carb Mazda RX4 21.0 6 160.0 110 3.90 2.620 16.46 0 1 4 4 Mazda RX4 Wag 21.0 6 160.0 110 3.90 2.875 17.02 0 1 4 4 Datsun 710 22.8 4 108.0 93 3.85 2.320 18.61 1 1 4 1 Hornet 4 Drive 21.4 6 258.0 110 3.08 3.215 19.44 1 0 3 1 Hornet Sportabout 18.

R入門1

Rの入門 # modulus operator (remainder of 5 divided by 2) print(5 %% 2) [1] 1 要素は偶数個なので、medianは真ん中の二つの数字の平均を取る median(c(3, 7, 6, 10, 3, 7)) [1] 6.5 var <- 10. print(var^2) print(var/3) [1] 100 [1] 3.333333 lang.domain <- "statistics" lang.domain <- toupper(lang.domain) print(lang.domain) [1] "STATISTICS" # # substitutes every "i" for "I" lang.domain <- "statistics" print(gsub("i", "I", lang.domain) ) [1] "statIstIcs" lang.domain <- "statistics" print(substr(lang.domain, 1,4)) [1] "stat" # combines character strings lang.

Rを使って、コイン投げをシミュレート

500回コインを投げるとします。毎回表と裏のどれかをシミュレートする。 sample関数はTRUEになっていますが、これは毎回0と1の中からランダムに選ぶという意味、1回選んだら、それを除いて、残りから選ぶではない。 link > # 表が出る確率0.5 > pHeads = 0.5 > # 0(裏)と1(表)二種類の可能性しかない。裏がでる確率が 1-pHeads > flipSequence = sample( x=c(0,1), prob=c(1-pHeads,pHeads), size=N, replace=TRUE) > # 実際にシミュレートした結果 > flipSequence [1] 1 0 0 1 1 0 1 0 1 0 1 0 0 1 0 1 1 0 1 0 0 0 0 1 1 1 1 [28] 0 1 0 1 0 1 0 0 0 1 0 0 0 0 0 1 0 1 1 1 0 0 0 0 0 0 1 [55] 1 0 1 0 0 0 0 1 1 1 1 0 0 0 0 1 1 0 0 0 1 0 0 1 0 0 1 [82] 0 1 0 0 0 1 1 0 1 1 1 1 0 1 0 1 1 0 0 0 0 1 1 0 1 0 1 .