Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- gstadf.test <- function(y, r0 = 0.01 + 1.8/sqrt(length(y)), cnst = TRUE, test_type = "sadf"){
- result <- list()
- result$y <- y
- result$r0 <- r0
- result$cnst <- cnst
- result$test_type <- test_type
- # Часть 1. Оценка NW.
- y_0 <- y - y[1]
- N <- length(y)
- tau_l <- 0.1
- kerU <- function(x){
- result <- c()
- for(i in 1:length(x)){
- if((x[i] < 0) & (x[i] >= -1)){
- result[i] <- 1
- } else {
- result[i] <- 0
- }
- }
- return(result)
- }
- my <- diff(y_0)
- mx <- y_0[1:(length(y_0)-1)]
- HT <- seq(N^(-0.5), N^(-0.1), by=0.01)
- rx <- (1:N)/N
- TSCV <- Inf
- for(hi in 1:length(HT)){
- h1 <- HT[hi]
- rr1 <- rep(0, length(my))
- for(k in floor(tau_l*N):length(my)){
- z1 <- kerU((rx[k]-rx)/h1)
- z1 <- z1[2:length(z1)]
- rr1[k] <- sum(mx*z1*my)/sum(mx*z1*mx)
- }
- u <- my - rr1*mx
- ee <- u[floor(tau_l*N):length(u)]
- TSCV1 <- mean(ee^2)
- if(TSCV1 < TSCV){
- TSCV <- TSCV1
- u_hat <- u
- h <- h1
- }
- }
- result$u_hat <- u_hat
- result$h <- h
- result$w_sq <- mean(u_hat^2)
- # Часть 2. Переиндексация.
- new_index <- reindex(u_hat)
- y_tt <- y[(new_index$new_index + 1)]
- if(cnst == TRUE){
- y_tt <- y_tt - mean(y_tt)
- }
- # Часть 3. Сам тест.
- result$t_values <- c()
- if(test_type == "sadf"){
- for(i in floor(r0*length(y_tt)):length(y_tt)){
- result$t_values[(i - floor(r0*length(y_tt)) + 1)] <-
- (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)
- }
- result$sadf_value <- max(result$t_values)
- if(length(y_tt)-1 == 100){
- result$cr_value <- 1.37
- } else if(length(y_tt)-1 == 200){
- result$cr_value <- 1.41
- } else if(length(y_tt)-1 == 400){
- result$cr_value <- 1.49
- } else if(length(y_tt)-1 == 800){
- result$cr_value <- 1.51
- } else if(length(y_tt)-1 == 1600){
- result$cr_value <- 1.51
- }
- result$is_explosive <- ifelse(result$sadf_value > result$cr_value, 1, 0)
- }
- return(result)
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement