2017-10-20 15 views
2

私は、非対称t分布または正規分布が特定のデータセットに適しているかどうかを示しています。これを行うにあたり、当てはめられた正規分布とフィッティングされたt分布の両方を重ね合わせることにしました。ggplot2を使用した非対称t分布の重畳

x <- data.frame(rnorm(500)) 
    names(x) <- c("test.data") 

    ggplot(x,aes(x=test.data)) + 
     stat_function(fun = dnorm, args=list(mean=mean(x$test.data,na.rm=TRUE), 
     sd=sd(x$test.data,na.rm=TRUE)), aes(colour = 'Normal')) + 
     geom_histogram(aes(y = ..density..), alpha = 0.4) 

下のグラフが得られます:

enter image description here

今、私は私が持っている非対称t分布、のために同じことをやりたい正規分布については、これはstat_funを使用しても問題ありません形状(nu)、位置(mu)、分散(sigma)および非中心パラメータ(γ)を含む。 stat_function関数でt分布を入力すると、パッケージ統計からdt関数を使用して、形状と非中心パラメータのみを指定できます。

4つのパラメータをすべて事前に指定できるヒストグラムにt分布を重ねる方法はありますか?

答えて

3

これを試してみてください:

library(ggplot2) 
set.seed(1) 
x <- data.frame(rt(5000,df=5,ncp=1)*10+7) 
names(x) <- c("test.data") 

# Define a Student t distribution with shape (nu) and location (mu) 
dt2 <- function(x, mu, nu, df, ncp) { 
    dt((x-mu)/nu,df,ncp)/nu 
} 

ggplot(x,aes(x=test.data)) + 
stat_function(fun = dt2, args=list(mu=7, nu=10, df=5, ncp=1), 
       aes(colour = 'Student t'), lwd=1) + 
geom_histogram(aes(y = ..density..), bins=100, alpha = 0.4) 

enter image description here

関連する問題