卡方分布

Ent*_*opy 3 r distribution chi-squared data-fitting

试图fitdistr()在R.中使用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");
Run Code Online (Sandbox Code Playgroud)

谢谢你的帮助!

Ben*_*ker 6

  1. 如上所述,?fitdistr

类"fitdistr"的对象,包含四个组件的列表,... sd:估计的标准错误,

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

  1. 不需要估计比例参数; 您需要按直方图箱的宽度进行缩放,或者freq=FALSE在绘制直方图时使用.见下面的代码.

  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)  
Run Code Online (Sandbox Code Playgroud)

配件.

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]
Run Code Online (Sandbox Code Playgroud)

(对于它的价值,看起来打印方法中可能存在使用fitdistr何时的错误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")
Run Code Online (Sandbox Code Playgroud)