Implementation Kołmogorow-Smirnow in R

74 Views Asked by At

I have a problem with implementing a Kołmogorow-Smirnow test for below problem:

Let 1, … , and 1, … , be independent random samples from the distributions (, ^2) and (, ^2) respectively. Consider test with size = 0.05 for the hypothesis 0: ≤ against the alternative 1: > — the Kolmogorov-Smirnov test for stochastic ordering. Draw on a single plot the empirical power curve for = 60, = 70, = 5, ^2 = 1, depending on . Assume = 4, 4.02, 4.04, … , 6.

Here is my code:

mc <- 10000
MC <- 10000
alpha <- 0.05
mx <- 5  
sigma_sq <- 1  
s <- seq(4, 6, by=0.02)  
MW <- numeric()
MK <- numeric()

for (k in 1:length(s)) {
  for (j in 1:mc) {
    X <- rnorm(60, mx, sqrt(sigma_sq))
    Y <- rnorm(70, s[k], sqrt(sigma_sq))
    nx <- length(X)
    ny <- length(Y)
    Z <- c(X, Y)
    T <- c()
    for (i in 1:nx) {
      T[i] <- -1/nx
    }
    for (i in (nx + 1):(nx + ny)) {
      T[i] <- 1/ny
    }
    V <- cbind(Z, T)
    V <- V[order(V[, 1], decreasing=FALSE), ]
    SP <- cumsum(V[, 2])
    SN <- -cumsum(V[, 2])
    KSP <- max(SP)
    KSN <- max(SN)
    KSPMC <- numeric(MC)
    KSNMC <- numeric(MC)
    for (i in 1:MC) { 
      ZMC <- runif(length(s), 0, 1) 
      TMC <- ifelse(seq_along(s) <= nx, -1/nx, 1/ny) 
      VMC <- cbind(ZMC, TMC) 
      VMC <- VMC[order(VMC[, 1], decreasing=FALSE), ] 
      SPMC <- cumsum(VMC[, 2])
      SNMC <- -cumsum(VMC[, 2])
      KSPMC[i] <- max(SPMC)
      KSNMC[i] <- max(SNMC)
    } 
    MW <- c(MW, mean(KSPMC > KSP))
    MK <- c(MK, mean(KSNMC > KSN))
  }
}

plot(MK, col="blue")

Could you please tell me what I'm doing wrong and help me correct my code?

By using ks.test I get this red line: https://i.stack.imgur.com/ERaNC.png

but from my code I get totally different picture:

https://i.stack.imgur.com/od5SZ.png

0

There are 0 best solutions below