【发布时间】:2017-04-26 20:09:14
【问题描述】:
我正在使用 NBA 投篮数据,并尝试使用不同的回归技术创建投篮预测模型。但是,在尝试使用逻辑回归模型时,我遇到了以下警告消息:警告消息: glm.fit:算法没有收敛。此外,预测似乎根本不起作用(与原始 Y 变量(成或未命中)没有变化)。我将在下面提供我的代码。我从这里得到数据:Shot Data.
nba_shots <- read.csv("shot_logs.csv")
library(dplyr)
library(ggplot2)
library(data.table)
library("caTools")
library(glmnet)
library(caret)
nba_shots_clean <- data.frame("game_id" = nba_shots$GAME_ID, "location" =
nba_shots$LOCATION, "shot_number" = nba_shots$SHOT_NUMBER,
"closest_defender" = nba_shots$CLOSEST_DEFENDER,
"defender_distance" = nba_shots$CLOSE_DEF_DIST, "points" = nba_shots$PTS,
"player_name" = nba_shots$player_name, "dribbles" = nba_shots$DRIBBLES,
"shot_clock" = nba_shots$SHOT_CLOCK, "quarter" = nba_shots$PERIOD,
"touch_time" = nba_shots$TOUCH_TIME, "game_result" = nba_shots$W
, "FGM" = nba_shots$FGM)
mean(nba_shots_clean$shot_clock) # NA
# this gave NA return which means that there are NAs in this column that we
# need to clean up
# if the shot clock was NA I assume that this means it was the end of a
# quarter and the shot clock was off.
# For now I'm going to just set all of these NAs equal to zero, so all zeros
# mean it is the end of a quarter
# checking the amount of NAs
last_shots <- nba_shots_clean[is.na(nba_shots_clean$shot_clock),]
nrow(last_shots) # this tells me there is 5567 shots taken when the shot
# clock was turned off at the end of a quarter
# setting these NAs equal to zero
nba_shots_clean[is.na(nba_shots_clean)] <- 0
# checking to see if it worked
nrow(nba_shots_clean[is.na(nba_shots_clean$shot_clock),]) # it worked
# create a test and train set
split = sample.split(nba_shots_clean, SplitRatio=0.75)
nbaTrain = subset(nba_shots_clean, split==TRUE)
nbaTest = subset(nba_shots_clean, split==FALSE)
# logistic regression
nbaLogitModel <- glm(FGM ~ location + shot_number + defender_distance +
points + dribbles + shot_clock + quarter + touch_time, data=nbaTrain,
family="binomial", na.action = na.omit)
nbaPredict = predict(nbaLogitModel, newdata=nbaTest, type="response")
cm = table(nbaTest$FGM, nbaPredict > 0.5)
print(cm)
这给了我以下的输出,告诉我预测没有做任何事情,因为它和以前一样。
FALSE TRUE
0 21428 0
1 0 17977
非常感谢任何指导。
【问题讨论】:
-
尝试提供一个最小的reproducible example 样本输入数据。如果我们无法运行代码,就很难为您提供帮助。
-
@MrFlick 通过链接提供的 csv 文件不够好?
-
没有。链接到其他网站上的数据是脆弱的,并且可能不安全。如果问题真的只是模型拟合,那么所有的数据操作代码都只是干扰真正问题的噪音。如果您尽可能轻松地帮助您,您更有可能获得帮助。
-
好的,我明白为什么这会让人们更容易提供帮助。我在下面得到了答案,但对于未来的问题,我将确保提供带有样本输入数据的最少可重复示例。谢谢!
标签: r logistic-regression prediction