# Deploying a car price model using R and AzureML

**Longhow Lam's Blog » R**, and kindly contributed to R-bloggers]. (You can report issue about the content on this page here)

Want to share your content on R-bloggers? click here if you have a blog, or here if you don't.

Recently Microsoft released the AzureML R package, it allows R users to publish their R models (or any R function) as a web service on the Microsoft Azure Machine Learning platform. Of course, I wanted to test the new package, so I performed the following steps.

**Sign up for Azure Machine Learning**

To make use of this service you will need to sign up for an account. There is a free tier account, go to the Azure machine learning website.

**Get the AzureML package**

The azureml package can be installed from GitHub, follow the instructions here.

**Create a model in R to deploy**

In a previous blog post I described how to estimate the value of a car. I scraped the Dutch site www.autotrader.nl where used cars are sold. For more than 100.000 used cars I have their make, model, sales price, mileage (kilometers driven) and age. Let’s focus again on Renault, and instead of only using mileage I am also going to use the age, and the car model in my predictive splines model. For those who are interested the Renault data (4MB, Rda format, with many more variables) is available here.

library(splines) RenaultPriceModel = lm(Price ~ ns(Kilometers, df=5)*model + ns(Age,df=3) , data = RenaultCars)

Adding interaction between Kilometers and car model increases the predictive power. Suppose I am satisfied with the model, it has a nice R-squared (0.92) on a hold-out set, now I am going to create a scoring function from the model object

RenaultScorer = function(Kilometers, Age, model) { require(splines) return( predict.lm( RenaultPriceModel, data.frame("Kilometers" = Kilometers, "Age" = Age, "model" = model) ) ) } # predict the price for a 1 year old Renault Espace that has driven 50000 KM RenaultScorer(50000,365,"ESPACE") 33616.91

To see how estimated car prices vary over the age and kilometers, I created some contour plots. It seems that Clios loose value **over years** while Espaces loose value **over kilometers**. Sell your five year old Espace with 100K kilometers for 20.000 Euro, so that you can buy a new Clio (0 years and 0 kilometers)..

Before you can publish R functions on Azure you need to obtain the security credentials to your Azure Machine Learning workspace. See the instructions here. The function, *RenaultScorer,* can now be published as a web service on the AzureML platform using the **publishWebservice** function.

library(AzureML) myWsID = "123456789101112131415" myAuth = "ABCDEFGHIJKLMNOPQRSTUVWXYZ" # publish the R function to AzureML RenaultWebService = publishWebService( "RenaultScorer", "RenaultPricerOnline", list("Kilometers" = "float", "Age" = "float", "model" = "string"), list("prijs" = "float"), myWsID, myAuth )

**Test the web service**

**In R**, the AzureML package contains the **consumeList** function to call the web service we have just created. The key and the location of the web service that we want to call are contained in the return object of the **publishWebservice**.

### predict the price of a one year old Renault Espace using the web service RenaultPrice = consumeLists( RenaultWebService[[2]][[1]]["PrimaryKey"], RenaultWebService[[2]][[1]]$ApiLocation, list("Kilometers" = 50000, "Age" = 365, "model" = "ESPACE") ) RenaultPrice prijs 1 33616.90625

In the **Azure machine learning studio** you can also test the web service.

My web service (called **RenaultPricerOnline)** is visible now in Azure machine Learning studio, click on it and it will launch a dialog box to enter kilometers, age and model.

The call results again in a price of 33616.91. The call takes some time, but that’s because the performance is throttled for free tier accounts.

**Conclusion**

In R I have created a car price prediction model, but more likely than not, the predictive model will be consumed by another application than R, It is not difficult to publish an R model as a web service on the Azure machine learning platform. Once you have done that, AzureML will generate the api key and the link to the web service, so that you can provide that to anyone who wants to consume the predictive model in their applications.

**leave a comment**for the author, please follow the link and comment on their blog:

**Longhow Lam's Blog » R**.

R-bloggers.com offers

**daily e-mail updates**about R news and tutorials about learning R and many other topics. Click here if you're looking to post or find an R/data-science job.

Want to share your content on R-bloggers? click here if you have a blog, or here if you don't.