[R] with data in the form of an R data objecte: Monte Carlo simulation in R
David Winsemius
dwinsemius at comcast.net
Wed Mar 24 13:34:41 CET 2010
On Mar 23, 2010, at 11:00 PM, Hongwei Dong wrote:
> Hi, please use the following the matrix z as the example:
>
> x<-c(2,4,5,7,6,9,8,2,0)
> y<-matrix(x,3,3)
> z<-apply(y,2,function(x)x/sum(x))
> z
>
[,1] [,2] [,3]
[1,] 0.1818182 0.3181818 0.8
[2,] 0.3636364 0.2727273 0.2
[3,] 0.4545455 0.4090909 0.0
require(Hmisc)
# rMultinomial expects its subject probs to be in rows rather than in
columns
# here is twenty realizations of three subjects
rMultinom(t(z), 20)
[,1] [,2] [,3] [,4] [,5] [,6] [,7] [,8] [,9] [,10] [,11] [,12] [,
13] [,14]
[1,] 3 3 2 2 1 3 3 3 3 2 2
2 2 3
[2,] 2 3 1 2 3 3 3 1 2 1 1
1 3 3
[3,] 1 1 1 2 1 1 1 1 1 1 1
1 1 1
[,15] [,16] [,17] [,18] [,19] [,20]
[1,] 3 2 3 3 3 2
[2,] 1 1 3 3 3 3
[3,] 2 1 1 1 1 1
>
>
>
> On Tue, Mar 23, 2010 at 6:59 PM, David Winsemius <dwinsemius at comcast.net
> > wrote:
>
> On Mar 23, 2010, at 9:05 PM, Hongwei Dong wrote:
>
> Hi, R-helpers,
>
> I'm trying to use R to do a Monte Carlo simulation and need the
> help. What I
> have is a matrix that consists of the probabilities for the persons to
> choose zones. For example, in the matrix shown below, each column
> represents
> a person, and each row represents a zone. So, the probability that
> the first
> person will choose the 2nd zone is 30%.
>
> 25% 30% 10% 30% 20% 0% 20% 50% 60% 50% 0% 10% 20% 0% 20%
>
> As Alex Trebeck would say: Can you put that in the form of an R data
> object?
>
>
>
> Based on this matrix, I want to locate the persons to zones based on
> the
> probability using a Monte Carlo method. The result I want to see is
> like
> this:
>
> 0 0 0 0 0 0 0 1 1 1 0 0 0 0 0
>
> Could anyone please give some help? Thanks.
>
> You cannot specify what the result _will_ be and call the process
> simulation of results at the same time. The sample function should
> work for a vector. Why not use it?
>
> --
> David.
>
>
> David Winsemius, MD
> West Hartford, CT
>
>
David Winsemius, MD
West Hartford, CT
More information about the R-help
mailing list