问题将日期格式从数据框转换为时间序列

时间:2018-06-02 14:38:18

标签: r rstudio

我在网上找到了这个代码:

#Integration and co-integration analysis of equity indexes.
library(urca)
library(vars)
## Loading data set and converting to zoo
data(EuStockMarkets)

Assets=as.zoo(EuStockMarkets)

## Aggregating as month’s−enddput series

AssetsM=aggregate(Assets , as.yearmon ,  tail , 1)
head(AssetsM)

## Applying unit root tests for sub−sample

AssetsMsub=window( AssetsM,start =start( AssetsM ), end = "Jun 1996")

# class = "yearmon"), class = c("zooreg", "zoo"), frequency = 12)

## Levels

ADF=lapply(AssetsMsub,ur.df,type = "drift",selectlags = "AIC")

有关EuStockMarkets数据的详细信息:

  

描述

     

包含主要欧洲股票指数的每日收盘价:   德国DAX(宜必思),瑞士SMI,法国CAC和英国富时。数据   在营业时间内抽样,即省略周末和假日。   使用

     

EuStockMarkets

     

格式

     

多变量时间序列,对4个变量进行了1860次观察。该   对象属于“mts”类。

我尝试使用我自己的data.frame格式的数据。由于第一列是具有混合格式的日期向量,因此我使用标准格式。

dput datetest2 = ow.names = c(NA, 6L), class = "data.frame") 

但我对代码的最后一行有一个问题:

#Integration and co-integration analysis of equity indexes.
library(urca)
library(vars)

## Loading data set and converting to zoo
rm(list = setdiff(ls(), lsf.str()))

datetest2 <- read.csv("~/Desktop/datetest2.csv")

## As date aren't in uniformised format we create a date vector and replace the old one

DateTS <-as.Date(as.Date("2017/10/02"):as.Date("2018/05/22"), origin="1970/01/01") 
datetest2[,1]=DateTS

## Convert data frame to c("mts", "ts", "matrix"))

data <- ts(datetest2[,-1],start=datetest2[1,1],end=datetest2[NROW(datetest2),1],frequency = 1 )

Assets=as.zoo(data)

## Aggregating as month’s−end series

AssetsM=Assets
AssetsM=aggregate(Assets , as.yearmon ,  tail , 1)
head(AssetsM)

## Applying unit root tests for sub−sample

AssetsMsub=window(AssetsM,start =start( AssetsM ), end = end(AssetsM))

## Levels

ADF=lapply(AssetsMsub,ur.df,type = "drift",selectlags = "AIC")

此错误:charToDate(x)出错:字符串不是标准的明确格式

但我不清楚,因为日期已经是一个明确的格式,任何想法?

> dput(head(datetest2,10))
structure(list(X = structure(c(17441, 17442, 17443, 17444, 17445, 
17446, 17447, 17448, 17449, 17450), class = "Date"),X1 = c(0.001266303, 
-0.021048584, -0.020621217, 0.023149588, 0.009748078, 0.012748959, 
0.040634667, 0.034437722, 0.002087083, 0.009260646), X2 = c(0.037842672, 
-0.219755806, 0.052179239, -0.020359985, -0.147664555, 0.121832339, 
-0.022406655, 0.076908587, -0.026717135, 0.044910046), X3 = c(-0.022771565, 
-0.0411676, 0.030648901, -0.000196444, 0.004834399, 0.011694903, 
-0.026136535, -0.064203569, 0.026439081, 0.024184196), X4 = c(-0.016205216, 
-0.017019091, 0.00068362, 0.010874844, 0.042127166, 0.008165155, 
-0.008100346, -0.037033935, 0.008304639, 0.011900759), X5 = c(-0.058696937, 
-0.024072712, -0.026573368, -0.011153745, -0.07178172, 0.067360919, 
-0.074169252, -0.063135862, 0.017187075, 0.008108153), X6 = c(-0.021498233, 
-0.019484207, -0.018313765, 0.00523917, 0.00867141, 0.008787067, 
0.010594121, -0.057712044, 0.011102414, 0.00334613), X7 = c(-0.018186256, 
0.015393258, -0.027814412, 0.015910798, -0.007320437, 0.005577137, 
-0.021271535, -0.048851434, 0.0154376, 0.00688708), X8= c(-0.061737994, 
-0.038319809, -0.019420551, -0.034385314, 0.001193193, 0.004084886, 
-0.085468062, 0.000636325, 0.074028454, 0.023084468), X9= c(0.028155324, 
-0.089042438, -0.075688421, 0.035312622, 0.037694432, 0.07525801, 
-0.119479271, -0.09718284, 0.007190581, 0.021934404), X10 = c(-0.054497537, 
-0.044083621, -0.045526127, 0.206964088, 0.233137354, 0.130054454, 
-0.1195141, -0.055098329, 0.074006512, 0.02588484)), row.names = c(NA, 
10L), class = "data.frame")

0 个答案:

没有答案