This is an R Markdown document. Markdown is a simple formatting syntax for authoring HTML, PDF, and MS Word documents. For more details on using R Markdown see http://rmarkdown.rstudio.com.
When you click the Knit button a document will be generated that includes both content as well as the output of any embedded R code chunks within the document. You can embed an R code chunk like this:
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???
## [1] "H" "M" "M" "H" "H" "M" "M" "M" "M"
kobe[1:9,6]
## [1] "H" "M" "M" "H" "H" "M" "M" "M" "M"
print("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?")
## [1] "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?"
print("A streak length of 1 typically means that there is only one occurrence of either a hit or a miss, followed by the opposite outcome. Now, regarding a streak length of 0, this would imply that there was no streak at all, meaning that the player neither made nor missed a shot in consecutive attempts.")
## [1] "A streak length of 1 typically means that there is only one occurrence of either a hit or a miss, followed by the opposite outcome. Now, regarding a streak length of 0, this would imply that there was no streak at all, meaning that the player neither made nor missed a shot in consecutive attempts."
## 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
table(kobe_streak)
## kobe_streak
## 0 1 2 3 4
## 39 24 6 6 1
## 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?
table(kobe_streak)
## kobe_streak
## 0 1 2 3 4
## 39 24 6 6 1
print("39 out of 133 shots did not end up in a streak. 24 times Kobe had streak of 1 and his longest streak was of length 4 which was achived only one in teh")
## [1] "39 out of 133 shots did not end up in a streak. 24 times Kobe had streak of 1 and his longest streak was of length 4 which was achived only one in teh"
## 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"
## 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 .
## 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
table(sim_fair_coin)
## sim_fair_coin
## heads tails
## 489 511
table(sim_fair_coin)/1000
## sim_fair_coin
## heads tails
## 0.489 0.511
## 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
## 1.987 8.013
## 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()
sim_unfair_coin2 <- sample(outcomes, size = 100, replace = TRUE, prob = c(0.2, 0.8))
table(sim_unfair_coin2)
## sim_unfair_coin2
## heads tails
## 18 82
print("11 out of 100 times head was the outcome in the flipping of unfair coin")
## [1] "11 out of 100 times head was the outcome in the flipping of unfair coin"
set.seed(5)
## Simulating the Independent Shooter
## 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(.45, 0.55))
table(sim_basket)
## sim_basket
## H M
## 58 75
set.seed(5)
## 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
#sim_basket
## 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] 0 2 0 1 2 0 0 0 1 0 0 5 0 0 0 0 1 1 0 0 0 0 0 2 0 1 1 1 1 2 4 0 0 0 0 1 5 4
## [39] 1 1 1 0 2 0 0 0 2 2 0 0 0 1 1 0 5 0 0 0 0 0 0 1 0 0 0 0 1 0 3 0 2 0 0 0 0 0
barplot(table(calc_streak(sim_basket)))
table(calc_streak(sim_basket))
##
## 0 1 2 3 4 5
## 46 16 8 1 2 3
#############################################################################
##.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?
print("Typical streak leangth of simulated player is also 1. 21 shots in a streak length of 1. Longest streak is of length 4 same as Kobe")
## [1] "Typical streak leangth of simulated player is also 1. 21 shots in a streak length of 1. Longest streak is of length 4 same as Kobe"
# .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????)
sim_basket2 <- sample(outcomes, size = 133, replace = TRUE, prob = c(.45, 0.55))
table(sim_basket2)
## sim_basket2
## H M
## 58 75
table(calc_streak(sim_basket))/133
##
## 0 1 2 3 4 5
## 0.345864662 0.120300752 0.060150376 0.007518797 0.015037594 0.022556391
table(kobe_streak)/133
## kobe_streak
## 0 1 2 3 4
## 0.293233083 0.180451128 0.045112782 0.045112782 0.007518797
print("When I run teh simulation 2nd time, it yields somewhat similar output and distribution when compared to the previously run simulation but not exactly same. Streaks in random events like coin flips or shots by an independent shooter can be somewhat unpredictable. While there are probabilities associated with each outcome (e.g., heads or tails for a coin flip, making or missing a shot for a shooter), the exact sequence of outcomes can vary from one simulation to another.To ensure that your simulation results are reproducible, you can seed the random number generator in R with a fixed value before running the simulation. Seeding ensures that the same sequence of random numbers is generated each time you run the simulation with the same seed value. This way, you can replicate the same simulation results if needed.")
## [1] "When I run teh simulation 2nd time, it yields somewhat similar output and distribution when compared to the previously run simulation but not exactly same. Streaks in random events like coin flips or shots by an independent shooter can be somewhat unpredictable. While there are probabilities associated with each outcome (e.g., heads or tails for a coin flip, making or missing a shot for a shooter), the exact sequence of outcomes can vary from one simulation to another.To ensure that your simulation results are reproducible, you can seed the random number generator in R with a fixed value before running the simulation. Seeding ensures that the same sequence of random numbers is generated each time you run the simulation with the same seed value. This way, you can replicate the same simulation results if needed."
# .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
print("The distribution of simulated player vs Kobe is very similar. Majority shots in 0 streak and 1 streak. To prove hot hand model, I compared % of data in streak 2 of the simulation vs Kobe data. Kobe has 0.045% of shots in streak 2 vs simulated player has 0.06% of shots in streak 2. This evidence proves that hot hand model does not fit Kobe's shooting pattern ")
## [1] "The distribution of simulated player vs Kobe is very similar. Majority shots in 0 streak and 1 streak. To prove hot hand model, I compared % of data in streak 2 of the simulation vs Kobe data. Kobe has 0.045% of shots in streak 2 vs simulated player has 0.06% of shots in streak 2. This evidence proves that hot hand model does not fit Kobe's shooting pattern "