gpt4 book ai didi

r - 用不同大小的 bin 绘制概率热图/hexbin

转载 作者:行者123 更新时间:2023-12-03 22:29:03 27 4
gpt4 key购买 nike

这与另一个问题有关:Plot weighted frequency matrix .

我有这个图形(由下面的 R 代码生成):multisample

#Set the number of bets and number of trials and % lines
numbet <- 36
numtri <- 1000
#Fill a matrix where the rows are the cumulative bets and the columns are the trials
xcum <- matrix(NA, nrow=numbet, ncol=numtri)
for (i in 1:numtri) {
x <- sample(c(0,1), numbet, prob=c(5/6,1/6), replace = TRUE)
xcum[,i] <- cumsum(x)/(1:numbet)
}
#Plot the trials as transparent lines so you can see the build up
matplot(xcum, type="l", xlab="Number of Trials", ylab="Relative Frequency", main="", col=rgb(0.01, 0.01, 0.01, 0.02), las=1)

我非常喜欢这个图的构建方式,它显示更频繁的路径比稀有路径更暗(但对于打印演示来说不够清晰)。我想做的是为数字生成某种 hexbin 或热图。仔细想想,情节似乎必须包含不同大小的垃圾箱(参见我的信封草图背面):

binsketch

我的问题是: 如果我使用上面的代码模拟一百万次运行,我如何将它呈现为热图或 hexbin,以及草图中所示的不同大小的 bin?

澄清一下:我不想依靠透明度来显示审判通过情节的一部分的稀有性。相反,我想用热来表示稀有,并显示一条常见的路径为热(红色),而罕见的路径为冷(蓝色)。另外,我不认为垃圾箱的大小应该相同,因为第一个试验只有两个路径可以放置的地方,但最后一个有更多。因此,我选择了一个不断变化的 bin 比例,基于这个事实。 本质上,我正在计算路径通过单元格的次数(第 1 列中的 2 次,第 2 列中的 3 次等),然后根据它通过的次数为单元格着色。

更新:我已经有一个类似于@Andrie 的图,但我不确定它是否比顶部图清晰得多。我不喜欢这张图的不连续性(以及为什么我想要某种热图)。我认为因为第一列只有两个可能的值,所以它们之间不应该有巨大的视觉差距等等。因此我设想了不同大小的垃圾箱。我仍然觉得分箱版本会更好地显示大量样本。

plot2

更新:此 website概述了绘制热图的过程:

To create a density (heatmap) plot version of this we have to effectively enumerate the occurrence of these points at each discrete location in the image. This is done by setting a up a grid and counting the number of times a point coordinate "falls" into each of the individual pixel "bins" at every location in that grid.



也许该网站上的某些信息可以与我们已有的信息相结合?

更新:我拿了安德里写的一些内容 question , 达到这一点,这与我的构想非常接近:
heatmap
numbet <- 20
numtri <- 100
prob=1/6
#Fill a matrix
xcum <- matrix(NA, nrow=numtri, ncol=numbet+1)
for (i in 1:numtri) {
x <- sample(c(0,1), numbet, prob=c(prob, 1-prob), replace = TRUE)
xcum[i, ] <- c(i, cumsum(x)/cumsum(1:numbet))
}
colnames(xcum) <- c("trial", paste("bet", 1:numbet, sep=""))

mxcum <- reshape(data.frame(xcum), varying=1+1:numbet,
idvar="trial", v.names="outcome", direction="long", timevar="bet")

#from the other question
require(MASS)
dens <- kde2d(mxcum$bet, mxcum$outcome)
filled.contour(dens)

我不太明白发生了什么,但这似乎更像是我想要制作的(显然没有不同大小的垃圾箱)。

更新:这与此处的其他图相似。不太对劲:

hexbin
plot(hexbin(x=mxcum$bet, y=mxcum$outcome))

最后一试。如上:
enter image description here
image(mxcum$bet, mxcum$outcome)

这很好。我只是想让它看起来像我的手绘草图。

最佳答案

编辑

我认为以下解决方案可以满足您的要求。

(注意这很慢,尤其是 reshape 步骤)

numbet <- 32
numtri <- 1e5
prob=5/6
#Fill a matrix
xcum <- matrix(NA, nrow=numtri, ncol=numbet+1)
for (i in 1:numtri) {
x <- sample(c(0,1), numbet, prob=c(prob, 1-prob), replace = TRUE)
xcum[i, ] <- c(i, cumsum(x)/cumsum(1:numbet))
}
colnames(xcum) <- c("trial", paste("bet", 1:numbet, sep=""))

mxcum <- reshape(data.frame(xcum), varying=1+1:numbet,
idvar="trial", v.names="outcome", direction="long", timevar="bet")


library(plyr)
mxcum2 <- ddply(mxcum, .(bet, outcome), nrow)
mxcum3 <- ddply(mxcum2, .(bet), summarize,
ymin=c(0, head(seq_along(V1)/length(V1), -1)),
ymax=seq_along(V1)/length(V1),
fill=(V1/sum(V1)))
head(mxcum3)

library(ggplot2)

p <- ggplot(mxcum3, aes(xmin=bet-0.5, xmax=bet+0.5, ymin=ymin, ymax=ymax)) +
geom_rect(aes(fill=fill), colour="grey80") +
scale_fill_gradient("Outcome", formatter="percent", low="red", high="blue") +
scale_y_continuous(formatter="percent") +
xlab("Bet")

print(p)

enter image description here

关于r - 用不同大小的 bin 绘制概率热图/hexbin,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7305803/

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