Now that you can define functions, use if statements and for-loops. It’s time to actually simulate the chance a random group of 5 people has an avarage iq greater than or equal to 110.
The simulate_chance(n)
pseudocode1 (not real R code) below returns the simulated chance that a group of 5 people are having a group iq (group_iq(5)
) greater than or equal to 110 based on n
generated groups:
group_iq <- function(n){
mean(rnorm(n, 100, 15))
}
simulate_chance <- function(n) {
counter <- 0
# TODO: correct the for-loop definition so that it runs n times
for n times {
# TODO: generate a group iq for a group of 5 people (use group_iq function)
# TODO: correct the if condition
if the iq value is greater than or equal to 110 {
# TODO: add one to the counter variable
}
}
# TODO: calculate (and return) the frequency that the mean group iq is greater than or equal to 110
}
write the function simulate_chance(n)
based on the given pseudocode to simulate the chance a random group of 5 people has a group iq greater than or equal to 110 based on n
randomly generated group iq’s. You can generate a group iq with the group_iq(n)
function you wrote in exercise 1.
Expected chance
We have calculated the chance using the central limit theorem, the exact chance is equal to \(0.06801856\)
Avoid duplication of effort
We have written the
group_iq(n)
function from exercise 1 for you.