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

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

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错误:

# 将每日价格转换为整洁数据框中的月度回报率
asset_returns_dplyr <- 
  prices %>%
  to.monthly(indexAt = "lastof", OHLC = FALSE) %>%
  # 将索引转换为日期
  data.frame(date = index(.)) %>%
  # 现在删除索引,因为它被转换为行名称
  remove_rownames() %>%
  gather(asset, prices, -date) %>%
  group_by(asset) %>%
  mutate(returns = (log(prices) - log(lag(prices)))) %>%
  select(-prices) %>%
  spread(asset, returns) %>%
  select(date, symbols) %>%
  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")))


<details>
<summary>英文:</summary>

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: 

    
    # Converting Daily Prices to Monthly Returns in the tidyverse
    asset_returns_dplyr = 
      prices |&gt;
      to.monthly(indexAt = &quot;lastof&quot;, OHLC = FALSE) |&gt;
      # convert the index to a date
      data.frame(date = index(.)) |&gt;
      # now remove the index because it got converted to row names
      remove_rownames() |&gt;
      gather(asset, prices, -date) |&gt;
      group_by(asset) |&gt; 
      mutate(returns = (log(prices) - log(lag(prices)))) |&gt;
      select(-prices) |&gt;
      spread(asset, returns) |&gt;
      select(date, symbols)
      na.omit()

    Error in `spread()`:
    ! Each row of output must be identified by a unique combination of keys.
    ℹ Keys are shared for 625 rows
    • 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, 5 
    540, 541, 542, 543, 544,
    545, 546, 547, 548, 549, 550, 551, 552, 553, 554, 555, 556, 557, 558, 559, 560, 561, 
    562, 

    structure(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(&quot;xts&quot;, 
    &quot;zoo&quot;), src = &quot;yahoo&quot;, updated = structure(1685658877.66735, class 
    = c(&quot;POSIXct&quot;, 
    &quot;POSIXt&quot;)), index = structure(c(1356912000, 1357084800, 1357171200, 
    1357257600, 1357516800, 1357603200, 1357689600, 1357776000, 
    1357862400, 
    1358121600), tzone = &quot;UTC&quot;, tclass = &quot;Date&quot;), dim = c(10L, 5L
    ), dimnames = list(NULL, c(&quot;SPY&quot;, &quot;EFA&quot;, &quot;IJS&quot;, &quot;EEM&quot;, &quot;AGG&quot;)))

</details>


# 答案1
**得分**: 1

这里存在几个问题:

- 在 Stack Overflow 发布的代码应该是完整的。缺少了 `library` 语句。
- `spread` 和 `gather` 通常不再使用,而应改为使用 tidyr 中的 `pivot_wider` 和 `pivot_longer`。
- `dot` 不能与 `|&gt;` 一起使用,只能与 `%&gt;%` 一起使用。
- 通过不转换为数据框架并使用 `diff.xts` 可以显著简化代码。

以下是修改后的代码示例:

```R
library(quantmod) # 同时引入了 zoo 和 xts

rets <- prices |>
  to.monthly(indexAt = "lastof", OHLC = FALSE) |>
  diff(log = TRUE)
rets
##                   SPY        EFA        IJS         EEM          AGG
## 2013-01-31 0.03151816 0.02174571 0.02694797 0.006965536 -0.001171007

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

rets |>
  fortify.zoo(name = "date")
##         date        SPY        EFA        IJS         EEM          AGG
## 1 2012-12-31         NA         NA         NA          NA           NA
## 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

library(quantmod) # also pulls in zoo and xts

rets &lt;- prices |&gt;
  to.monthly(indexAt = &quot;lastof&quot;, OHLC = FALSE) |&gt;
  diff(log = TRUE)
rets
##                   SPY        EFA        IJS         EEM          AGG
## 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:

rets |&gt;
  fortify.zoo(name = &quot;date&quot;)
##         date        SPY        EFA        IJS         EEM          AGG
## 1 2012-12-31         NA         NA         NA          NA           NA
## 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:

确定