尝试为 tibble 创建一个日期列。希望从价格 xts 对象的索引中获取值。

huangapple go评论94阅读模式
英文:

Trying to create a date column for tibble. Want to take the values from the index of a prices xts obkect

问题

我首先遇到的错误是在索引(.)))行。对象'.'未找到。当我将'.'放入索引行时,我在spread(asset, return)中遇到错误,错误信息是每行输出必须由唯一键组合标识。请查找代码和spread错误:

  1. # 将每日价格转换为整洁数据框中的月度回报率
  2. asset_returns_dplyr <-
  3. prices %>%
  4. to.monthly(indexAt = "lastof", OHLC = FALSE) %>%
  5. # 将索引转换为日期
  6. data.frame(date = index(.)) %>%
  7. # 现在删除索引,因为它被转换为行名称
  8. remove_rownames() %>%
  9. gather(asset, prices, -date) %>%
  10. group_by(asset) %>%
  11. mutate(returns = (log(prices) - log(lag(prices)))) %>%
  12. select(-prices) %>%
  13. spread(asset, returns) %>%
  14. select(date, symbols) %>%
  15. na.omit()

错误信息在spread()中:
! 每行输出必须由唯一组合的键标识。
ℹ 键被共享了 625 行
• 501、502、503、504、505、506、507、508、509、510、511、512、513、514、515、516、517、
518、519、520、521、522、523、524、525、526、527、528、529、530、531、532、533、534、535、536、537、538、539、540、541、542、543、544、
545、546、547、548、549、550、551、552、553、554、555、556、557、558、559、560、561、562、

结构(c(117.827011108398, 120.846908569336, 120.57389831543,
121.103385925293, 120.772438049316, 120.424964904785,
120.73104095459,
121.690849304199, 121.682556152344, 121.59984588623,
42.5093727111816,
43.1672821044922, 42.7486114501953, 42.9654159545898,
42.7785224914551,
42.5392799377441, 42.7560882568359, 43.3018455505371,
43.3915596008301,
43.4438934326172, 34.7362937927246, 35.6893730163574,
35.6421546936035,
35.9212112426758, 35.7108459472656, 35.5734710693359,
35.6765060424805,
35.7194404602051, 35.693675994873, 35.6850929260254,
35.7743263244629,
36.4761009216309, 36.2179832458496, 36.2905693054199,
36.0163269042969,
35.6936645507812, 35.8469200134277, 36.1615180969238,
35.8711166381836,
36.0243835449219, 86.7947540283203, 86.6931762695312,
86.4743957519531,
86.5681304931641, 86.5212860107422, 86.5993957519531,
86.5369033813477,
86.5290603637695, 86.6150131225586, 86.6931762695312), class =
c("xts",
"zoo"), src = "yahoo", updated = structure(1685658877.66735, class
= c("POSIXct",
"POSIXt")), index = structure(c(1356912000, 1357084800, 1357171200,
1357257600, 1357516800, 1357603200, 1357689600, 1357776000,
1357862400,
1358121600), tzone = "UTC", tclass = "Date"), dim = c(10L, 5L
), dimnames = list(NULL, c("SPY", "EFA", "IJS", "EEM", "AGG")))

  1. <details>
  2. <summary>英文:</summary>
  3. The first error I get is from the index(.))) line. Which is object &#39;.&#39; not found. When I put &#39;.&#39; into the index line I get an error in the spread(asset, return), which is Each row of output must be identified by a unique combination of keys. Please find code and spread error:
  4. # Converting Daily Prices to Monthly Returns in the tidyverse
  5. asset_returns_dplyr =
  6. prices |&gt;
  7. to.monthly(indexAt = &quot;lastof&quot;, OHLC = FALSE) |&gt;
  8. # convert the index to a date
  9. data.frame(date = index(.)) |&gt;
  10. # now remove the index because it got converted to row names
  11. remove_rownames() |&gt;
  12. gather(asset, prices, -date) |&gt;
  13. group_by(asset) |&gt;
  14. mutate(returns = (log(prices) - log(lag(prices)))) |&gt;
  15. select(-prices) |&gt;
  16. spread(asset, returns) |&gt;
  17. select(date, symbols)
  18. na.omit()
  19. Error in `spread()`:
  20. ! Each row of output must be identified by a unique combination of keys.
  21. ℹ Keys are shared for 625 rows
  22. • 501, 502, 503, 504, 505, 506, 507, 508, 509, 510, 511, 512, 513, 514, 515, 516, 517,
  23. 518, 519, 520, 521, 522,
  24. 523, 524, 525, 526, 527, 528, 529, 530, 531, 532, 533, 534, 535, 536, 537, 538, 539, 5
  25. 540, 541, 542, 543, 544,
  26. 545, 546, 547, 548, 549, 550, 551, 552, 553, 554, 555, 556, 557, 558, 559, 560, 561,
  27. 562,
  28. structure(c(117.827011108398, 120.846908569336, 120.57389831543,
  29. 121.103385925293, 120.772438049316, 120.424964904785,
  30. 120.73104095459,
  31. 121.690849304199, 121.682556152344, 121.59984588623,
  32. 42.5093727111816,
  33. 43.1672821044922, 42.7486114501953, 42.9654159545898,
  34. 42.7785224914551,
  35. 42.5392799377441, 42.7560882568359, 43.3018455505371,
  36. 43.3915596008301,
  37. 43.4438934326172, 34.7362937927246, 35.6893730163574,
  38. 35.6421546936035,
  39. 35.9212112426758, 35.7108459472656, 35.5734710693359,
  40. 35.6765060424805,
  41. 35.7194404602051, 35.693675994873, 35.6850929260254,
  42. 35.7743263244629,
  43. 36.4761009216309, 36.2179832458496, 36.2905693054199,
  44. 36.0163269042969,
  45. 35.6936645507812, 35.8469200134277, 36.1615180969238,
  46. 35.8711166381836,
  47. 36.0243835449219, 86.7947540283203, 86.6931762695312,
  48. 86.4743957519531,
  49. 86.5681304931641, 86.5212860107422, 86.5993957519531,
  50. 86.5369033813477,
  51. 86.5290603637695, 86.6150131225586, 86.6931762695312), class =
  52. c(&quot;xts&quot;,
  53. &quot;zoo&quot;), src = &quot;yahoo&quot;, updated = structure(1685658877.66735, class
  54. = c(&quot;POSIXct&quot;,
  55. &quot;POSIXt&quot;)), index = structure(c(1356912000, 1357084800, 1357171200,
  56. 1357257600, 1357516800, 1357603200, 1357689600, 1357776000,
  57. 1357862400,
  58. 1358121600), tzone = &quot;UTC&quot;, tclass = &quot;Date&quot;), dim = c(10L, 5L
  59. ), dimnames = list(NULL, c(&quot;SPY&quot;, &quot;EFA&quot;, &quot;IJS&quot;, &quot;EEM&quot;, &quot;AGG&quot;)))
  60. </details>
  61. # 答案1
  62. **得分**: 1
  63. 这里存在几个问题:
  64. - 在 Stack Overflow 发布的代码应该是完整的。缺少了 `library` 语句。
  65. - `spread` 和 `gather` 通常不再使用,而应改为使用 tidyr 中的 `pivot_wider` 和 `pivot_longer`。
  66. - `dot` 不能与 `|&gt;` 一起使用,只能与 `%&gt;%` 一起使用。
  67. - 通过不转换为数据框架并使用 `diff.xts` 可以显著简化代码。
  68. 以下是修改后的代码示例:
  69. ```R
  70. library(quantmod) # 同时引入了 zoo 和 xts
  71. rets <- prices |>
  72. to.monthly(indexAt = "lastof", OHLC = FALSE) |>
  73. diff(log = TRUE)
  74. rets
  75. ## SPY EFA IJS EEM AGG
  76. ## 2013-01-31 0.03151816 0.02174571 0.02694797 0.006965536 -0.001171007

如果你确实想将 xts 对象转换为数据框架,可以使用 fortify.zoo。例如:

  1. rets |>
  2. fortify.zoo(name = "date")
  3. ## date SPY EFA IJS EEM AGG
  4. ## 1 2012-12-31 NA NA NA NA NA
  5. ## 2 2013-01-31 0.03151816 0.02174571 0.02694797 0.006965536 -0.001171007

希望这些修改能够帮助你解决问题。

英文:

There are several problems here:

  • code posted to SO should be complete. The library statement(s) are missing.
  • spread and gather are normally no longer used and pivot_wider and pivot_longer from tidyr are used instead.
  • dot cannot be used with |>, only with %>% .
  • the code can be simplified significantly by not converting to data frame and using diff.xts

This gives

  1. library(quantmod) # also pulls in zoo and xts
  2. rets &lt;- prices |&gt;
  3. to.monthly(indexAt = &quot;lastof&quot;, OHLC = FALSE) |&gt;
  4. diff(log = TRUE)
  5. rets
  6. ## SPY EFA IJS EEM AGG
  7. ## 2013-01-31 0.03151816 0.02174571 0.02694797 0.006965536 -0.001171007

If you did want to convert an xts object to a data frame use fortify.zoo. For example:

  1. rets |&gt;
  2. fortify.zoo(name = &quot;date&quot;)
  3. ## date SPY EFA IJS EEM AGG
  4. ## 1 2012-12-31 NA NA NA NA NA
  5. ## 2 2013-01-31 0.03151816 0.02174571 0.02694797 0.006965536 -0.001171007

huangapple
  • 本文由 发表于 2023年6月2日 06:05:43
  • 转载请务必保留本文链接:https://go.coder-hub.com/76386018.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定