Monday, November 11, 2013

An Application Package on Linear Regression Analysis ~~ VisualBasic Code Snippet + GUI Design

Okkk.. as promised, here is my app package(software) on Simple Linear Regression Analysis Using Visual basic.
Fact! i said in the beginning that every post i make will be for those who know OR have little knowledge about programming. if you don't, then, you need help ~~> 4rm some1 who knows some1 who knows some1 who knows how to program.....lol......just joking....... u need to learn it some how.
"Some1 asked me ~ is it a robust program?" i answered ~~ that 's an meant for you to figure out. you too, yes! you;
          -----------------
          Screen shots
          -----------------


               --------------
                Source Code
               --------------
Public Class frmmain
    Public n As Integer

    Public xCount As Double
    Public countx As Integer, counterx As Integer

    Public yCount As Double
    Public county As Integer, countery As Integer

    Public x_yCount As Double
    Public countxt As Integer, counterxt As Integer

    Public x_xCount As Double
    Public countxSquare As Integer, counterxSquare As Integer

    Public xxCount As Double
    Public countxx As Integer, counterxx As Integer

    Public xyCount As Double
    Public countxy As Integer, counterxy As Integer

    Public y_yCount As Double
    Public countySquare As Integer, counterySquare As Integer

    Public yyCount As Double
    Public countyy As Integer, counteryy As Integer

    Public ymean As Double, xmean As Double

    Public slope As Double, intercept As Double, lstSquare As Double,         pearsons As Double, standarderror As Double

    Public i As Integer ' for scatter plot

    Private Sub CmdAddx_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles CmdAddx.Click
        Try
            If txtXvalue.Text = Double.NaN Then
            End If
        Catch ex As Exception
            MessageBox.Show("Value entered is not a number, Enter value of type integer or double", "SLRegression", MessageBoxButtons.OK, MessageBoxIcon.Information)
            txtXvalue.Focus()
            GoTo uuu
        End Try

        If txtXvalue.Text = String.Empty Then
        Else
            lstx.Items.Add(txtXvalue.Text)
            txtXvalue.Text = String.Empty

            txtXvalue.Focus()
        End If
uuu:
End Sub

    Private Sub ExitToolStripMenuItem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ExitToolStripMenuItem.Click
        Me.Close()
    End Sub

    Private Sub ClearTheValuesOfXToolStripMenuItem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ClearTheValuesOfXToolStripMenuItem.Click
        lstx.Items.Clear()
    End Sub

    Private Sub ClearTheValuesOfYToolStripMenuItem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ClearTheValuesOfYToolStripMenuItem.Click
        lsty.Items.Clear()
    End Sub

    Private Sub cmdAddy_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles cmdAddy.Click
        Try
            If txtYvalue.Text = Double.NaN Then
            End If
        Catch ex As Exception
            MessageBox.Show("Value entered is not a number, Enter value of type integer or double", "SLRegression", MessageBoxButtons.OK, MessageBoxIcon.Information)
            txtYvalue.Focus()
            GoTo yyy
        End Try

        If txtYvalue.Text = String.Empty Then
        Else

            lsty.Items.Add(txtYvalue.Text)
            txtYvalue.Text = String.Empty
            txtYvalue.Focus()
        End If
yyy:
    End Sub

    Public Sub AllResultsToolStripMenuItem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles AllResultsToolStripMenuItem.Click
        If lstx.Items.Count = 0 Or lsty.Items.Count = 0 Then
            MessageBox.Show("No input values for x or y", "SLRegression", MessageBoxButtons.OK, MessageBoxIcon.Exclamation)
            txtXvalue.Focus()
            GoTo empty
        End If

        If lstx.Items.Count = 1 Or lsty.Items.Count = 1 Then
            MessageBox.Show("Required input should be more than 1 for both x & y", "SLRegression", MessageBoxButtons.OK, MessageBoxIcon.Exclamation)
            txtXvalue.Focus()
            GoTo empty
        End If

        If lstx.Items.Count = lsty.Items.Count Then

            xCount = 0
            yCount = 0
            x_xCount = 0
            y_yCount = 0
            x_yCount = 0
            xxCount = 0
            xyCount = 0
            yyCount = 0
            ymean = 0
            xmean = 0
            slope = 0
            intercept = 0
            lstSquare = 0
            pearsons = 0
            i = 0
            lstxx.Items.Clear()
            lstxy.Items.Clear()
            lstyy.Items.Clear()

            'Public Sub n() ' entry count
            n = lstx.Items.Count
            ' End Sub

            ' Public Sub sumOfx()     ' SUM OF X
            countx = lstx.Items.Count

            For counterx = 1 To countx
                xCount = xCount + lstx.Items(counterx - 1)
            Next
            'End Sub

            ' Public Sub sumOfy()        ' SUM OF Y
            county = lsty.Items.Count

            For countery = 1 To county
                yCount = yCount + lsty.Items(countery - 1)
            Next
            '  End Sub

            ' Public Sub computexXy() ' COMPUTING X TIMES Y // xt, where t for times

            ' initialising variables
            lstxy.Items.Clear()
            countxt = lstx.Items.Count
            For counterxt = 1 To countxt

           x_yCount = ((lstx.Items(counterxt - 1)) * (lsty.Items(counterxt - 1)))

           lstxy.Items.Add(x_yCount)
            Next
            ' End Sub

            ' Public Sub computexXx()    ' COMPUTING X TIMES X
            ' initialising variables
            countxSquare = lstx.Items.Count

            For counterxSquare = 1 To countxSquare
                x_xCount = ((lstx.Items(counterxSquare - 1)) * (lstx.Items(counterxSquare - 1)))

                lstxx.Items.Add(x_xCount)
            Next
            ' End Sub

            '   Public Sub sumOfxx()  'SUM OF X TIMES X
            countxx = lstxx.Items.Count

            For counterxx = 1 To countxx
                xxCount = xxCount + lstxx.Items(counterxx - 1)
            Next
            '   End Sub

            '     Public Sub sumOfxy() ' SUM OF X*Y

            countxy = lstxy.Items.Count
            For counterxy = 1 To countxy
                xyCount = xyCount + lstxy.Items(counterxy - 1)
            Next
            '      End Sub

            ' Public Sub computeyXy()    ' COMPUTING Y TIMES Y

            ' initialising variables
            countySquare = lsty.Items.Count

            For counterySquare = 1 To countySquare
                y_yCount = ((lsty.Items(counterySquare - 1)) * (lsty.Items(counterySquare - 1)))

                lstyy.Items.Add(y_yCount)
            Next
            ' End Sub

            '   Public Sub sumOfyy()  'SUM OF Y TIMES Y

            countyy = lstyy.Items.Count

            For counteryy = 1 To countyy
                yyCount = yyCount + lstyy.Items(counteryy - 1)
            Next
            '   End Sub

            ' FILLING VACANT SPACES
            lblSumOfX.Text = xCount
            lblSumOfY.Text = yCount
            lblSumOfXY.Text = xyCount
            lblSumOfXX.Text = xxCount
            lblSumOfYY.Text = yyCount

            ' GETTING THE MEAN OF X & Y
            ymean = yCount / n
            xmean = xCount / n

            ' Calculating the slope  -- b
            slope = ((n * xyCount) - (xCount * yCount)) / ((n * xxCount) - (xCount * xCount))

            ' Calculating the intercept -- a
            intercept = (yCount / n) - (slope * (xCount / n))

            ' COMPUTIN D LST SQUARE
            lstSquare = Math.Sqrt(((intercept * yyCount) - (slope * xyCount) - (n * (ymean * ymean))) / (yyCount - (n * (ymean * ymean))))

            ' COMPUTING THE STANDARD_ERROR OF ESTIMATION
            standarderror = (Math.Sqrt((yCount * yCount) - (intercept * yCount) - (slope * xyCount)) / Math.Sqrt(n - 2))

            ' COMPUTING PEARSONS CofC
            pearsons = (xyCount - (n * xmean * ymean)) / Math.Sqrt((yyCount - (n * (ymean * ymean))) * (xxCount - (n * (xmean * xmean))))

            lblResult.Text = "  The mean of x is " & Math.Round(xmean, 2) & " ; " & "The mean of y is " & Math.Round(ymean, 2) _
                    & " ; " & "The Regression line y = " & Math.Round(intercept, 2) & " + " & Math.Round(slope, 2) & "x" & _
                    "  " & vbCr & "  The Standard error of estimation = " & Math.Round(standarderror, 2) & _
                    "  " & vbCr & "  The Pearsons Coefficient of Correlation (r) = " & Math.Round(pearsons, 2)

            '' PLOtting the graph
            'TextBox1.Text = lstSquare
            ScatterPlot.chartType = MSChart20Lib.VtChChartType.VtChChartType2dXY

            ScatterPlot.RowCount = n
            ScatterPlot.ColumnCount = 2

            For i = 1 To ScatterPlot.RowCount

                ScatterPlot.Row = i
                ScatterPlot.RowLabel = i

                ScatterPlot.Column = 1
                ScatterPlot.Data = lstx.Items(i - 1)

                ScatterPlot.Column = 2
                ScatterPlot.Data = lsty.Items(i - 1)
            Next
            Scattercover.Visible = Fals
        Else
            MessageBox.Show("No corresponding y value for a given value of x or vice-versa", "SLRegression", MessageBoxButtons.OK, MessageBoxIcon.Information)
        End If
empty:
        If lstx.Items.Count = 0 Then
            lblResult.Text = String.Empty
        Else
        End If

        '' the regression line
        ScatterPlot.Plot.SeriesCollection(1).StatLine.Flag = MSChart20Lib.VtChStats.VtChStatsRegression
    End Sub

    Private Sub ScatterPlotToolStripMenuItem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ScatterPlotToolStripMenuItem.Click
        Me.AllResultsToolStripMenuItem.PerformClick()
        lblResult.Text = String.Empty

        If lstx.Items.Count = 0 Then
            lblResult.Text = String.Empty
        End If
    End Sub

    Private Sub XMeanToolStripMenuItem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles XMeanToolStripMenuItem.Click
        Me.AllResultsToolStripMenuItem.PerformClick()

        Scattercover.Visible = True
        If lstx.Items.Count = 0 Then
            lblResult.Text = String.Empty
        Else
            lblResult.Text = "The mean of x is " & Math.Round(xmean, 2)
        End If
    End Sub

    Private Sub YMeanToolStripMenuItem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles YMeanToolStripMenuItem.Click
        Me.AllResultsToolStripMenuItem.PerformClick()
        Scattercover.Visible = True

        If lstx.Items.Count = 0 Then
            lblResult.Text = String.Empty
        Else
            lblResult.Text = "The mean of y is " & Math.Round(ymean, 2)
        End If
    End Sub

    Private Sub TheRegressionLineToolStripMenuItem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles TheRegressionLineToolStripMenuItem.Click
        Me.AllResultsToolStripMenuItem.PerformClick()
        Scattercover.Visible = True

        If lstx.Items.Count = 0 Then
            lblResult.Text = String.Empty
        Else
            lblResult.Text = "The Regression line y = " & Math.Round(intercept, 2) & " + " & Math.Round(slope, 2) & "x"
        End If
    End Sub

    Private Sub LeastSquareToolStripMenuItem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles LeastSquareToolStripMenuItem.Click
        Me.AllResultsToolStripMenuItem.PerformClick()
        Scattercover.Visible = True

        If lstx.Items.Count = 0 Then
            lblResult.Text = String.Empty
        Else
            lblResult.Text = "The LeastSquare of the Regression line = " & Math.Round(lstSquare, 2)
        End If
    End Sub

    Private Sub PearsonsCOfCToolStripMenuItem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles PearsonsCOfCToolStripMenuItem.Click
        Me.AllResultsToolStripMenuItem.PerformClick()
        Scattercover.Visible = True

        If lstx.Items.Count = 0 Then
            lblResult.Text = String.Empty
        Else
            lblResult.Text = "The Pearsons Coefficient of Correlation for the Regression line r = " & Math.Round(pearsons, 2)
        End If
    End Sub

    Private Sub BlankResultPageToolStripMenuItem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs)

    End Sub

    Private Sub NewToolStripMenuItem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles NewToolStripMenuItem.Click
        If Scattercover.Visible = False Then
            Scattercover.Visible = True
        End If
        ResultsToolStripMenuItem.Enabled = True
        ClearTheValuesOfXToolStripMenuItem.Enabled = True
        ClearTheValuesOfYToolStripMenuItem.Enabled = True
        lblcover.Visible = False
        lstx.Items.Clear()
        lsty.Items.Clear()
        lstxy.Items.Clear()
        lstxx.Items.Clear()
        lstyy.Items.Clear()
        lblSumOfX.Text = ""
        lblSumOfY.Text = ""
        lblSumOfXX.Text = ""
        lblSumOfXY.Text = ""
        lblSumOfYY.Text = ""
        lblResult.Text = String.Empty

    End Sub

    Private Sub cmdRemoveX_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles cmdRemoveX.Click
        lstx.Items.Remove(lstx.SelectedItem)
    End Sub

    Private Sub cmdRemoveY_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles cmdRemoveY.Click
        lsty.Items.Remove(lsty.SelectedItem)
    End Sub

  
    Private Sub StandardErrorOfEstimationToolStripMenuItem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles StandardErrorOfEstimationToolStripMenuItem.Click
        Me.AllResultsToolStripMenuItem.PerformClick()
        Scattercover.Visible = True

        If lstx.Items.Count = 0 Then
            lblResult.Text = String.Empty
        Else

            lblResult.Text = "The Standard Error of Estimation = " & Math.Round(standarderror, 2)
        End If
    End Sub
End Class


~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
Brief Description
Looking at the last screen shot, i included the name(s) of some control(s)/tool  for easy understanding of the program. Some of these contol(s) include
  • txtXValue: - text box for entering values of x
  • cmdAddX: - click on this button to add ‘x’ value(s) into list box x and vice-versa
  • cmdRemoveX: - use this button to remove values entered beginning from the last value
  • lblResult: - displays all results which include “the mean of both x and y”, “the regression line”, “the standard error of estimation” and “pearsons coefficient of correlation”
  • lstX: - shows all values of x entered
  • lstY: - shows all values of Y entered
  • lstXX: - shows the multiplication of each x value
  • lstXY: - shows the multiplication of each x and y value
  • lstYY: - shows the multiplication of each y value
  • ScatterPlot: - a plot of the observed bivariate outcome variable (y axis) against it predictor variable (x axis), with a dot for each pair of bivariate observations.
View the menu item(s) used in the design of this application











       Sample video on this application 

~~need the executable file (software) feel free to reach me

No comments: