I am trying to determine which model is better, whether it is the Weibull model or the truncated Weibull model, using the Kolmogorov-Smirnov test. I use the following code:
dataaa <- c(7.40, 6.50, 3.80, 4.3, 4.5, 2, 5.30, 8.20, 9, 6.5, 5, 5.2, 10, 3.2, 3.5, 5.2, 3.1)
length(dataaa)
# estimate Weibull parameters
library('EnvStats')
eweibull(dataaa, method = "mle")
# I find shape = 2.73 and scale = 6.14.
# Fitting truncated Weibull
library(cmdstanr)
library(ggplot2)
set.seed(20220118)
N <- 17 # Number of observations
shape <- 2.73 # shape parameter of Weibull distribution
scale <- 6.14 # scale parameter of Weibull distribution
L <- 5 # truncation point
D <- rweibull(N, shape, scale)
Dt <- D[D > L]
ggplot(data.frame(D = D, trunc = (D <= L))) +
geom_histogram(aes(x = D, fill = trunc), binwidth = 1, boundary = 0)
set_cmdstan_path(path = NULL)
cmdstan_path()
model1 <- cmdstan_model("weibull_truncated.stan")
fit1 <- model1$sample(list(N = length(Dt), L = L, Y = Dt),
seed = 1,
iter_warmup = 2000, iter_sampling = 2000,
refresh = 400,
chains = 4, parallel_chains = 4)
fit1$summary(c("alpha", "sigma"))
# Using a truncated weibull, I find shape = 2.74, scale =6.21.
#Perform KS test for Weibull test
ks_test_result <- ks.test(dataaa, "pweibull", shape = 2.73, scale = 6.14) #Perform a KS test comparing the data to a Weibull distribution with the estimated parameters
But I couldn’t use KS test for truncated Weibull.
I am looking forward for your help.
Edited by @jsocolar for R syntax highlighting.