Function overview
Prototype
LinearTrend (
_yValues; _xValues )
Parameters
_yValues List of Y-values
_xValues List of X-values
Description
Tags:
Regression Linear Graph
For a given dataset { ( X , Y ) ; ( X , Y ) ; ... ; ( X, Y ) } this function returns the linear regression equation based on the least square method.
y = B + A x
Very useful to find trends in datasets, or to plot the trendline in a graph.
Examples
Sample input
_yValues = List ( 100 ; 150 ; 200 ; 400 ; 200 ; 200 ; 200 ; 100 ; 300 )
_xValues = List ( 1 ; 2 ; 3 ; 4 ; 5 ; 6 ; 7 ; 8 )
Sample output
y = 155,36 + 11,31 x
Function code
/*
For a given dataset { ( X1 , Y1 ) ; ( X2 , Y2 ) ; ... ; ( Xn , Yn ) } this function returns the linear regression equation based on the least square method.
y = B + A x
Very useful to find trends in datasets, or to plot the trendline in a graph.
Developped by : Andries Heylen [BH&A]
mail: andries_heylen@bh-a.com
v1.0 08/12/2008 Andries Heylen [BH&A]
Example:
input:
_yValues = List ( 100 ; 150 ; 200 ; 400 ; 200 ; 200 ; 200 ; 100 ; 300 )
_xValues = List ( 1 ; 2 ; 3 ; 4 ; 5 ; 6 ; 7 ; 8 )
returns:
y = 155,36 + 11,31 x
Function uses:
CustomList by Agnes:
http://www.fmcustomfunctions.com/fid/118
*/
Let (
[
$listY = _yValues
; _Nmb = ValueCount ( $ListY )
; $listX = _xValues
; _SerieX = Substitute ( $listX ; ¶ ; ";" )
; _SumX = Evaluate ( "Sum ( " & _SerieX & ")" )
; _SerieY = Substitute ( $listY ; ¶ ; ";" )
; _SumY = Evaluate ( "Sum ( " & _SerieY & ")" )
; _ListXY = CustomList ( 1 ; _Nmb ; "GetValue ( $ListX ; [n] ) * GetValue ( $ListY ; [n] )" )
; _SerieXY = Substitute ( _ListXY ; ¶ ; ";" )
; _SumXY = Evaluate ( "Sum ( " & _SerieXY & " ) " )
; _ListX2 = CustomList ( 1 ; _Nmb ; "GetValue ( $ListX ; [n] ) ^ 2" )
; _SerieX2 = Substitute ( _ListX2 ; ¶ ; ";" )
; _SumX2 = Evaluate ( "Sum ( " & _SerieX2 & " ) " )
; _b = Round ( ( _Nmb * _SumXY - _SumX * _SumY ) / ( _Nmb * _SumX2 - _SumX ^2 ) ; 2 )
; _a = Round ( ( _SumY - _b * _SumX ) / _Nmb ; 2 )
]
;
"y = " & _a & " + " & _b & "x"
)
// ===================================
/*
This function is published on FileMaker Custom Functions
to check for updates and provide feedback and bug reports
please visit http://www.fmfunctions.com/fid/135
Prototype: LinearTrend( _yValues; _xValues )
Function Author: Andries Heylen (http://www.fmfunctions.com/mid/57)
Last updated: 10 December 2008
Version: 1.3
*/
// ===================================
/*__LITBR__For a given dataset { ( X1 , Y1 ) ; ( X2 , Y2 ) ; ... ; ( Xn , Yn ) } this function returns the linear regression equation based on the least square method.__LITBR____LITBR__y = B + A x__LITBR____LITBR__Very useful to find trends in datasets, or to plot the trendline in a graph.__LITBR____LITBR____LITBR__Developped by : Andries Heylen [BH&A]__LITBR__mail: andries_heylen@bh-a.com__LITBR____LITBR__v1.0 08/12/2008 Andries Heylen [BH&A]__LITBR____LITBR__Example:__LITBR____LITBR__input:__LITBR___yValues = List ( 100 ; 150 ; 200 ; 400 ; 200 ; 200 ; 200 ; 100 ; 300 )__LITBR___xValues = List ( 1 ; 2 ; 3 ; 4 ; 5 ; 6 ; 7 ; 8 )__LITBR____LITBR__returns:__LITBR____LITBR__y = 155,36 + 11,31 x__LITBR____LITBR____LITBR__Function uses:__LITBR____LITBR__CustomList by Agnes: __LITBR__http://www.fmcustomfunctions.com/fid/118__LITBR____LITBR__*/__LITBR____LITBR____LITBR__Let ( __LITBR__[__LITBR__ $listY = _yValues__LITBR__; _Nmb = ValueCount ( $ListY )__LITBR__; $listX = _xValues __LITBR__; _SerieX = Substitute ( $listX ; ¶ ; ";" )__LITBR__; _SumX = Evaluate ( "Sum ( " & _SerieX & ")" )__LITBR__; _SerieY = Substitute ( $listY ; ¶ ; ";" )__LITBR__; _SumY = Evaluate ( "Sum ( " & _SerieY & ")" )__LITBR__; _ListXY = CustomList ( 1 ; _Nmb ; "GetValue ( $ListX ; [n] ) * GetValue ( $ListY ; [n] )" )__LITBR__; _SerieXY = Substitute ( _ListXY ; ¶ ; ";" )__LITBR__; _SumXY = Evaluate ( "Sum ( " & _SerieXY & " ) " )__LITBR__; _ListX2 = CustomList ( 1 ; _Nmb ; "GetValue ( $ListX ; [n] ) ^ 2" )__LITBR__; _SerieX2 = Substitute ( _ListX2 ; ¶ ; ";" )__LITBR__; _SumX2 = Evaluate ( "Sum ( " & _SerieX2 & " ) " )__LITBR__; _b = Round ( ( _Nmb * _SumXY - _SumX * _SumY ) / ( _Nmb * _SumX2 - _SumX ^2 ) ; 2 )__LITBR__; _a = Round ( ( _SumY - _b * _SumX ) / _Nmb ; 2 )__LITBR__]__LITBR__;__LITBR__"y = " & _a & " + " & _b & "x"__LITBR____LITBR____LITBR__)__LITBR____LITBR__// ===================================__LITBR__/*__LITBR____LITBR__ This function is published on FileMaker Custom Functions__LITBR__ to check for updates and provide feedback and bug reports__LITBR__ please visit http://www.fmfunctions.com/fid/135__LITBR____LITBR__ Prototype: LinearTrend( _yValues; _xValues )__LITBR__ Function Author: Andries Heylen (http://www.fmfunctions.com/mid/57)__LITBR__ Last updated: 10 December 2008__LITBR__ Version: 1.3__LITBR____LITBR__*/__LITBR__// ===================================
Login or register to comment
Create a new account with fmcustomfunctions.com or login to post a comment.
Comments
02 July 2009
In this case I have a problem. When I copy and paste this code in, I get an error report in the line:
; _SumY = Evaluate ( "Sum ( " & _SerieY & ")" )
with the blue highlight on "Sum ( "
I've checked the variables are entered correctly.
FM10.0 v1 Advanced.
22 July 2009
I tried to see what the error might be, but I don't understand the problem.
Can you provide me the x-values and y-values you are trying to evaluate?
Did you try the example given? this should work normally.
Thanks
Andries
(Edited by Andries Heylen on 22/07/09 )
27 July 2009
As a first time user of custom functions, I think I was misled by the following text:
"Function uses:
CustomList by Agnes:
http://www.fmcustomfunctions.com/fid/118"
This means "This function relies upon...", but I took it to mean "This function can be applied to..."