J'essaie d'utiliser la fonction de prévision pour prédire les valeurs d'une régression logistique et j'obtiens le nombre de lignes incorrect. Cette question a déjà été posée R Warning: newdata' had 15 rows but variables found have 22 rowsRégression logistique R-prédire: Non de lignes erreur
et j'ai essayé l'approche mais j'ai toujours l'erreur. Voici le code
# Split as training and test sets
train_idx <- trainTestSplit(adult,trainPercent=75,seed=1111)
train <- adult[train_idx, ]
test <- adult[-train_idx, ]
xtrain <- train[,1:7]
ytrain <- train[,8]
xtrain1 <- dummy.data.frame(xtrain, sep = ".")
xtrain2 <- as.matrix(xtrain1)
xtest <- test[,1:7]
ytest <- test[,8]
xtest1 <- dummy.data.frame(xtest, sep = ".")
xtest2 <- as.matrix(xtest1)
fit=glm(ytrain~xtrain2,family=binomial)
a=predict(fit,newdata=xtrain1,type="response")
b=ifelse(a>0.5,1,0)
confusionMatrix(b,ytrain)
Confusion Matrix and Statistics
Reference
Prediction 0 1
0 16065 3157
1 968 2430
Accuracy : 0.8176
95% CI : (0.8125, 0.8227)
# Predict with test dataframe
a=predict(fit,xtest1,type="response")
: 'newdata' had 7541 rows but variables found have 22620 rows
2: In predict.lm(object, newdata, se.fit, scale = 1, type = ifelse(type == :
prediction from a rank-deficient fit may be misleading
>
J'ai aussi essayé
names(xtest1)=names(xtrain1) and
a=predict(fit,xtest1,type="response")
Ils étaient les mêmes de toute façon, mais je reçois la même erreur. C'est un problème très contre-intuitif. S'il vous plaît aider ...
s'il vous plaît voir le premier commentaire de Ben Bolker: https://stackoverflow.com/questions/9028662/predict-maybe-im-not-understanding-it – Alex
Merci @Alex. Fait le changement! –