Plot in function
On Nov 6, 2012, at 8:44 AM, Pauli wrote:
Hello,
I am a R beginner and I have a question about a litte function I found.
Here is the code:
# Gambler's Ruin Problem.
# seed capital: k
# rounds: n
# probability of success: p
# number of trials: N
# graphical output (yes/no): draw
# Wait for new graphic (yes/no): ask
ruin<- function( N = 1, n = 10, k = 1, p = 1 / 2,
draw = FALSE, ask = FALSE ){
if(draw)
par( ask = ask )
r <- 0
for( i in 1:N ){
x <- k + cumsum (sample( c(-1, 1),replace = TRUE, n,prob = c(1-p, p)))
if( min(x) <= 0 ){
r <- r + 1
if(draw)
ruin.plot( k, x, col = "red",main = paste(i, "th trial: ruin!" ) )
}
else if(draw)
ruin.plot( k, x, main = paste( i, "th trial: no ruin" ),ylim =
c( 0, max(x) ) )
}
return(r / N)
}
Now I want to start it with for example
"ruin(N=100,n=1000,k=50,draw=TRUE,ask=TRUE)" but i received the message,
that there is an unused argument: (col = "red",main = paste(i, "th trial:
ruin!" ) ).
What is wrong with the code?
I do not see the function ruin.plot defined. And sure enough, I get:
ruin(N=100,n=1000,k=50,draw=TRUE,ask=TRUE)
Error: could not find function "ruin.plot" ####################### and provide commented, minimal, self-contained, reproducible code. # ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ -- David Winsemius, MD Alameda, CA, USA