Below are the solutions to these exercises on probability functions.

if (!'MASS' %in% installed.packages()) install.packages('MASS') library(MASS) #################### # # # Exercise 1 # # # #################### set.seed(1) random_numbers <- runif(100, min = .5, max = 6.5) (round(random_numbers))

## [1] 2 3 4 6 2 6 6 4 4 1 2 2 5 3 5 3 5 6 3 5 6 2 4 1 2 3 1 3 6 3 3 4 3 2 5 ## [36] 5 5 1 5 3 5 4 5 4 4 5 1 3 5 5 3 6 3 2 1 1 2 4 4 3 6 2 3 2 4 2 3 5 1 6 ## [71] 3 6 3 3 3 6 6 3 5 6 3 5 3 2 5 2 5 1 2 1 2 1 4 6 5 5 3 3 5 4

#################### # # # Exercise 2 # # # #################### set.seed(1) coin_tosses <- rbinom(n = 1000, prob = .48, size = 10) mean(coin_tosses-5) * 10

## [1] -1.93

#################### # # # Exercise 3 # # # #################### set.seed(1) (dice_result <- rmultinom(n = 30, size = 1, prob = rep(1/6,6)))

## [,1] [,2] [,3] [,4] [,5] [,6] [,7] [,8] [,9] [,10] [,11] [,12] [,13] ## [1,] 0 0 1 0 0 0 0 0 0 0 0 0 0 ## [2,] 0 1 0 0 0 0 0 0 0 0 0 0 0 ## [3,] 0 0 0 0 1 1 1 0 1 0 1 0 0 ## [4,] 1 0 0 0 0 0 0 0 0 0 0 1 0 ## [5,] 0 0 0 0 0 0 0 0 0 0 0 0 1 ## [6,] 0 0 0 1 0 0 0 1 0 1 0 0 0 ## [,14] [,15] [,16] [,17] [,18] [,19] [,20] [,21] [,22] [,23] [,24] ## [1,] 0 0 0 0 0 0 0 0 1 0 0 ## [2,] 0 1 0 0 0 0 1 0 0 0 0 ## [3,] 0 0 0 0 0 0 0 0 0 1 0 ## [4,] 1 0 0 1 0 1 0 1 0 0 0 ## [5,] 0 0 0 0 0 0 0 0 0 0 1 ## [6,] 0 0 1 0 1 0 0 0 0 0 0 ## [,25] [,26] [,27] [,28] [,29] [,30] ## [1,] 0 0 0 0 0 0 ## [2,] 0 0 0 0 0 1 ## [3,] 0 0 0 0 0 0 ## [4,] 0 1 0 0 0 0 ## [5,] 0 0 1 1 1 0 ## [6,] 1 0 0 0 0 0

sum(dice_result[6,]==1)

## [1] 6

#################### # # # Exercise 4 # # # #################### apply(dice_result, 1, function(x){ sum(x==1) })

## [1] 2 4 6 7 5 6

#################### # # # Exercise 5 # # # #################### set.seed(1) heigths <- rnorm(n = 1000, mean = 1.70, sd = .1) plot(density(heigths), main = 'Simulating Heights') set.seed(1) heigths2 <- rnorm(n = 10000, mean = 1.70, sd = .1) lines(density(heigths2), col = 'red', lwd =2)

#################### # # # Exercise 6 # # # #################### c(qnorm(.05, mean = 1.70, sd = .1),qnorm(.95, mean = 1.70, sd = .1))

## [1] 1.535515 1.864485

#################### # # # Exercise 7 # # # #################### set.seed(1) population <- mvrnorm(100000, mu = c(170, 60), Sigma = matrix(c(10,17,17,100), nrow = 2) ) summary(population)

## V1 V2 ## Min. :156.4 Min. : 13.71 ## 1st Qu.:167.9 1st Qu.: 53.19 ## Median :170.0 Median : 60.01 ## Mean :170.0 Mean : 59.98 ## 3rd Qu.:172.1 3rd Qu.: 66.73 ## Max. :184.0 Max. :102.43

#################### # # # Exercise 8 # # # #################### # now we do a kernel density estimate dens <- kde2d(population[,1], population[,2], n = 50) # now plot your results persp(dens, shade = .3, border = NA )

#################### # # # Exercise 9 # # # #################### set.seed(1) lambda <- rgamma(n = 1,shape = 20, scale = .5) set.seed(1) rpois(n = 1, lambda = lambda)

## [1] 7

#################### # # # Exercise 10 # # # #################### set.seed(1) lambdas <- rgamma(n = 100,shape = 20, scale = .5) set.seed(1) simulate <- rpois(100, lambdas) plot(table(simulate))

**What's next:**

- Become a Top R Programmer Fast with our Individual Coaching Program
- Explore all our (>4000) R exercises
- Find an R course using our R Course Finder directory
- Subscribe to receive weekly updates and bonus sets by email
- Share with your friends and colleagues using the buttons below

me says

ans 1

random_numbers <- runif(100, min = .5, max = 6.5)

should it be min=1, max=6 ?

Francisco Méndez says

Hey, nope the problem with min=1, max=6 is that you are giving less probability to occur to 1 and 6. Try plotting the values and you will understand it better.

simulation <- runif(10000, min = 1, max = 6)

plot(simulation) #Check the plot