R set outliers in time series NA in series already containing NA -


i have time series containing nas , sudden jumps this:

input=c(1:5, na, 6:7,0,9:12) 

in 7,0,9 considered jump 0 should replaced na.

i remove first value in sudden jump (with set value of qualifies jump, in example change > 1) occurs , set na

the output example should this:

output=c(1:5,na,6:7,na,9:12) 

i want set outliers na, not want overwrite remaining values. jump can both negative , positive.

problems encountered:

  1. the value after existing na value being counted jump
  2. the "jump back" after outlier being counted jump

both of resulted in more necessary nas, try keep original data possible.

any ideas? have been stuck while. in advance!

there 3 situations similar require different degrees of difficulties in terms of exceptions:

situation 1

if pattern jumps 1-increase couple of interruptions, create vector_check resembles perfect vector. in input deviates should set na:

vector_check <- min(input):max(input) inds         <- vector_check != input input[inds]  <- na 

situation 2

if pattern less predictable , wish 'irregular' pattern, you'll more complicated situation. possible solution create while-loop checks increments larger 2 (or whatever value seems sensible) , replaces problematic location bump_inds na. here assume outlier creates 2 large increments: 1 because value drops (increases) , 1 because rises (drops down) old value. process proceeds until no problematic locations remain:

bump_ind <- rep(0, 3)  while(length(bump_ind) > 1){   bump_ind        <- which( abs(diff(input)) > 2 )   input[bump_ind[2]] <- na }  input # [1]  1  2  3  4  5 na  6  7 na  9 10 11 12 

situation3

a third option, based on real data sensor shows data not have jump previous level:

input    <- c(20.2,20.2,20.2,20.2,20.1,20.2,20.2,20.1,20.2, 20.2,20.2,20.2,17.7,               18.9,19.3,19.4,19.4,19.4,19.5,19.5,19.5) bump_ind <- rep(0, 3)  while(length(bump_ind) > 1){   bump_ind        <- which( abs(diff(input)) > 2 )   if(length(bump_ind) > 2){     bump_ind <- bump_ind[1:2]   }   if( length(bump_ind) == 1 ){       input[bump_ind[1] + 1] <- na   } else if( diff(bump_ind > 1) ){       input[bump_ind[1] + 1] <- na   } else{       input[bump_ind[2]] <- na   } }  input # [1] 20.2 20.2 20.2 20.2 20.1 20.2 20.2 20.1 20.2 20.2 20.2 20.2   na 18.9 19.3 # [16] 19.4 19.4 19.4 19.5 19.5 19.5 

Comments

Popular posts from this blog

android - InAppBilling registering BroadcastReceiver in AndroidManifest -

python Tkinter Capturing keyboard events save as one single string -

sql server - Why does Linq-to-SQL add unnecessary COUNT()? -