3

R で使用して chi_square 分布を当てはめようとしています。fitdistr()これに関するドキュメントはここにあります (私にはあまり役に立ちません): https://stat.ethz.ch/R-manual/R-devel/library/MASS/html/fitdistr. html

質問 1:chi_df以下の出力は次のとおりです 3.85546875 (0.07695236)。2番目の数字は何ですか?分散または標準偏差?

質問 2: fitdistrChi-SQ 分布によって定義される「k」を生成します。スケーリング定数「A」を取得するためにデータを適合させるにはどうすればよいですか? 以下の14〜17行目をばかげて使用しています。明らかに良くない。

質問 3: カイ二乗分布は特定の x 範囲に対してのみ定義されていますか? (分散は 2K として定義されますが、平均 = k です。これには、制約のある x 範囲が必要です...プログラミングではなく統計に関する質問...)

nnn = 1000;
## Generating a chi-sq distribution
chii <- rchisq(nnn,4, ncp = 0);  
## Plotting Histogram
chi_hist <- hist(chii);   
## Fitting. Gives probability density which must be scaled.
chi_df <- fitdistr(chii,"chi-squared",start=list(df=3)); 
chi_k <- chi_df[[1]][1];

## Plotting a fitted line:
## Spanning x-length of chi-sq data
x_chi_fit <- 1:nnn*((max(chi_hist[[1]][])-min(chi_hist[[1]][]))/nnn);

## Y data using eqn for probability function
y_chi_fit <- (1/(2^(chi_k/2)*gamma(chi_k/2)) * x_chi_fit^(chi_k/2-1) * exp(-x_chi_fit/2));
## Normalizing to the peak of the histogram
y_chi_fit <- y_chi_fit*(max(chi_hist[[2]][]/max(y_chi_fit)));

## Plotting the line
lines(x_chi_fit,y_chi_fit,lwd=2,col="green");

ご協力いただきありがとうございます!

4

1 に答える 1

6
  1. 上でコメントしたように?fitdistr

クラス '"fitdistr"' のオブジェクト、4 つのコンポーネントを持つリスト、... sd: 推定標準誤差、

... 括弧内の数値がパラメータの標準誤差になるようにします。

  1. スケール パラメータを推定する必要はありません。ヒストグラムビンの幅でスケーリングするか、ヒストグラムを描画するときに使用する必要がありfreq=FALSEます。以下のコードを参照してください。

  2. カイ 2 乗分布は、非負の実数で定義されます。これは、標準正規分布の 2 乗の分布であるため、理にかなっています (これは統計上の問題であり、プログラミングの問題ではありません)。

データの設定:

nnn <- 1000
## ensure reproducibility; not a big deal in this case,
##  but good practice
set.seed(101)
## Generating a chi-sq distribution
chii <- rchisq(nnn,4, ncp = 0)  

フィッティング。

library(MASS)
## use method="Brent" based on warning
chi_df <- fitdistr(chii,"chi-squared",start=list(df=3),
                   method="Brent",lower=0.1,upper=100)
chi_k <- chi_df[[1]][1]

fitdistr(それが価値があるのは、が使用されているときのprintメソッドにバグがある可能性があるようです。また、境界を指定する必要はなく、method="Brent"使用することもできます...)method="BFGS"

ヒストグラム

chi_hist <- hist(chii,breaks=50,col="gray")
## scale by N and width of histogram bins
curve(dchisq(x,df=chi_k)*nnn*diff(chi_hist$breaks)[1],
      add=TRUE,col="green")
## or plot histogram already scaled to a density
chi_hist <- hist(chii,breaks=50,col="gray",freq=FALSE)   
curve(dchisq(x,df=chi_k),add=TRUE,col="green")
于 2015-03-04T14:38:34.387 に答える