[R] simulation

David Winsemius dwinsemius at comcast.net
Fri Apr 6 05:51:27 CEST 2012


On Apr 5, 2012, at 10:57 PM, Christopher Kelvin wrote:

> Hello,
> i need to simulate 100 times, n=40 ,
> the distribution has 90% from X~N(0,1) + 10% from X~N(20,10)
> Is my loop below correct?
> Thank you
>
> n=40
> for(i in 1:100){
> x<-rnorm(40,0,1)  # 90% of n
>

You are overwriting x and y and at the end of that loop you will only  
have two vectors of length 40 each. If you wanted a 90 10 weighting  
then why not lengths of 36 and 4???

To do repeated simulations you will find this help page  useful:

?replicate

> z<-rnorm(40,20,10)  # 10% of n
> }
> x+z

At this point you should not be using "+" but rather the c() function  
if you are trying to join those two vectors. I think you need to spend  
more time working through "Introduction to R".


>
>
> ______________________________________________
> 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.

David Winsemius, MD
West Hartford, CT



More information about the R-help mailing list