简体   繁体   中英

Kolmogorov-Smirnov test using L-moments instead of mle estimates in R

I am trying to perform a KS test to assess the suitability of fitting a Pearson III distribution to my data. Using mle implemented in fitdist from the fitdistrplus package we obtain parameter estimates which can be directly plugged into ks.test :

library(FAdist)
library(fitdistrplus)

> summary(x) #summary of my data vector
Min.   1st Qu.  Median  Mean   3rd Qu.  Max. 
144.8   646.0  1031.0  1130.0  1472.0  4283.0 

fit.p3 <- fitdist(x, "gamma3", start=list(shape=1,scale=100, thres=100))
> fit.p3
Fitting of the distribution ' gamma3 ' by maximum likelihood 
Parameters:
       estimate Std. Error
shape   2.60075  0.2408922
scale 400.45463 28.5769539
thres  88.22411 29.6652668

> ks.test(x, "pgamma3", shape= fit.p3$estimate["shape"],
+         scale = fit.p3$estimate["scale"], thres = fit.p3$estimate["thres"])

One-sample Kolmogorov-Smirnov test

data:  x
D = 0.0328, p-value = 0.2405
alternative hypothesis: two-sided

This works fine.

Aside: I am aware that performing a KS test using parameter estimates from the data invalidates the test. I have left out the simulation procedure I use to work around this to ensure the clarity of my question and simplicity of the code.

Now, calculating L-moments:

library(lmomco)
lmom <- lmom.ub(x)
para <- parpe3(lmom)
> para
$type
[1] "pe3"

$para
         mu       sigma       gamma 
1129.738563  628.035773    1.040752 

$source
[1] "parpe3"

ks.test requires using the pgamma3 function which only accepts shape , scale , and thres arguments. My question is how can I adapt ks.test to use the L-moments rather than the mle estimates?

you can choose one of this solution 1. define function in fitdistrplus using lmomco and then perform the test. in this case you will perform the test directly with (mu,sigma,gamma) 2. estimate the (scale shape location) parameter using lmoment

`library("lmomco")
library("fitdistrplus")
lmompe3 <-lmoms(X, nmom = 5)
parape3 <- parpe3(lmompe3, checklmom=TRUE)
dpe3 <- function(x,mu,sigma,gamma ){pdfpe3(x,list(type="pe3",para=c(mu,sigma,gamma),source="parpe3"))}
ppe3 <- function(q,mu,sigma,gamma ) {cdfpe3(q,list(type="pe3",para=c(mu,sigma,gamma),source="parpe3"))}
qpe3 <- function(p,mu,sigma,gamma ) {quape3(p,list(type="pe3",para=c(mu,sigma,gamma),source="parpe3"))}
ks.test(X, "ppe3",mu, sigma, gamma)
library("nsRFA")
 library("fitdistrplus")
 l_mom <- Lmoments(X) #nsRFA package
 lmompe3 <-lmoms(X, nmom = 5) #Lmomco package

 # 2. Find parameter from lmoment
 parameters <- par.gamma(l_mom[1],l_mom[2],l_mom[4]) #nsRFA package
 # 3. find mu sigma gamma parameter
  mu_sigma_gamma <-     par2mom.gamma(parameters$alfa,parameters$beta,parameters$xi); mu_sigma_gamma 
parape3 <- parpe3(lmompe3, checklmom=TRUE);parape3#Lmomco package
ks.test(X, "pgamma3", shape=alpha,scale=beta,thres=xi)`

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM