steinchen
steinchen

Reputation: 43

How do I randomly generate data that is distributed according to my own density function in R?

I'm looking for a method that lets me randomly generate data according to my own pre-defined Probability density function

Is there a method that lets me do that? Or at least one that generates data according to this specific function?

Upvotes: 0

Views: 81

Answers (1)

Assuming that your own pre-defined pdf has $y \in {0, 1}$, the pdf is the pdf of a Bernoulli distribution with parameter $\pi$. Using that a Bernoulli random variable corresponds to a Binomial with number of trials equal to 1 ($n=1$), you can draw from the pdf using the following code:

pi <- 0.5 
n <- 10 # Number of draws from specified pdf 
draws <- rbinom(n, 1, pi) # Bernoulli corresponds to Binom with `size = 1`
print(draws) 
# Outputs: [1] 1 0 0 1 0 0 1 1 0 1

Upvotes: 1

Related Questions