简体   繁体   English

如何 plot R 中的日志返回数据在 x 轴上显示日期

[英]How to plot the log-return data in R showing the date in the x-axis

I have time-series data as follows:我有时间序列数据如下:

structure(list(Date = structure(1271203200, tzone = "UTC", class = c("POSIXct", 
"POSIXt")), P_Sh = 3164.966), row.names = c(NA, -1L), class = c("tbl_df", 
"tbl", "data.frame"))

I would like to find the log of the return values and plot it.我想找到返回值和 plot 的日志。 I tried the following code:我尝试了以下代码:

x <- (diff(log(FinDat[10,c(1,3)]))[-1])

but I got the following error.但我收到以下错误。

   non-numeric variable(s) in data frame: Date

I tried to follow the following code:我尝试遵循以下代码:

 library(tscopula)
  data(bitcoin)
  View(bitcoin)
  X <- (diff(log(bitcoin))[-1]) * 100 # log-returns (as percentages)
   length(X)
  #> [1] 1043
   plot(X, type = "h")

> str(bitcoin)
An ‘xts’ object on 2015-12-31/2019-12-31 containing:
  Data: num [1:1044, 1] 431 434 433 432 429 ...
 - attr(*, "dimnames")=List of 2
  ..$ : NULL
  ..$ : chr "BTCUSD=X.Close"
  Indexed by objects of class: [Date] TZ: UTC
  xts Attributes:  
List of 2
 $ src    : chr "yahoo"
 $ updated: POSIXct[1:1], format: "2020-12-18 14:28:23"
       

I would like to have a figure similar to the following one.我想要一个类似于下图的图。 这里

and the dput of bitcoin[500,] is as follows: dput bitcoin[500,]的输出如下:

structure(9888.614258, class = c("xts", "zoo"), src = "yahoo", updated = structure(1608290903.22052, class = c("POSIXct", 
"POSIXt")), index = structure(1511913600, tzone = "UTC", tclass = "Date"), .Dim = c(1L, 
1L), .Dimnames = list(NULL, "BTCUSD=X.Close"))

EDIT编辑

I class of my data is:我的数据 class 是:

> class(FinDat)
[1] "tbl_df"     "tbl"        "data.frame"

The class of the bitcoin is:比特币的 class 是:

 > class(bitcoin)
 [1] "xts" "zoo"

The class of your data different from the bitcoin example.您的数据的 class 与比特币示例不同。 'xts' eases working with time-series data. 'xts' 简化了时间序列数据的处理。 So, first of all, you need to transfer your data to 'xts' object.因此,首先,您需要将数据传输到“xts”object。

  Library(xts)
  Dat<-xts(FinDat[ ,-1],FinDat[ ,1])

Then, use the same code used in the bitcoin example.然后,使用与比特币示例相同的代码。

This should work.这应该有效。

I am going to assume that FinDat is the name of your dataframe.我将假设FinDat是您的 dataframe 的名称。 If this is the case the expresion FinDat[10,c(1,3)] is wrong because your data frame only has two dimension FinDat[ rows, columns] you can not introduce a vector where your column number should be.如果是这种情况,则FinDat[10,c(1,3)]是错误的,因为您的数据框只有二维FinDat[ rows, columns]您不能在列号应该存在的位置引入向量。 In the case of diff(log(bitcoin))[-1] the author is saying I ant to apply the diff function to a dataframe which is not taking the firs column.diff(log(bitcoin))[-1]的情况下,作者说我 ant 将 diff function 应用到 dataframe 不占用列。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM