# predict() function in R

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")

• Welcome to the site! newdata = test_data in predict you gave it train_data. – Toros91 Apr 9 '18 at 6:23
• 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. – bradS Apr 9 '18 at 8:40
• 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 – deepguy Apr 9 '18 at 8:50
• you need to remove the target variable and pass it on to the model for future prediction. – Toros91 Apr 9 '18 at 9:30
• 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 ? – deepguy Apr 9 '18 at 14:52