How to populate predictions in test data after training the model on train set in R?

r
logistic_regression

#1

Hi,

I am pretty new to R.

I was recently working on sample project for logistic regression in R. I have 2 two data set (train and test). I started applying logistic regression on R to predict value of dependent variable.

Now , I want to know to how to populate value of dependent variable in test data set using prediction made on train dataset?


#2

@Rehan_Quershi,

You must have used something like

model<-glm(dependentVariable~. , data=train)

now you should use

prediction<- predict(model, test)

Hope this helps!


#3

Hello Aditya,
Thanks for your suggestions.
i executed
prediction<- predict(model, test)
as well , but the thing is that using this result , how can i populate value of dependent variable in test dataset?
I hope you got my question.


#4

@Rehan_Quershi,

do you want to have the predicted value column to be attached to the test data?
If yes, please try this.

output <- cbind(testdata, prediction)

the output vector will have all the columns of the test dataset + the predicted value as the last column.


#5

I have used following commands in Practice Problem : Loan Prediction - 2 . But I don’t know how to convert the pred value (which is numeric ) in character type (yes / no for Loan_Status )

model1 <-glm(Loan_Status~Gender+Married+Dependents+Education+Self_Employed+Property_Area+ApplicantIncome+CoapplicantIncome+LoanAmount+Loan_Amount_Term+Credit_History,family=binomial(link=‘logit’),train_dataset)

pred <- predict(model1, test_dataset)

output <- cbind(test_dataset, pred)


#6

Hi @Ankitb_5

do a predict with type response and then fixe the probability level to the classification, the following lines give one example.

pred <- predict(model1, test_dataset, type=“response”)
loanapproved <-ifelse( pred > .51, “yes”, "no)
output <- cbind(test_dataset, loanapproved )

Have a good day

Alain


#7

Adding to the reply given by @Lesaffrea.

Prediction output will be matrix in your case. So, you can use

pred_data <- cbind(test_data, ifelse(predicted[,2] > .51, “yes”, “no”))
colnames(pred_data)[13] <- “Loan_YN”.


#8

Hi,
I have a test data set like this…
X Y
70 nA
71 NA
72 Na
74 Na
And I have a train data like this
X y
32 43
43 53
45 56
46 47
56 60
So,here after training a model using train data…I need to predict what is the value of Y for each and every value of X in test data…
Please any one help me out for this scenario using Rstudio.Thanks in advance