Visual C#

Microsoft Visual C# Statistics: The Linear Correlation Coefficient

In statistics, a correlation is a value that defines a relationship between two variables so that it shows how one of the variables is tied (related) to…

4 min read863 words

Introduction#

In statistical analysis, correlation describes how two variables are related, showing how changes in one correspond to changes in the other. The linear correlation coefficient, denoted as r, measures the relationship strength between two sample elements, often labeled x and y. This means data is viewed as pairs of these elements. Also known as the Pearson product moment correlation coefficient, named after Karl Pearson, the calculation uses this formula:

The factors in this equation are:

FactorDescription
nThe number of pairs of elements
xAn element of the series
yThe element corresponding to x in the pair
ΣxThe sum of the x elements
ΣyThe sum of the y elements
ΣxyThe sum of the products of each pair
Σx 2The sum of the squares of the x values
(Σx) 2The square of the Σx sums
Σy 2The sum of the squares of the y values
(Σy) 2The square of the Σy sum

Hands-on Exercise: Building the Application

  1. Start Microsoft Visual Studio
  2. To create a new application, on the main menu, click File -> New Project...
  3. In the middle list, click Windows Forms Application
  4. Change the Name to LinearCorrelationCoefficient1
  5. Click OK
  6. In the Solution Explorer, right-click Form1.cs and click Rename
  7. Type Exercise.cs and press Enter
  8. Design the form as follows: Control (Name) Text Other Properties Label x Label y Label Values to Add: TextBox txtX TextBox txtY Button btnAdd Add ListView lvwValues FullRowSelect: True GridLines: True View: Details Columns (Name) Text TextAlign Width colIndex # 30 colX x Right 70 colY y Right 70 colXSquared x 2 Right 90 colYSquared y 2 Right 90 colXY xy Right 100 Button btnCalculate Calculate Label n = TextBox txtN Label Σx = TextBox txtSumX Label Σx 2 = TextBox txtSumXSquare Label Σxy = TextBox txtSumXY Label Σy = TextBox txtSumY Label Σy 2 = TextBox txtSumYSquare PictureBox pbxR Label = TextBox txtR Label Conclusion TextBox txtConclusion Button btnClear Clear/Reset Button btnClose Close
  9. Double-click an unoccupied area of the form
  10. Return to the form and double-click the Add button
  11. Return to the form
  12. Double-click the Calculate button
  13. Return to the form
  14. Double-click the Clear/Reset button
  15. Return to the form
  16. Double-click the Close button
  17. Change the file as follows: using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; using System.Drawing; using System.Linq; using System.Text; using System.Windows.Forms; namespace LinearCorrelationCoefficient1 { public partial class LinearCorrelationCoefficient : Form { List<SamplePair> pairs; public LinearCorrelationCoefficient() { InitializeComponent(); } private void ShowPairs() { lvwValues.Items.Clear(); double SumX = 0.00, SumY = 0.00, SumXSquare = 0.00, SumYSquare = 0.00, SumXY = 0.00; int i = 0; foreach( SamplePair sp in pairs) { ListViewItem lviPair = new ListViewItem((i + 1).ToString()); lviPair.SubItems.Add(sp.x.ToString("F")); lviPair.SubItems.Add(sp.y.ToString("F")); lviPair.SubItems.Add(sp.xSquared.ToString("0.0000#")); lviPair.SubItems.Add(sp.ySquared.ToString("0.0000#")); lviPair.SubItems.Add(sp.xy.ToString("0.0000#")); lvwValues.Items.Add(lviPair); i++; } foreach (SamplePair sp in pairs) { SumX += sp.x; SumY += sp.y; SumXSquare += (sp.x * sp.x); SumYSquare += (sp.y * sp.y); SumXY += (sp.x * sp.y); } ListViewItem lviTotals = new ListViewItem("Totals"); lviTotals.SubItems.Add("Σx = " + SumX.ToString("F")); lviTotals.SubItems.Add("Σy = " + SumY.ToString("F")); lviTotals.SubItems.Add("Σx² = " + SumXSquare.ToString("0.0000#")); lviTotals.SubItems.Add("Σy² = " + SumYSquare.ToString("0.0000#")); lviTotals.SubItems.Add("Σxy = " + SumXY.ToString("0.0000#")); lvwValues.Items.Add(lviTotals); } private void LinearCorrelationCoefficient_Load(object sender, EventArgs e) { pairs = new List<SamplePair>(); } private void btnAdd_Click(object sender, EventArgs e) { double x = 0.00, y = 0.00; if( txtX.Text == "" ) { MessageBox.Show("You must enter a value for x."); return; } if (txtY.Text == "") { MessageBox.Show("You must enter a value for y."); return; } try { x = double.Parse(txtX.Text); } catch (FormatException) { } try { y = double.Parse(txtY.Text); } catch (FormatException) { } SamplePair pair = new SamplePair(x, y); pairs.Add(pair); ShowPairs(); txtX.Text = ""; txtY.Text = ""; txtX.Focus(); ShowPairs(); } private void btnCalculate_Click(object sender, EventArgs e) { double SumX = 0.00, SumY = 0.00, SumXSquare = 0.00, SumYSquare = 0.00, SumXY = 0.00, r = 0.00; foreach (SamplePair sp in pairs) { SumX += sp.x; SumY += sp.y; SumXSquare += (sp.x * sp.x); SumYSquare += (sp.y * sp.y); SumXY += (sp.x * sp.y); } double NTimesSumXY = pairs.Count * SumXY; double SumXTimesSumY = SumX * SumY; double SquareRoot1 = pairs.Count * SumXSquare - Math.Pow(SumX, 2); double SquareRoot2 = pairs.Count * SumYSquare - Math.Pow(SumY, 2); r = (NTimesSumXY - SumXTimesSumY) / (Math.Sqrt(SquareRoot1) * Math.Sqrt(SquareRoot2)); txtN.Text = pairs.Count.ToString(); txtSumX.Text = SumX.ToString("F"); txtSumY.Text = SumY.ToString("F"); txtSumXSquare.Text = SumXSquare.ToString(); txtSumYSquare.Text = SumYSquare.ToString(); txtSumXY.Text = SumXY.ToString("F"); txtR.Text = r.ToString("0.00#"); } private void btnClear_Click(object sender, EventArgs e) { pairs.Clear(); lvwValues.Items.Clear(); txtN.Text = ""; txtSumX.Text = ""; txtSumY.Text = ""; txtSumXSquare.Text = ""; txtSumYSquare.Text = ""; txtSumXY.Text = ""; txtR.Text = ""; } private void btnClose_Click(object sender, EventArgs e) { Close(); } } public class SamplePair { public double x { get; set; } public double y { get; set; } public SamplePair(double X, double Y) { x = X; y = Y; } public double xSquared { get { return x * x; } } public double ySquared { get { return y * y; } } public double xy { get { return x * y; } } } }
  18. To execute, press F5
  19. Enter each pair of values and click Add each time
  20. After adding the values, click Calculate
  21. Close the form and return to your programming environment

Home Copyright © 2010-2011 C# Key