Advertisement
Guest User

Untitled

a guest
Jan 15th, 2019
60
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
R 2.07 KB | None | 0 0
  1. gstadf.test <- function(y, r0 = 0.01 + 1.8/sqrt(length(y)), cnst = TRUE, test_type = "sadf"){
  2.   result <- list()
  3.   result$y <- y
  4.   result$r0 <- r0
  5.   result$cnst <- cnst
  6.   result$test_type <- test_type
  7.  
  8.   # Часть 1. Оценка NW.
  9.   y_0 <- y - y[1]
  10.   N <- length(y)
  11.   tau_l <- 0.1
  12.   kerU <- function(x){
  13.     result <- c()
  14.     for(i in 1:length(x)){
  15.       if((x[i] < 0) & (x[i] >= -1)){
  16.         result[i] <- 1
  17.       } else {
  18.         result[i] <- 0
  19.       }
  20.     }
  21.     return(result)
  22.   }
  23.   my <- diff(y_0)
  24.   mx <- y_0[1:(length(y_0)-1)]
  25.   HT <- seq(N^(-0.5), N^(-0.1), by=0.01)
  26.   rx <- (1:N)/N
  27.   TSCV <- Inf
  28.   for(hi in 1:length(HT)){
  29.     h1 <- HT[hi]
  30.     rr1 <- rep(0, length(my))
  31.     for(k in floor(tau_l*N):length(my)){
  32.       z1 <- kerU((rx[k]-rx)/h1)
  33.       z1 <- z1[2:length(z1)]
  34.       rr1[k] <- sum(mx*z1*my)/sum(mx*z1*mx)
  35.     }
  36.     u <- my - rr1*mx
  37.     ee <- u[floor(tau_l*N):length(u)]
  38.     TSCV1 <- mean(ee^2)
  39.     if(TSCV1 < TSCV){
  40.       TSCV <- TSCV1
  41.       u_hat <- u
  42.       h <- h1
  43.     }
  44.   }
  45.   result$u_hat <- u_hat
  46.   result$h <- h
  47.   result$w_sq <- mean(u_hat^2)
  48.  
  49.   # Часть 2. Переиндексация.
  50.   new_index <- reindex(u_hat)
  51.   y_tt <- y[(new_index$new_index + 1)]
  52.   if(cnst == TRUE){
  53.     y_tt <- y_tt - mean(y_tt)
  54.   }
  55.  
  56.   # Часть 3. Сам тест.
  57.   result$t_values <- c()
  58.   if(test_type == "sadf"){
  59.     for(i in floor(r0*length(y_tt)):length(y_tt)){
  60.       result$t_values[(i - floor(r0*length(y_tt)) + 1)] <-
  61.         (y_tt[i]^2 - y_tt[1]^2 - result$w_sq*(i-1)/N)/(result$w_sq^0.5*2*sum(y_tt[1:(i-1)]^2)^0.5)
  62.     }
  63.     result$sadf_value <- max(result$t_values)
  64.     if(length(y_tt)-1 == 100){
  65.       result$cr_value <- 1.37
  66.     } else if(length(y_tt)-1 == 200){
  67.       result$cr_value <- 1.41
  68.     } else if(length(y_tt)-1 == 400){
  69.       result$cr_value <- 1.49
  70.     } else if(length(y_tt)-1 == 800){
  71.       result$cr_value <- 1.51
  72.     } else if(length(y_tt)-1 == 1600){
  73.       result$cr_value <- 1.51
  74.     }
  75.     result$is_explosive <- ifelse(result$sadf_value > result$cr_value, 1, 0)
  76.   }
  77.   return(result)
  78. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement