卡方分布

时间:2015-03-03 20:59:33

标签: r distribution data-fitting chi-squared

尝试在R中使用fitdistr()拟合chi_square分布。这里的文档就在这里(对我来说不是很有用):https://stat.ethz.ch/R-manual/R-devel/library/MASS/html/fitdistr.html

问题1:下面的chi_df具有以下输出:3.85546875 (0.07695236)。第二个数字是多少?方差或标准差?

问题2:fitdistr生成由Chi-SQ分布定义的'k'。如何拟合数据,使得缩放常数为“A”?我愚蠢地使用下面的第14-17行。显然不好。

问题3:Chi-SQ分布仅定义为某个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");

感谢您的帮助!

1 个答案:

答案 0 :(得分:5)

  1. 如上所述,?fitdistr
  2.   

    类'“fitdistr”'的对象,一个包含四个组件的列表,   ...        sd:估计的标准误差,

    ...所以带括号的数字是参数的标准误差。

    1. 不需要估算比例参数;您需要按直方图区间的宽度缩放,在绘制直方图时只需使用freq=FALSE。请参阅以下代码。

    2. 卡方分布是在非负实数上定义的,这是有道理的,因为它是平方标准正态分布(这是一个统计问题,而不是编程问题)。

    3. 设置数据:

      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]
      

      (对于它的价值,看起来使用fitdistrmethod="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")