[R] Hi, Coding problem

Uwe Ligges ligges at statistik.tu-dortmund.de
Thu Feb 26 09:54:15 CET 2009



Ssophia wrote:
> 
>   Hi, there 
> 
>  
> 
>   Below is my code to one Homework question. I couldn't come up with the reasonable answer. 
                             ^^^^^^^^

PLEASE do read the posting guide http://www.R-project.org/posting-guide.html

>     could you please help me to figure out what is the problem with my code? 
>     thank you 
> 
> 
>     Question is Coding P{X=j} =(1/2)^(j+1) + (1/2) *2^(j-1)/3^j
>     my code is 
>     sim <- function(n.gen){
>     urandom <- runif(n.gen)
>     sim.vector <- rep(0,n.gen)
>     for(j in 1:n.gen){
>      i <- 1
>      p <- 5/12
>      F <- p
>      while(urandom[j] >= F){
>       p <- p*((1/2)^(i+1)+1/3*(2/3)^i)/((1/2)^i+(1/2)*(2/3)^i)
>       F <- F+p
>       i<-i+1
>       }
>      sim.vector[j] <- i
>     }
>     # output
>     sim.vector
>     }
>     
>      
>     
>     result is 
>     
>        1    2    3    4    5    6    7    8   11 
>     0.37 0.22 0.16 0.13 0.05 0.02 0.03 0.01 0.01 
>     
>      
>     
>     always, there are some numbers missing, it should be continuous. 
>     
>     why 9 and 10 are missing 
>     
>     thank you 
>     
>     
>     
>     sophia
>     
>   
> 
> _________________________________________________________________
> [[elided Hotmail spam]]
> 
> 	[[alternative HTML version deleted]]
> 
> ______________________________________________
> R-help at r-project.org mailing list
> https://stat.ethz.ch/mailman/listinfo/r-help
> PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
> and provide commented, minimal, self-contained, reproducible code.




More information about the R-help mailing list