[R] simple simulation problem

ripley@stats.ox.ac.uk ripley at stats.ox.ac.uk
Sun Mar 17 23:06:43 CET 2002


On Sun, 17 Mar 2002, Yannick Wurm wrote:

> Help!
>
> I'm desperate! I don't understand why my function won't work!
> Its for a simple simulation exercise: robin hood fires an arrow
> at a target with 50cm radius, and his aim follows a centered
> normal distribution with a standard deviation of 40cm (on x and
> y axis, independently). Here he takes 10 shots and is awarded
> points if he hits close to the center.
>
> For some reason, this function always returns a score of 870?!?!
> I'm probably missing something really quite obvious but I just
> can't figure out what it is.

All your conditions are true (and so each arrow scores 87).  For, say,

40 < x < 50

is evaluated as (40 < x) < 50, and (40 < x) is coerced to 0 (false) or 1
(true).

I think you wanted  (40 < x) && (x < 50)

Better, you can vectorize this, e.g.

2*(d < 50) + 3*(d < 40) + 5*(d < 25) + 10*(d < 15) + 30*(d < 5)

(as you will never get any of those values exactly and probably mean <=
anyway).

You can simulate from d directly (d^2 is exponential) and of course
compute the distribution of the score without simulation.

As a matter of style, `score_sum(score,2)' is about the least readable
way to write  `score <- score + 2' that I can imagine.

> thanks for your help (you guys are great!),
>
> Yannick.
>
>
> function(){
> score_0
> x_rnorm(10, mean=0, sd=40)
> y_rnorm(10, mean=0, sd=40)
> d_sqrt(x^2 + y^2)
> for(i in 1:10) {
> 	if (d[i] > 50) print("désolé\n")
> 	if (40 < d[i] < 50) score_sum(score,2)
> 	if (25 < d[i] < 40) score_sum(score,5)
> 	if (15 < d[i] < 25) score_sum(score,10)
> 	if ( 5 < d[i] < 15) score_sum(score,20)
> 	if ( 0 < d[i] <  5) score_sum(score,50)
> }
> print(d)
> print(score)
> }
>
> ------------------------------------
>       yannick.wurm at insa-lyon.fr
> http://homepage.mac.com/yannickwurm/
> tel: 06.16.41.71.92    icq: 22044361
>
> -.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-
> r-help mailing list -- Read http://www.ci.tuwien.ac.at/~hornik/R/R-FAQ.html
> Send "info", "help", or "[un]subscribe"
> (in the "body", not the subject !)  To: r-help-request at stat.math.ethz.ch
> _._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._
>

-- 
Brian D. Ripley,                  ripley at stats.ox.ac.uk
Professor of Applied Statistics,  http://www.stats.ox.ac.uk/~ripley/
University of Oxford,             Tel:  +44 1865 272861 (self)
1 South Parks Road,                     +44 1865 272860 (secr)
Oxford OX1 3TG, UK                Fax:  +44 1865 272595

-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-
r-help mailing list -- Read http://www.ci.tuwien.ac.at/~hornik/R/R-FAQ.html
Send "info", "help", or "[un]subscribe"
(in the "body", not the subject !)  To: r-help-request at stat.math.ethz.ch
_._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._



More information about the R-help mailing list