calculate a probability function in R

Question:

Good afternoon.

It's my first intervention on the stack, I'm new to R , and my questions are pretty basic.

I need to generate a sample of 1000 observations from a distribution function of W .
W is a discrete random variable, which takes the values ​​from 1 to 6 , represented by the sides of a die, given that it is biased and whose probability function is p1=0.25 , p2=0.16 , p3=0.18 , p4=0.17 , p5=0.14 , p6=0.10 .

How can I write this function in R?

Thanks

Answer:

To sample from a discrete variable that takes a finite number of values, one can use the sample base function.
Before running the sample function or another function that generates pseudorandom numbers, it is always better to call set.seed .

set.seed(7228)

W <- 1:6
p <- c(0.25, 0.16, 0.18, 0.17, 0.14, 0.10)
w <- sample(W, 1000, replace = TRUE, prob = p)

head(w, n = 20)
#[1] 4 4 1 3 6 3 5 4 1 1 2 4 4 4 4 2 6 6 5 6

See if the outcome proportions are similar to the given probabilities.

tw <- table(w)
print(tw/sum(tw), digits = 2)
#w
#   1    2    3    4    5    6 
#0.23 0.16 0.16 0.19 0.14 0.12

They don't look very different. If necessary, a Kolmogorov-Smirnov test can always be run, since both p and the sample proportions come from a continuous distribution.

ks.test(p, tw/sum(tw))

With a p-value = 0.8928 it is concluded that yes, the distributions are not significantly different.

Scroll to Top