※参考<統計ソフトRに入力するコマンド>
統計ソフトRのインストール手順をまとめた記事も作成していますので、よろしければご参考ください。
library(BasketballAnalyzeR)
library(gridExtra)
library(dplyr)
Pbox2526 <- read.csv(file="Pbox_2526.csv")
dts.PbP.2526 <- read.csv(file="[10-21-2025]-[04-12-2026]-combined-stats.csv")
dts.PbP.2526$player <- sub("^.*/", "", dts.PbP.2526$player)
dts.PbP.2526$converted_x <- NA
dts.PbP.2526$converted_y <- NA
PbP2526 <- PbPmanipulation(dts.PbP.2526)
assignInNamespace(
"droplev_by_col",
function(data) {
idx <- sapply(data, is.factor)
if (any(idx)) {
data[, idx] <- lapply(data[, idx, drop = FALSE], droplevels)
}
data
},
ns = "BasketballAnalyzeR"
)
# Bubble plot of the players
Pbox2526b <- subset(Pbox2526, MIN>=500)
data2526b <- subset(Pbox2526b, Team=="San Antonio Spurs")
attach(data2526b)
X2 <- data.frame(P=Player, P2M=P2M/GP, P3M=P3M/GP, FTp, AS=(P2A+P3A+FTA)/GP)
detach(data2526b)
labs1 <- c("2-point shots made per Game", "3-point shots made per Game", "Free throws (% made)", "Total shots attempted per Game")
ss <- bubbleplot(X2, id="P", x="P2M", y="P3M", col="FTp", size="AS", labels=labs1, title="Bubble plot of San Antonio Spurs' players: Shooting stats and shots attempted (NBA 2025 - 2026 Regular season)", text.size=3.5, mx = mean(Pbox2526b$P2M/Pbox2526b$GP), my = mean(Pbox2526b$P3M/Pbox2526b$GP))
attach(data2526b)
Y2 <- data.frame(P=Player, DREB=DREB/GP, STL=STL/GP, BLK=BLK/GP, PM=PM/GP)
detach(data2526b)
labs2 <- c("Defensive Rebounds per Game", "Blocks per Game", "Plus-Minus per Game", "Steals per Game")
ds <- bubbleplot(Y2, id="P", x="DREB", y="BLK", col="PM", size="STL", labels=labs2, title="Bubble plot of San Antonio Spurs' players: Defensive stats and Plus-Minus (NBA 2025 - 2026 Regular season)", text.size=3.5, mx = mean(Pbox2526b$DREB/Pbox2526b$GP), my = mean(Pbox2526b$BLK/Pbox2526b$GP))
# Radar chart of the players
Pbox2526r <- subset(Pbox2526, MIN>=500)
attach(Pbox2526r)
T <- data.frame(P2M,P3M, FTM, REB=OREB+DREB, AST, STL, BLK)/MIN
detach(Pbox2526r)
T2 <- data.frame(Team=Pbox2526r$Team, Player=Pbox2526r$Player, T)
listplots <- radialprofile(data=T2[3:9], title=T2$Player, std=TRUE)
listplotsT <- listplots[T2$Team=="San Antonio Spurs"]
rc1 <- grid.arrange(grobs=listplotsT[1:10], ncol=4)
# Variability diagram of the traditional stats
Pbox2526v <- subset(Pbox2526, Team=="San Antonio Spurs" & MIN>=500)
vrb <- variability(data=Pbox2526v, data.var=c("P2M", "P3M", "FTM", "OREB", "DREB","AST","STL","BLK","TOV"), size.var=c("MIN"))
vd <- plot(vrb, title="Variability diagram of the traditional stats, San Antonio Spurs' players (NBA 2025 - 2026 Regular season)")
# Shot chart of the team
PbP2526sc <- subset(PbP2526, data_set != "2025-26 Playoffs" & team == "SAS") %>%
mutate(
half_x = ifelse(original_x <= 50,
original_x,
100 - original_x),
half_y = ifelse(original_x <= 50,
original_y,
100 - original_y),
xx = (half_y - 50) * 0.5,
yy = half_x * 0.94 - 46.5
)
sc <- shotchart(data=PbP2526sc, x="xx", y="yy", z="playlength", num.sect=5, type="sectors", scatter=FALSE, result="result")
scd <- shotchart(data=PbP2526sc, x="xx", y="yy", type="density-hexbin", nbins=50, palette="bwr")
# Density estimation of the field shots
data2526d <- subset(PbP2526, data_set!="2025-26 Playoffs" & result!="" & shot_distance!="" & team== "SAS")
data2526d$ShotType <- as.character(data2526d$ShotType)
dpd <- densityplot(
data = data2526d,
shot.type = "field",
var = "shot_distance",
best.scorer = TRUE,
title = "Density estimation of the field shots of San Antonio Spurs, with respect to shot distance (NBA 2025 - 2026 Regular Season)"
)
dpt <- densityplot(
data = data2526d,
shot.type = "field",
var = "totalTime",
best.scorer = TRUE,
title = "Density estimation of the field shots of San Antonio Spurs, with respect to total time (NBA 2025 - 2026 Regular Season)"
)
dpl <- densityplot(
data = data2526d,
shot.type = "field",
var = "playlength",
best.scorer = TRUE,
title = "Density estimation of the field shots of San Antonio Spurs, with respect to play length (NBA 2025 - 2026 Regular Season)"
)
# Expected points of the teams
data2526t <- subset(
PbP2526,
data_set != "2025-26 Playoffs" &
result != "" &
shot_distance != "" &
team == "SAS" &
shot_distance < 40
)
data2526o <- subset(
PbP2526,
data_set != "2025-26 Playoffs" &
result != "" &
shot_distance != "" &
oppTeam == "SAS" &
shot_distance < 40
)
data2526t <- mutate(data2526t, player = "San Antonio Spurs")
data2526o <- mutate(data2526o, player = "Opponent teams")
data2526TO <- bind_rows(data2526t, data2526o)
pl <- c("San Antonio Spurs","Opponent teams")
mypal <- colorRampPalette(c("purple","red","red"))
epd <- expectedpts(data2526TO, players=pl, col.team="gray", palette=mypal, col.hline="transparent", var="shot_distance", xlab="Shot distance", title="Expected points of San Antonio Spurs and opponent teams, with respect to shot distance (NBA 2025 - 2026 Regular Season)")
ept <- expectedpts(data2526TO, bw=1500, players=pl, col.team="gray", palette=mypal, col.hline="transparent", var="totalTime", xlab="Total time", title="Expected points of San Antonio Spurs and opponent teams, with respect to total time (NBA 2025 - 2026 Regular Season)")
epl <- expectedpts(data2526TO, players=pl, col.team="gray", palette=mypal, col.hline="transparent", var="playlength", xlab="Play length", title="Expected points of San Antonio Spurs and opponent teams, with respect to play length (NBA 2025 - 2026 Regular Season)")
# Network of assists
PbP2526an <- subset(PbP2526, data_set!="2025-26 Playoffs" & team== "SAS")
PbP2526AN <- PbP2526an[!(PbP2526an$event_type=="shot" & PbP2526an$result=="missed"),]
netdataAN <- assistnet(PbP2526AN)
an <- plot(netdataAN, layout="circle", edge.thr=20, node.col="FGPTS", node.size="ASTPTS")
TABan <- netdataAN$assistTable
Xan <- netdataAN$nodeStats
names(Xan)[1] <- "Player"
dataAN <- merge(Xan, Pbox2526, by="Player")
dataANsp <- subset(dataAN, Team=="San Antonio Spurs")
mypal <- colorRampPalette(c("blue", "yellow", "red"))
asp <- scatterplot(dataANsp, data.var=c("ASTPTS", "FGPTS"), z.var="MIN", labels=dataANsp$Player, palette=mypal, repel_labels=TRUE, title="Scatter plot of San Antonio Spurs' players: ASTPTS and FGPTS (NBA 2025 - 2026 Regular Season)")
selan <- which(dataAN$MIN>1500 & dataAN$Team=="San Antonio Spurs")
tabAN <- TABan[selan, selan]
no.pl <- nrow(tabAN)
pR <- pM <- vector(no.pl, mode="list")
GiniM <- array(NA, no.pl)
GiniR <- array(NA, no.pl)
for (pl in 1:no.pl){
ineqplM <- inequality(tabAN[pl,], npl=no.pl)
GiniM[pl] <- ineqplM$Gini
ineqplR <- inequality(tabAN[,pl], npl=no.pl)
GiniR[pl] <- ineqplR$Gini
title <- rownames(tabAN)[pl]
pM[[pl]] <- plot(ineqplM, title=title)
pR[[pl]] <- plot(ineqplR, title=title)
}
lca <- grid.arrange(grobs=pM, nrow=2)
lcr <- grid.arrange(grobs=pR, nrow=2)
# Shot chart of the opponent teams
PbP2526sco <- subset(PbP2526, data_set != "2025-26 Playoffs" & oppTeam == "SAS") %>%
mutate(
half_x = ifelse(original_x <= 50,
original_x,
100 - original_x),
half_y = ifelse(original_x <= 50,
original_y,
100 - original_y),
xx = (half_y - 50) * 0.5,
yy = half_x * 0.94 - 46.5
)
sco <- shotchart(data=PbP2526sco, x="xx", y="yy", z="playlength", num.sect=5, type="sectors", scatter=FALSE, result="result")
scod <- shotchart(data=PbP2526sco, x="xx", y="yy", type="density-hexbin", nbins=50, palette="bwr")
# Density estimation of the field shots of the opponent teams
data2526do <- subset(PbP2526, data_set!="2025-26 Playoffs" & result!="" & shot_distance!="" & oppTeam== "SAS")
data2526do$ShotType <- as.character(data2526do$ShotType)
dpo <- densityplot(data=data2526do, shot.type="field", var="shot_distance", best.scorer=TRUE, title="Density estimation of the field shots of San Antonio Spurs' opponents, with respect to shot distance (NBA 2025 - 2026 Regular Season)")
⇐ 2024-25 |🏀|
※データ分析を実践する際に参考にしている書籍『Basketball Data Science: With Applications in R』の紹介記事も書いていますので、よろしければ下記よりご確認ください。

