2015-07-21 40 views
1

我編寫了這行代碼以獲取IBM股票在當日前一天最後一天收盤價漲跌的跡象。我是quantmod的新手,所以我根據我的R知識編寫了這行代碼,但我得到了一些奇怪的結果。什麼是錯的?我得到了一個奇怪的總結結果和重複的指示。 這裏是我的代碼行:使用quantmod創建新變量後出現了奇怪的結果

> IBM_TRAIN$LastDay_green<- ifelse((lag(IBM_TRAIN$IBM.Open,k=1) < lag(IBM_TRAIN$IBM.Close,k=1)),1,0) 
> summary(IBM_TRAIN$LastDay_green) 
    Index   LastDay_green 
Min. :2007-01-03 Min. :0.0000 
1st Qu.:2008-10-15 1st Qu.:0.0000 
Median :2010-07-21 Median :1.0000 
Mean :2010-07-21 Mean :0.5418 
3rd Qu.:2012-04-24 3rd Qu.:1.0000 
Max. :2014-01-30 Max. :1.0000 
         NA's :1769  
Warning message: 
In data.row.names(row.names, rowsi, i) : 
    some row.names duplicated: 15,17,19,23,25,27,29,33,35,37,39,43,45,47,49,53,55,57,61,63,65,67,71,73,75,77,81,83,85,87,91,93,95,97,101,103,105,107,111,113,115,119,121,123,125,129,131,133,135,139,141,143,145,149,151,153,155,159,161,163,165,169,171,173,175,179,181,183,185,189,191,193,197,199,201,203,207,209,211,213,217,219,221,223,227,229,231,233,237,241,245,247,249,251,255,257,259,261,265,267,269,271,275,277,279,281,285,287,289,291,295,297,299,301,305,307,309,311,315,317,319,321,325,327,329,333,335,337,339,343,345,347,349,353,355,357,359,363,365,367,369,373,375,377,379,383,385,387,389,393,395,397,399,403,405,407,409,413,415,417,419,423,425,427,429,433,435,441,443,445,447,451,453,455,457,461,463,465,467,471,473,475,477,483,485,491,493,497,499,501,503,507,509,511,513,517,519,521,525,527,529,531,535,537,539,541,545,547,549,551,555,557,559,563,565,567,569,573,575,577,579,583,585,587,589,593,595,597,601,603,605,607,611,613,615,617,621,623,625,627,631,633,635,637,641,643,645,647,651,653,655,657 [... truncated] 
+1

請提供[再現的示例](http://stackoverflow.com/q/5963269/271616),幷包括從'的輸出sessionInfo'。 –

+1

我試着用'getSymbols(「IBM」)獲得的數據運行你的代碼版本,並且它工作正常。我想知道這是不是你的訓練/測試分裂過程的人爲因素? – ulfelder

+0

謝謝ulfelder.That是問題所在。但我仍然不明白這個字段有兩個摘要(Index&regular summary),爲什麼我不能得到str()函數的正常輸出? – mql4beginner

回答

1

我不能評論所以加入這個作爲一個答案。 索引摘要是因爲第一次操作的結果是XTS。

我認爲這應該給摘要僅colums值 -

summary(coredata(IBM_TRAIN$LastDay_green)) 
1
library(quantmod) 
#get stock data 
getSymbols("IBM") 


#Show last two days of the series 
    tail(IBM, n=2) 

#series will show you the change day to day. 
dailyReturn(IBM)) 



      daily.returns 
2015-07-13 0.0145553042 
2015-07-14 -0.0045460147 
2015-07-15 -0.0004744796 
2015-07-16 0.0146561503 
2015-07-17 **0.0088303801** 
2015-07-20 **0.0041157383** 
+0

這很好,但我正在研究構建自己的信號所以我想了解我的代碼行有什麼問題。 – mql4beginner