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

R 问题:将日期格式从数据帧转换为时间序列,r,rstudio,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=aggr

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

#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(Ibis)、瑞士SMI、法国CAC和英国FTSE。数据 在营业时间取样,即省略周末和节假日。 用法

欧洲证券市场

格式

一个多变量时间序列,包含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")

请输入日期测试2。如果没有实际的dataset.row.names=c(NA,6L),class=“data.frame”)就很难提供帮助,这是您没有复制所有dput的dput。也许我不清楚。我对您的csv文件的外观很感兴趣。例如,前两行mtcars的dput(来自tidyverse软件包的数据)看起来是这样的>dput(mtcars[1:2,])结构(列表(mpg=c(21,21),cyl=c(6,6),disp=c(160,160),hp=c(110,110),drat=c(3.9,3.9),wt=c(2.62,2.875),qsec=c(16.46,17.02),vs=c(0,0),am=c(1,1),gear=c(4,4),carb=c(4,4),(“mpg”),“cyl”,“disp”,“hp”,“drat”,“wt”,“qsec”,“vs”,“am”,“gear”,“carb”,row.names=c(“马自达RX4”,“马自达RX4 Wag”),class=“data.frame”)dput(head(datetest2,2))结构(list(X=结构(c(1744117442),class=“Date”),X1=c(0.001266303,-.021048584),X2=c(0.037842672,-.219755806),X2=c(-0.022771565,-.0411676),X3=c(-0.016205216,-0.017019091),X4=c(-0.058696937,-0.024072712),X5=c(-0.021498233,-0.019484207),X6=c(-0.018186256,0.015393258),X7=c(-0.061737994,-0.038319809),X8=c(0.028155324,-.089042438),X9=c(-0.054497537,-0.04408321)),row.names=1:2,class=“数据框这样更好吗?对不起,我不太清楚它更好。但是,它只是前两行。你能提供多一点吗?另外,我试着一行一行地运行你的代码,看起来你还有很多bug。。。
> 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")