calculate a probability function in R

4

Good afternoon.

It's my first time in the stack, I'm a beginner in R , and my doubts are pretty basic.

I need to generate a sample of 1000 observations of a W distribution function. W is a discrete random variable, which assumes the values of 1 to 6 , represented by the sides of a given, given that it is addicted 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?

Thank you

    
asked by anonymous 26.10.2018 / 19:10

2 answers

2

The way to solve this problem is to use the inverse cumulative distribution function of this variable.

You know the probabilities of the variable X assume each of the possible values (from 1 to 6 ), then you can construct the cumulative distribution function ( F(x) = P(X <= x) ):

F(x) =

  • 0.25 if X
26.10.2018 / 19:41
4

To sample a discrete variable that takes a finite number of values, you can use the base function sample . Before running the sample function or other function that generates pseudo-random 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 proportions of the result 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 do not seem to be very different. If necessary, a Kolmogorov-Smirnov test can always be run, since both% wt% and sample proportions come from a continuous distribution.

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

With p , it should be noted that the distributions are not significantly different.

    
26.10.2018 / 19:30