2012-07-18 9 views
7

私はChebyshevフィルターを実装して時系列を滑らかにしようとしていますが、残念ながらデータシリーズにはNAsがあります。例えばNフィルターを扱うRフィルター()

t <- seq(0, 1, len = 100)      
x <- c(sin(2*pi*t*2.3) + 0.25*rnorm(length(t)),NA, cos(2*pi*t*2.3) + 0.25*rnorm(length(t))) 

私は、チェビシェフフィルタを使用しています:cf1 = cheby1(5, 3, 1/44, type = "low")

私はシリーズが台無し注文/位置のNAを除外ではなく、時間をフィルタ処理しようとしています。だから、私はすでにna.rm=Tを試しましたが、そのような議論はないようです。 次に

z <- filter(cf1, x) # apply filter 

ありがとうございます。

答えて

1

compelete.cases機能を使用して、事前にNAsを削除できます。欠落しているデータを代入することも考えられます。 mtsdiまたはAmelia IIパッケージをチェックしてください。

EDIT:

はここRcppとソリューションです。 tsオブジェクトが欠損値を持つことができる場合

require(inline) 
require(Rcpp) 
t <- seq(0, 1, len = 100) 
set.seed(7337) 
x <- c(sin(2*pi*t*2.3) + 0.25*rnorm(length(t)),NA, cos(2*pi*t*2.3) + 0.25*rnorm(length(t))) 
NAs <- x 
x2 <- x[!is.na(x)] 
#do something to x2 
src <- ' 
Rcpp::NumericVector vecX(vx); 
Rcpp::NumericVector vecNA(vNA); 
int j = 0; //counter for vx 
for (int i=0;i<vecNA.size();i++) { 
    if (!(R_IsNA(vecNA[i]))) { 
    //replace and update j 
    vecNA[i] = vecX[j]; 
    j++; 
    } 
} 
return Rcpp::wrap(vecNA); 
' 
fun <- cxxfunction(signature(vx="numeric", 
          vNA="numeric"), 
        src,plugin="Rcpp") 
if (identical(x,fun(x2,NAs))) 
    print("worked") 
# [1] "worked" 
+0

complete.caseがna.omitと同じであるかどうか疑問に思っています。また、私は観測されたSSTの時系列を使用しているので、それが欠けている値を入力することは良い考えであるかどうかはわかりません。 –

+0

このアップデートがこの問題を解決することを願っています。 – chandler

2

x <- x[!is.na(x)]を使用してNAsを削除してからフィルタを実行してください。

+0

申し訳ありませんが、私はna.omitを使用している場合、それは大量発注まで、私はちょうどNAを、残りのフィルタの後、NAの値をお勧めしますが、他のすべての非NA値を渡すことができます。 –

+0

申し訳ありませんが、私はあなたが何を求めているのか分かりません。値を同じ位置に保ち、データにスペースを入れたいですか? –

+0

tseriesパッケージのna.remove()またはna.remove.ts()は、必要な処理を行いますか? –

1

私は知らないが、あなただけのNA値を再挿入したい場合は、あなたがR.utilsから?insertを使用することができます。これはスピードが重要です役に立つかもしれません。これを行うより良い方法があるかもしれません。

install.packages(c('R.utils', 'signal')) 
require(R.utils) 
require(signal) 
t <- seq(0, 1, len = 100)      
set.seed(7337) 
x <- c(sin(2*pi*t*2.3) + 0.25*rnorm(length(t)), NA, NA, cos(2*pi*t*2.3) + 0.25*rnorm(length(t)), NA) 
cf1 = cheby1(5, 3, 1/44, type = "low") 
xex <- na.omit(x) 
z <- filter(cf1, xex) # apply 
z <- as.numeric(z) 
for (m in attributes(xex)$na.action) { 
    z <- insert(z, ats = m, values = NA) 
} 
all.equal(is.na(z), is.na(x)) 
?insert 
0

ここには、NAsを含む信号をフィルタリングするために使用できる関数があります。 NAsはゼロではなく無視されます。

次に、NAsがフィルタリングされた信号の任意のポイントで取ることができる重みの最大パーセンテージを指定できます。特定のポイントにNAs(および実際のデータが少なすぎる)がある場合、フィルタリングされた信号自体はNAに設定されます。

# This function applies a filter to a time series with potentially missing data 

filter_with_NA <- function(x, 
          window_length=12,       # will be applied centrally 
          myfilter=rep(1/window_length,window_length), # a boxcar filter by default 
          max_percentage_NA=25)      # which percentage of weight created by NA should not be exceeded 
{ 
    # make the signal longer at both sides 
    signal <- c(rep(NA,window_length),x,rep(NA,window_length)) 
    # see where data are present and not NA 
    present <- is.finite(signal) 

    # replace the NA values by zero 
    signal[!is.finite(signal)] <- 0 
    # apply the filter 
    filtered_signal <- as.numeric(filter(signal,myfilter, sides=2)) 

    # find out which percentage of the filtered signal was created by non-NA values 
    # this is easy because the filter is linear 
    original_weight <- as.numeric(filter(present,myfilter, sides=2)) 
    # where this is lower than one, the signal is now artificially smaller 
    # because we added zeros - compensate that 
    filtered_signal <- filtered_signal/original_weight 
    # but where there are too few values present, discard the signal 
    filtered_signal[100*(1-original_weight) > max_percentage_NA] <- NA 

    # cut away the padding to left and right which we previously inserted 
    filtered_signal <- filtered_signal[((window_length+1):(window_length+length(x)))] 
    return(filtered_signal) 
}