Get startedGet started for free

From rbinom to dbinom

The code shows that

  1. Simulates the number of clicks/visitors (n_clicks) from 100 shown ads using the rbinom function given that the underlying proportion of clicks is 10%.
  2. Calculates the probability of getting 13 visitors (prob_13_visitors).

That is, in probability notation it's calculating P(n_visitors = 13 | proportion_clicks = 10%).

This exercise is part of the course

Fundamentals of Bayesian Data Analysis in R

View Course

Exercise instructions

  • First, try running this code.
  • Then, rewrite this code so that it calculates prob_13_visitors using dbinom instead.

Remember: dbinom directly returns a probability and takes the following arguments: dbinom(x = , size = , prob = ) where size and prob are the same as for rbinom, but x is now the data you want to calculate the probability for.

Hands-on interactive exercise

Have a go at this exercise by completing this sample code.

# Rewrite this code so that it uses dbinom instead of rbinom
n_ads_shown <- 100
proportion_clicks <- 0.1
n_visitors <- rbinom(n = 99999, 
    size = n_ads_shown, prob = proportion_clicks)
prob_13_visitors <- sum(n_visitors == 13) / length(n_visitors)
prob_13_visitors
Edit and Run Code