→ celestialgod:你的時間列式ROW NAMES吧... 11/07 19:30
→ Wush978:try `strptime(row.names(x))` 11/07 21:58
>strptime(row.names(time),"%y-%m-%d")
出現character(0),跑code一樣出現
錯誤在xts: order.by requires an appropriate time-based object
應該是要轉換成POSIXt或POSIXct,因為當初從網上抓資料下來有一行code是
x=c("POSIXt","POSIXct"),網上抓下來的資料就可以直接跑沒問題。
不知道為什麼存檔後再讀入檔案就沒辦法跑。
但是我用as.POSIXct(row.names=("time"))
會出現錯誤:缺少引數 "x",也沒有預設值
不知道怎麼改,問題出在哪。
→ Wush978:可以po一下`dump(x, file="x.R")`的結果嗎? 11/08 13:28
→ Wush978:sorry, 應該是dump("x", file="x.R") 11/08 13:29
po在上面
→ celestialgod:x<-read.table("s.txt",header=T); 11/08 15:13
→ celestialgod:time = strptime(row.names(x), "%Y-%m-%d") 11/08 15:13
→ celestialgod:原po,你INPUT的變數是錯的... 11/08 15:13
但是改過再跑一樣出現error,直接用R從網路上抓的資料就能跑。不知道問題出在哪
→ celestialgod:我的可以 11/08 20:04
我是指改過雖然成功了,但跑我的code出現同樣的error。
→ celestialgod:你的資料要不要PO一小段上來看看 11/08 22:29
妳是說原始檔嗎?
推 Wush978:"%Y", 不是"%y". 我用strptime(row.names(x),"%Y-%m-%d") 11/08 23:30
我是用這個code下去跑的,雖然沒顯示error 但是之後跑我的code出現同樣的error in xts
→ celestialgod:%Y有成功,%y失敗 11/08 23:54
→ Wush978:Hmm... 之後什麼code? 11/09 00:42
http://ideone.com/prg5vf
跑到最後一行時會出現error in xts(x = vec.ret, order.by = index(xts.ret)):
order.by requires an appropriate time-based object
http://stackoverflow.com/questions/9757430/xts-error-order-by-requires-an-appropriate-time-based-object
這是我參考外國網站有人跟我遇到一樣的問題,可是還是沒辦法解決。
→ celestialgod:原PO把你的資料取前三行跟rowname, colname列出來 11/09 01:58
→ celestialgod:可能前十列就夠了 11/09 01:58
→ celestialgod:這樣才能知道為什麼套到你的資料無法使用 11/09 01:58
https://www.dropbox.com/s/bq5jw1tolvscjlv/s.txt
這是我的測試檔
→ celestialgod:你的資料第一行才是時間,不是rowname, 11/10 04:05
→ celestialgod:因此,應該是用time = strptime(x[,1], "%Y-%m-%d") 11/10 04:05
可是strptime這個函數是把時間轉換成y-m-d格式,我的資料本身已經是y-m-d格式了。
→ celestialgod:抱歉,我誤會了,可是我用rowname也是有成功的 11/10 04:07
→ celestialgod:你的CODE恩..太多,實在沒時間幫你看... 11/10 04:08
那請問有其他發問平台,可以讓我問問題嗎?
因為我這問題自己解決不了,可是又一定要解決。
這是我dump用R抓下來的資料,
structure(c(23.59, 22.28, 25.39, 24.86, 23.51, 22.24, 22.59,...), .Dim = c(167L, 4L),
.Dimnames = list(NULL, c("XLY.Adjusted","XLP.Adjusted", "XLE.Adjusted", "XLF.Adjusted")),
index = structure(c(949276800,951782400, 954460800, 957052800, 959731200,
962323200, 965001600,967680000, 970272000, 972950400, 975542400,... ),
tclass ="Date"), class = c("xts", "zoo"),
.indexCLASS = "Date", .indexFORMAT = "%Y-%m-%d",
tclass = "Date",tzone = "UTC", src = "yahoo",
updated = structure(1384070717.82835, class = c("POSIXct", "POSIXt")))
差別應該是class不同,當初儲存檔案時把他改成data.frame
不知道在怎麼把檔案的class從data.frame轉換成xts應該才是問題所在。
※ 編輯: goodparent1 來自: 114.37.240.23 (11/10 21:47)
推 Wush978:你可以參考 ?xts 中的範例 11/10 23:32
→ Wush978:裡面有把sample_matrix轉換成xts的過程 11/10 23:32
→ Wush978:發問平台可以參考置底文章,裡面有列一些 11/10 23:33
→ Wush978:我今天弄好Rstudio後看了一下,問題大概在monthly.returns 11/11 11:24
推 Wush978:你可能ROC函數要研究一下 11/11 11:25
→ Wush978:你的問題其實滿複雜的,你的問法也要讓別人花很多時間找真 11/11 11:25
→ Wush978:正的問題,所以除非運氣好遇到剛好有相關經驗的網友,否則 11/11 11:25
→ Wush978:找到你可以接受答案可能機會不大... 11/11 11:26
→ Wush978:btw, 最後一行改成xts.ret = as.xts(monthly.returns)可跑 11/11 11:32
→ Wush978:但是我完全不知道跑出來的答案是不是對的,所以請自己小心 11/11 11:33
推 celestialgod:版主用心! 11/11 12:15
真的非常謝謝兩位幫忙,真的感恩。我在自己研究進修一下。
※ 編輯: goodparent1 來自: 134.208.22.38 (11/11 13:50)