# NOT RUN {
library(HelpersMG)
# _______________________________________________________________
# right censored distribution with gamma distribution
# _______________________________________________________________
# Detection limit
DL <- 100
# Generate 100 random data from a gamma distribution
obc <- rgamma(100, scale=20, shape=2)
# remove the data below the detection limit
obc[obc>DL] <- +Inf
# search for the parameters the best fit these censored data
result <- cutter(observations=obc, upper_detection_limit=DL,
cut_method="censored")
result
plot(result, xlim=c(0, 150), breaks=seq(from=0, to=150, by=10), col.mcmc=NULL)
plot(result, xlim=c(0, 150), breaks=seq(from=0, to=150, by=10))
# _______________________________________________________________
# The same data seen as truncated data with gamma distribution
# _______________________________________________________________
obc <- obc[is.finite(obc)]
# search for the parameters the best fit these truncated data
result <- cutter(observations=obc, upper_detection_limit=DL,
cut_method="truncated")
result
plot(result, xlim=c(0, 150), breaks=seq(from=0, to=150, by=10))
# _______________________________________________________________
# left censored distribution with gamma distribution
# _______________________________________________________________
# Detection limit
DL <- 10
# Generate 100 random data from a gamma distribution
obc <- rgamma(100, scale=20, shape=2)
# remove the data below the detection limit
obc[obc<DL] <- -Inf
# search for the parameters the best fit these truncated data
result <- cutter(observations=obc, lower_detection_limit=DL,
cut_method="censored")
result
plot(result, xlim=c(0, 200), breaks=seq(from=0, to=300, by=10))
# _______________________________________________________________
# left censored distribution with mixture of gamma distribution
# _______________________________________________________________
#' # Detection limit
library(HelpersMG)
# Generate 200 random data from a gamma distribution
set.seed(1234)
obc <- c(rgamma(100, scale=10, shape=5), rgamma(100, scale=20, shape=10))
LDL <- 20
l <- seq(from=0, to=LDL, length.out=1001)
p <- pgamma(l, scale=10, shape=5)*0.5+pgamma(l, scale=20, shape=10)
deltal <- l[2]-l[1]
expected_LDL <- sum((l[-1]-deltal/2)*(p[-1]-p[-length(p)]))/sum((p[-1]-p[-length(p)]))
# remove the data below the detection limit
obc[obc<LDL] <- -Inf
UDL <- 300
l <- seq(from=UDL, to=1000, length.out=1001)
p <- pgamma(l, scale=10, shape=5)*0.5+pgamma(l, scale=20, shape=10)
deltal <- l[2]-l[1]
expected_UDL <- sum((l[-1]-deltal/2)*(p[-1]-p[-length(p)]))/sum((p[-1]-p[-length(p)]))
obc[obc>UDL] <- +Inf
# search for the parameters the best fit these truncated data
result1_gamma <- cutter(observations=obc, lower_detection_limit=LDL,
upper_detection_limit = UDL,
distribution="gamma",
cut_method="censored", n.iter=5000, debug=0)
result1_normal <- cutter(observations=obc, lower_detection_limit=LDL,
upper_detection_limit = UDL,
distribution="normal",
cut_method="censored", n.iter=5000)
result1_lognormal <- cutter(observations=obc, lower_detection_limit=LDL,
upper_detection_limit = UDL,
distribution="lognormal",
cut_method="censored", n.iter=5000)
result1_Weibull <- cutter(observations=obc, lower_detection_limit=LDL,
upper_detection_limit = UDL,
distribution="Weibull",
cut_method="censored", n.iter=5000)
result1_generalized.gamma <- cutter(observations=obc, lower_detection_limit=LDL,
upper_detection_limit = UDL,
distribution="generalized.gamma",
cut_method="censored", n.iter=5000)
result2_gamma <- cutter(observations=obc, lower_detection_limit=LDL,
upper_detection_limit = UDL,
distribution="gamma",
n.mixture=2,
cut_method="censored", n.iter=5000)
result2_normal <- cutter(observations=obc, lower_detection_limit=LDL,
upper_detection_limit = UDL,
distribution="normal",
n.mixture=2,
cut_method="censored", n.iter=5000)
result2_lognormal <- cutter(observations=obc, lower_detection_limit=LDL,
upper_detection_limit = UDL,
distribution="lognormal",
n.mixture=2,
cut_method="censored", n.iter=5000)
result2_Weibull <- cutter(observations=obc, lower_detection_limit=LDL,
upper_detection_limit = UDL,
distribution="Weibull",
n.mixture=2,
cut_method="censored", n.iter=5000)
result2_generalized.gamma <- cutter(observations=obc, lower_detection_limit=LDL,
upper_detection_limit = UDL,
distribution="generalized.gamma",
n.mixture=2,
cut_method="censored", n.iter=5000)
compare_AIC(nomixture.gamma=result1_gamma,
nomixture.normal=result1_normal,
nomixture.lognormal=result1_lognormal,
nomixture.Weibull=result1_Weibull,
nomixture.generalized.gamma=result1_generalized.gamma,
mixture.gamma=result2_gamma,
mixture.normal=result2_normal,
mixture.lognormal=result2_lognormal,
mixture.Weibull=result2_Weibull,
mixture.generalized.gamma=result2_generalized.gamma)
plot(result2_gamma, xlim=c(0, 600), breaks=seq(from=0, to=600, by=10))
plot(result2_generalized.gamma, xlim=c(0, 600), breaks=seq(from=0, to=600, by=10))
# _______________________________________________________________
# left and right censored distribution
# _______________________________________________________________
# Generate 100 random data from a gamma distribution
obc <- rgamma(100, scale=20, shape=2)
# Detection limit
LDL <- 10
# remove the data below the detection limit
obc[obc<LDL] <- -Inf
# Detection limit
UDL <- 100
# remove the data below the detection limit
obc[obc>UDL] <- +Inf
# search for the parameters the best fit these censored data
result <- cutter(observations=obc, lower_detection_limit=LDL,
upper_detection_limit=UDL,
cut_method="censored")
result
plot(result, xlim=c(0, 150), col.DL=c("black", "grey"),
col.unobserved=c("green", "blue"),
breaks=seq(from=0, to=150, by=10))
# _______________________________________________________________
# Example with two values for lower detection limits
# corresponding at two different methods of detection for example
# with gamma distribution
# _______________________________________________________________
obc <- rgamma(50, scale=20, shape=2)
# Detection limit for sample 1 to 50
LDL1 <- 10
# remove the data below the detection limit
obc[obc<LDL1] <- -Inf
obc2 <- rgamma(50, scale=20, shape=2)
# Detection limit for sample 1 to 50
LDL2 <- 20
# remove the data below the detection limit
obc2[obc2<LDL2] <- -Inf
obc <- c(obc, obc2)
# search for the parameters the best fit these censored data
result <- cutter(observations=obc,
lower_detection_limit=c(rep(LDL1, 50), rep(LDL2, 50)),
cut_method="censored")
result
# It is difficult to choose the best set of colors
plot(result, xlim=c(0, 150), col.dist="red",
col.unobserved=c(rgb(red=1, green=0, blue=0, alpha=0.1),
rgb(red=1, green=0, blue=0, alpha=0.2)),
col.DL=c(rgb(red=0, green=0, blue=1, alpha=0.5),
rgb(red=0, green=0, blue=1, alpha=0.9)),
breaks=seq(from=0, to=200, by=10))
# ________________________________________________________________________
# left censored distribution comparison of normal, lognormal,
# weibull, generalized gamma, and gamma without Bayesian MCMC
# Comparison with Akaike Information Criterion
# ________________________________________________________________________
# Detection limit
DL <- 10
# Generate 100 random data from a gamma distribution
obc <- rgamma(100, scale=20, shape=2)
# remove the data below the detection limit
obc[obc<DL] <- -Inf
result_gamma <- cutter(observations=obc, lower_detection_limit=DL,
cut_method="censored", distribution="gamma",
n.iter=NULL)
plot(result_gamma)
result_lognormal <- cutter(observations=obc, lower_detection_limit=DL,
cut_method="censored", distribution="lognormal",
n.iter=NULL)
plot(result_lognormal)
result_weibull <- cutter(observations=obc, lower_detection_limit=DL,
cut_method="censored", distribution="weibull",
n.iter=NULL)
plot(result_weibull)
result_normal <- cutter(observations=obc, lower_detection_limit=DL,
cut_method="censored", distribution="normal",
n.iter=NULL)
plot(result_normal)
result_generalized.gamma <- cutter(observations=obc, lower_detection_limit=DL,
cut_method="censored", distribution="generalized.gamma",
n.iter=NULL)
plot(result_generalized.gamma)
compare_AIC(gamma=result_gamma,
lognormal=result_lognormal,
normal=result_normal,
Weibull=result_weibull,
Generalized.gamma=result_generalized.gamma)
# ______________________________________________________________________________
# left censored distribution comparison of normal, lognormal,
# weibull, generalized gamma, and gamma
# ______________________________________________________________________________
# Detection limit
DL <- 10
# Generate 100 random data from a gamma distribution
obc <- rgamma(100, scale=20, shape=2)
# remove the data below the detection limit
obc[obc<DL] <- -Inf
# search for the parameters the best fit these truncated data
result_gamma <- cutter(observations=obc, lower_detection_limit=DL,
cut_method="censored", distribution="gamma")
result_gamma
plot(result_gamma, xlim=c(0, 250), breaks=seq(from=0, to=250, by=10))
result_lognormal <- cutter(observations=obc, lower_detection_limit=DL,
cut_method="censored", distribution="lognormal")
result_lognormal
plot(result_lognormal, xlim=c(0, 250), breaks=seq(from=0, to=250, by=10))
result_weibull <- cutter(observations=obc, lower_detection_limit=DL,
cut_method="censored", distribution="weibull")
result_weibull
plot(result_weibull, xlim=c(0, 250), breaks=seq(from=0, to=250, by=10))
result_normal <- cutter(observations=obc, lower_detection_limit=DL,
cut_method="censored", distribution="normal")
result_normal
plot(result_normal, xlim=c(0, 250), breaks=seq(from=0, to=250, by=10))
result_generalized.gamma <- cutter(observations=obc, lower_detection_limit=DL,
cut_method="censored", distribution="generalized.gamma")
result_generalized.gamma
plot(result_generalized.gamma, xlim=c(0, 250), breaks=seq(from=0, to=250, by=10))
# ___________________________________________________________________
# Test for similarity in gamma left censored distribution between two
# datasets
# ___________________________________________________________________
obc1 <- rgamma(100, scale=20, shape=2)
# Detection limit for sample 1 to 50
LDL <- 10
# remove the data below the detection limit
obc1[obc1<LDL] <- -Inf
obc2 <- rgamma(100, scale=10, shape=2)
# remove the data below the detection limit
obc2[obc2<LDL] <- -Inf
# search for the parameters the best fit these censored data
result1 <- cutter(observations=obc1,
distribution="gamma",
lower_detection_limit=LDL,
cut_method="censored", n.iter=NULL)
logLik(result1)
plot(result1, xlim=c(0, 200),
breaks=seq(from=0, to=200, by=10))
result2 <- cutter(observations=obc2,
distribution="gamma",
lower_detection_limit=LDL,
cut_method="censored", n.iter=NULL)
logLik(result2)
plot(result2, xlim=c(0, 200),
breaks=seq(from=0, to=200, by=10))
result_totl <- cutter(observations=c(obc1, obc2),
distribution="gamma",
lower_detection_limit=LDL,
cut_method="censored", n.iter=NULL)
logLik(result_totl)
plot(result_totl, xlim=c(0, 200),
breaks=seq(from=0, to=200, by=10))
compare_AIC(Separate=list(result1, result2),
Common=result_totl, factor.value=1)
compare_BIC(Separate=list(result1, result2),
Common=result_totl, factor.value=1)
# }
Run the code above in your browser using DataLab