2011-11-17 29 views
2

......在整個季度中,樣條線(最好)或線性插值(OK)或重複值(罰款)。問題是我不知道如何將getFin()viewFin()返回的數據類型轉換爲timeSeries類型可用的數據類型。這裏是我的代碼:如何將quantmod的viewfin函數返回的季度數據轉換爲月度時間序列?

getFin('F') 
x <- viewFin(F.f, "BS", period="Q")["Total Common Shares Outstanding",]*1000 

我期望的輸出是

> x 
GMT  x.ts 
2011-09-01 3816000 
2011-08-01 3816000 
2011-07-01 3816000 
2011-06-01 3815000 
2011-05-01 3815000 
2011-04-01 3815000 
2011-03-01 3813000 
2011-02-01 3813000 
2011-01-01 3813000 
2010-12-01 3778000 
2010-11-01 3778000 
2010-10-01 3778000 
2010-09-01 3484000 

然而,這裏是一些實際的輸出:

> x 
2011-09-30 2011-06-30 2011-03-31 2010-12-31 2010-09-30 
    3816000 3815000 3813000 3778000 3484000 
> str(x) 
Named num [1:5] 3816000 3815000 3813000 3778000 3484000 
- attr(*, "names")= chr [1:5] "2011-09-30" "2011-06-30" "2011-03-31" "2010-12-31" ... 

它看起來像x對象是在一些奇怪的反向格式,其中鍵是數字值,並且該值是日期的字符串。當我嘗試提取日期或數字組件時,我無法隔離數字部分以生成時間序列對象。

理想的情況下,去我的期望的輸出,我就可以說

mydates <- timeSequence(from = "2011-01-01", to=Sys.Date(), by = "month") 
series <- timeSeries(x$data, mydates) 

但我似乎無法提取數字數據部分。

UPDATE

herehere,我適應了下面的代碼:

getFin('F') 
x <- viewFin(F.f, "BS", period="Q")["Total Common Shares Outstanding",]*1000 
zoox = zoo(x, order.by=as.Date(names(x))) 
x2 <- na.spline(merge(zoox, foo=zoo(NA, order.by=seq(start(zoox), end(zoox), "month")))[, 1]) 

然而,我的輸出軋液的日期了一下,打亂了插值:

>x2 
2010-09-30 2010-10-30 2010-11-30 2010-12-30 2010-12-31 2011-01-30 2011-03-02 
    3484000 3623591 3720509 3776671 3778000 3804738 3813071 
2011-03-30 2011-03-31 2011-04-30 2011-05-30 2011-06-30 2011-07-30 2011-08-30 
    3813025 3813000 3813100 3814116 3815000 3814976 3814884 
2011-09-30 
    3816000 

正如你所看到的,我有博12-30日和12-31日,2011年3月的3個值,但沒有2月等。如何解決這個問題?

+2

歡迎來到Stackoverflow,你可以請你的答案作爲答案,而不是將其添加到問題?這將有助於未來的訪問者解決這個問題。 – Stedy

回答

0

更新2

請張貼一個更好的答案!這實在是太醜了,但在這裏就是我得到了一些可以接受的:

getFin('F') 
x <- viewFin(F.f, "BS", period="Q")["Total Common Shares Outstanding",]*1000 
zoox = zoo(x, order.by=as.Date(names(x))) 
foo=zoo(NA, order.by=seq(as.Date(as.character(timeFirstDayInMonth(start(zoox)))), as.Date(as.character(timeFirstDayInMonth(end(zoox)))), "month")) 
foo2 <- na.approx(merge(zoox, foo)[, 1]) 
fx <- merge(foo2,foo, all=FALSE)[,1] 

它轉換

> x 
2011-09-30 2011-06-30 2011-03-31 2010-12-31 2010-09-30 
    3816000 3815000 3813000 3778000 3484000 

> fx 
2010-10-01 2010-11-01 2010-12-01 2011-01-01 2011-02-01 2011-03-01 2011-04-01 
    3487196 3586261 3682130 3778389 3790444 3801333 3813022 
2011-05-01 2011-06-01 2011-07-01 2011-08-01 2011-09-01 
    3813681 3814363 3815011 3815348 3815685 

我覺得這太醜陋是真實的,所以我張貼此回答,但希望別人張貼一個人。

1

我剛剛有同樣的問題 - 我寫了兩個函數來實現它。

首先,你需要一個日期函數來移動日期,因爲你可能想要月結束日期,但是R更簡單地處理第一日期。

日期函數

toLastDay <- function(dateObj, monAdv=0, ToFirst = FALSE) 
# takes date object, transforms and returns a dat object 
{ 
    tt <- as.POSIXlt(dateObj) 
    tt$mon <- tt$mon + monAdv # moves the month 
    tt$mday <- 1L    # make date the first 
    if(ToFirst) {    
     tt <- as.Date(tt) 
    } else { 
     tt$mon <- tt$mon + 1L # go to the first of the next month 
     tt <- as.Date(tt) - 1L # subtract one day, yielding the last of prior month 
    } 
    return(tt) 
} 

現在你想要那個拿數據做了線性插補功能 - 我使用na.approx功能zooxts。在這裏,您可能需要中期日期 - 根據我的經驗,大多數快速數據最好認爲是中期。

# make Qtrly monthly -- with obsv in the mid qtr 
qtr2Mon <- function(QD) 
{ 
    fromD <- toLastDay(index(first(QD)), -2L, ToFirst = TRUE) 
    toD <- toLastDay(index(last(QD)), ToFirst = TRUE) 
    q2m_dates <- toLastDay(seq(fromD, toD, by = 'mon')) 
    emptyX <- xts(, q2m_dates) 
    QD_adj <- QD 
    index(QD_adj) <- toLastDay(index(QD), monAdv = -1L) 
    mm <- merge(emptyX, QD_adj) 
    mm_filled <- na.approx(mm) 
    return(mm_filled) 
} 

首先,我們做一個qtrly xts對象 - 把它當作qtrly GDP或一些這樣的

Sys.setenv(TZ = 'GMT') 
require(xts) 

qdates <- seq(as.Date("2000-03-01"), as.Date("2013-06-01"), by = "3 mon") 
qdates <- toLastDay(qdates) 
qdata <- rnorm(length(qdates), mean = 1) 
qtr_XTS <- xts(qdata, order.by = qdates) 

現在我們可以使用上面的庫函數將其轉換爲每月,假設線性增長等

mon_fromQtr <- qtr2Mon(qtr_XTS) 

完成!

相關問題