Does anyone have any experience in doing linear regression in a CRBasic situation? Any code to share? I want to compare two variables on an on-going basis, and plot the regression equation along with the actual data in RTMC Pro. Of course, the alternative is post-processing, but it would be of value to do it on an continuing basis.
Thanks
Can I suggest you have a look at the CovSpa or Covariance instructions which calculate most of the statistics necessary to perform a regression online. I know others have used these instructions in the past for regressions, but I do not have any simple program examples to share.
Andrew
Going back almost 10 years I found an example in an old archive. Hopefully you can work from this:
'CR1000 Series Datalogger
'Demonstrates how to do a linear regression either on a block of stored data or
'it could be done on data coming in real-time, with the regression calculated over a given
'time interval.
'Uses the Covariance instruction to do the stats.
'Declare Public Variables
'Example:
Public Rdata(10,2), regdata(2),i, j, doit As Boolean
Public slope, offset
'Some test data
Data -4,2
Data 4,7
Data -10,45
Data 2,34
Data 4,45
Data -23,167
'Define Data Tables
'This table is a working table which calculates the stats.
'The result is calculated when "doit" is set true or is could be at fixed
'intervals by including a datainterval command.
DataTable (Test,doit,-1)
Covariance (2,Regdata,FP2,False,2)
Average(2,regdata,fp2,false)
EndTable
'Main Program
BeginProg
'Read the test data into an array. x and y values.
For i= 1 To 10
Read Rdata(i,1)
Read Rdata(i,2)
Next i
i=1
Scan (1,Sec,0,0)
'Load the test data into a two element array, a pair at a time
Regdata(1)=Rdata(i,1)
Regdata(2)=Rdata(i,2)
If i=6 Then 'Every 6th call calculate the stats
doit=true
i=0
EndIf
CallTable Test
doit=false
i=i+1
'Calculate the slope as Covariance/Variance
Slope=test.regdata_Cov(2)/test.regdata_Cov(1)
'Solve for the offset using the averages
offset=test.regdata_Avg(2)-slope*test.regdata_Avg(1)
NextScan
EndProg
Thanks for digging that up.