Read in Data
nba_data <- read.csv("C:/Users/patty/Documents/INFOSCI 310/2022-2023_NBA_player_stats_formatted.csv")
Data Inspection
str(nba_data)
'data.frame': 679 obs. of 30 variables:
$ Rk : int 1 2 3 4 5 6 6 6 7 8 ...
$ Player: chr "Precious Achiuwa" "Steven Adams" "Bam Adebayo" "Ochai Agbaji" ...
$ Pos : chr "C" "C" "C" "SG" ...
$ Age : int 23 29 25 22 22 24 24 24 27 24 ...
$ Tm : chr "TOR" "MEM" "MIA" "UTA" ...
$ G : int 55 42 75 59 77 59 36 23 72 68 ...
$ GS : int 12 42 75 22 20 3 3 0 70 68 ...
$ MP : num 20.7 27 34.6 20.5 21.8 15 14.7 15.5 27.4 32.6 ...
$ FG : num 3.6 3.7 8 2.8 3.2 2.2 2.3 2.1 3.4 5.9 ...
$ FGA : num 7.3 6.3 14.9 6.5 6.8 5 4.7 5.4 7.7 9.2 ...
$ FG.1 : num 0.485 0.597 0.54 0.427 0.47 0.444 0.488 0.384 0.44 0.644 ...
$ X3P : num 0.5 0 0 1.4 1.2 1 1 1.1 2 0 ...
$ X3PA : num 2 0 0.2 3.9 3.5 2.7 2.4 3.1 5.1 0.1 ...
$ X3P. : num 0.269 0 0.083 0.355 0.353 0.384 0.402 0.361 0.399 0.1 ...
$ X2P : num 3 3.7 8 1.4 2 1.2 1.3 1 1.4 5.9 ...
$ X2PA : num 5.4 6.2 14.7 2.7 3.4 2.3 2.3 2.3 2.7 9.1 ...
$ X2P. : num 0.564 0.599 0.545 0.532 0.591 0.515 0.578 0.415 0.518 0.653 ...
$ eFG. : num 0.521 0.597 0.541 0.532 0.56 0.547 0.591 0.488 0.571 0.645 ...
$ FT : num 1.6 1.1 4.3 0.9 1.4 0.7 0.8 0.6 1.6 2.4 ...
$ FTA : num 2.3 3.1 5.4 1.2 1.9 1 1.1 0.9 1.8 3.3 ...
$ FT. : num 0.702 0.364 0.806 0.812 0.75 0.667 0.692 0.619 0.905 0.733 ...
$ ORB : num 1.8 5.1 2.5 0.7 1.1 0.3 0.2 0.3 0.8 3.3 ...
$ DRB : num 4.1 6.5 6.7 1.3 3.7 1.5 1.4 1.5 2.4 6.5 ...
$ TRB : num 6 11.5 9.2 2.1 4.8 1.7 1.6 1.8 3.3 9.8 ...
$ AST : num 0.9 2.3 3.2 1.1 1.3 1.8 2.1 1.4 2.3 1.7 ...
$ STL : num 0.6 0.9 1.2 0.3 0.6 0.5 0.7 0.3 0.9 0.8 ...
$ BLK : num 0.5 1.1 0.8 0.3 0.6 0.4 0.4 0.3 0.2 1.2 ...
$ TOV : num 1.1 1.9 2.5 0.7 0.8 0.9 1.3 0.4 1 1.4 ...
$ PF : num 1.9 2.3 2.8 1.7 1.9 1.5 1.6 1.3 1.6 2.3 ...
$ PTS : num 9.2 8.6 20.4 7.9 9 6.2 6.3 5.9 10.4 14.3 ...
summary(nba_data)
Rk
Min. : 1.0
1st Qu.:132.5
Median :264.0
Mean :266.0
3rd Qu.:399.5
Max. :539.0
Player
Length:679
Class :character
Mode :character
Pos
Length:679
Class :character
Mode :character
Age
Min. :19.00
1st Qu.:23.00
Median :25.00
Mean :26.03
3rd Qu.:29.00
Max. :42.00
Tm
Length:679
Class :character
Mode :character
G
Min. : 1.00
1st Qu.:22.00
Median :45.00
Mean :43.34
3rd Qu.:65.50
Max. :83.00
GS
Min. : 0.00
1st Qu.: 0.00
Median : 6.00
Mean :20.07
3rd Qu.:36.50
Max. :83.00
MP
Min. : 1.00
1st Qu.:12.15
Median :18.80
Mean :19.46
3rd Qu.:27.55
Max. :41.00
FG
Min. : 0.000
1st Qu.: 1.500
Median : 2.600
Mean : 3.249
3rd Qu.: 4.200
Max. :11.200
FGA
Min. : 0.000
1st Qu.: 3.400
Median : 5.700
Mean : 6.921
3rd Qu.: 9.200
Max. :22.200
FG.1
Min. :0.0000
1st Qu.:0.4150
Median :0.4540
Mean :0.4622
3rd Qu.:0.5050
Max. :1.0000
X3P
Min. :0.0000
1st Qu.:0.3000
Median :0.8000
Mean :0.9965
3rd Qu.:1.5000
Max. :4.9000
X3PA
Min. : 0.000
1st Qu.: 1.000
Median : 2.400
Mean : 2.783
3rd Qu.: 4.150
Max. :11.400
X3P.
Min. :0.0000
1st Qu.:0.2860
Median :0.3460
Mean :0.3181
3rd Qu.:0.3880
Max. :1.0000
X2P
Min. : 0.000
1st Qu.: 0.900
Median : 1.600
Mean : 2.255
3rd Qu.: 3.000
Max. :10.500
X2PA
Min. : 0.000
1st Qu.: 1.700
Median : 3.100
Mean : 4.138
3rd Qu.: 5.550
Max. :17.800
X2P.
Min. :0.0000
1st Qu.:0.4840
Median :0.5350
Mean :0.5271
3rd Qu.:0.5940
Max. :1.0000
eFG.
Min. :0.0000
1st Qu.:0.4960
Median :0.5370
Mean :0.5329
3rd Qu.:0.5790
Max. :1.0000
FT
Min. : 0.000
1st Qu.: 0.400
Median : 0.900
Mean : 1.367
3rd Qu.: 1.700
Max. :10.000
FTA
Min. : 0.000
1st Qu.: 0.600
Median : 1.200
Mean : 1.769
3rd Qu.: 2.300
Max. :12.300
FT.
Min. :0.0000
1st Qu.:0.6670
Median :0.7600
Mean :0.7116
3rd Qu.:0.8410
Max. :1.0000
ORB
Min. :0.0000
1st Qu.:0.3000
Median :0.7000
Mean :0.8423
3rd Qu.:1.1000
Max. :5.1000
DRB
Min. :0.00
1st Qu.:1.40
Median :2.30
Mean :2.62
3rd Qu.:3.40
Max. :9.60
TRB
Min. : 0.000
1st Qu.: 1.800
Median : 3.000
Mean : 3.461
3rd Qu.: 4.500
Max. :12.500
AST
Min. : 0.000
1st Qu.: 0.800
Median : 1.300
Mean : 2.012
3rd Qu.: 2.700
Max. :10.700
STL
Min. :0.0000
1st Qu.:0.3000
Median :0.5000
Mean :0.6018
3rd Qu.:0.8000
Max. :3.0000
BLK
Min. :0.0000
1st Qu.:0.1000
Median :0.3000
Mean :0.3704
3rd Qu.:0.5000
Max. :3.0000
TOV
Min. :0.000
1st Qu.:0.500
Median :0.900
Mean :1.067
3rd Qu.:1.400
Max. :4.100
PF
Min. :0.000
1st Qu.:1.200
Median :1.600
Mean :1.661
3rd Qu.:2.200
Max. :5.000
PTS
Min. : 0.000
1st Qu.: 4.100
Median : 6.900
Mean : 8.859
3rd Qu.:11.550
Max. :33.100
head(nba_data)
NA
Data Cleaning (if needed)
nba_data <- na.omit(nba_data)
Compute metrics Calculate points per minute as an efficiency indicator
nba_data$Efficiency <- nba_data$PTS / nba_data$MP
Statistical Analysis a)Descriptive statistics
mean(nba_data$Efficiency)
[1] 0.4215845
median(nba_data$Efficiency)
[1] 0.4049296
sd(nba_data$Efficiency)
[1] 0.1566084
Group Analysis Examine efficiency by player positions
aggregate(nba_data$Efficiency, by=list(nba_data$Pos), mean)
cor(nba_data$Efficiency, nba_data$PTS)
[1] 0.7428214
cor(nba_data$Efficiency, nba_data$TRB)
[1] 0.3615683
cor(nba_data$Efficiency, nba_data$AST)
[1] 0.4172295
cor(nba_data$Efficiency, nba_data$eFG.)
[1] 0.4044894
cor(nba_data$Efficiency, nba_data$X3P.)
[1] 0.2269389
6.Efficiency vs Free Throw Percentage
cor(nba_data$Efficiency, nba_data$FT.)
[1] 0.2536861
cor(nba_data$Efficiency, nba_data$MP)
[1] 0.4436417
cor(nba_data$Efficiency, nba_data$TOV)
[1] 0.5650747
cor(nba_data$Efficiency, nba_data$PF)
[1] 0.2586232
install.packages("ggplot2")
WARNING: Rtools is required to build R packages but is not currently installed. Please download and install the appropriate version of Rtools before proceeding:
https://cran.rstudio.com/bin/windows/Rtools/
Installing package into ‘C:/Users/patty/AppData/Local/R/win-library/4.3’
(as ‘lib’ is unspecified)
trying URL 'https://cran.rstudio.com/bin/windows/contrib/4.3/ggplot2_3.4.4.zip'
Content type 'application/zip' length 4299925 bytes (4.1 MB)
downloaded 4.1 MB
package ‘ggplot2’ successfully unpacked and MD5 sums checked
The downloaded binary packages are in
C:\Users\patty\AppData\Local\Temp\RtmpIVyESS\downloaded_packages
library("ggplot2")
Warning: package ‘ggplot2’ was built under R version 4.3.2
ggplot(nba_data, aes(x=Efficiency, y=PTS)) +
geom_point() +
geom_smooth(method="lm", color="blue") +
ggtitle("Efficiency vs Points Per Game") +
xlab("Efficiency") + ylab("Points Per Game")
ggplot(nba_data, aes(x=Efficiency, y=TRB)) +
geom_point() +
geom_smooth(method="lm",color="blue") +
ggtitle("Efficiency vs Total Rbounds") +
xlab("Efficiency") + ylab("Total Rebounds")
ggplot(nba_data, aes(x=Efficiency, y=AST)) +
geom_point() +
geom_smooth(method="lm", color="blue") +
ggtitle("Efficiency vs. Assists") +
xlab("Efficiency") + ylab("Assists")
ggplot(nba_data, aes(x=Efficiency, y=eFG.)) +
geom_point() +
geom_smooth(method="lm", color="blue") +
ggtitle("Efficiency vs. Field Goal Percentage") +
xlab("Efficiency") + ylab("Field Goal Percentage")
ggplot(nba_data, aes(x=Efficiency, y=X3P.)) +
geom_point() +
geom_smooth(method="lm", color="blue") +
ggtitle("Efficiency vs. 3-Point Percentage") +
xlab("Efficiency") + ylab("3-Point Percentage")
ggplot(nba_data, aes(x=Efficiency, y=FT.)) +
geom_point() +
geom_smooth(method="lm", color="blue") +
ggtitle("Efficiency vs. Free Throw Percentage") +
xlab("Efficiency") + ylab("Free Throw Percentage")
ggplot(nba_data, aes(x=Efficiency, y=MP)) +
geom_point() +
geom_smooth(method="lm", color="blue") +
ggtitle("Efficiency vs. Minutes Played") +
xlab("Efficiency") + ylab("Minutes Played")
ggplot(nba_data, aes(x=Efficiency, y=TOV)) +
geom_point() +
geom_smooth(method="lm", color="blue") +
ggtitle("Efficiency vs. Turnovers") +
xlab("Efficiency") + ylab("Turnovers")
ggplot(nba_data, aes(x=Efficiency, y=PF)) +
geom_point() +
geom_smooth(method="lm", color="blue") +
ggtitle("Efficiency vs. Personal Fouls") +
xlab("Efficiency") + ylab("Personal Fouls")
Boxplot of Efficiency by Position
ggplot(nba_data, aes(x=Pos, y=Efficiency)) +
geom_boxplot() +
ggtitle("Distribution of Efficiency by Position") +
xlab("Position") + ylab("Efficiency")
Scatter Plot of Efficiency vs Points per Game, Colored by Position
ggplot(nba_data, aes(x=Efficiency, y=PTS, color=Pos)) +
geom_point() +
ggtitle("Efficiency vs Points per Game by Position") +
xlab("Efficiency") + ylab("Points per Game")
Facetted Scatter Plot for Each Postion
ggplot(nba_data, aes(x=Efficiency, y=PTS)) +
geom_point() +
facet_wrap(~Pos) +
ggtitle("Efficiency vs Points per Game for Each position") +
xlab("Efficiency") + ylab("Points per Game")
Boxplot of Efficiency by Team
ggplot(nba_data, aes(x=Tm, y=Efficiency)) +
geom_boxplot() +
ggtitle("Distribution of Player Efficiency Across NBA Teams") +
xlab("Team") + ylab("Efficiency") +
theme(axis.text.x = element_text(angle = 90, hjust = 1))
Team Avg Eff
team_efficiency <- aggregate(Efficiency ~ Tm, data = nba_data, mean)
ggplot(team_efficiency, aes(x=Tm, y=Efficiency)) +
geom_bar(stat="identity") +
ggtitle("Average Efficiency of NBA Teams") +
xlab("Team") + ylab("Average Efficiency") +
theme(axis.text.x = element_text(angle= 90, hjust = 1))
Histogram of Player Efficiency
ggplot(nba_data, aes(x=Efficiency)) +
geom_histogram(binwidth = 0.1, fill="blue", color="black") +
ggtitle("Histogram of Player Efficiency") +
xlab("Efficiency") + ylab("Frequency")
Efficiency vs Age Scatter Plot
ggplot(nba_data, aes(x=Age, y=Efficiency)) +
geom_point() +
geom_smooth(method="lm", color="red") +
ggtitle("Efficiency vs. Age") +
xlab("Age") + ylab("Efficiency")
Efficiency Heatmap by Position and Team
ggplot(nba_data, aes(x=Tm, y=Pos, fill=Efficiency)) +
geom_tile() +
scale_fill_gradient(low="white", high="red") +
theme_minimal() +
theme(axis.text.x = element_text(angle = 45, hjust = 1, size = 8), # Adjust angle and size
axis.text.y = element_text(size = 8)) +
ggtitle("Efficiency Heatmap by Position and Team") +
xlab("Team") + ylab("Position")
NA
NA