2011-05-27 89 views
6

我想補充一個拋物線線,使用R表示95%置信區間,以這種拋硬幣的情節:添加95%置信區間累積情節

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

這裏是什麼,我正在尋找一個例子:graph

更新: @ bill_080的回答非常好。不過我已經計算出100000次擲硬幣:

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

,我真的想只是95%的限制添加到情節:toss

plot.ts(cumsum(100ktoss)) 

花了幾個小時來計算我的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 ... 

我想爲每個段添加相同的95%限制,就像這樣。 現在解決了

@ bill_080非常感謝。這是最終產品:

cum

回答

9

試試這個。所有循環都是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")) 

enter image description here

編輯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) 

enter image description here

編輯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 

enter image description here

+0

@ bill_080真的,真的很好。如果你查看我的個人資料,你會看到我有一個投擲硬幣的情節,你的是一個美麗。對我來說唯一可能有點複雜。 :) – 2011-05-27 20:06:07

+1

@RSoul,我添加了關聯代碼的另一個情節。它顯示了5%線的上限/下限。這只是下注數的平方根乘以你想要的概率的乘數。在代碼中,'qnorm()'函數爲您提供了乘數。對於5%,'qnorm(0.05)'給出-1.644,'qnorm(0.95)'給出1.644。 – 2011-05-27 20:56:04

+0

@ bill_080非常好。正是我想要的。這就是爲什麼我在論文中承認了StackOverflow.com的用戶。非常感謝。我可以使用你更有趣的情節。我還沒有決定。 – 2011-05-27 21:04:59