ks.test中的错误来自于累积分布函数.由于离散分布在R中没有内置函数,因此该函数不为真



我有这个离散数据,我想做一个样本kolmogorov smirnov测试,但当我运行以下代码时,它会给我一个错误

d <- c(5, 11, 21, 31, 46, 75, 98, 122, 145, 165, 196, 224, 245, 293,321, 330, 350, 420)
#log likelihood function for the discrete distribution
#*********************************************************
loglik <-function(param){   
q <- param[1]
if(q<=0){return(NaN)}
b <- param[2]
if(b<=0){return(NaN)}
c <- param[3]
if(c<=0){return(NaN)}
sum(log((q^(sqrt(d)*(1+b*c^d)))-(q^(sqrt(d+1)*(1+b*c^(d+1)))))) 
}
# maximum likelihood estimation using maxLik function
#*****************************************************
library(maxLik)
mle <- maxLik(loglik, start=c(q=0.9658,b=0.1237,c=1.0086), control=list(printLevel=2)) 

# the cumulative distribution function of the discrete distribution
#******************************************************************* 
cdf <- function(param){    
if(q<=0){return(NaN)}
b <- param[2]
if(b<=0){return(NaN)}
c <- param[3]
if(c<=0){return(NaN)}

cdf= 1-q^(sqrt(d)(1+b*c^d))
}
#one-sample kolmogorov smirnov test
#**********************************
ks<- ks.test(d, cdf, q=coef(mle)[1], b=coef(mle)[2], c= coef(mle)[3] ) 

错误:

Error in y(sort(x), ...) : 
unused arguments (q = coef(mle)[1], b = coef(mle)[2], c = coef(mle)[3])

当我尝试测试cdf功能时,

cdf(q=2,b=3,c=3)

R给我以下错误

Error in cdf(q = 2, b = 3, c = 3) : 
unused arguments (q = 2, b = 3, c = 3)

我认为ks.test的错误来自于错误的累积分布函数。

问题中的代码有几个错误,包括向cdf传递了错误数量的参数。

d <- c(5, 11, 21, 31, 46, 75, 98, 122, 145, 165, 196, 224, 245, 293,321, 330, 350, 420)
# log likelihood function for the discrete distribution
loglik <-function(param){   
if(any(param <= 0)){
NaN
} else {
q <- param[1]
b <- param[2]
c <- param[3]
sum(log((q^(sqrt(d)*(1+b*c^d)))-(q^(sqrt(d+1)*(1+b*c^(d+1)))))) 
}
}
# maximum likelihood estimation using maxLik function
library(maxLik)
start_param <- c(q = 0.9658, b = 0.1237, c = 1.0086)
mle <- maxLik(loglik, start = start_param, control=list(printLevel=2)) 

# the cumulative distribution function of the discrete distribution
cdf <- function(d, param){    
if(any(param <= 0)){
NaN
} else {
q <- param[1]
b <- param[2]
c <- param[3]
1 - q^(sqrt(d)*(1+b*c^d))
}
}
#one-sample kolmogorov smirnov test
ks <- ks.test(d, "cdf", coef(mle)) 
#
#   One-sample Kolmogorov-Smirnov test
#
#data:  d
#D = 0.080566, p-value = 0.9991
#alternative hypothesis: two-sided

最新更新