Steph Curry is allegedly a 91% free throw shooter. Let's say that in a specific game, he goes 6 for 10.
One commentator says, "Steph is getting worse at free throws in his old age!!"
The other says, "No, this is just an unlucky result - you could easily hit only 6 shots as a 91% free throw shooter."
Who's right??
First we need to learn how to write a "loop" in R, which is a tool we will use to have the computer do something repeatedly.
First some preliminaries to remind you how to worth with the index part of a vector. Make this vector in R. Work in a script up top.
blerg <- c(10,2,5,18,12,27,15,17,2,3)
Run the following and make sense of the number:
length(blerg)
It's really helpful for us to think about how to pick out certain values from a vector, or change certain values in a vector. Remember that we use the [ ] symbol with an index to pick out certain numbers... Run the following below...
blerg[4] #Run this, it should give you 18. Do you see why? Look at what place 18 is in the vector above...
#Predict what each of these are going to give you before running them:
blerg[8]
blerg[12]
Okay now that we remember how to pick out certain values, we can use similar notation to replace values. Replace the 27 in the vector with the number 7.12381 by filling in numbers below in place of %. Neither of them will be 27!!!
Run blerg again so you can make sure it worked.
blerg[%] <- %
blerg
Replace the 17 in the vector with the number 888 with a similar method
blerg[%] <- %
Now also add the number 666 at the end, so in the 11th slot.
blerg[%] <- %
A really useful thing in programming is called a "loop". It's a way to get the computer to do a bunch of things over and over again so you don't have to. For example, let's say I wanted to calculate the square of all the numbers between 1 and 10. One way to do it is this:
1^2
2^2
3^2
4^2
5^2
6^2
7^2
8^2
9^2
10^2
But that's exhausting!!!!! A better way to do it is with something called a for loop. Here is a loop that does all that above in one line.
for (i in 1:10){ print(i^2) }
i is a variable here, and the numbers we want to use for i are given in the parentheses. The function we want to do each time is in the curly brackets. This means take 1 and put it in for i, then take 2 and put it in for i, then take 3.... etc etc up until 10.
Modify the loop to make the following. You can edit the original loop each time:
# Write a for loop that prints out the first 100 squares.
# Write a for loop that prints out the first 10 multiples of 77 (you'll need to change the i^2 part this time!)
# Write a for loop that prints out the first 10 even numbers.
Now a loop involving a vector. Write a for loop that prints off each element of this vector v on a different line. Remember that vector[5] gets the 5th element of a vector...
v <- c("I","ate","a","chocolate","chip","muffin","#blessed")
for ( ) {
}
If I did v[4] <- "yum" it would replace the fourth element of v with "yum". So the word "chocolate" would be replaced by "yum". Let's say I wanted to put "yum" in every slot. I could do this...
v[1] <- "yum"
v[2] <- "yum"
v[3] <- "yum"
v[4] <- "yum"
v[5] <- "yum"
v[6] <- "yum"
But this could be much more efficient with a for loop... write one. We won't use print() here... we did that before because the task was to print things. But here, that's not the task. The task is to replace the words with yum using a command like the one above.
Check your vector v after to make sure it worked.
v <- c("I","ate","a","chocolate","chip","muffin","#blessed")
for ( ) {
}
v
We are building up to generating a set of numbers and storing them. Your task is to generate a list of the first 20 even numbers and then store them into a vector I have created for you, called evenNumbers. You can think of it like this:
In step 1, you will store 2 in the 1st spot...
In step 2, you will store 4 in the 2nd spot...
so in step i, you will store ______ in the ______ spot...
evenNumbers <- c() # Don't mess with this line, just write code in the loop below. This is supposed to be empty.
for ( i in %:% ) {
}
This is how we are going to investigate this problem. Here is a vector that will have X 0's and Y 1's. The rep() means repeat. 0 will represent a miss and 1 will represent a make. Replace X and Y with numbers that make sense for a 90% free throw shooter (we'll round!)
freeThrows <- c(rep(0,X),rep(1,Y))
Now we will use the sample function to sample a certain number of these. WAY QUICKER than if we were to do it by hand! Investigate the code below and make sure you understand what it's doing.
sample(freeThrows,10,replace=TRUE)
How many free throws did we make on this sampled list? Well we just need to add up the number of ones in the list, which a sum() function will do. Call sum() on the sample. Run your code from the line above 10 more times (with the sum). Just to see what it does each time. If you have it set up right, it should give you a slightly different number each time!!!
Write a for loop that:
Simulates 1000 times
Shooting 10 free throws from a 90% free throw shooter
Finds the sum of each sample and collects the result in an empty vector
Gets a histogram of the results.
Then analyze...
What's the most likely value? Why does that make sense?
What's the probability that Steph hits only 6 free throws?
probs <- c(1,1,1,1,1,1,1,1,1,0)
freeThrows <- c()
for (i in 1:1000){
freeThrows[i]<-sum(sample(probs,10, replace=TRUE))
}
hist(freeThrows)
length(freeThrows[freeThrows<=6])/1000