If you want to plot trend lines in the RadHtmlChart for ASP.NET AJAX control and get a basic understanding on statistics terms like linear/logarithmic regression lines, ordinary least square method and r-squared measurement, do not miss the opportunity to examine this blog post to the end.
The term “linear regression” (Figure 1) refers to an approach in statistics, related to modelling the relationship between a dependent variable/variables (usually denoted by Y) and an independent variable (usually denoted by X).
Regression lines are also known as trend lines, and there are different types such as linear, logarithmic, exponential and so on. Because the linear regression with a single independent variable is the basic case of the regression modelling, this is the one we will examine.
Figure 1: A sample linear regression plot in MS Excel that shows the relationship between the quantity and yield of bonds.
Linear regressions can be used to forecast/predict values by developing a model over a data set of observation. Say you have the results from a student survey about the number of coffee cups drunk before attending an exam and the marks after the exam. Now you can find a potential relationship between both variables, so you can predict the exam mark of a student that will drunk "n" cups of coffee.
In the case of a multivariate regression, you can also determine which independent variables have or have no effect over the dependent one. For example, you can develop a bank score card that illustrates which characteristics (for example, age, salary, marital status and so on), and how they are related to the solvency of a potential creditor.
Before proceeding further, we will also cover another statistics term, tightly related to the linear regression subject–the ordinary least squares method.
The OLS method purpose is to estimate such parameters (α - the slope and β - the intercept) of the linear regression model (y = α*x + β) that the sum of squared residuals (i.e., the sum of the differences between the observed and predicted responses in a data set) is the minimum.
Imagine we have a bunch of random points, and we plot a straight line through it. Now our target is to find the minimum aggregate of the vertical distances between the points and the line which is illustrated by the estimates of the α and β parameters as follows:
To get a better understanding of the above formulae we will create a sample data set of (X,Y) points in MS Excel and calculate the corresponding parameters as illustrated in Table 1.
Table 1: Ordinary least squares method formulas in MS Excel with a sample data set.
For the moment ,we will pay attention to the steps that create only the left side of the table:
Since we have the necessary formula sand logic in MS Excel, we can simply transpose that in .NET.
We are going to use a DataTable data source type for our current purpose, but the same logic can be applied to any other data source, as well:
Let’s take a look at the C# code below to make things clearer:
Example 1: A part of the OrdinaryLeastSquares method that shows how to calculate slope and intercept.
R-squared, a.k.a. the coefficient of determination is a statistical measure that illustrates how well the data fits to the regression model. The coefficient ranges from 0 to 1, where values close to 1 indicate a good model (the variability of X explains well the variability of Y) while values close to 0 indicate the contrary.
You can see how the coefficient is calculated in Figure 2.
Figure 2: Formulas of the r-squared measure.
Now, we can get back to Table 1 but have a look this time at the right part of it, responsible for the r-squared calculation.
And the C# code analogue:
Example 2: A part of the OrdinaryLeastSquares method that shows how to calculate the coefficient of determination.
To plot a liner regression in RadHtmlChart with the current example, follow these steps:
If you wonder what the RegressionModelType parameter is for, here come the custom modifications you can do in this example. You can easily add support for a logarithmic regression by adding a field that calculates the Ln of X and use it instead of the original X field:
The code above resides inside the OrdinaryLeastSquares method.
You can also show the regression model to the legend. Just create the corresponding format string:
Then pass it to the Name property of the series:
I think it is high time we saw our final result in Figure 3:
Figure 3: RadHtmlChart that has its second series fits the data of the first series and displays the regression model in the legend.
We went through the basics of a popular approach in statistics–the simple linear regression--and illustrated how to integrate it in the Telerik chart for ASP.NET AJAX control. Please feel free to share your thoughts and feedback. The source code of the demo application is also available in the Plot Regression Models with RadHtmlChart code library.
Danail Vasilev is a Tech Support Engineer at Telerik’s ASP.NET AJAX Division, where he is mainly responsible for RadHtmlChart, RadGauge and RadButton controls. He joined the company in 2012 and ever since he has been responsible for providing help to customers of Telerik UI for ASP.NET AJAX suite and improving the online resources. Apart from work he likes swimming and reading books.
Subscribe to be the first to get our expert-written articles and tutorials for developers!