Take a baseline variable \(Y\) (any
distribution with d/p/q/r functions) and a monotone map
\(V=g(Y)\in(0,1)\). The package gives
the law of \(V\) and the full toolchain
around it.
library(UnitTrans)
trans_list()
#> name forward
#> 1 rational V = Y / (1 + Y)
#> 2 exponential V = exp(-Y)
#> 3 logit V = 1 / (1 + exp(-Y))
#> 4 tanh V = 2 / (1 + exp(Y)) = 1 - tanh(Y / 2)
#> 5 probit V = pnorm(Y)
#> 6 cloglog V = 1 - exp(-exp(Y))
#> inverse domain decreasing
#> 1 Y = V / (1 - V) nonneg FALSE
#> 2 Y = -log(V) positive TRUE
#> 3 Y = log(V / (1 - V)) real FALSE
#> 4 Y = log(2 / V - 1) = 2 atanh(1 - V) positive TRUE
#> 5 Y = qnorm(V) real FALSE
#> 6 Y = log(-log(1 - V)) real FALSEFrom a shell:
R CMD build UnitTrans
R CMD check UnitTrans_0.2.0.tar.gz
or inside R: testthat::test_local("UnitTrans"). Expect
FAIL 0.
detect_transformations("gamma", c(shape = 3, rate = 1))
#> transformation valid v_min v_max
#> 1 rational TRUE 1.792704e-02 0.9503208
#> 2 exponential TRUE 4.923884e-09 0.9819113
#> 3 logit TRUE 5.045634e-01 1.0000000
#> 4 tanh TRUE 9.847768e-09 0.9908731
#> 5 probit TRUE 5.072820e-01 1.0000000
#> 6 cloglog TRUE 6.388356e-01 1.0000000
#> note
#> 1 valid
#> 2 valid
#> 3 valid, but V covers only a small part of (0,1)
#> 4 valid
#> 5 valid, but V covers only a small part of (0,1)
#> 6 valid, but V covers only a small part of (0,1)
check_domain("norm", c(0, 1), "rational")$message
#> [1] "baseline puts mass 0.5 outside the domain of 'rational' (needs nonneg support)"fit <- fit_trans(x, "gamma", "rational")
summary(fit)
#> Transformed distribution fit
#> baseline: gamma | transformation: rational | n = 300
#>
#> Estimate Std.Err
#> shape 2.40069 0.18403
#> rate 1.24934 0.10649
#>
#> logLik = 148.282 AIC = -292.564 BIC = -285.157 AICc = -292.524
#>
#> Wald tests:
#> Estimate Std.Err z Pr(>|z|)
#> shape 2.40069 0.18403 13.04523 0
#> rate 1.24934 0.10649 11.73253 0res <- fit_all(x, c("gamma", "lnorm", "weibull"), criterion = "AIC")
res
#> Ranking by AIC
#> model k logLik AIC BIC AICc delta weight
#> lnorm + exponential 2 148.80 -293.61 -286.20 -293.57 0.0000 3.8280e-01
#> lnorm + tanh 2 148.63 -293.25 -285.84 -293.21 0.3553 3.2049e-01
#> gamma + rational 2 148.28 -292.56 -285.16 -292.52 1.0421 2.2734e-01
#> gamma + tanh 2 146.47 -288.94 -281.53 -288.90 4.6662 3.7130e-02
#> lnorm + rational 2 146.27 -288.53 -281.12 -288.49 5.0749 3.0267e-02
#> gamma + exponential 2 143.21 -282.43 -275.02 -282.39 11.1770 1.4319e-03
#> weibull + rational 2 142.24 -280.49 -273.08 -280.45 13.1201 5.4197e-04
#> weibull + tanh 2 133.22 -262.45 -255.04 -262.41 31.1584 6.5617e-08
#> weibull + exponential 2 129.02 -254.03 -246.62 -253.99 39.5759 9.7536e-10
best <- best_model(res)gof_trans(best, B = 50, seed = 1)
#> test statistic p.value valid_boot
#> 1 KS 0.02489779 1.0000000 50
#> 2 CvM 0.02942380 0.8823529 50
#> 3 AD 0.22103358 0.8823529 50If a p-value is small, return to Step 5 with other baselines or transformations.
confint(best)
#> 2.5 % 97.5 %
#> meanlog -0.7858113 -0.6655857
#> sdlog 0.4887202 0.5737326
confint(best, method = "profile")
#> 2.5 % 97.5 %
#> meanlog -0.7860042 -0.6653927
#> sdlog 0.4913966 0.5767425
head(residuals(best))
#> [1] -0.6714716 -0.3130081 0.2729509 1.4562292 -0.9253941 1.4016432
summary_trans(best$dist, best$coef, best$trans)
#> mean variance skewness ex.kurtosis
#> 0.59769164 0.02362464 -0.56285404 -0.01666194
hazard_trans(c(.2, .5, .8), best$dist, best$coef, best$trans)
#> [1] 0.1828659 2.2971861 20.0626292simulation_study(best$dist, best$coef, best$trans, n = c(50, 100), R = 20, seed = 1)
#> n parameter true mean_est bias rmse coverage failed
#> 1 50 meanlog -0.7256985 -0.7306826 -0.004984163 0.06810434 0.95 0
#> 2 50 sdlog 0.5312264 0.5227500 -0.008476416 0.06306605 0.80 0
#> 3 100 meanlog -0.7256985 -0.7082491 0.017449421 0.05209934 0.90 0
#> 4 100 sdlog 0.5312264 0.5377134 0.006486999 0.03494260 1.00 0Use a larger R (for example 500) and more sample sizes
for a publication.