thenatural10
11-04-2010, 08:12 AM
I wrote a program that will do linear regression based on linear, exponential, or power functions. I get the linear to work but b for exponential and power requires it be a ln(b). I know that log function returns natural log but every way I try to add it into the program, it gives me response that it is an invalid procedure call or argument.
Please help! My program is due in 4 hours!
(I have b = log(b) under the 3rd elseif just to show one thing I tried)
Thank you!
Option Explicit
Private Sub CommandButton1_Click()
'
Dim x(1 To 100) As Single, y(1 To 100) As Single, line As Single
Dim sumx As Single, sumx2 As Single, sumy As Single, sumy2 As Single, x1 As Single, lny As Single, lnx As Single
Dim m As Single, b As Single, r As Single, sumxy As Single, lnb As Single, xyvalues As Single
Dim j As Integer
Open ("H:\Eng 160\HW8\leastsquares.txt") For Input As 1
Input #1, xyvalues
For j = 1 To xyvalues
Input #1, x(j), y(j)
Next j
sumy = 0
sumx = 0
sumxy = 0
sumy2 = 0
sumx2 = 0
line = InputBox(" What type of line would you like to calculate? (1) Linear, (2) Exponential, (3) Power.")
If line = 1 Then ' linear
For j = 1 To xyvalues
sumx = sumx + x(j)
sumx2 = sumx2 + x(j) ^ 2
sumy = sumy + y(j)
sumy2 = sumy2 + y(j) ^ 2
sumxy = sumxy + x(j) * y(j)
Next j
Call regression(sumxy, sumx, sumy, sumx2, m, b, sumy2, r)
MsgBox (" the equation of the line is y=" & m & "x +" & b & ". Regression = " & r)
ElseIf line = 2 Then ' exponential
For j = 1 To xyvalues
x1 = x(j)
lny = Log(y(j))
sumy = sumy + lny
sumy2 = sumy2 + lny ^ 2
sumx = sumx + x1
sumx2 = sumx2 + x1 ^ 2
sumxy = sumxy + lny + x1
Next j
Call regression(sumxy, sumx, sumy, sumx2, m, b, sumy2, r)
MsgBox (" the equation of the line is y=" & b & "e^" & m & "x. Regression = " & r)
ElseIf line = 3 Then ' power
For j = 1 To xyvalues
lny = Log(y(j))
lnx = Log(x(j))
sumy = sumy + lny
sumy2 = sumy2 + lny ^ 2
sumx = sumx + lnx
sumx2 = sumx2 + lnx ^ 2
sumxy = sumxy + lny + lnx
Next j
Call regression(sumxy, sumx, sumy, sumx2, m, b, sumy2, r)
b = Log(b)
MsgBox (" the equation of the line is y=" & b & "x ^" & m & ". Regression = " & r)
End If
Close
End Sub
Sub regression(ByVal sumxy As Single, ByVal sumx As Single, ByVal sumy As Single, ByVal sumx2 As Single, ByRef m As Single, ByRef b As Single, ByVal sumy2 As Single, ByRef r As Single)
Dim logb As Single
m = (11 * sumxy - sumx * sumy) / (11 * sumx2 - sumx ^ 2)
b = (sumy - m * sumx) / 11
r = (11 * sumxy - sumx * sumy) / (Sqr(11 * sumx2 - sumx ^ 2) * (Sqr(11 * sumy2 - sumy ^ 2)))
End Sub
Please help! My program is due in 4 hours!
(I have b = log(b) under the 3rd elseif just to show one thing I tried)
Thank you!
Option Explicit
Private Sub CommandButton1_Click()
'
Dim x(1 To 100) As Single, y(1 To 100) As Single, line As Single
Dim sumx As Single, sumx2 As Single, sumy As Single, sumy2 As Single, x1 As Single, lny As Single, lnx As Single
Dim m As Single, b As Single, r As Single, sumxy As Single, lnb As Single, xyvalues As Single
Dim j As Integer
Open ("H:\Eng 160\HW8\leastsquares.txt") For Input As 1
Input #1, xyvalues
For j = 1 To xyvalues
Input #1, x(j), y(j)
Next j
sumy = 0
sumx = 0
sumxy = 0
sumy2 = 0
sumx2 = 0
line = InputBox(" What type of line would you like to calculate? (1) Linear, (2) Exponential, (3) Power.")
If line = 1 Then ' linear
For j = 1 To xyvalues
sumx = sumx + x(j)
sumx2 = sumx2 + x(j) ^ 2
sumy = sumy + y(j)
sumy2 = sumy2 + y(j) ^ 2
sumxy = sumxy + x(j) * y(j)
Next j
Call regression(sumxy, sumx, sumy, sumx2, m, b, sumy2, r)
MsgBox (" the equation of the line is y=" & m & "x +" & b & ". Regression = " & r)
ElseIf line = 2 Then ' exponential
For j = 1 To xyvalues
x1 = x(j)
lny = Log(y(j))
sumy = sumy + lny
sumy2 = sumy2 + lny ^ 2
sumx = sumx + x1
sumx2 = sumx2 + x1 ^ 2
sumxy = sumxy + lny + x1
Next j
Call regression(sumxy, sumx, sumy, sumx2, m, b, sumy2, r)
MsgBox (" the equation of the line is y=" & b & "e^" & m & "x. Regression = " & r)
ElseIf line = 3 Then ' power
For j = 1 To xyvalues
lny = Log(y(j))
lnx = Log(x(j))
sumy = sumy + lny
sumy2 = sumy2 + lny ^ 2
sumx = sumx + lnx
sumx2 = sumx2 + lnx ^ 2
sumxy = sumxy + lny + lnx
Next j
Call regression(sumxy, sumx, sumy, sumx2, m, b, sumy2, r)
b = Log(b)
MsgBox (" the equation of the line is y=" & b & "x ^" & m & ". Regression = " & r)
End If
Close
End Sub
Sub regression(ByVal sumxy As Single, ByVal sumx As Single, ByVal sumy As Single, ByVal sumx2 As Single, ByRef m As Single, ByRef b As Single, ByVal sumy2 As Single, ByRef r As Single)
Dim logb As Single
m = (11 * sumxy - sumx * sumy) / (11 * sumx2 - sumx ^ 2)
b = (sumy - m * sumx) / 11
r = (11 * sumxy - sumx * sumy) / (Sqr(11 * sumx2 - sumx ^ 2) * (Sqr(11 * sumy2 - sumy ^ 2)))
End Sub