美文网首页
R软件xts包基础内容学习(时间序列)

R软件xts包基础内容学习(时间序列)

作者: PersonaeYxxx | 来源:发表于2018-07-26 11:28 被阅读0次

    zoo是时间序列的基础库,而xts在zoo基础上进行了拓展,其对日期的处理较zoo更为精细。
                    zoo = matrix + index
                 xts = vector + matrix + attributed
    即xts由三部分组成。
    1.索引部分:时间类型向量
    2.数据部分:以矩阵为基础类型,支持可以与矩阵相互转换的任何类型
    3.属性部分:附件信息,包括时区,索引时间类型的格式等

    一、安装xts包

    > install.packages("xts")
    

    二、时间类型数据

    在学习xts之前,需要先了解时间类型数据。

    1. 在R语言里面有两个基本的函数:as.POSIXlt() 和 as.POSIXct()可以把表示时间的字符串转化为时间类型数据。
    > myDataTimeStr = "2013-12-19 10:17:07"      #此时数据类型为character
    > class(myDataTimeStr)
    [1] "character"
    > myPOSIXct = as.POSIXct(myDataTimeStr)      #虽然表现形式一样,但通过class()可知已完成数据类型转换
    > class(myPOSIXct)
    [1] "POSIXct" "POSIXt" 
    

    *注意:一周是按0到6开始的,0是星期日

    1. datetime的默认格式是“yyyy-mm-dd hh:mm:ss”或“yyyy/mm/dd hh:mm:ss”。
      若要改变时间表达格式,可以使用format = "..."
    > myPOSIXct1 = as.POSIXct("19-12-2003 10:17:07",format = "%d-%m-%Y %H:%M:%S")
    > myPOSIXct1
    [1] "2003-12-19 10:17:07 CST"
    
    1. “POSIXct”对象有一个内部表示,它是来自某个起始datetime的秒数,我们也可以从数值数据中创建它们。
    > myPOSIXct2 = as.POSIXct(0,origin = "1970-01-01")
    > myPOSIXct2
    [1] "1970-01-01 08:00:00 CST"
    > as.numeric(myPOSIXct2)
    [1] 0
    
    > myPOSIXct2 = as.POSIXct(0,origin = "1970-01-02")
    > myPOSIXct2
    [1] "1970-01-02 08:00:00 CST"
    > as.numeric(myPOSIXct2)
    [1] 86400
    

    请注意,因为“CST”(中国标准时间)比“gm/UTC”时间晚8个小时,所以日期显示为“1970-01-01 08:00 CST”。

    1. 在许多情况下,最好在“GMT(UTC)”中定义datetimes,以避免在处理datetimes时出现时区问题。
    > as.POSIXct("1960-01-01",tz = "GMT")
    [1] "1960-01-01 GMT"
    
    1. 可以使用Sys.setenv(TZ = "UTC")来设置系统市区到“GMT(UTC)”,使其变为默认时区。
      但如果你想再转到“CST”(中国标准时间),使用Sys.setenv(TZ = "CST")是无效的,这是因为CST有四种不同的含义。建议可以用Sys.setenv(TZ = "Asia/Shanghai"),或者使用ISOdatetime()函数。
    > myPOSIXct3 = ISOdatetime(year=2013,month=12,day=19,hour=10,min=17,sec=7,tz = "")
    > class(myPOSIXct3)
    [1] "POSIXct" "POSIXt" 
    > myPOSIXct3
    [1] "2013-12-19 10:17:07 CST"
    
    1. 时间数据的加减
    > myPOSIXct2
    [1] "1970-01-02 08:00:00 CST"
    > myPOSIXct4 = myPOSIXct2 - 8*60*60
    > myPOSIXct4
    [1] "1970-01-02 CST"
    
    1. 当前时间
    > Sys.time()
    

    三、xts基础内容

    1. 构建xts
    > library(xts)
    
    > dates <- seq(as.Date("2016-01-01"),length = 5,by = "days")
    > dates
    [1] "2016-01-01" "2016-01-02" "2016-01-03" "2016-01-04" "2016-01-05"
    
    > data <- rnorm(5)           #随机生成五个数字
    > smith <- xts(x = data,order.by = dates)
    > smith
                      [,1]
    2016-01-01 -1.92935732
    2016-01-02  0.47103271
    2016-01-03  1.15349653
    2016-01-04  0.85077517
    2016-01-05  0.05895633
    
    2. 解构xts

    当使用时间序列时,有时需要将您的时间序列分离到其核心数据和索引属性中,以进行额外的分析和操作。核心数据是“xts”的矩阵部分。您可以使用coredata()将其与“xts”对象分隔开。xts对象的索引部分可用index()函数来获得。

    > hayek_core <- coredata(hayek)
    > class(hayek_core)
    [1] "matrix"
    > hayek_index <- index(hayek)
    > class(hayek_index)
    [1] "Date"
    
    3. 取出时间1至时间2之间的数据
    > x <- as.xts(x)    #将x从zoo变为xts
    > x_2016 <- x["2016"]     #提取2016年的数据
    > jan_march <- x["2016-01-01/2016-03-22"]    #提取2016年1月1日至3月22日的数据
    
    #提取某几天的数据
    > dates <- as.Date(c("2016-06-04","2016-06-08"))
    > thedata <- x[dates] 
    
    4. 提取最后几天的数据

    使用last()first()函数

    > temp.max <- c(74,78,79,80,90,89,87,89,81,83,93,89,86,89,75,79)
    > temp.mean <- c(69,66,68,76,79,78,80,73,72,81,82,78,80,72,69,77)
    > temp.min <- c(60,56,59,69,68,70,72,72,67,64,69,77,68,68,60,60)
    > temps <- xts(cbind(temp.max,temp.mean,temp.min),order.by = as.Date("2017-07-01")+0:15)
    > lastweek <- last(temps,"1 week")                       #提取最后一周的数据
    > lastweek
               temp.max temp.mean temp.min
    2017-07-10       83        81       64
    2017-07-11       93        82       69
    2017-07-12       89        78       77
    2017-07-13       86        80       68
    2017-07-14       89        72       68
    2017-07-15       75        69       60
    2017-07-16       79        77       60
    > last(lastweek,2)                                       #提取最后一周的最后两天的数据
               temp.max temp.mean temp.min
    2017-07-15       75        69       60
    2017-07-16       79        77       60
    > first(lastweek,"-2 days")                              #提取最后一周除开始两天以外的数据
               temp.max temp.mean temp.min
    2017-07-12       89        78       77
    2017-07-13       86        80       68
    2017-07-14       89        72       68
    2017-07-15       75        69       60
    2017-07-16       79        77       60
    > first(last(first(temps,"2 weeks"),"1 week"),"3 days")   #提取第二周前三天的数据
               temp.max temp.mean temp.min
    2017-07-03       79        68       59
    2017-07-04       80        76       69
    2017-07-05       90        79       68
    
    5. 结合多个时间序列

    使用merge()函数。注意dataframe不能直接使用merge()

    > merge(x, y, join = "inner")   #以x,y都有的时间为准
    > merge(x, y, join = "left", fill = 0)    #以x的时间为准,y空缺部分补0
    > merge(x, y, join = "right")   #以y的时间为准
    
    6. 滞后时间序列&先行时间序列

    创造一个x的先行一期时间序列:

    > lead_x <- lag(x, k = -1)
    

    创造一个x的滞后一期时间序列:

    > lag_x <- lag(x, k = 1)
    
    7. 差分

    一阶差分,即x(t)-x(t-k)可以通过diff()得到。

    #Calculate the first order 12 month difference of AirPass
    > diff(AirPass, lag = 12, difference = 1), n=15)
    
    8. 按时间间隔查找数据

    enpoints()

    9. period.apply
    10. 分割

    split()xts.split()

    11. 发现周期性

    periodicity()

    相关文章

      网友评论

          本文标题:R软件xts包基础内容学习(时间序列)

          本文链接:https://www.haomeiwen.com/subject/chivmftx.html