nishiru3の日記

備忘録です。ネットのゴミ。

ggplot2のメモ(その3)

正規分布

library(ggplot2)
# 正規分布
# seq(a,b,n):下限a、上限b、分割数nの等差数列を作る。
x <-  seq(-4, 4, length=50)
# mean:平均値、sd:標準偏差(sd = sqrt(sigma^2))
fx <- dnorm(x=x,mean=0,sd = 1)
data <-data.frame(x=x,y=fx)
ggplot(data, aes(x=x,y=fx)) + geom_line()

f:id:nishiru3:20180630123737p:plain

対数正規分布

library(ggplot2)
# 対数正規分布
# seq(a,b,n):下限a、上限b、分割数nの等差数列を作る。
x <-  seq(0.01, 10, length=1000)
# f(x) = 1/(sqrt(2*pi)sigma x) exp {-(logx-mu)^2 / (2 sigma^2)}
# meanlog:mu、sdlog:sigma
fx <- dlnorm(x=x, meanlog = 0, sdlog=1)
data <-data.frame(x=x,y=fx)
ggplot(data, aes(x=x,y=fx)) + geom_line()
# xを対数にしたもの。上記の正規分布と同じになる。
ggplot(data, aes(x=log(x),y=fx)) + geom_line()

f:id:nishiru3:20180630124633p:plain

次のグラフは横軸xlog(x)にして表示したものである。
f:id:nishiru3:20180630124648p:plain

カイ二乗分布(その2)

カイ二乗分布(その1)の続き

カイ二乗分布の密度関数

f:id:nishiru3:20180630105359p:plain

密度関数の導出

f:id:nishiru3:20180630105513p:plain

ガンマ分布の再生性を利用した導出

f:id:nishiru3:20180630105617p:plain

f:id:nishiru3:20180630105727p:plain

参照

ガンマ分布については次にまとめている。
nishiru3.hatenablog.com

カイ二乗分布(その1)

密度関数を導出するための準備

最終的には、カイ二乗分布の密度関数、モーメント母関数、期待値、分散を求めるが、
密度関数を求めるために準備を行う。

f:id:nishiru3:20180630061540p:plain

f:id:nishiru3:20180630061743p:plain

f:id:nishiru3:20180630061755p:plain

ggplot2のメモ2

折れ線グラフの例

指数分布とガンマ分布の事例を示す。
指数分布もガンマ分布も\lambdaの表現方法が大きく二つあるので注意。
下記では\lambda=rateで設定しているが、scaleを使うとrateの逆数(=1/\lambda)で指定することができる。

library(ggplot2)
# 指数分布
x <-0:100
# fx = rate * exp{-rate*x}
fx <-dexp(x=x,rate=0.04)
data <-data.frame(x=x,y=fx)
ggplot(data, aes(x=x,y=fx)) + geom_line()

f:id:nishiru3:20180624231725p:plain

library(ggplot2)
# ガンマ分布
x <-0:100
# fx = rate^shape / Gamma(alpha) x^{alpha - 1} exp{-rate x}
# shape:形状係数、rate:ラムダ、sclae:1/ラムダ
fx <- dgamma(x=x,shape =2, rate = 0.1)
data <-data.frame(x=x,y=fx)
ggplot(data, aes(x=x,y=fx)) + geom_line()

f:id:nishiru3:20180624231651p:plain

参考文献

Rグラフィックスクックブック ―ggplot2によるグラフ作成のレシピ集

Rグラフィックスクックブック ―ggplot2によるグラフ作成のレシピ集

参考記事

指数分布やガンマ分布は下記の記事参照。

nishiru3.hatenablog.com