1
1

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 3 years have passed since last update.

Rで確率分布のグラフを作成する

Posted at

分布の名前をすぐ忘れてしまうので、Rで実際に分布を作りつつまとめてみる。

確率分布とは、確率変数の値(x軸)と、その値に応じた確率(y軸)の関係を表したもの
y軸は0以上、足し合わせると1になるという性質を持つ

  • 確率変数
    値に応じた確率が存在する変数X
    確率変数が連続値の場合と離散値の場合で、確率分布の扱いが異なる

  • 連続変数
    y軸は確率密度で、0以上の値をとる
    確率を求める場合は、aからbの範囲で囲んだ面積を積分で求める(確率密度関数)
    但し、連続値=特定の値に対応する確率が存在しないので
    累積分布関数にすることで、y軸の範囲を0以上1以下の値で表現できる         

  • 離散変数y軸は確率
    0以上1以下の値
    確率を求める場合は、aやbの値を取る確率(確率質量関数)

ベルヌーイ分布

ある確率の事象が起こるか起こらないか。
この「起こるか起こらないか」をベルヌーイ試行という。
事象が起こる確率pのみ決めてあげれば分布ができる。 X軸は0か1の離散変数
関数がないのでサンプル関数を使用

tibble(x=c(0,1), y=dbern(x,0.8)) %>% 
  ggplot(aes(x, y)) +       
  geom_bar(stat='identity') 

Rplot02.png

二項分布

ベルヌーイ試行を何回もやった時の発生確率。
事象が起こる確率pと何回試行したかの回数nを決めてあげる。 X軸は試行回数なので離散変数

tibble(x=seq(1,30,1), y=dbinom(x,30,0.5)) %>% 
  ggplot(aes(x, y)) +       
  geom_bar(stat='identity') 

Rplot03.png

幾何分布

ベルヌーイ試行で「初めて」イベントが起こるまでの回数。
事象が起こる確率pを決めてあげる。 X軸は試行回数なので離散変数

tibble(x=seq(1,20,1), y=dgeom(x,0.3)) %>% 
  ggplot(aes(x, y)) +       
  geom_bar(stat='identity') 

Rplot04.png

一様分布

MINとMAXの値の間で一定の確率の分布
MINとMAXの値を決めればOK X軸は連続変数

# 確率密度関数
tibble(x=seq(-6,6,1), y=dunif(x,-5,5)) %>% 
  ggplot(aes(x, y)) +       
  geom_bar(stat='identity') 

# 累積分布関数
tibble(x=seq(-6,6,1), y=punif(x,-5,5)) %>% 
  ggplot(aes(x, y)) +       
  geom_line(stat='identity') 

Rplot06.png Rplot05.png

正規分布

真ん中の平均(μ)と広がりの分散(σ^2)を決める
平均50、分散100としたものが偏差値。
偏差値70の人は2σなので、全体の5%、つまり上位2.5%にいるってこと
二項分布のnが大きくなると、正規分布っぽいものになる X軸は連続変数

# 確率密度関数
tibble(x=seq(-6,6,1), y=dnorm(x,0,2)) %>% 
  ggplot(aes(x, y)) +       
  geom_line(stat='identity') 

# 累積分布関数
tibble(x=seq(-6,6,1), y=pnorm(x,0,2)) %>% 
  ggplot(aes(x, y)) +       
  geom_line(stat='identity') 

Rplot07.pngRplot08.png

ポアソン分布

一定期間内のレアなイベントの発生回数の確率
期間内の発生確率(λ)を決めてあげる
二項分布のnをすごく大きくしてpを小さくしたものがポワソン分布。
また、ポワソン分布のnをすごく大きくして、且つλも大きくすると正規分布に似ていく X軸は回数なので離散変数

# 確率密度関数
tibble(x=seq(0,20,1), y=dpois(x,3)) %>% 
  ggplot(aes(x, y)) +       
  geom_bar(stat='identity') 

Rplot09.png

指数分布

一定期間内にレアなイベントが「初めて」起こるまでの時間間隔
期間内の平均発生数(λ)を決めてあげる X軸は連続変数

# 確率密度関数
tibble(x=seq(0,20,1), y=dexp(x,1)) %>% 
  ggplot(aes(x, y)) +       
  geom_line(stat='identity') 

# 累積分布関数
tibble(x=seq(0,20,1), y=pexp(x,1)) %>% 
  ggplot(aes(x, y)) +       
  geom_line(stat='identity') 

Rplot11.pngRplot12.png

参考

1
1
1

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
1
1

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?