load("c:/Statistics/nba_pbp_14_24.RData")
    View(all_pbp_data) 
##################
     ### Homework 1 ###
     ##################
     
     ### Task 1 ###
     
     # Read in the all_nba_pbp_data.csv file. It is big, so it might take some time!
     # After you read in the data, select 5 players that you 
     # want to use for subsetting the data. You can either make
     # one object or 5 different objects. It is completely up
     # to you!
     
     # You can remove the large object by using the rm(object_name) function.
     
     ### Task 2 ###
     
     # For each of the 5 players, find their mean/sd distance. 
     # Which of the players has the longest mean distance? 
     # Which player has the tightest sd?
     
     ### Task 3 ###
     
     # What is each player's total probability of making a shot?
     # What is each player's probability of making a 2-point shot?
     # What is each player's probability of making a 3-point shot?
     
     ### Task 4 ### 
     
     # What is each players expected shot value for 2-point field goals?
     # What is each players expected shot value for 3-point field goals?
     
     ### Task 5 ###
     
     # Give me 3 ideas for your final project. 
     
     ### Task 6 ###
     
     # Compile this document into an html file. File > Compile Report...
     # You might be asked to install some packages, so definitely do that.
     # Submit the html file on Canvas!
  #Task 1
    Tim_Duncan = subset(all_pbp_data, all_pbp_data$shooter == "Tim Duncan")
    View(Tim_Duncan)
    Ray_Allen = subset(all_pbp_data, all_pbp_data$shooter == "Ray Allen")
    View (Ray_Allen)
    Mo_Williams = subset(all_pbp_data, all_pbp_data$shooter == "Mo Williams")
    View(Mo_Williams)
    Kevin_Garnett = subset(all_pbp_data, all_pbp_data$shooter == "Kevin Garnett")
    View(Kevin_Garnett)
    Vince_Carter = subset(all_pbp_data, all_pbp_data$shooter == "Vince Carter")
    View(Vince_Carter)
    
    #Task 2  
    mean(all_pbp_data$distance[all_pbp_data$shooter == "Tim Duncan"], na.rm = TRUE)
## [1] 4.243767
    mean(all_pbp_data$distance[all_pbp_data$shooter == "Ray Allen"], na.rm = TRUE) 
## [1] 12.29412
    mean(all_pbp_data$distance[all_pbp_data$shooter == "Mo Williams"], na.rm = TRUE)    
## [1] 15.54348
    mean(all_pbp_data$distance[all_pbp_data$shooter == "Kevin Garnett"], na.rm = TRUE)    
## [1] 10.0922
    mean(all_pbp_data$distance[all_pbp_data$shooter == "Vince Carter"], na.rm = TRUE)    
## [1] 17.0766
# vince carter had the longest mean distance
    sd(all_pbp_data$distance[all_pbp_data$shooter == "Tim Duncan"], na.rm = TRUE)
## [1] 6.031986
    sd(all_pbp_data$distance[all_pbp_data$shooter == "Ray Allen"], na.rm = TRUE)    
## [1] 11.97792
    sd(all_pbp_data$distance[all_pbp_data$shooter == "Mo Williams"], na.rm = TRUE)    
## [1] 9.023759
    sd(all_pbp_data$distance[all_pbp_data$shooter == "Kevin Garnett"], na.rm = TRUE)    
## [1] 8.979262
    sd(all_pbp_data$distance[all_pbp_data$shooter == "Vince Carter"], na.rm = TRUE)    
## [1] 11.16997
# Tim Duncan had the tightest standard deviation on distance
    #Task 3
    prop.table(table(Tim_Duncan$shooter, Tim_Duncan$result), margin = 1)
##             
##                    made     missed
##   Tim Duncan 0.94736842 0.05263158
    prop.table(table(Ray_Allen$shooter, Ray_Allen$result), margin = 1)
##            
##             made
##   Ray Allen    1
    prop.table(table(Mo_Williams$shooter, Mo_Williams$result), margin = 1)
##              
##                     made     missed
##   Mo Williams 0.97826087 0.02173913
    prop.table(table(Kevin_Garnett$shooter, Kevin_Garnett$result), margin = 1)
##                
##                       made     missed
##   Kevin Garnett 0.96453901 0.03546099
    prop.table(table(Vince_Carter$shooter, Vince_Carter$result), margin = 1)
##               
##                     made    missed
##   Vince Carter 0.5584958 0.4415042
prop.table(table(all_pbp_data$shooter == "Tim Duncan"| all_pbp_data$shooter == "Tim Duncan ", all_pbp_data$result), margin = 1)
##        
##              made    missed
##   FALSE 0.4547610 0.5452390
##   TRUE  0.5066667 0.4933333
prop.table(table(all_pbp_data$shooter == "Ray Allen" | all_pbp_data$shooter == "Ray Allen ", all_pbp_data$result), margin = 1)   
##        
##              made    missed
##   FALSE 0.4547980 0.5452020
##   TRUE  0.4146341 0.5853659
prop.table(table(all_pbp_data$shooter == "Mo Williams" | all_pbp_data$shooter == "Mo Williams ", all_pbp_data$result), margin = 1)
##        
##              made    missed
##   FALSE 0.4548093 0.5451907
##   TRUE  0.4302103 0.5697897
Overall_Shooting_Duncan = subset(all_pbp_data
                          , all_pbp_data$shooter == "Tim Duncan"| 
                            all_pbp_data$shooter == "Tim Duncan ")

Overall_Shooting_Allen = subset(all_pbp_data
                          , all_pbp_data$shooter == "Ray Allen")

Overall_Shooting_Williams = subset(all_pbp_data
                          , all_pbp_data$shooter == "Mo Williams")

Overall_Shooting_Garnett = subset(all_pbp_data
                                   , all_pbp_data$shooter == "Kevin Garnett")

Overall_Shooting_Carter = subset(all_pbp_data
                                  , all_pbp_data$shooter == "Vince Carter")


prop.table(table(Overall_Shooting_Duncan$result))
## 
##      made    missed 
## 0.5066667 0.4933333
#Overall Tim Duncan made 50.67% of his shots
prop.table(table(Overall_Shooting_Allen$result))
## 
## made 
##    1
#Overall Ray Allen made 100% of his shots
prop.table(table(Overall_Shooting_Williams$result))
## 
##       made     missed 
## 0.97826087 0.02173913
#Overall Mo Williams made 97.83% of his shots
prop.table(table(Overall_Shooting_Garnett$result))
## 
##       made     missed 
## 0.96453901 0.03546099
#Overall Kevin Garnett made 96.45% of his shots
prop.table(table(Overall_Shooting_Carter$result))
## 
##      made    missed 
## 0.5584958 0.4415042
 #Overall Vince Carter made 55.85% of his shots



prop.table(table(Overall_Shooting_Duncan$result
                 , Overall_Shooting_Duncan$three),margin =2)
##         
##              FALSE      TRUE
##   made   0.5089286 0.0000000
##   missed 0.4910714 1.0000000
 # in this data, Tim Duncan made 50.89% of his 2 point shots
 #in this data, Tim Duncan made 0% of his 3 point shots 
 
 prop.table(table(Overall_Shooting_Allen$result
                  , Overall_Shooting_Allen$three), margin =2)
##       
##        FALSE TRUE
##   made     1    1
 #in this data, Ray Allen made 100% of his 2 point shots
 #in this data, Ray Allen made 100% of his 3 point shots
 
 prop.table(table(Overall_Shooting_Williams$result
                  , Overall_Shooting_Williams$three), margin =2)
##         
##               FALSE       TRUE
##   made   0.97058824 1.00000000
##   missed 0.02941176 0.00000000
 #in this data, Mo Williams made 97.06% of his 2 point shots
 #in this data, Mo Williams made 100% of his 3 point shots
 
 prop.table(table(Overall_Shooting_Garnett$result
                  , Overall_Shooting_Garnett$three), margin =2)
##         
##               FALSE
##   made   0.96453901
##   missed 0.03546099
 #in this data, Kevin Garnett made 96.45% of his 2 point shots
 #in this data, Kevin garnett never attempted a 3 point shot
 
 prop.table(table(Overall_Shooting_Carter$result
                  , Overall_Shooting_Carter$three), margin= 2)
##         
##              FALSE      TRUE
##   made   0.6452703 0.4976303
##   missed 0.3547297 0.5023697
 #in this data, Vince carter made 64.53% of his 2 point shots
 #in this data, Vince carter made 49.76% of his 3 point shots
 
 ### Task 4 ### 
 
Tim_Duncan2 = 2*.5089
#Tim Duncan's expected value of a 2 point shot is 1.0178 pts
 Tim_Duncan3 = 3*1.0
#Tim Duncan's expected value of a 3 point shot is 3 pts
 Ray_Allen2 = 2*1.0
#Ray Allen's expected value of a 2 point shot is 2 pts
 Ray_Allen3 = 3*1.0
#Ray Allen's expected value of a 3 point shot is 3 pts 
 Mo_Williams2 = 2*.9706
#Mo Williams' expected value of a 2 point shot is 1.9412 pts
 Mo_Williams3 = 3*1.0
#Mo Williams' expected value of a 3 point shot is 3 pts
 Kevin_Garnett2 = 2*.9645
#Kevin Garnett's expected value of a 2 point shot is 1.929 pts
 Kevin_Garnett3 = 3*0
#Kevin Garnett's expected value of a 3 point shot is 0 pts (Unknown since he didn't attempt one in this data set)
Vince_Carter2 = 2*.6453
#Vince Carter's expected value of a 2 point shot is 1.2906 pts
 Vince_Carter3 = 3*.4976
#Vince Carter's expected value of a 3 point shot is 1.4928 pts 
 
 
 ### Task 5 ### 
 
 #What is the expected value of a successfully converted 4th down attempt in relation to field position?
 
 #At what point on the field is the expected value of a successful 4th down attempt great enough to risk?
 
 #What is the expected value of a 2 point attempt from the 1 or 2 for both a passing and rushing play, and is it greater than the expected value of a standard pat try?