【问题标题】:Find log difference using values based on max difference使用基于最大差异的值查找对数差异
【发布时间】:2020-03-27 12:14:17
【问题描述】:

这应该很简单,但我很难做到。我使用生成汇总列max_predicted_diff 的基本diff() 函数创建了一个汇总输出,其中包括跨越15 个值(lag = 15) 的值的最大差异。但是,我现在想使用那些用于计算 max_predicted_diff 的相同值,但通过计算它们之间的对数差异。我从dse 包中找到了diffLog,但随后我遇到了dplyr 问题,因为我使用了与计算max_predicted_diff 相同的值。


示例

print(head(df,10))

   Sample Predicted
1   apple 0.7356986
2   apple 0.7388222
3   apple 0.7419447
4   apple 0.7450658
5   apple 0.7481857
6   apple 0.7513042
7   apple 0.7544212
8   apple 0.7575368
9   apple 0.7606509
10  apple 0.7637635
library(dplyr)

df %>% summarise(max_predicted_diff = max(diff(Predicted, lag = 15)))

  max_predicted_diff
1               0.04670478

如何找出用于找到 0.04670478 答案的值?然后我如何总结使用的这两个值的日志?我使用max() 来查找max_predicted_diff,但是我将使用什么汇总函数来解决日志值的差异?我不认为max() 在这里工作,因为我认为diffLog 不会使用与max_predicted_diff 相同的值(只是loged)?


使用dse 包中的diffLog(),我可以轻松计算对数差异,但我不知道它使用了哪些值以及如何使用与查找max_predicted_diff 相同的值。

library(dse)

df %>% summarise(max_predicted_diff_log = max(diffLog(Predicted, lag = 15)))
  max_predicted_diff_log

1               0.06154992

可重现的数据

df structure(list(Sample = c("apple", "apple", "apple", "apple", 
"apple", "apple", "apple", "apple", "apple", "apple", "apple", 
"apple", "apple", "apple", "apple", "apple", "apple", "apple", 
"apple", "apple", "apple", "apple", "apple", "apple", "apple", 
"apple", "apple", "apple", "apple", "apple", "apple", "apple", 
"apple", "apple", "apple", "apple", "apple", "apple", "apple", 
"apple", "apple", "apple", "apple", "apple", "apple", "apple", 
"apple", "apple", "apple", "apple", "apple", "apple", "apple", 
"apple", "apple", "apple", "apple", "apple", "apple", "apple", 
"apple", "apple", "apple", "apple"), Predicted = c(0.735698569365871, 
0.738822222617743, 0.741944657028027, 0.74506582323819, 0.748185672193904, 
0.751304155146149, 0.754421223652273, 0.75753682957702, 0.760650925093515, 
0.76376346268421, 0.766874395141795, 0.76998367557007, 0.773091257384776, 
0.776197094314395, 0.779301140400904, 0.782403350000502, 0.785503677784295, 
0.788602078738943, 0.791698508167276, 0.794792921688872, 0.797885275240596, 
0.800975525077113, 0.804063627771357, 0.807149540214967, 0.810233219618698, 
0.813314623512785, 0.81639370974728, 0.819470436492359, 0.822544762238589, 
0.825616645797166, 0.828686046300123, 0.831752923200501, 0.83481723627249, 
0.837878945611542, 0.84093801163445, 0.843994395079395, 0.847048057005967, 
0.850098958795148, 0.853147062149278, 0.856192329091979, 0.859234721968058, 
0.862274203443374, 0.865310736504688, 0.868344284459473, 0.871374810935701, 
0.874402279881605, 0.877426655565415, 0.880447902575054, 0.883465985817829, 
0.886480870520078, 0.889492522226799, 0.892500906801256, 0.895505990424551, 
0.898507739595181, 0.901506121128565, 0.904501102156547, 0.907492650126881, 
0.910480732802683, 0.913465318261867, 0.91644637489656, 0.919423871412485, 
0.922397776828334, 0.925368060475109, 0.928334691995449)), row.names = c(NA, 
64L), class = "data.frame")

【问题讨论】:

  • 我对@9​​87654342@一无所知,但至于您的“使用哪些值来找到答案”,我建议which.max
  • 如果你想找位置,试试df %>% mutate(rn = row_number(), new = replace_na(Predicted - lag(Predicted, n = 15), -999)) %>% filter(new == max(new))

标签: r dplyr diff logarithm summarize


【解决方案1】:

这就是你所追求的吗?

library(dplyr, warn.conflicts = FALSE)

df %>% 
  mutate(
    lag15 = lag(Predicted, n = 15),
    lag_diff = Predicted -  lag15
  ) %>%
  filter(lag_diff == max(lag_diff, na.rm = TRUE))

#>   Sample Predicted     lag15   lag_diff
#> 1  apple 0.7824034 0.7356986 0.04670478

【讨论】:

  • 不知道为什么我的 cmets 不断被删除,但这非常有效!谢谢!
猜你喜欢
  • 2021-12-24
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2016-11-17
  • 2023-04-02
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多