小编Fra*_*fka的帖子

绘制具有不同大小的箱的概率热图/ hexbin

这与另一个问题有关:绘制加权频率矩阵.

我有这个图形(由R中的代码生成): 多重采样

#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", …
Run Code Online (Sandbox Code Playgroud)

plot r probability heatmap

14
推荐指数
1
解决办法
2835
查看次数

绘制加权频率矩阵

这个问题与我之前提出的两个不同的问题有关:

1)重现频率矩阵图

2)为累积图添加95%置信区间

我希望在R中重现这个情节:boringmatrix

我已经做到这一点,使用图形下面的代码:的multiplot

#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 …
Run Code Online (Sandbox Code Playgroud)

r probability frequency matrix weighted

13
推荐指数
1
解决办法
955
查看次数

我可以列出按日期排序的对象吗?

这对我来说非常有用.我可以ls()按日期(最后修改或类似的东西)对输出进行排序吗?

背景:我有一个非常凌乱的工作空间,各种各样的data.frames,variablesplots.我需要找到一个data.frame,我不记得我称它为什么,只是我最近正在研究它.因此ls()按修改日期排序将帮助我找出我称之为的内容.

澄清一下:这不是关于使用的问题history().我经常使用历史记录,并且在我认为可能需要时经常保存它.在这种情况下,历史不包括所需的时间,所以我找不到data.frame使用history().

附加:除了找到最近创建的任务之外data.frame,一般来说,能够ls()根据日期排序是非常有用的.我在工作区中有超过100个物体,我已经工作了2年多(搜索2年history也听起来不是很有趣).如果可以按时间顺序对这些对象进行排序,那么我会想到哪些对象是较新的(可能是编辑过的),哪些是较旧的(也许是原始的).

我试图通过将单个对象缓慢移动到较小的相关工作空间来改进我的工作流程.但是这个过程需要时间,而且基本上没有意义(因为我接近数据分析的结束).

r

7
推荐指数
1
解决办法
1038
查看次数

为累积图添加95%置信区间

我想添加一条抛物线,用R表示这个抛硬币图的95%置信限:

x  <- sample(c(-1,1), 60000, replace = TRUE)
plot.ts(cumsum(x), ylim=c(-250,250))
Run Code Online (Sandbox Code Playgroud)

这是我正在寻找的一个例子:图形

更新: @ bill_080的答案非常好.但是我已经计算了10万枚硬币投掷:

str(100ktoss)
num [1:100000] -1 1 1 1 -1 -1 1 -1 -1 -1 ...
Run Code Online (Sandbox Code Playgroud)

而且我真的想在这个情节中添加95%的限制:折腾

plot.ts(cumsum(100ktoss))
Run Code Online (Sandbox Code Playgroud)

花了几个小时来计算我的100K硬币投掷,当我尝试用@ bill_080的代码复制时,我的内存耗尽(100,000).

最后更新:好的.最后一个问题 我有一个几轮累积命中的情节,在一个图表上,每一轮的开始被钳制在零(实际上是1或-1,取决于它是赢还是输).

>str(1.ts)  
Time-Series [1:35] from 1 to 35: 1 2 1 2 3 4 5 4 5 6 ...  
>str(2.ts)  
Time-Series [1:150] from 36 to 185: -1 0 1 0 -1 -2 -1 0 1 2 ...  
Run Code Online (Sandbox Code Playgroud)

我想为每个段添加相同的95%限制,就像这样.现在解决了:

@ bill_080非常感谢.这是最终产品:

附带

plot r sum

6
推荐指数
1
解决办法
1646
查看次数

R传奇中的小型股?

这是一个简单的问题.

我试图用R中的小大写字母写一个带有文本的图例.

我可以使用tikzDevice编写绘图并在LaTex中手动将绘图更改为小型大写,但我想知道R本身是否可行

谢谢.

这是我目前使用的R代码:

legend("bottomright", inset=.05, c(expression(Delta*ZRT1), expression(Delta*ZRT2)), lty=1:2, pch=1:2)

这是我试图进入R传奇的LaTex表达式:

\Delta Z\textsc{rt\oldstylenums{1}}

text r smallcaps

6
推荐指数
1
解决办法
2069
查看次数

根据其他数据框创建列

我有一个看似简单的要求,但我似乎无法达到预期的效果.我的数据帧相当大,所以我只是给出一个截图:在此输入图像描述

我想要的是:if ps2c$ps == present2$pn then ps2c$sf == present2$sf.

如果不清楚,每个ps2c$ps都有一个present2$pn(present2只是平均数据).

r

5
推荐指数
1
解决办法
1793
查看次数

从/ dev/random生成随机整数

这不是功课.我想在R中从/ dev/random生成一个随机整数序列(0:9之间的50位数字).我有一个硬件熵密钥.

我找到了两个"想法",我无法向我提供我所追求的数字:

1)RDieHarder.似乎允许访问/ dev/random,但我不能让它生成我需要的整数序列.例如

>library(RDieHarder)  
>x  <-dieharder(rng="/dev/urandom", psample=50) #urandom used for example
Run Code Online (Sandbox Code Playgroud)

2)精度包可以提供真正的随机数,但似乎已过时,我无法看到如何从/ dev/random进行排序.例如

>library(accuracy)
>x=runifT(50)
Run Code Online (Sandbox Code Playgroud)

是的我已阅读Knuth等并了解TRNG的问题(因此硬件熵密钥).

还有其他想法吗?谢谢.

random r

5
推荐指数
1
解决办法
1797
查看次数

ggplot上的抛物线参考线

我正在将我的情节转移到ggplot中.几乎没有这个(代码来自前一个问题):

ggplot情节应该是这样的

#Set the bet sequence and the % lines
betseq <- 0:700 #0 to 700 bets
perlin <- 0.05 #Show the +/- 5% lines on the graph

#Define a function that plots the upper and lower % limit lines
dralim <- function(stax, endx, perlin) {
  lines(stax:endx, qnorm(1-perlin)*sqrt((stax:endx)-stax))
  lines(stax:endx, qnorm(perlin)*sqrt((stax:endx)-stax))
}

#Build the plot area and draw the vertical dashed lines
plot(betseq, rep(0, length(betseq)), type="l", ylim=c(-50, 50), main="", xlab="Trial Number", ylab="Cumulative Hits")
abline(h=0)
abline(v=35, lty="dashed") #Seg 1
abline(v=185, lty="dashed") …
Run Code Online (Sandbox Code Playgroud)

r ggplot2

5
推荐指数
1
解决办法
826
查看次数

计算硬币翻转序列中的交替次数

我有一系列的1和0,我想计算交替的数量.例如

x <- rbinom(10, 1, 1/2)
> x
 [1] 0 0 1 1 1 1 1 0 1 0
Run Code Online (Sandbox Code Playgroud)

因此,我想计算(在R中)序列从1到0交替(或翻转)的次数.在上述顺序中,交替次数(手动计数)为4.

simulation r coin-flipping

4
推荐指数
2
解决办法
1657
查看次数

ggplot中的概率热图

我在一年前问了这个问题并得到了这个"概率热图"的代码: 热图

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), …
Run Code Online (Sandbox Code Playgroud)

r probability ggplot2

4
推荐指数
1
解决办法
1546
查看次数