簡體   English   中英

R:在大x范圍內擬合卡方分布

[英]R: Fitting Chi-squared distribution with large x range

在有限范圍內很容易獲得卡方分布的良好擬合:

library(MASS)
nnn <- 1000
set.seed(101)
chii <- rchisq(nnn,4, ncp = 0) ## Generating a chi-sq distribution
chi_df <- fitdistr(chii,"chi-squared",start=list(df=3),method="BFGS") ## Fitting
chi_k <- chi_df[[1]][1] ## Degrees of freedom
chi_hist <- hist(chii,breaks=50,freq=FALSE) ## PLotting the histogram
curve(dchisq(x,df=chi_k),add=TRUE,col="green",lwd=3) ## Plotting the line

但是,假設我有一個數據集,其中的分布分布在X軸上,而它的新值卻由類似下面的值給出:
chii <- 5*rchisq(nnn,4, ncp = 0)

不知道 實際數據集的 這個乘法因子 5 ,如何將 rchisq() /復雜數據 規格化 以與 fitdistr() 很好地匹配

在此先感謝您的幫助!

您將不得不在自由度之間循環,以找到最適合您的數據的方式。 首先,您可能知道卡方分布的均值是自由度,讓我們用它來調整數據並解決問題。

總而言之,您會在可能的自由度之間循環,以找到最適合您調整后的數據的自由度。

library(MASS)
nnn <- 1000
set.seed(101)

x <- round(runif(1,1,100)) # generate a random multiplier
chii <- x*rchisq(nnn,4, ncp = 0) ## Generating a shifted chi-sq distribution

max_df <- 100 # max degree of freedom to test (here from 1 to 100)
chi_df_disp <- rep(NA,max_df)

# loop across degree of freedom
for (i in 1:max_df) {
  chii_adjusted <- (chii/mean(chii))*i # Adjust the chi-sq distribution so that the mean matches the tested degree of freedom 
  chi_fit <- fitdistr(chii_adjusted,"chi-squared",start=list(df=i),method="BFGS") ## Fitting
  chi_df_disp[i] <- chi_fit$estimate/i # This is going to give you the dispersion between the fitted df and the tested df
}

# Find the value with the smallest dispersion (i.e. the best match between the estimated df and the tested df)
real_df <- which.min(abs(chi_df_disp-1))
print(real_df) # print the real degree of freedom after correction

現在,您可以使用“真實”自由度來調整卡方分布並繪制理論分布線。

chii_adjusted <- (chii/mean(chii))*real_df
chi_hist <- hist(chii_adjusted,breaks=50,freq=FALSE) ## PLotting the histogram
curve(dchisq(x,df=real_df),add=TRUE,col="green",lwd=3) ## Plotting the line

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM