[R] Simulation based on runif to get mean

ruipbarradas ruipbarradas at sapo.pt
Tue Jan 30 12:17:11 CET 2018


Hello,
Right. Missed that one.
Rui Barradas


Enviado a partir do meu smartphone Samsung Galaxy.-------- Mensagem original --------De: Eric Berger <ericjberger at gmail.com> Data: 30/01/2018  10:12  (GMT+00:00) Para: Rui Barradas <ruipbarradas at sapo.pt> Cc: Daniel Nordlund <djnordlund at gmail.com>, smart hendsome <putra_autumn86 at yahoo.com>, r-help at r-project.org Assunto: Re: [R] Simulation based on runif to get mean 
Or a shorter version of Rui's approach:
set.seed(2511)    # Make the results reproduciblefun <- function(n){  f <- function(){    c(mean(runif(5,1,10)),mean(runif(5,10,20)))  }  replicate(n, f())}fun(10)
On Tue, Jan 30, 2018 at 12:03 PM, Rui Barradas <ruipbarradas at sapo.pt> wrote:
Hello,



Another way would be to use ?replicate and ?colMeans.





set.seed(2511)    # Make the results reproducible



fun <- function(n){

    f <- function(){

        a <- runif(5, 1, 10)

        b <- runif(5, 10, 20)

        colMeans(cbind(a, b))

    }

    replicate(n, f())

}



fun(10)



Hope this helps,



Rui Barradas



On 1/30/2018 8:58 AM, Daniel Nordlund wrote:


On 1/29/2018 9:03 PM, smart hendsome via R-help wrote:


Hello everyone,

I have a question regarding simulating based on runif.  Let say I have generated matrix A and B based on runif. Then I find mean for each matrix A and matrix B.  I want this process to be done let say 10 times. Anyone can help me.  Actually I want make the function that I can play around with the number of simulation process that I want. Thanks.

Eg:

a <- matrix(runif(5,1, 10))



b <- matrix(runif(5,10, 20))



c <- cbind(a,b); c



mn <- apply(c,2,mean); mn



Regards,

Zuhri



    [[alternative HTML version deleted]]



______________________________________________

R-help at r-project.org mailing list -- To UNSUBSCRIBE and more, see

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.






Here is a straight forward implementation of your code in a function with a parameter for the number simulations you want to run.



sim <- function(n){

   mn <- matrix(0,n, 2)

   for(i in 1:n) {

     a <- runif(5,1, 10)

     b <- runif(5,10, 20)

     c <- cbind(a,b)

     mn[i,] <- apply(c, 2, mean)

     }

   return(mn)

   }

# run 10 iterations

sim(10)



In your case, there doesn't seem to be a need to create a and b as matrices; vectors work just as well.  Also, several of the statements could be combined into one.  Whether this meets your needs depends on what your real world task actually is.





Hope this is helpful,



Dan






______________________________________________

R-help at r-project.org mailing list -- To UNSUBSCRIBE and more, see

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.


	[[alternative HTML version deleted]]



More information about the R-help mailing list