download.file(“http://www.openintro.org/stat/data/kobe.RData”, destfile = “kobe.RData”)

load("kobe.RData")  # Kobe's shooting stats in few games
head(kobe)
##    vs game quarter time                                             description
## 1 ORL    1       1 9:47                 Kobe Bryant makes 4-foot two point shot
## 2 ORL    1       1 9:07                               Kobe Bryant misses jumper
## 3 ORL    1       1 8:11                        Kobe Bryant misses 7-foot jumper
## 4 ORL    1       1 7:41 Kobe Bryant makes 16-foot jumper (Derek Fisher assists)
## 5 ORL    1       1 7:03                         Kobe Bryant makes driving layup
## 6 ORL    1       1 6:01                               Kobe Bryant misses jumper
##   basket
## 1      H
## 2      M
## 3      M
## 4      H
## 5      H
## 6      M
tail(kobe)
##      vs game quarter    time                        description basket
## 128 ORL    3       4    3:57  Bryant Jump Shot: Made (28 PTS)        H
## 129 ORL    3       4    3:33        Bryant Layup Shot: Missed        M
## 130 ORL    3       4    2:02          Bryant 3pt Shot: Missed        M
## 131 ORL    3       4 00:23.9          Bryant 3pt Shot: Missed        M
## 132 ORL    3       4 00:06.9          Bryant 3pt Shot: Missed        M
## 133 ORL    3       4 00:00.5 Bryant Layup Shot: Made (31 PTS)        H
dim(kobe)
## [1] 133   6

For example, in Game 1 Kobe had the following sequence of hits and misses from his nine shot attempts in the first quarter:

To verify this use the following command:

kobe$basket[1:9] ## what is another way to get the same data??? kobe[1:9,6]

Within the nine shot attempts, there are six streaks, which are separated by a “|” above.

Their lengths are one, zero, two, zero, zero, zero (in order of occurrence).

1.What does a streak length of 1 mean, i.e. how many hits and misses are in a streak of 1? What about a streak length of 0?

A streak length of 1 means one hit followed by a miss In a streak of 1, there are 6 misses and 3 hits A streak length of 0 means that a miss will first occur after a miss that ended the preceding streak.

The custom function calc_streak , which was loaded in with the data, may be used to calculate the lengths of all shooting streaks and then look at the distribution.

  kobe_streak <- calc_streak(kobe$basket)
##  Lets see how this looks...
  kobe_streak  
##  [1] 1 0 2 0 0 0 3 2 0 3 0 1 3 0 0 0 0 0 1 1 0 4 1 0 1 0 1 0 1 2 0 1 2 1 0 0 1 0
## [39] 0 0 1 1 0 1 0 2 0 0 0 3 0 1 0 1 2 1 0 1 0 0 1 3 3 1 1 0 0 0 0 0 1 1 0 0 0 1
   barplot(table(kobe_streak))

    table(kobe$basket)/133
## 
##         H         M 
## 0.4360902 0.5639098

Note that instead of making a histogram, we chose to make a bar plot from a table of the streak data.

The bar plot is preferable here since our variable is discrete – counts – instead of continuous.

2.Describe the distribution of Kobe’s streak lengths from the 2009 NBA finals.

What was his typical streak length? How long was his longest streak of baskets?

Distribution is right skewed. 
The typical streak length is 0. 
Longest streak is 4. 

Compared to What?

We’ve shown that Kobe had some long shooting streaks, but are they long enough to support the belief

that he had hot hands? What can we compare them to?

Pr(shot1 =H) =.45

Pr(shot2= h given Shot1 = H) > .45 ?? eg. 0.60 if Kobe has a hot hand.

So Now that we’ve phrased the situation in terms of independent shots, the question is:

how do we tell if Kobe’s shooting streaks are long enough to indicate that he has hot hands?

We can compare his streak lengths to someone without hot hands: an independent shooter.

Simulations in R

We don’t have any data from a shooter we know to have independent shots.

But it is very easy to simulate such data in R.

In a simulation, you set the ground rules of a random process and then the computer uses

random numbers to generate an outcome that adheres to those rules.

As a simple example, you can simulate flipping a fair coin with the following.

#define the outcomes
    outcomes <- c("heads", "tails")
    sample(outcomes, size = 1, replace = TRUE)
## [1] "tails"
sim_fair_coin <- sample(outcomes, size = 100, replace = TRUE)
sim_fair_coin
##   [1] "tails" "heads" "tails" "tails" "heads" "heads" "tails" "tails" "heads"
##  [10] "tails" "tails" "tails" "heads" "tails" "tails" "heads" "tails" "heads"
##  [19] "heads" "heads" "tails" "heads" "heads" "tails" "tails" "tails" "tails"
##  [28] "tails" "tails" "heads" "tails" "heads" "tails" "tails" "heads" "tails"
##  [37] "tails" "tails" "heads" "tails" "heads" "tails" "heads" "tails" "tails"
##  [46] "tails" "heads" "tails" "tails" "tails" "tails" "heads" "tails" "heads"
##  [55] "tails" "heads" "heads" "tails" "tails" "tails" "tails" "heads" "tails"
##  [64] "heads" "heads" "tails" "heads" "tails" "tails" "heads" "heads" "tails"
##  [73] "tails" "tails" "heads" "tails" "tails" "tails" "heads" "heads" "tails"
##  [82] "tails" "tails" "tails" "tails" "tails" "heads" "tails" "tails" "heads"
##  [91] "heads" "tails" "heads" "tails" "tails" "tails" "heads" "heads" "heads"
## [100] "heads"

#The vector outcomes can be thought of as a hat with two slips of paper in it: one slip says heads # and the other says tails . The function sample draws one slip from the hat and tells us if it was # a head or a tail.

Run the second command listed above several times. Just like when flipping a coin, sometimes you’ll

get a heads, sometimes you’ll get a tails, but in the long run, you’d expect to get roughly

equal numbers of each.

If you wanted to simulate flipping a fair coin 100 times, you could either run the function 100

times or, more simply, adjust the size argument, which governs how many samples to draw

(the replace = TRUE argument indicates we put the slip of paper back in the hat before

drawing again). Save the resulting vector of heads and tails in a new object called sim_fair_coin .

sim_fair_coin <- sample(outcomes, size = 100, replace = TRUE)
sim_fair_coin
##   [1] "heads" "tails" "tails" "tails" "tails" "heads" "heads" "heads" "tails"
##  [10] "tails" "heads" "tails" "tails" "tails" "heads" "tails" "heads" "heads"
##  [19] "tails" "heads" "tails" "tails" "heads" "tails" "heads" "heads" "tails"
##  [28] "tails" "heads" "tails" "heads" "heads" "heads" "tails" "heads" "tails"
##  [37] "heads" "tails" "heads" "heads" "tails" "tails" "heads" "tails" "tails"
##  [46] "tails" "tails" "heads" "tails" "tails" "tails" "heads" "tails" "heads"
##  [55] "heads" "tails" "tails" "tails" "tails" "tails" "heads" "tails" "heads"
##  [64] "tails" "tails" "heads" "heads" "heads" "heads" "tails" "tails" "heads"
##  [73] "tails" "tails" "heads" "heads" "heads" "tails" "tails" "tails" "heads"
##  [82] "tails" "heads" "heads" "tails" "tails" "tails" "heads" "heads" "heads"
##  [91] "tails" "tails" "heads" "tails" "tails" "tails" "tails" "heads" "heads"
## [100] "heads"

SET A SEED

#set.seed(5)

sim_fair_coin <- sample(outcomes, size = 1000, replace = TRUE)

To view the results of this simulation, type the name of the object and then use table to

count up the number of heads and tails.

    sim_fair_coin
##   [1] "heads" "tails" "tails" "tails" "tails" "heads" "heads" "heads" "tails"
##  [10] "tails" "heads" "tails" "tails" "tails" "heads" "tails" "heads" "heads"
##  [19] "tails" "heads" "tails" "tails" "heads" "tails" "heads" "heads" "tails"
##  [28] "tails" "heads" "tails" "heads" "heads" "heads" "tails" "heads" "tails"
##  [37] "heads" "tails" "heads" "heads" "tails" "tails" "heads" "tails" "tails"
##  [46] "tails" "tails" "heads" "tails" "tails" "tails" "heads" "tails" "heads"
##  [55] "heads" "tails" "tails" "tails" "tails" "tails" "heads" "tails" "heads"
##  [64] "tails" "tails" "heads" "heads" "heads" "heads" "tails" "tails" "heads"
##  [73] "tails" "tails" "heads" "heads" "heads" "tails" "tails" "tails" "heads"
##  [82] "tails" "heads" "heads" "tails" "tails" "tails" "heads" "heads" "heads"
##  [91] "tails" "tails" "heads" "tails" "tails" "tails" "tails" "heads" "heads"
## [100] "heads"
    table(sim_fair_coin)
## sim_fair_coin
## heads tails 
##    45    55
    table(sim_fair_coin)/1000
## sim_fair_coin
## heads tails 
## 0.045 0.055

Since there are only two elements in outcomes , the probability that we “flip” a coin and it lands

heads is 0.5. Say we’re trying to simulate an unfair coin that we know only lands heads 20% of the

time. We can adjust for this by adding an argument called prob , which provides a vector of

two probability weights.

       sim_unfair_coin <- sample(outcomes, size = 10000, replace = TRUE, prob = c(0.2, 0.8))
       table(sim_unfair_coin)/1000
## sim_unfair_coin
## heads tails 
## 2.028 7.972

Notice prob=c(0.2, 0.8) indicates that for the two elements in the outcomes vector,

we want to select the first one, heads , with probability 0.2 and the second one, tails with

probability 0.8.

Another way of thinking about this is to think of the outcome space as a bag of 10 chips,

where 2 chips are labeled “head” and 8 chips “tail”. Therefore at each draw, the probability of

drawing a chip that says “head”” is 20%, and “tail” is 80%.

3.In the simulation of flipping the unfair coin 100 times, how many flips came up heads?

read more on the sample function in help. ?sample()

table(sim_unfair_coin)
## sim_unfair_coin
## heads tails 
##  2028  7972

Simulating the Independent Shooter

Simulating a basketball player who has independent shots uses the same mechanism that we use to

simulate a coin flip. To simulate a single shot from an independent shooter with a shooting percentage

of 50% we type,

      outcomes <- c("H", "M")
      sim_basket <- sample(outcomes, size = 1, replace = TRUE, prob = c(.5, 0.5))

To make a valid comparison between Kobe and our simulated independent shooter, we need to

align both their shooting percentage and the number of attempted shots.

4.What change needs to be made to the sample function so that it reflects a shooting percentage

of 45%? Make this adjustment, then run a simulation to sample 133 shots.

Assign the output of this simulation to a new object called sim_basket .

      outcomes <- c("H", "M")
      sim_basket <- sample(outcomes, size = 133, replace = TRUE, prob = c(.436, 0.564))
      sim_basket
##   [1] "H" "M" "H" "M" "H" "H" "M" "M" "H" "H" "M" "M" "H" "M" "M" "H" "H" "H"
##  [19] "H" "H" "H" "M" "M" "M" "M" "H" "M" "M" "M" "M" "H" "M" "M" "M" "H" "M"
##  [37] "H" "M" "H" "H" "M" "M" "M" "M" "H" "M" "M" "H" "H" "M" "M" "M" "H" "H"
##  [55] "H" "M" "M" "H" "M" "M" "H" "H" "M" "M" "M" "H" "M" "M" "M" "H" "H" "H"
##  [73] "H" "H" "H" "H" "H" "H" "H" "H" "H" "H" "H" "M" "M" "H" "H" "H" "H" "H"
##  [91] "M" "M" "H" "M" "H" "H" "H" "H" "M" "H" "M" "M" "M" "M" "H" "M" "M" "M"
## [109] "H" "M" "M" "M" "M" "M" "M" "M" "M" "H" "M" "H" "M" "M" "M" "M" "H" "H"
## [127] "M" "M" "H" "M" "H" "M" "M"

Note that we’ve named the new vector sim_basket , the same name that we gave to the

previous vector reflecting a shooting percentage of 50%.

In this situation, R overwrites the old object with the new one, so always make sure that

you don’t need the information in an old vector before reassigning its name.

With the results of the simulation saved as sim_basket , we have the data necessary to compare

Kobe to our independent shooter. We can look at Kobe’s data alongside our simulated data.

      kobe$basket
##   [1] "H" "M" "M" "H" "H" "M" "M" "M" "M" "H" "H" "H" "M" "H" "H" "M" "M" "H"
##  [19] "H" "H" "M" "M" "H" "M" "H" "H" "H" "M" "M" "M" "M" "M" "M" "H" "M" "H"
##  [37] "M" "M" "H" "H" "H" "H" "M" "H" "M" "M" "H" "M" "M" "H" "M" "M" "H" "M"
##  [55] "H" "H" "M" "M" "H" "M" "H" "H" "M" "H" "M" "M" "M" "H" "M" "M" "M" "M"
##  [73] "H" "M" "H" "M" "M" "H" "M" "M" "H" "H" "M" "M" "M" "M" "H" "H" "H" "M"
##  [91] "M" "H" "M" "M" "H" "M" "H" "H" "M" "H" "M" "M" "H" "M" "M" "M" "H" "M"
## [109] "H" "H" "H" "M" "H" "H" "H" "M" "H" "M" "H" "M" "M" "M" "M" "M" "M" "H"
## [127] "M" "H" "M" "M" "M" "M" "H"
      sim_basket
##   [1] "H" "M" "H" "M" "H" "H" "M" "M" "H" "H" "M" "M" "H" "M" "M" "H" "H" "H"
##  [19] "H" "H" "H" "M" "M" "M" "M" "H" "M" "M" "M" "M" "H" "M" "M" "M" "H" "M"
##  [37] "H" "M" "H" "H" "M" "M" "M" "M" "H" "M" "M" "H" "H" "M" "M" "M" "H" "H"
##  [55] "H" "M" "M" "H" "M" "M" "H" "H" "M" "M" "M" "H" "M" "M" "M" "H" "H" "H"
##  [73] "H" "H" "H" "H" "H" "H" "H" "H" "H" "H" "H" "M" "M" "H" "H" "H" "H" "H"
##  [91] "M" "M" "H" "M" "H" "H" "H" "H" "M" "H" "M" "M" "M" "M" "H" "M" "M" "M"
## [109] "H" "M" "M" "M" "M" "M" "M" "M" "M" "H" "M" "H" "M" "M" "M" "M" "H" "H"
## [127] "M" "M" "H" "M" "H" "M" "M"

Both data sets represent the results of 133 shot attempts, each with the same shooting percentage of

45%. We know that our simulated data is from a shooter that has independent shots.

That is, we know the simulated shooter does not have a hot hand. Each Shot in an independedt computer

generated random number following our Hit probability weight

COMPARE & ANALYZE THE TWO.. What is you conclusion????

Using calc_streak , compute the streak lengths of sim_basket .

      calc_streak(sim_basket)
##  [1]  1  1  2  0  2  0  1  0  6  0  0  0  1  0  0  0  1  0  0  1  1  2  0  0  0
## [26]  1  0  2  0  0  3  0  1  0  2  0  0  1  0  0 14  0  5  0  1  4  1  0  0  0
## [51]  1  0  0  1  0  0  0  0  0  0  0  1  1  0  0  0  2  0  1  1  0  0
       barplot(table(calc_streak(sim_basket)))

#############################################################################

##.Describe the distribution of streak lengths. What is the typical streak length for this ## simulated independent shooter with a 45% shooting percentage? How long is the player’s longest streak ## of baskets in 133 shots?

The distribution is right skewed.

The typical streak length is 0.

The longest streak of baskets is length 6.

.If you were to run the simulation of the independent shooter a second time,

how would you expect its streak distribution to compare to the distribution from the question

above? Exactly the same? Somewhat similar? ?Totally different Explain your reasoning.

(SEEDING THE SIMULATION????)

It would be similar.

.How does Kobe Bryant’s distribution of streak lengths compare to the distribution of streak

lengths for the simulated shooter? Using this comparison,

do you have evidence that the hot hand model fits Kobe’s shooting patterns? Explain

Kobe’s streaks are shorter in length. Conclusion is that shots are independent and hot hand model doesn’t fit.