Skip to content

Plot in function

5 messages · Pauli, R. Michael Weylandt, David Winsemius +1 more

#
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?

Best regards
Pauli



--
View this message in context: http://r.789695.n4.nabble.com/Plot-in-function-tp4648576.html
Sent from the R help mailing list archive at Nabble.com.
#
On Tue, Nov 6, 2012 at 4:44 PM, Pauli <paul.kraus85 at web.de> wrote:
It looks like there's a problem in the call to ruin.plot() but since
we don't have the source for that, we can't really help you out.

Can you cite the code you're using / provide it in full if it's
original to you? A fully reproducible example would be awesome:

http://stackoverflow.com/questions/5963269/how-to-make-a-great-r-reproducible-example

Cheers,
Michael
#
On Nov 6, 2012, at 8:44 AM, Pauli wrote:

            
I do not see the function ruin.plot defined. And sure enough, I get:
Error: could not find function "ruin.plot"

#######################
and provide commented, minimal, self-contained, reproducible code.
# ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
--

David Winsemius, MD
Alameda, CA, USA
#
On 11/07/2012 03:44 AM, Pauli wrote:
Hi Pauli,
Since you received the "unused argument" error message, you may have 
defined the "ruin.plot" function somewhere without the "col=" argument. 
I tried changing the call to "ruin.plot" to simply "plot", but your k 
and x values are of different lengths, so they won't plot anyway. I 
think what you may be trying to achieve is more like this:

ruin<-function( N = 1, n = 10, k = 1, p = 1 / 2,
  draw = FALSE, ask = FALSE ){

  if(draw) par( ask = ask )
  r <- 0
  plot(0,xlim=c(0,N),ylim=c(-20,k),type="n",main="Gambler's Ruin plot")
  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) {
     points(i, x[which.min(x)], col = "red" )
     text(i,x[which.min(x)]-2,"ruin!")
    }
   }
   else if(draw) points(i, x[which.min(x)], col="green")
  }
  return(r / N)
}

ruin(N=100,n=1000,k=50,draw=TRUE,ask=TRUE)

Jim
3 days later