应用贝叶斯变点检测算法使用bcp到R中的分组数据帧

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

Applying Bayesian Changepoint Detection algorithm using bcp to a grouped data frame in R

问题

我有一个分组的数据框,我想应用bcp函数来计算每个点的变化后验概率。

我的数据如下:

  1. # 安装PACMAN
  2. if (!require("pacman", character.only = TRUE)) {
  3. install.packages("pacman")
  4. }
  5. pacman::p_load(bcp,tidyverse)
  6. df <- data.frame(
  7. date = c(seq(Sys.Date(), by = -1, length.out = 1000), seq(Sys.Date(), by = -1, length.out = 1000)),
  8. value = c(rnorm(200, mean = 20, sd = 1), rnorm(800, mean = 17, sd = 2), rnorm(400, mean = 200, sd = 3), rnorm(600, mean = 150, sd = 4)),
  9. product = c(rep("A", 1000), rep("B", 1000))
  10. )

通过将我的数据框筛选为单个变量,并将其分配给新变量,然后应用bcp()函数,我看到它返回一个包含12个列表的结果:

  1. x <- df %>%
  2. filter(product == "A")
  3. y <- bcp(x$value)

我尝试使用group_map,它返回了只有两列的结果,这可能不是理想的。不过,我不知道为什么只返回两列:

  1. df %>%
  2. group_by(product) %>%
  3. group_map(~ bcp(.x$value))

我还尝试了group_modify,但无法得到正确的语法来解析出正确的字段:

  1. df %>%
  2. group_by(product) %>%
  3. group_modify(~ {
  4. bcp::bcp(.x$value) %>%
  5. tibble::enframe(name = "name", value = "value")
  6. })

以及:

  1. df %>%
  2. group_by(product) %>%
  3. group_modify(~ bcp::bcp(.x$value) %>%
  4. pluck("posterior.prob"))

关于如何在每个分组的基础上将bcp函数的'posterior.prob'附加到我的原始数据框上,如果有任何指导,将不胜感激。

英文:

I have a grouped dataframe which I would like to apply the bcp function to calculate for each point the posterior probability of there being a change at each point.

My data looks as follows:

  1. # INSTALL PACMAN
  2. if (!require(&quot;pacman&quot;, character.only = TRUE)) {
  3. install.packages(&quot;pacman&quot;)
  4. }
  5. pacman::p_load(bcp,tidyverse)
  6. df &lt;- data.frame(
  7. date = c(seq(Sys.Date(), by = -1, length.out = 1000), seq(Sys.Date(), by = -1, length.out = 1000)),
  8. value = c(rnorm(200, mean = 20, sd = 1), rnorm(800, mean = 17, sd = 2), rnorm(400, mean = 200, sd = 3), rnorm(600, mean = 150, sd = 4)),
  9. product = c(rep(&quot;A&quot;, 1000), rep(&quot;B&quot;, 1000))
  10. )

By filtering my df to a single variable and assigning it to a new variable and applying bcp() I see it returns a list of 12

  1. x &lt;- df %&gt;%
  2. filter(product == &quot;A&quot;)
  3. y &lt;- bcp(x$value)

I've tried using group_map which returns only two columns which is not ideal, I've no idea why only two columns are returned:

  1. df %&gt;%
  2. group_by(product) %&gt;%
  3. group_map(~ bcp(.x$value))

I've also tried group_modify but I can't get the syntax correct to parse out the correct fields:

  1. df %&gt;%
  2. group_by(product) %&gt;%
  3. group_modify(~ {
  4. bcp::bcp(.x$value) %&gt;%
  5. tibble::enframe(name = &quot;name&quot;, value = &quot;value&quot;)
  6. })

As well as:

  1. df %&gt;%
  2. group_by(product) %&gt;%
  3. group_modify(~ bcp::bcp(.x$value) %&gt;%
  4. pluck(&quot;posterior.prob&quot;))

Any guidance on how I can append the 'posterior.prob' from the bcp function to my original df on a per group basis would be greatly appreciated.

答案1

得分: 1

I'm not familiar with the bcp package but does this give you what you want?

  1. posterior_prob <- map(df %>%
  2. group_by(product) %>%
  3. group_map(~ bcp(.x$value)), pluck("posterior.prob")) %>%
  4. unlist()
  5. df$posterior_prob_var <- posterior_prob
  6. head(df)
  7. # date value product posterior_prob_var
  8. # 1 2023-05-10 21.90542 A 0.002
  9. # 2 2023-05-09 19.61293 A 0.000
  10. # 3 2023-05-08 20.46336 A 0.002
  11. # 4 2023-05-07 21.22534 A 0.000
  12. # 5 2023-05-06 19.37578 A 0.000
  13. # 6 2023-05-05 18.94408 A 0.002

(Note: This is the translated code section. If you have any further questions or need additional assistance, please let me know.)

英文:

I'm not familiar with the bcp package but does this give you what you want?

  1. posterior_prob &lt;- map(df %&gt;%
  2. group_by(product) %&gt;%
  3. group_map(~ bcp(.x$value)), pluck(&quot;posterior.prob&quot;)) %&gt;%
  4. unlist()
  5. df$posterior_prob_var &lt;- posterior_prob
  6. head(df)
  7. # date value product posterior_prob_var
  8. # 1 2023-05-10 21.90542 A 0.002
  9. # 2 2023-05-09 19.61293 A 0.000
  10. # 3 2023-05-08 20.46336 A 0.002
  11. # 4 2023-05-07 21.22534 A 0.000
  12. # 5 2023-05-06 19.37578 A 0.000
  13. # 6 2023-05-05 18.94408 A 0.002

huangapple
  • 本文由 发表于 2023年5月10日 19:06:20
  • 转载请务必保留本文链接:https://go.coder-hub.com/76217659.html
匿名

发表评论

匿名网友

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

确定