You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
#' Estimate Paralogistic Parameters#'#' @family Parameter Estimation#' @family Paralogistic#'#' @details This function will attempt to estimate the paralogistic shape and rate#' parameters given some vector of values.#'#' @description The function will return a list output by default, and if the parameter#' `.auto_gen_empirical` is set to `TRUE` then the empirical data given to the#' parameter `.x` will be run through the `tidy_empirical()` function and combined#' with the estimated paralogistic data.#'#' The method of parameter estimation is:#' - MLE#'#' @param .x The vector of data to be passed to the function.#' @param .auto_gen_empirical This is a boolean value of TRUE/FALSE with default#' set to TRUE. This will automatically create the `tidy_empirical()` output#' for the `.x` parameter and use the `tidy_combine_distributions()`. The user#' can then plot out the data using `$combined_data_tbl` from the function output.#'#' @examples#' library(dplyr)#' library(ggplot2)#'#' x <- mtcars$mpg#' output <- util_paralogistic_param_estimate(x)#'#' output$parameter_tbl#'#' output$combined_data_tbl |>#' tidy_combined_autoplot()#'#' t <- tidy_paralogistic(50, 2.5, 1.4)[["y"]]#' util_paralogistic_param_estimate(t)$parameter_tbl#'#' @return#' A tibble/list#'#' @export#'util_paralogistic_param_estimate<-function(.x, .auto_gen_empirical=TRUE) {
# Tidyeval ----x_term<- as.numeric(.x)
minx<- min(x_term)
maxx<- max(x_term)
n<- length(x_term)
unique_terms<- length(unique(x_term))
# Checks ----if (n<2||unique_terms<2) {
rlang::abort(
message="The data must have at least two (2) unique data points.",
use_cli_format=TRUE
)
}
# Get initial parameter estimatesmean_x<- mean(x_term, na.rm=TRUE)
var_x<- var(x_term, na.rm=TRUE)
shape_mme<-2*mean_x^2/ (var_x-mean_x^2)
rate_mme<-2*mean_x/ (var_x-mean_x^2)
# shape_mmue <- 2 * mean_x^2 / (var_x * (n - 1) / n - mean_x^2) |> abs()# rate_mmue <- 2 * mean_x / (var_x * (n - 1) / n - mean_x^2) |> abs()# MLEneg_log_lik_paralogis<-function(par, data) {
shape<-par[1]
rate<-par[2]
-sum(actuar::dparalogis(data, shape=shape, rate=rate, log=TRUE))
}
mle_params<-stats::optim(
c(shape_mme, rate_mme),
neg_log_lik_paralogis,
data=x_term,
method="L-BFGS-B",
lower= c(1e-10, 1e-10)
)$parshape_mle<-mle_params[[1]]
rate_mle<-mle_params[[2]]
# Return Tibble ----if (.auto_gen_empirical) {
te<- tidy_empirical(.x=x_term)
# td_mme <- tidy_paralogistic(# .n = n, .shape = round(shape_mme, 3),# .rate = round(rate_mme, 3)# )td_mle<- tidy_paralogistic(
.n=n, .shape= round(shape_mle, 3),
.rate= round(rate_mle, 3)
)
# td_mmue <- tidy_paralogistic(# .n = n, .shape = round(shape_mmue, 3),# .rate = round(rate_mmue, 3)# )combined_tbl<- tidy_combine_distributions(te, td_mle)
}
ret<-dplyr::tibble(
dist_type="Paralogistic",
samp_size=n,
min=minx,
max=maxx,
mean=mean_x,
var=var_x,
method="MLE",
shape=shape_mle,
rate=rate_mle,
shape_rate_ratio= c(shape_mle/rate_mle)
)
# Return ----
attr(ret, "tibble_type") <-"parameter_estimation"
attr(ret, "family") <-"paralogistic"
attr(ret, "x_term") <-.x
attr(ret, "n") <-nif (.auto_gen_empirical) {
output<-list(
combined_data_tbl=combined_tbl,
parameter_tbl=ret
)
} else {
output<-list(
parameter_tbl=ret
)
}
return(output)
}
Example:
>x<-mtcars$mpg>output<- util_paralogistic_param_estimate(x)
>>output$parameter_tbl# A tibble: 1 × 10dist_typesamp_sizeminmaxmeanvarmethodshaperateshape_rate_ratio<chr><int><dbl><dbl><dbl><dbl><chr><dbl><dbl><dbl>1Paralogistic3210.433.920.136.3MLE4.140.0336123.>>output$combined_data_tbl|>+ tidy_combined_autoplot()
>>t<- tidy_paralogistic(50, 2.5, 1.4)[["y"]]
> util_paralogistic_param_estimate(t)$parameter_tbl# A tibble: 1 × 10dist_typesamp_sizeminmaxmeanvarmethodshaperateshape_rate_ratio<chr><int><dbl><dbl><dbl><dbl><chr><dbl><dbl><dbl>1Paralogistic500.03581.290.5040.0637MLE2.631.361.93>x<-mtcars$mpg>output<- util_paralogistic_param_estimate(x)
>>output$parameter_tbl# A tibble: 1 × 10dist_typesamp_sizeminmaxmeanvarmethodshaperateshape_rate_ratio<chr><int><dbl><dbl><dbl><dbl><chr><dbl><dbl><dbl>1Paralogistic3210.433.920.136.3MLE4.140.0336123.>>output$combined_data_tbl|>+ tidy_combined_autoplot()
>>t<- tidy_paralogistic(50, 2.5, 1.4)[["y"]]
> util_paralogistic_param_estimate(t)$parameter_tbl# A tibble: 1 × 10dist_typesamp_sizeminmaxmeanvarmethodshaperateshape_rate_ratio<chr><int><dbl><dbl><dbl><dbl><chr><dbl><dbl><dbl>1Paralogistic500.03581.290.5040.0637MLE2.631.361.93
AIC
Function:
#' Calculate Akaike Information Criterion (AIC) for Paralogistic Distribution#'#' This function calculates the Akaike Information Criterion (AIC) for a paralogistic distribution fitted to the provided data.#'#' @family Utility#' @family Paralogistic#' @author Steven P. Sanderson II, MPH#'#' @description#' This function estimates the shape and rate parameters of a paralogistic#' distribution from the provided data using maximum likelihood estimation,#' and then calculates the AIC value based on the fitted distribution.#'#' @param .x A numeric vector containing the data to be fitted to a paralogistic distribution.#'#' @details#' This function fits a paralogistic distribution to the provided data using maximum#' likelihood estimation. It estimates the shape and rate parameters#' of the paralogistic distribution using maximum likelihood estimation. Then, it#' calculates the AIC value based on the fitted distribution.#'#' Initial parameter estimates: The function uses the method of moments estimates#' as starting points for the shape and rate parameters of the paralogistic#' distribution.#'#' Optimization method: The function uses the optim function for optimization.#' You might explore different optimization methods within optim for potentially#' better performance.#'#' Goodness-of-fit: While AIC is a useful metric for model comparison, it's#' recommended to also assess the goodness-of-fit of the chosen model using#' visualization and other statistical tests.#'#' @examples#' # Example 1: Calculate AIC for a sample dataset#' set.seed(123)#' x <- tidy_paralogistic(30, .shape = 2, .rate = 1)[["y"]]#' util_paralogistic_aic(x)#'#' @return#' The AIC value calculated based on the fitted paralogistic distribution to the provided data.#'#' @name util_paralogistic_aicNULL#' @export#' @rdname util_paralogistic_aicutil_paralogistic_aic<-function(.x) {
# Tidyevalx<- as.numeric(.x)
# Negative log-likelihood function for paralogistic distributionneg_log_lik_paralogis<-function(par, data) {
shape<-par[1]
rate<-par[2]
-sum(actuar::dparalogis(data, shape=shape, rate=rate, log=TRUE))
}
# Get initial parameter estimates: method of momentspe<-TidyDensity::util_paralogistic_param_estimate(x)$parameter_tbl|>
subset(method=="MLE")
# Fit paralogistic distribution using optimfit_paralogis<-stats::optim(
c(pe$shape, pe$rate),
neg_log_lik_paralogis,
data=x,
method="L-BFGS-B",
lower= c(1e-10, 1e-10)
)
# Extract log-likelihood and number of parameterslogLik_paralogis<--fit_paralogis$valuek_paralogis<-2# Number of parameters for paralogistic distribution (shape and rate)# Calculate AICAIC_paralogis<-2*k_paralogis-2*logLik_paralogis# Return AICreturn(AIC_paralogis)
}
Param Estimate
Function:
Example:
AIC
Function:
Example:
``r
The text was updated successfully, but these errors were encountered: