Richard
Richard

Reputation: 197

predict() error: replacement has more rows than data

I really can't figure out why I'm getting an error when I use predict(). I checked this post but I'm still getting the same error predict(). I split a data frame into two (1. Train, 2. Test).

I ran a logistic model in train and applied it to test but am getting an error. Here's the code:

train=rteam[which(rteam$season!="A"),]
test=rteam[which(rteam$season=="A"),]
length(train$outcome)
#[1] 163478
#ength(test$outcome)
[1] 8246

logit.1=glm(outcome ~ hometeam + dpoints.diff + opoints.diff + outcome.sma5 + opp.outcome.sma5, data=train,
+             family="binomial", na.action=na.exclude)


test$predict=predict(logit.1, data=test, type="response")
# Error in `$<-.data.frame`(`*tmp*`, "predict", value = c(NA, NA, NA, NA,  : 
#  replacement has 163478 rows, data has 8246

I keep getting this error. I ran the predict statement again as a stand alone vector and it returned a vector with a length of the train data frame.

predict=predict(logit.1, data=test, type="response")
length(predict)
# [1] 163478

Any ideas on what's going on? Is my code wrong?

Upvotes: 3

Views: 7684

Answers (1)

M--
M--

Reputation: 28910

predict() requires newdata= rather than data=!

test$predict=predict(logit.1, newdata=test, type="response")
length(test$predict)
# [1] 8246

Upvotes: 0

Related Questions