# Predictive analytics using PI and R

Blog Post created by Ahmad Fattahi on Nov 17, 2010

Have you ever desired to know what is a most likely trajectory your tag is going to follow? Ever wanted to be able to predict which way an equipment's temperature or pressure is moving based on what you have seen in the past several hours? How about ability to predict when an equipment is likely to fail? If so, you may be interested in this post. This is a sequel to my previous post on combining PI and R.

Our goal here is to write a script in R that reads PI data and makes some projections into the future. Here is how I'm composing the story: we collect PI data from a single tag into a flat text file. We can do this in several ways such as using PI DataLink or piconfig scripting. Then we read the values and timestamps from the file into R in the form of two vectors (I have added some noise to the values to make the situation more realistic). Note that R doesn't like commas; so different entries are separated either by space or end-of-line character.

The next step is to make a regression to best model the values as a function of time. I have used a 3rd degree polynomial. The degree is arbitrary and should be chosen to best represent the physical model. The blue part of the resulting graph shows the real events in dots and the continuous line shows the regression model.

The next step is to put this whole thing to a test. The green part of the graph is the prediction. Some more events are read from PI which have not been used to make the regression. Those are plotted against what the regression model is predicting. Note how closely the model follows the data; but of course as we get farther in time the quality of the predictor deteriorates.

This is a predictor based on a polynomial regression. There are many other ways to make such models. My goal here is to showcase one example of how effectively and simply PI and R could be combined to create such predictive models. The other stressing point is the performance. The whole procedure finishes in a blink of an eye on my humbly light-weight laptop  Please find attached the R script along with the flat file containing PI data.