gpt4 book ai didi

r - 用于矩阵和 image() 或 heatmap() 的分箱数据

转载 作者:行者123 更新时间:2023-12-04 12:19:16 25 4
gpt4 key购买 nike

我有一个包含三列的数据框,我想制作数据的图像/热图。

三列是 pe、vix 和 ret,其中 pe 和 vix 是 x 和 y,ret 是 z。

数据框中有 220 行,所以我想尽可能对数据进行分箱,范围如下。

关于如何对 x 和 y 数据进行分箱以及创建用于 image() 的矩阵有什么建议吗?

> range(matr$pe)
[1] 13.32 44.20
> range(matr$vix)
[1] 10.42 59.89
> range(matr$ret)
[1] -0.09274936 0.04693118
> class(matr)
[1] "data.frame"
> head(matr)
pe vix ret
1 20.86 13.16 -0.002931561
2 20.46 12.53 -0.003546889
3 20.52 12.42 0.006339165
4 20.61 13.47 0.009683174
5 20.57 11.26 -0.002666668
6 20.81 11.73 0.002895003

最佳答案

这就是我最终做的事情。我使用 akima 包中的 interp() 函数来创建适当分箱的矩阵对象。它似乎完成了数据框的分箱和“矩阵化”工作。附带说明一下,为了制作带有图例的热图,我最终使用了 fields 包中的 image.plot() 方法。这是代码:

par(bg = 3)
image.plot(s,xlab="P/E Ratio", ylab="VIX",
main="Contour Map of SPY Returns vs P/E Ratio and Vix")
abline(v=(seq(0,100,5)), col=6, lty="dotted")
abline(h=(seq(0,100,5)), col=6, lty="dotted")
contour(s, add=TRUE)

以及任何感兴趣的人的最终产品: enter image description here

感谢大家的帮助和建议。

关于r - 用于矩阵和 image() 或 heatmap() 的分箱数据,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7230241/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com