【问题标题】:Add 95% confidence limits to cumulative plot向累积图添加 95% 置信限
【发布时间】:2011-05-27 17:13:17
【问题描述】:

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

x  <- sample(c(-1,1), 60000, replace = TRUE)
plot.ts(cumsum(x), ylim=c(-250,250))

这是我正在寻找的示例:

更新:@bill_080 的回答非常好。但是我已经计算了 100,000 次抛硬币:

str(100ktoss)
num [1:100000] -1 1 1 1 -1 -1 1 -1 -1 -1 ...

我真的很想在该图中添加 95% 的限制:

plot.ts(cumsum(100ktoss))

计算我的 100,000 次抛硬币需要几个小时,当我尝试使用 @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 ...  

我想为每个段添加相同的 95% 限制,就像这样。 现已解决

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

【问题讨论】:

    标签: r plot sum


    【解决方案1】:

    试试这个。所有循环都是for循环,所以你可以轻松添加更多计算。

    #Set the number of bets and number of trials and % lines
    numbet <- 6000 #6000 bets
    numtri <- 1000 #Run 1000 trials of the 6000 bets
    perlin <- 0.05 #Show the +/- 5% lines on the graph
    rantri <- 60 #The 60th trial (just a random trial to be drawn)
    
    #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(-1,1), numbet, replace = TRUE)
      xcum[,i] <- cumsum(x)
    }
    
    #Plot the trials as transparent lines so you can see the build up
    matplot(xcum, type="l", xlab="Number of Bets", ylab="Cumulative Sum", main="Cumulative Results", col=rgb(0.01, 0.01, 0.01, 0.02))
    grid()
    
    #Sort the trials of each bet so you can pick out the desired %
    xcumsor <- xcum
    for (i in 1:numbet) {
      xcumsor[i,] <- xcum[i,order(xcum[i,])]
    }
    
    #Draw the upper/lower limit lines and the 50% probability line     
    lines(xcumsor[, perlin*numtri], type="l", lwd=2, col=rgb(1, 0.0, 0.0)) #Lower limit
    lines(xcumsor[, 0.5*numtri], type="l", lwd=3, col=rgb(0, 1, 0.0)) #50% Line
    lines(xcumsor[, (1-perlin)*numtri], type="l", lwd=2, col=rgb(1, 0.0, 0.0)) #Upper limit
    
    #Show one of the trials
    lines(xcum[, rantri], type="l", lwd=1, col=rgb(1, 0.8, 0)) #Random trial
    
    #Draw the legend
    legend("bottomleft", legend=c("Various Trials", "Single Trial", "50% Probability", "Upper/Lower % Limts"), bg="white", lwd=c(1, 1, 3, 2), col=c("darkgray", "orange", "green", "red"))
    

    编辑1 ============================================== ==============

    如果您只是想绘制 +/- 5% 的线,它只是一个平方根函数。代码如下:

    #Set the bet sequence and the % lines
    betseq <- 1:100000 #1 to 100,000 bets
    perlin <- 0.05 #Show the +/- 5% lines on the graph
    
    #Calculate the Upper and Lower limits using perlin
    #qnorm() gives the multiplier for the square root
    upplim <- qnorm(1-perlin)*sqrt(betseq)
    lowlim <- qnorm(perlin)*sqrt(betseq)
    
    #Get the range for y
    yran <- range(upplim, lowlim)
    
    #Plot the upper and lower limit lines
    plot(betseq, upplim, ylim=yran, type="l", xlab="", ylab="")
    lines(betseq, lowlim)
    

    编辑2 ============================================= =====

    要在正确的位置添加抛物线,如果定义一个函数可能会更容易。请记住,因为新函数 (dralim) 使用 lines,所以在调用 dralim 之前,绘图必须存在。使用一些与编辑 1 中的代码相同的变量:

    #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") #Seg 2
    abline(v=385, lty="dashed") #Seg 3
    abline(v=485, lty="dashed") #Seg 4
    abline(v=585, lty="dashed") #Seg 5
    
    #Draw the % limit lines that correspond to the vertical dashed lines by calling the
    #new function dralim.
    dralim(0, 35, perlin) #Seg 1
    dralim(36, 185, perlin) #Seg 2
    dralim(186, 385, perlin) #Seg 3
    dralim(386, 485, perlin) #Seg 4
    dralim(486, 585, perlin) #Seg 5
    dralim(586, 701, perlin) #Seg 6
    

    【讨论】:

    • @bill_080 真的,真的很好。如果您查看我的个人资料,您会发现我喜欢掷硬币的情节,而您的则是美女。唯一对我来说可能有点太复杂了。 :)
    • @RSoul,我添加了另一个带有相关代码的图。它显示了 5% 线的上限/下限。它只是下注数字的平方根乘以您想要的百分比概率的乘数。在代码中,qnorm() 函数为您提供了乘数。对于 5%,qnorm(0.05) 给出 -1.644,qnorm(0.95) 给出 1.644。
    • @bill_080 那太好了。正是我想要的。这就是我在论文中感谢 StackOverflow.com 用户的原因。非常感谢。我可能会使用你更有趣的情节。我还没有决定。
    • @bill_080 我可以计算每一轮的单独抛物线,但不能将它们放在 x 轴上的正确位置。
    • @RSoul,我用编辑 2 更新了我的答案。将来,如果您为最终更新提出一个新问题,可能会更好。您仍然可以参考原始问题。
    猜你喜欢
    • 2022-08-24
    • 2015-11-17
    • 2012-12-07
    • 2013-02-11
    • 1970-01-01
    • 2022-11-25
    • 2019-10-07
    • 1970-01-01
    相关资源
    最近更新 更多