Search code examples
rrandomsamplingmultinomial

Locate each observation to level by using the probability


I have a matrix of probability. Each row is the probability that observation i is fall in level 1, 2, 3. For example, row 1: this represent the first observation fall in level1 with probability = 0.2 , level2 = 0.3, and level3 = 0.5. At the end I want to get a column using the probability matrix to locate each observation to level 1,2, or 3, something similar to 1,2,3,3,2,.......

I tried to use rmultinom by sampling one sample from each row with the corresponding probability, but I'm not sure if it is the correct way or there is a better method.

px1=c(0.2, 0.3,0.5)
px2=c(0.1, 0.2,0.7)
px3=c(0.5, 0.1,0.4)
px4=c(0.3, 0.3,0.4)
px5=c(0.4, 0.3,0.3)
px6=c(0.5, 0.1,0.4)
px7=c(0.2, 0.3,0.5)
px8=c(0.5,0.4,0.1)
px9=c(0.2,0 .5,0.3)
px10=c(0.6,0.3,0.1)

prob1=matrix(c(px1,px2,px3,px4,px5,px6,px7,px8,px9,px10), ncol=3, nrow=10)
x1=rmultinom(1,1,prob=prob1[1,])
> x1
     [,1]
[1,]    0
[2,]    1
[3,]    0

Dose that mean observation 1 is in level 2?


Solution

  • Yes, in your example, that output means you sampled the first observation as falling into level 2. Using rmultinom is okay, but it would probably be more convenient to use the sample function:

    lvls <- sapply(1:nrow(prob1),function(x) sample(1:3,1,prob=prob1[x,]))
    

    If you wanted to use rmultinom, you could do so as:

    lvls <- sapply(1:nrow(prob1),function(x) which(rmultinom(1,1,prob=prob1[x,])==1))