简体   繁体   中英

What does a matrix probability do in random sample distribution function

Hi I was looking for some help on trying to understand what does a probability matrix achieve when sampling, I am having a hard time wrapping my head around the prob[a, b] does, to be honest the syntax here seems a bit different than in other languages, first because we pass indexes to a matrix and it construct a bigger one (that is kinda cool) but I digress I currently have the following bernoulli sampling:

N <- 8
prob <- matrix(c(0.1,0.1,0.5,0.8), nrow=2)
a <- sample(1:2, size=N, replace=TRUE)
b <- rbern(N, ifelse(l==1, 0.5, .1)) + 1
rbern(N, prob = prob[a, b])

What I am unable to understand well is when sampling I give a matrix of 8x8, not sure which probability is going to use to sample if I am only asking for 8 observations.

It will simply take the first N values from the matrix used in the prob argument (starting with the first column).

Consider the following code.

N <- 8
m <- matrix(sample(0:1, N^2, 1), N, N)
m
#>      [,1] [,2] [,3] [,4] [,5] [,6] [,7] [,8]
#> [1,]    0    1    0    1    1    0    0    1
#> [2,]    1    1    1    0    0    0    1    1
#> [3,]    0    1    0    1    0    1    1    1
#> [4,]    1    0    0    1    0    0    0    1
#> [5,]    0    1    0    0    0    0    1    0
#> [6,]    0    1    1    1    0    1    1    0
#> [7,]    1    0    0    0    0    1    1    0
#> [8,]    0    0    1    1    1    1    0    0
rbinom(N, 1, prob = m)
#> [1] 0 1 0 1 0 0 1 0

Only the first N values of m are used for probabilities, so the result of rbinom(N, 1, prob = m) is the same as the first column of m (since all probabilities are either 0 or 1).

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM