0
$\begingroup$

While predicting, what happens if we pass the newdata along with the target variable? Do we need to isolate the target variable before feeding into predict function?

data = read.csv("diabetes.csv")  #768 observations of 9 variables

#I split the data into train and test
set.seed(123)
IDS = 1:nrow(data)
train_data = sample(x = IDS,size = nrow(data)*0.7)
test_data = sample(x = setdiff(IDS,train_data),size = nrow(data)*0.3)
train_data = data[train_data,]
test_data = data[test_data,]

#Build logistic regression model
log_model1 = glm(formula = Outcome ~ .,data = train_data,family = 'binomial')

#Predict it on test or train
train_predict = predict(object = log_model1,newdata = train_data,type = "response")
$\endgroup$
  • $\begingroup$ Welcome to the site! newdata = test_data in predict you gave it train_data. $\endgroup$ – Toros91 Apr 9 '18 at 6:23
  • $\begingroup$ As @Toros91 notes, you've passed in the training data - if you need to extract fitted values for your training data, you can use log_model1$fitted.values. I'd suggest also that you explicitly specify the link function in the family - see rdocumentation.org/packages/stats/versions/3.4.3/topics/family for an explanation. $\endgroup$ – bradS Apr 9 '18 at 8:40
  • $\begingroup$ Thanks for the reply guys.. But here do I need to pass the test/train along with target variable ? or do I have to isolate the independent features from target before feeding it into predict ? Does it make any difference $\endgroup$ – deepguy Apr 9 '18 at 8:50
  • $\begingroup$ you need to remove the target variable and pass it on to the model for future prediction. $\endgroup$ – Toros91 Apr 9 '18 at 9:30
  • $\begingroup$ Thank you Toros91... Just wanted to know what if I feed the test data along with target ! Will the system omits the target variable and considers only independent features ? $\endgroup$ – deepguy Apr 9 '18 at 14:52
2
$\begingroup$

As is common in R the answer depends and you should always double check to make sure including the target (or other) doesn’t affect the answer. But generally it won’t. Superfluous features are ignored as long as newdata includes all expected predictors present during training.

| improve this answer | |
$\endgroup$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.