【问题标题】:Change data to numeric type to determine which distribution fits better将数据更改为数值类型以确定哪种分布更适合
【发布时间】:2016-09-21 03:34:38
【问题描述】:

我试图找出最适合对数股票回报的分布。这是我的代码:

library(TTR)
sign="^GSPC"
start=19900101
end=20160101
x <- getYahooData(sign, start = start, end = end, freq = "daily")
x$logret <- log(x$Close) - lag(log(x$Close))
x=x[,6]

我想使用函数descdist(x, discrete = FALSE),这是我从这篇精彩的帖子https://stats.stackexchange.com/questions/132652/how-to-determine-which-distribution-fits-my-data-best 中得到的,但是 r 给了我这个错误:Error in descdist(x, discrete = FALSE) : data must be a numeric vector 如何将我的数据转换为数字向量??

dput(head(x)) 的输出是:

structure(c(NA, -0.00258888580664607, -0.00865029791190164, -0.00980414107803274, 
0.00450431207515223, -0.011856706127011), class = c("xts", "zoo"
), .indexCLASS = "Date", .indexTZ = "UTC", tclass = "Date", tzone = "UTC", index = structure(c(631238400, 
631324800, 631411200, 631497600, 631756800, 631843200), tzone = "UTC", tclass = "Date"), .Dim = c(6L, 
1L), .Dimnames = list(NULL, "logret"))

【问题讨论】:

  • class(x[,6]) 返回什么? class(x$Close) 返回什么?这些值“看起来”是数字吗?
  • x$logret &lt;- log(x$Close) - lag(log(x$Close)) 是股票收盘价的对数回报。那将是 ln(price2/price1)class(x[,6]) 是第 6 列,即感兴趣的对数数据所在的列。 @MrFlick
  • 这就是你所期望的,但我特别问class() 将在这些对象上返回什么。我正在尝试查看哪个可能不是数字,class() 是最好的方法。
  • OP,如果您将dput(head(x)) 的输出复制并粘贴到您的问题中,这可能会很有用,以帮助我们了解您的数据。
  • &gt; class(x) [1] "xts" "zoo"@MrFlick

标签: r vector dataframe


【解决方案1】:

使用as.numeric(na.omit(x))预处理x,或者直接运行

descdist(as.numeric(na.omit(x)), discrete = FALSE)

【讨论】:

  • 我似乎什至需要as.numeric,即使 x 已经是数字......很奇怪,但谢谢你的答案!
猜你喜欢
  • 2012-10-14
  • 1970-01-01
  • 2013-02-04
  • 1970-01-01
  • 1970-01-01
  • 2014-05-15
  • 2016-11-19
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多