Skip to content

Commit

Permalink
Merge pull request #456 from spsanderson/development
Browse files Browse the repository at this point in the history
prepping for CRAN
  • Loading branch information
spsanderson authored Apr 25, 2024
2 parents 71c480e + 59deca8 commit fbe3e8b
Show file tree
Hide file tree
Showing 24 changed files with 37 additions and 37 deletions.
2 changes: 1 addition & 1 deletion R/00_global_variables.R
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,6 @@ globalVariables(
"name", "prob", "rate", "sd_log", "shape1", "shape2", "size", "total", "total_deviance",
"abs_aic", "aic_value", "data", "dist_with_params", "ks", "lm", "lm_model", "mu",
"stan_dev", "tidy_ks", "prop", "dens_tbl", "cih", "cil", "mstat", "stat", ".",
"variable",".N"
"variable",".N",".cum_stat",".return_tibble",".sample","dof","ncp"
)
)
4 changes: 2 additions & 2 deletions R/est-param-chisq.R
Original file line number Diff line number Diff line change
Expand Up @@ -127,15 +127,15 @@ util_chisquare_param_estimate <- function(.x, .auto_gen_empirical = TRUE) {
neg_log_likelihood <- function(params) {
df <- params[1]
ncp <- params[2]
sum_densities <- sum(dchisq(x_term, df = df, ncp = ncp, log = TRUE))
sum_densities <- sum(stats::dchisq(x_term, df = df, ncp = ncp, log = TRUE))
return(-sum_densities)
}

# Initial guess for parameters
initial_params <- c(trunc(var(x_term)/2), trunc(mean(x_term)))

# Optimize parameters using optim() function
opt_result <- optim(par = initial_params, fn = neg_log_likelihood)
opt_result <- stats::optim(par = initial_params, fn = neg_log_likelihood)

# Extract estimated parameters
doff <- opt_result$par[1]
Expand Down
4 changes: 2 additions & 2 deletions R/utils-aic-beta.R
Original file line number Diff line number Diff line change
Expand Up @@ -60,11 +60,11 @@ util_beta_aic <- function(.x) {
shape2 <- par[2]
ncp <- par[3]
n <- length(data)
-sum(dbeta(data, shape1, shape2, ncp, log = TRUE))
-sum(stats::dbeta(data, shape1, shape2, ncp, log = TRUE))
}

# Fit beta distribution using optim
fit_beta <- optim(
fit_beta <- stats::optim(
c(trunc(pe$shape1), trunc(pe$shape2), 0),
neg_log_lik_beta,
data = x
Expand Down
4 changes: 2 additions & 2 deletions R/utils-aic-cauchy.R
Original file line number Diff line number Diff line change
Expand Up @@ -57,15 +57,15 @@ util_cauchy_aic <- function(.x) {
location <- par[1]
scale <- par[2]
n <- length(data)
-sum(dcauchy(data, location = location, scale = scale, log = TRUE))
-sum(stats::dcauchy(data, location = location, scale = scale, log = TRUE))
}

# Get initial parameter estimates (you might need to adjust this depending on your data)
# Here we use method of moments estimates as a starting point
pe <- TidyDensity::util_cauchy_param_estimate(x)$parameter_tbl

# Fit Cauchy distribution using optim
fit_cauchy <- optim(
fit_cauchy <- stats::optim(
c(pe$location, pe$scale),
neg_log_lik_cauchy,
data = x
Expand Down
4 changes: 2 additions & 2 deletions R/utils-aic-chisq.R
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ util_chisq_aic <- function(.x) {
x <- as.numeric(.x)

# Get parameters
pe <- TidyDensity::util_chisquare_param_estimate(x)$parameter_tbl |> head(1)
pe <- TidyDensity::util_chisquare_param_estimate(x)$parameter_tbl |> utils::head(1)

# Negative log-likelihood function for chi-square distribution
neg_log_lik_chisq <- function(par, data) {
Expand All @@ -39,7 +39,7 @@ util_chisq_aic <- function(.x) {
}

# Fit chi-square distribution to sample data (rchisq)
fit_chisq <- optim(
fit_chisq <- stats::optim(
c(pe$degrees_of_freedom, pe$ncp),
neg_log_lik_chisq,
data = x
Expand Down
4 changes: 2 additions & 2 deletions R/utils-aic-exponential.R
Original file line number Diff line number Diff line change
Expand Up @@ -45,14 +45,14 @@ util_exponential_aic <- function(.x) {
neg_log_lik_exponential <- function(par, data) {
rate <- par[1]
n <- length(data)
-sum(dexp(data, rate = rate, log = TRUE))
-sum(stats::dexp(data, rate = rate, log = TRUE))
}

# Get initial parameter estimate: reciprocal of the mean of the data
pe <- TidyDensity::util_exponential_param_estimate(x)$parameter_tbl

# Fit exponential distribution using optim
fit_exponential <- optim(
fit_exponential <- stats::optim(
pe$rate,
neg_log_lik_exponential,
data = x,
Expand Down
4 changes: 2 additions & 2 deletions R/utils-aic-gamma.R
Original file line number Diff line number Diff line change
Expand Up @@ -54,15 +54,15 @@ util_gamma_aic <- function(.x) {
shape <- par[1]
scale <- par[2]
n <- length(data)
-sum(dgamma(data, shape = shape, scale = scale, log = TRUE))
-sum(stats::dgamma(data, shape = shape, scale = scale, log = TRUE))
}

# Get initial parameter estimates: method of moments
pe <- TidyDensity::util_gamma_param_estimate(x)$parameter_tbl |>
subset(method == "EnvStats_MMUE")

# Fit gamma distribution using optim
fit_gamma <- optim(
fit_gamma <- stats::optim(
c(pe$shape, pe$scale),
neg_log_lik_gamma,
data = x
Expand Down
2 changes: 1 addition & 1 deletion R/utils-aic-geometric.R
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ util_geometric_aic <- function(.x) {

# Calculate AIC
k_geometric <- 1 # Number of parameters for geometric distribution (prob)
logLik_geometric <- sum(dgeom(x, prob = prob, log = TRUE))
logLik_geometric <- sum(stats::dgeom(x, prob = prob, log = TRUE))
AIC_geometric <- 2 * k_geometric - 2 * logLik_geometric

# Return AIC
Expand Down
2 changes: 1 addition & 1 deletion R/utils-aic-hypergeometric.R
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ util_hypergeometric_aic <- function(.x) {

# Calculate AIC
k_hypergeometric <- 3 # Number of parameters for hypergeometric distribution (m, n, and k)
logLik_hypergeometric <- sum(dhyper(x, m = m, n = n, k = k, log = TRUE))
logLik_hypergeometric <- sum(stats::dhyper(x, m = m, n = n, k = k, log = TRUE))
AIC_hypergeometric <- 2 * k_hypergeometric - 2 * logLik_hypergeometric

# Return AIC
Expand Down
4 changes: 2 additions & 2 deletions R/utils-aic-logistic.R
Original file line number Diff line number Diff line change
Expand Up @@ -53,15 +53,15 @@ util_logistic_aic <- function(.x) {
location <- par[1]
scale <- par[2]
n <- length(data)
-sum(dlogis(data, location = location, scale = scale, log = TRUE))
-sum(stats::dlogis(data, location = location, scale = scale, log = TRUE))
}

# Get initial parameter estimates: method of moments
pe <- TidyDensity::util_logistic_param_estimate(x)$parameter_tbl |>
subset(method == "EnvStats_MLE")

# Fit logistic distribution using optim
fit_logistic <- optim(
fit_logistic <- stats::optim(
c(pe$location, pe$scale),
neg_log_lik_logistic,
data = x
Expand Down
4 changes: 2 additions & 2 deletions R/utils-aic-lognormal.R
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ util_lognormal_aic <- function(.x) {
meanlog <- par[1]
sdlog <- par[2]
n <- length(data)
-sum(dlnorm(data, meanlog = meanlog, sdlog = sdlog, log = TRUE))
-sum(stats::dlnorm(data, meanlog = meanlog, sdlog = sdlog, log = TRUE))
}

# Get initial parameter estimates: method of moments
Expand All @@ -62,7 +62,7 @@ util_lognormal_aic <- function(.x) {
sdlog_est <- sqrt(m2 - m1^2)

# Fit log-normal distribution using optim
fit_lognormal <- optim(
fit_lognormal <- stats::optim(
c(meanlog_est, sdlog_est),
neg_log_lik_lognormal,
data = x
Expand Down
4 changes: 2 additions & 2 deletions R/utils-aic-normal.R
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ util_normal_aic <- function(.x) {
x <- as.numeric(.x)

# Get parameters
pe <- TidyDensity::util_normal_param_estimate(x)$parameter_tbl |> head(1)
pe <- TidyDensity::util_normal_param_estimate(x)$parameter_tbl |> utils::head(1)

# Negative log-likelihood function for normal distribution
neg_log_lik_norm <- function(par, data) {
Expand All @@ -40,7 +40,7 @@ util_normal_aic <- function(.x) {
}

# Fit normal distribution to population data (rnorm)
fit_norm <- optim(
fit_norm <- stats::optim(
c(pe$mu, pe$stan_dev),
neg_log_lik_norm,
data = x
Expand Down
2 changes: 1 addition & 1 deletion R/utils-aic-pareto.R
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@ util_pareto_aic <- function(.x) {
subset(method == "MLE")

# Fit Pareto distribution using optim
fit_pareto <- optim(
fit_pareto <- stats::optim(
c(pe$shape, pe$scale),
neg_log_lik_pareto,
data = x
Expand Down
2 changes: 1 addition & 1 deletion R/utils-aic-poisson.R
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ util_poisson_aic <- function(.x) {

# Calculate AIC
k_poisson <- 1 # Number of parameters for Poisson distribution (lambda)
logLik_poisson <- sum(dpois(x, lambda = lambda, log = TRUE))
logLik_poisson <- sum(stats::dpois(x, lambda = lambda, log = TRUE))
AIC_poisson <- 2 * k_poisson - 2 * logLik_poisson

# Return AIC
Expand Down
4 changes: 2 additions & 2 deletions R/utils-aic-weibull.R
Original file line number Diff line number Diff line change
Expand Up @@ -53,14 +53,14 @@ util_weibull_aic <- function(.x) {
shape <- par[1]
scale <- par[2]
n <- length(data)
-sum(dweibull(data, shape = shape, scale = scale, log = TRUE))
-sum(stats::dweibull(data, shape = shape, scale = scale, log = TRUE))
}

# Get initial parameter estimates: method of moments
pe <- TidyDensity::util_weibull_param_estimate(x)$parameter_tbl

# Fit Weibull distribution using optim
fit_weibull <- optim(
fit_weibull <- stats::optim(
c(pe$shape, pe$scale),
neg_log_lik_weibull,
data = x
Expand Down
24 changes: 12 additions & 12 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -50,18 +50,18 @@ library(ggplot2)

tidy_normal()
#> # A tibble: 50 × 7
#> sim_number x y dx dy p q
#> <fct> <int> <dbl> <dbl> <dbl> <dbl> <dbl>
#> 1 1 1 0.261 -3.68 0.000208 0.603 0.261
#> 2 1 2 0.491 -3.53 0.000571 0.688 0.491
#> 3 1 3 0.220 -3.37 0.00139 0.587 0.220
#> 4 1 4 -0.801 -3.21 0.00303 0.212 -0.801
#> 5 1 5 0.732 -3.06 0.00591 0.768 0.732
#> 6 1 6 -1.87 -2.90 0.0104 0.0308 -1.87
#> 7 1 7 -0.898 -2.74 0.0168 0.185 -0.898
#> 8 1 8 0.157 -2.59 0.0252 0.562 0.157
#> 9 1 9 -1.06 -2.43 0.0359 0.145 -1.06
#> 10 1 10 0.411 -2.27 0.0495 0.660 0.411
#> sim_number x y dx dy p q
#> <fct> <int> <dbl> <dbl> <dbl> <dbl> <dbl>
#> 1 1 1 0.227 -2.97 0.000238 0.590 0.227
#> 2 1 2 1.12 -2.84 0.000640 0.869 1.12
#> 3 1 3 1.26 -2.71 0.00153 0.897 1.26
#> 4 1 4 0.204 -2.58 0.00326 0.581 0.204
#> 5 1 5 1.04 -2.44 0.00620 0.852 1.04
#> 6 1 6 -0.180 -2.31 0.0106 0.429 -0.180
#> 7 1 7 0.299 -2.18 0.0167 0.618 0.299
#> 8 1 8 1.73 -2.04 0.0243 0.959 1.73
#> 9 1 9 -0.770 -1.91 0.0338 0.221 -0.770
#> 10 1 10 0.385 -1.78 0.0463 0.650 0.385
#> # ℹ 40 more rows
```

Expand Down
Binary file modified man/figures/README-more_than_nine_simulations-1.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file modified man/figures/README-more_than_nine_simulations-2.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file modified man/figures/README-more_than_nine_simulations-3.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file modified man/figures/README-more_than_nine_simulations-4.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file modified man/figures/README-plot_density-1.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file modified man/figures/README-plot_density-2.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file modified man/figures/README-plot_density-3.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file modified man/figures/README-plot_density-4.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.

0 comments on commit fbe3e8b

Please sign in to comment.