내 질문을 수정했습니다
성공적으로 생성 된 개체 만 유지하고 오류가 발생한 개체는 무시하고 싶습니다.
이것은 재현 가능한 예일뿐입니다. 내 원래 데이터 세트가 다릅니다.
다음 함수는 mtcars
데이터 세트의 모든 변수를 가져 와서 세 가지 이론적 분포를 피팅 한 다음 적합도 통계를 반환합니다.
library(fitdistrplus)
fit_distt <- function(var) {
v <- mtcars[, var]
f1 <- fitdist(data = v, distr = "norm")
f2 <- fitdist(data = v, distr = "nbinom")
f3 <- fitdist(data = v, distr = "gamma")
gofstat(f = list(f1, f2, f3),
chisqbreaks = c(0, 3, 3.5, 4, 4.5,
5, 10, 20, 30, 40),
fitnames = c("normal", "nbinom", "gamma"))
}
예를 들면 :
> fit_distt("gear")
Goodness-of-fit statistics
normal nbinom gamma
Kolmogorov-Smirnov statistic 0.2968616 0.4967268 0.3030232
Cramer-von Mises statistic 0.4944390 1.5117544 0.5153004
Anderson-Darling statistic 3.1060083 7.2858460 3.1742713
Goodness-of-fit criteria
normal nbinom gamma
Akaike's Information Criterion 74.33518 109.9331 72.07507
Bayesian Information Criterion 77.26665 112.8646 75.00655
일부 이론적 분포는 변수에 성공적으로 맞지 않고 fitdist
오류가 발생합니다.
> fit_distt("mpg")
<simpleError in optim(par = vstart, fn = fnobj, fix.arg = fix.arg, obs = data, gr = gradient, ddistnam = ddistname, hessian = TRUE, method = meth, lower = lower, upper = upper, ...): function cannot be evaluated at initial parameters>
Error in fitdist(data = v, distr = "nbinom") :
the function mle failed to estimate the parameters,
with the error code 100
이 오류 f2
는 nbinom
에 연속 변수 를 맞추려고 할 때 발생했습니다 mpg
. 그러나 norm
및 gamma
성공적 맞게.
gofstat
성공적으로 맞는 분포에 대해을 반환하고 오류가 발생한 분포를 무시하고 싶습니다 .
f2
함수에가 지정되어 있어도 오류가 발생하면 다음과 같은 출력이 필요합니다.
> fit_distt("mpg")
Goodness-of-fit statistics
normal gamma
Kolmogorov-Smirnov statistic 0.12485059 0.08841088
Cramer-von Mises statistic 0.08800019 0.03793323
Anderson-Darling statistic 0.58886727 0.28886166
Goodness-of-fit criteria
normal gamma
Akaike's Information Criterion 208.7555 205.8416
Bayesian Information Criterion 211.6870 208.7731
분명히 f2
함수에서 제거 할 수 있습니다 . 그러나 이는 각 변수에 대해 모든 코드를 반복하는 것을 의미합니다. 그것은 많은 코드입니다! 그래서 나는 여전히 기능을 사용하고 싶습니다.
그리고 모든 변수에 대해 함수를 사용할 수 있기를 원합니다. 를 사용 mtcars$mpg
하면에 대해 실패 nbinom
하지만을 사용 mtcars$vs
하면에 대해 실패합니다 gamma
. 어쨌든 오류가 발생한 피팅은 건너 뛰고 효과가있는 피팅에 대해보고하고 싶습니다 gofstat
.
purrr::possibly
오류에서 멈추지 않고 조용히 적합 결과를 반환하거나 오류를 던지는 데 사용할 수 있습니다 . 하지만 .NET Framework에서만 성공적으로 맞는 값을 반환하는 방법을 모르겠습니다 gofstat
.
으로 시도 할 수 있습니다 try
. 배포본을 맞추고 gofstat
제대로 작동 하는 경우 전달하는 목록에만 추가하십시오 .
library(fitdistrplus)
#> Loading required package: MASS
#> Loading required package: survival
fit_distt <- function(var) {
v <- mtcars[, var]
distributions <- c("norm", "nbinom", "gamma")
fs <- list()
fitted_distributions <- vector(mode = "character")
for (i in seq_along(distributions)) {
# try to fit the model
fit <- try(fitdist(data = v, distr = distributions[i]), silent = TRUE)
# if it works, add it to fs. If not, ¯\_(ツ)_/¯
if (!inherits(fit, "try-error")) {
fs[[length(fs)+1]] <- fit
fitted_distributions[length(fitted_distributions)+1] <- distributions[i]
}
}
gofstat(f = fs,
chisqbreaks = c(0, 3, 3.5, 4, 4.5,
5, 10, 20, 30, 40),
fitnames = fitted_distributions)
}
fit_distt("mpg")
#> <simpleError in optim(par = vstart, fn = fnobj, fix.arg = fix.arg, obs = data, gr = gradient, ddistnam = ddistname, hessian = TRUE, method = meth, lower = lower, upper = upper, ...): function cannot be evaluated at initial parameters>
#> Goodness-of-fit statistics
#> norm gamma
#> Kolmogorov-Smirnov statistic 0.12485059 0.08841088
#> Cramer-von Mises statistic 0.08800019 0.03793323
#> Anderson-Darling statistic 0.58886727 0.28886166
#>
#> Goodness-of-fit criteria
#> norm gamma
#> Akaike's Information Criterion 208.7555 205.8416
#> Bayesian Information Criterion 211.6870 208.7731
reprex 패키지 (v0.3.0)에 의해 2020-10-07에 생성됨
이 기사는 인터넷에서 수집됩니다. 재 인쇄 할 때 출처를 알려주십시오.
침해가 발생한 경우 연락 주시기 바랍니다[email protected] 삭제
몇 마디 만하겠습니다