Logistic regression R-predict : No of rows error : <p>I am trying to use the predict function to predict the values of a logistic regression and I am getting the incorrect number of rows. This question has already been asked 
<a href="https://stackoverflow.com/questions/40978688/r-warning-newdata-had-15-rows-but-variables-found-have-22-rows">R Warning: newdata&#39; had 15 rows but variables found have 22 rows</a></p>

<p>and I have tried the approach but I still get the error. Here is the code</p>

<pre><code># Split as training and test sets
train_idx &lt;- trainTestSplit(adult,trainPercent=75,seed=1111)
train &lt;- adult[train_idx, ]
test &lt;- adult[-train_idx, ]


xtrain &lt;- train[,1:7]
ytrain &lt;- train[,8]
xtrain1 &lt;- dummy.data.frame(xtrain, sep = ".")
xtrain2 &lt;- as.matrix(xtrain1)

xtest &lt;- test[,1:7]
ytest &lt;- test[,8]
xtest1 &lt;- dummy.data.frame(xtest, sep = ".")
xtest2 &lt;- as.matrix(xtest1)

fit=glm(ytrain~xtrain2,family=binomial)
a=predict(fit,newdata=xtrain1,type="response")
b=ifelse(a&gt;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
&gt; 
</code></pre>

<p>I also tried</p>

<pre><code>   names(xtest1)=names(xtrain1) and
    a=predict(fit,xtest1,type="response")
</code></pre>

<p>They were the same anyway but I get the same error. This is an issue that is very counter intuitive. Please help...</p>
