0

我有这个离散数据,我想做一个样本 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 中的错误来自错误的累积分布函数。

4

1 回答 1

0

问题中的代码有几个错误,包括将错误数量的参数传递给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
于 2020-10-13T19:51:49.127 回答