# (R) How could I predict the result of a new dataset based on the results on an old dataset

I have a historical dataset which tells who bought our products. This dataset contains

• ID,
• Age
• Gender
• Salary.

I have another set of data which contains the four fields above.

How should I use R to calculate the probability of purchase of each customer in the second dataset or whether they would buy our products (T/F)?

Should I use glm function? If yes, how should I approach this?

## migrated from stackoverflow.comDec 28 '16 at 14:18

This question came from our site for professional and enthusiast programmers.

• This question is too broad, prediction using data is a massive field with many techniques. Choosing a technique will depend on your data, the assumptions you can make and many other factors. Furthermore many jobs require multiple techniques along with validation to insure assumptions are met and prediction is accurate. You should probably do more research in prediction. To answer your last sentence glm specifically binomial regression is a potential method. – Adam Mccurdy Dec 28 '16 at 10:25
• Also you need a reproducible example stackoverflow.com/q/5963269/3817004 – Adam Mccurdy Dec 28 '16 at 10:26

In principle, you could fit a logistic regression (given that your response variable is binary (T/F) ) with 4 predictors (ID, Age,Gender and Salary). Then, examine the resulting model and if you are happy with it, you could use the model and the "other dataset" to perform prediction.

In R you could do fit the model by using GLM (there are other options too)

fit <- glm(Y~ID+AGE+GENDER+SALARY, data=OLDDATA, family=binomial())


And if you are happy with the model then you can use

prediction <- predict(fit,newdata=OTHERDATA)


As people mention, there are several analysis and diagnostics you should run to be sure you can actually do the prediction, but the central workflow is fit (you may need to do a lot of data management before this), evaluate the model and predict.

Alternative R syntax: You can also write the fitting instruction in R in this way

fit <- glm(Y~ID+AGE+GENDER+SALARY, data=OLDDATA, family='binomial')