2

別のR初心者の質問があります...

次のコードをベクトル化 (for ループインを回避) するにはどうすればよいですか。

# algorithm for getting entry prices (when signal > 0): look back from current
# position until you find first signal > 0,
# `mktdataclose` at that time is entry price
# `entryPrices` is an xts object representing entry prices
# if entryPrices are not available (is.null == TRUE) then wee need to reconstruct
# them from signal (xts object with 1 when entry signal triggered and 0 
# otherwise) and close prices available in mktdataclose (an xts object with the
# same length as signal and same dates just that it represents closing prices)

EntryPrices <- entryPrices
if (is.null(EntryPrices)) {
    # get entryprices as close prices on buy signal
    EntryPrices <- ifelse(signal > 0, mktdataclose, 0)
    entryPrice <- 0
    for (i in 1:NROW(signal)) {
        if (signal[i] > 0) entryPrice <- mktdataclose[i]
        EntryPrices[i] <- entryPrice
    }
}

私はSASデータのステップ方法を考えることに行き詰まっており、保持などを探している絶望的な.sapplyなどを理解するためにいくつかの簡単な例を見つけることができます.

ご親切にありがとうございました。

最高、サモ。

4

3 に答える 3

3

私が正しく理解していれば、あなたの問題は次のとおりです。長さの と の 2 つのベクトルがsignalあり、前回の値がtimeまたはそれ以前に 1 になるように、長さの新しいベクトルを作成したいと考えています。for ループを使用せずに、予想外に役立つことが多い関数を使用して、これを行うことができます (この質問は、この関数と を使用して同様に解決された以前の質問のいくつかと風味が似ていることに注意してください)。ここで、Gavin のデータを使用します。mktdataclosenEntryPricesnmktdataclose[i]mktdataclosesignalicummaxcumsum

set.seed(123)
signal <- sample(0:1, 10, replace = TRUE)
mktdataclose <- runif(10, 1, 10)  

私たちの問題はsignal、ベクトルを適切なインデックスのベクトルに変換することです。

indices <- cummax( seq_along(signal) * signal)

0 を除いて、これはまさにindices私たちが望むものです。次に、からEntryPrices非ゼロの値を抽出して設定します。indicesmktdataclose

EntryPrices <- c( rep(0, sum(indices==0)), mktdataclose[ indices ])

>   cbind(signal, indices, mktdataclose, EntryPrices)
      signal indices mktdataclose EntryPrices
 [1,]      0       0     9.611500    0.000000
 [2,]      1       2     5.080007    5.080007
 [3,]      0       2     7.098136    5.080007
 [4,]      1       4     6.153701    6.153701
 [5,]      1       5     1.926322    1.926322
 [6,]      0       5     9.098425    1.926322
 [7,]      1       7     3.214790    3.214790
 [8,]      1       8     1.378536    1.378536
 [9,]      1       9     3.951286    3.951286
[10,]      0       9     9.590533    3.951286
于 2011-03-19T15:06:37.693 に答える
0

信号は 0 と 1 であるため、次のようにベクトル化できると思います。

EntryPrices * signal
于 2011-03-19T12:14:43.723 に答える
0

より簡単に見つけることができる別の解決策を次に示します。Prasad の疑似データを使用しています。

> EntryPrices <- ifelse(signal > 0, mktdataclose, NA)
> EntryPrices <- na.locf(EntryPrices, na.rm=FALSE)
> cbind(signal,mktdataclose,EntryPrices)
      signal mktdataclose EntryPrices
 [1,]      0     9.611500          NA
 [2,]      1     5.080007    5.080007
 [3,]      0     7.098136    5.080007
 [4,]      1     6.153701    6.153701
 [5,]      1     1.926322    1.926322
 [6,]      0     9.098425    1.926322
 [7,]      1     3.214790    3.214790
 [8,]      1     1.378536    1.378536
 [9,]      1     3.951286    3.951286
[10,]      0     9.590533    3.951286
于 2011-03-22T11:56:59.687 に答える