====== Compute Linear Regression ======
Since this page was created a command has been added to Visual3D.
[[Visual3D:Documentation:Pipeline:Metric_Commands:Metric_To_Fit_Signal_To_Line|Metric_To_Fit_Signal_To_Line]]
This example uses a command script that calls a meta-command that will compute a linear regression of the data identified in the script. See also [[Visual3D:Documentation:Statistics:Meta_Command_for_Linear_Regression|Statistics_Example_5]] for another example.
Given the following definition of a linear regression:
Regression Equation: y = b + mx
Slope: m = (NΣXY - (ΣX)(ΣY)) / (NΣX^2 - (ΣX)^2)
Intercept: b = (ΣY - m(ΣX)) / N
where :
x and y are the variables.
m = The slope of the regression line
b = The intercept point of the regression line and the y axis.
N = Number of values or elements
X = array of x-components
Y = array of y-components
ΣX = Sum of x
ΣY = Sum of y
ΣXY = Sum of xy
ΣX^2 = Sum of x^2
==== Example Data ====
Given the following data:
|5 ||3.1|
|6 ||3.5|
|7 ||3.9|
|9 ||4.1|
|10||4.3|
|11||4.7|
==== Linear_Regression_Explicit Pipeline Script ====
This pipeline command script creates the signals and calls the **linear_regression_explicit** meta command.
!=========================================================================
! This script will call the meta-command linear_regression_explicit that
! will perform a linear regression of data that is placed in the GLOBAL
!
! Note: Regression Equation: y = b + mx
!
! Slope: m = (NΣXY - (ΣX)(ΣY)) / (NΣX^2 - (ΣX)^2)
!
! Intercept: b = (ΣY - m(ΣX)) / N
!
! where :
!
! x and y are the variables.
! m = The slope of the regression line
! b = The intercept point of the regression line and the y axis.
! N = Number of values or elements
! X = array of x-components
! Y = array of y-components
! ΣX = Sum of x
! ΣY = Sum of y
! ΣXY = Sum of xy
! ΣX^2 = Sum of x^2
!=========================================================================
! The GLOBAL needs to be active
Select_Active_File
/FILE_NAME=GLOBAL
! /QUERY=
;
!-------------------------------------------------------
! Create a signal containing the number of data points
!-------------------------------------------------------
Metric_Explicit
/RESULT_METRIC_NAME=N
/RESULT_METRIC_FOLDER=RESIDUAL
/METRIC_VALUE=6
;
! create a signal containing the data
Evaluate_Expression
/EXPRESSION=LIST(VECTOR(5,3.1),VECTOR(6,3.5),VECTOR(7,3.9),VECTOR(9,4.1),VECTOR(10,4.3),VECTOR(11,4.7))
/RESULT_NAME=TEST
/RESULT_TYPE=DERIVED
/RESULT_FOLDER=RESIDUAL
;
!--------------------------------------------------
! Call the meta command linear_regression_explicit
!--------------------------------------------------
linear_regression_explicit
/SIGNALX=DERIVED::RESIDUAL::TEST::X
/SIGNALY=DERIVED::RESIDUAL::TEST::Y
/NUM_POINTS= METRIC::RESIDUAL::N
/RESULT_NAME=RESULT
/RESULT_FOLDER=RESIDUAL
;
!--------------------------------------------------------------------------
! create signal representing the linear regression
! 4 temporary signals are created for clarity - STARTX, ENDX, STARTY, ENDY
!--------------------------------------------------------------------------
! Create STARTX at the first datapoint
Evaluate_Expression
/EXPRESSION=DERIVED::RESIDUAL::TEST::X[1]
/RESULT_NAME=STARTX
/RESULT_TYPE=METRIC
/RESULT_FOLDER=RESIDUAL
;
! Create ENDX at the Nth datapoint
Evaluate_Expression
/EXPRESSION=DERIVED::RESIDUAL::TEST::X[METRIC::RESIDUAL::N]
/RESULT_NAME=ENDX
/RESULT_TYPE=METRIC
/RESULT_FOLDER=RESIDUAL
;
! Create STARTY from the meta command regression results and STARTX (y = b + mx)
Evaluate_Expression
/EXPRESSION=METRIC::RESIDUAL::RESULT::X+METRIC::RESIDUAL::RESULT::Y*METRIC::RESIDUAL::STARTX
/RESULT_NAME=STARTY
/RESULT_TYPE=METRIC
/RESULT_FOLDER=RESIDUAL
;
! Create ENDY from the meta command regression results and ENDX (y = b + mx)
Evaluate_Expression
/EXPRESSION=METRIC::RESIDUAL::RESULT::X+METRIC::RESIDUAL::RESULT::Y*METRIC::RESIDUAL::ENDX
/RESULT_NAME=ENDY
/RESULT_TYPE=METRIC
/RESULT_FOLDER=RESIDUAL
;
! Create the line
Evaluate_Expression
/EXPRESSION=LIST(VECTOR(METRIC::RESIDUAL::STARTX,METRIC::RESIDUAL::STARTY),VECTOR(METRIC::RESIDUAL::ENDX,METRIC::RESIDUAL::ENDY))
/RESULT_NAME=RESULT_LINE
/RESULT_TYPE=METRIC
/RESULT_FOLDER=RESIDUAL
;
==== Graph the Data and Regression Line ====
The resulting signal **METRIC::RESIDUAL::RESULT_LINE** can be graphed in a report graph with the original data.
For example, create a report graph (2D Graph) as follows:
{{:Linear_regression1.jpg}}
Now create a second 2D graph at the same location containing the regression line
{{:Linear_regression2.jpg}}
The resulting report graph should appear as follows:
{{:Linear_regression3.jpg}}
==== Linear_Regression_Explicit Meta-Command ====
Below are the commands for the **Linear_Regression_Explicit** Meta-Commands. Go to [[Visual3D:Documentation:Pipeline:Meta_Commands:Meta_Commands_Overview|Pipeline Commands:Meta Commands]] for details on how to use Meta-commands.
! BEGIN_META
! META_CMD_NAME=Linear_Regression_Explicit
! META_PARAM= SIGNALX : string ::yes
! META_PARAM= SIGNALY : string ::yes
! META_PARAM= NUM_POINTS : string ::yes
! META_PARAM= RESULT_NAME : string ::yes
! META_PARAM= RESULT_FOLDER : string ::yes
! END_META
!========================================================================
! This meta-command will calculate the linear regression of data
! that is passed to the meta-command
!
! Note: Regression Equation: y = b + mx
!
! Slope: m = (NΣXY - (ΣX)(ΣY)) / (NΣX^2 - (ΣX)^2)
!
! Intercept: b = (ΣY - m(ΣX)) / N
!
! where :
!
! x and y are the variables.
! m = The slope of the regression line
! b = The intercept point of the regression line and the y axis.
! N = Number of values or elements
! X = array of x-components
! Y = array of y-components
! ΣX = Sum of x
! ΣY = Sum of y
! ΣXY = Sum of xy
! ΣX^2 = Sum of x^2
!==========================================================================
!---------------------------------------------------------------------------
! Create Temporary Variables - TEMPX and TEMPY from SIGNAL::X and SIGNAL::Y
!---------------------------------------------------------------------------
Set_Pipeline_Parameter
/PARAMETER_NAME=NAME
/PARAMETER_VALUE=::RESULT_NAME
;
Set_Pipeline_Parameter
/PARAMETER_NAME=FOLDER
/PARAMETER_VALUE=::RESULT_FOLDER
;
! Create TEMPX
Evaluate_Expression
/EXPRESSION=::SIGNALX
/RESULT_NAME=TEMPX
/RESULT_TYPE=DERIVED
/RESULT_FOLDER=RESIDUAL
;
! Create TEMPY
Evaluate_Expression
/EXPRESSION=::SIGNALY
/RESULT_NAME=TEMPY
/RESULT_TYPE=DERIVED
/RESULT_FOLDER=RESIDUAL
;
!----------------------------
! Sum X and Y
!----------------------------
! Sum X
Metric_Sum
/RESULT_METRIC_NAME=SUMX
/APPLY_AS_SUFFIX_TO_SIGNAL_NAME=FALSE
/RESULT_METRIC_FOLDER=RESIDUAL
/SIGNAL_TYPES=DERIVED
/SIGNAL_NAMES=TEMPX
/SIGNAL_FOLDER=RESIDUAL
/SIGNAL_COMPONENTS=X
/EVENT_SEQUENCE=
/EXCLUDE_EVENTS=
/GENERATE_MEAN_AND_STDDEV=FALSE
/APPEND_TO_EXISTING_VALUES=FALSE
;
! Sum Y
Metric_Sum
/RESULT_METRIC_NAME=SUMY
/APPLY_AS_SUFFIX_TO_SIGNAL_NAME=FALSE
/RESULT_METRIC_FOLDER=RESIDUAL
/SIGNAL_TYPES=DERIVED
/SIGNAL_NAMES=TEMPY
/SIGNAL_FOLDER=RESIDUAL
/SIGNAL_COMPONENTS=X
/EVENT_SEQUENCE=
/EXCLUDE_EVENTS=
/GENERATE_MEAN_AND_STDDEV=FALSE
/APPEND_TO_EXISTING_VALUES=FALSE
;
!----------------------
! Multiply X and Y
!----------------------
Evaluate_Expression
/EXPRESSION=DERIVED::RESIDUAL::TEMPX*DERIVED::RESIDUAL::TEMPY
/RESULT_NAME=XY
/RESULT_TYPE=DERIVED
/RESULT_FOLDER=RESIDUAL
;
!----------------------
! Sum XY
!----------------------
Metric_Sum
/RESULT_METRIC_NAME=SUMXY
/APPLY_AS_SUFFIX_TO_SIGNAL_NAME=FALSE
/RESULT_METRIC_FOLDER=RESIDUAL
/SIGNAL_TYPES=DERIVED
/SIGNAL_NAMES=XY
/SIGNAL_FOLDER=RESIDUAL
/SIGNAL_COMPONENTS=X
/EVENT_SEQUENCE=
/EXCLUDE_EVENTS=
/GENERATE_MEAN_AND_STDDEV=FALSE
/APPEND_TO_EXISTING_VALUES=FALSE
;
!---------------
! Square X
!---------------
Evaluate_Expression
/EXPRESSION=DERIVED::RESIDUAL::TEMPX*DERIVED::RESIDUAL::TEMPX
/RESULT_NAME=XX
/RESULT_TYPE=DERIVED
/RESULT_FOLDER=RESIDUAL
;
!-----------------
! Sum square of X
!-----------------
Metric_Sum
/RESULT_METRIC_NAME=SUMXX
/APPLY_AS_SUFFIX_TO_SIGNAL_NAME=FALSE
/RESULT_METRIC_FOLDER=RESIDUAL
/SIGNAL_TYPES=DERIVED
/SIGNAL_NAMES=XX
/SIGNAL_FOLDER=RESIDUAL
/SIGNAL_COMPONENTS=X
/EVENT_SEQUENCE=
/EXCLUDE_EVENTS=
/GENERATE_MEAN_AND_STDDEV=FALSE
/APPEND_TO_EXISTING_VALUES=FALSE
;
!------------------
! Calculate slope
!------------------
Evaluate_Expression
/EXPRESSION=(&::NUM_POINTS&*METRIC::RESIDUAL::SUMXY-METRIC::RESIDUAL::SUMX*METRIC::RESIDUAL::SUMY)/(&::NUM_POINTS&*METRIC::RESIDUAL::SUMXX-METRIC::RESIDUAL::SUMX^2)
/RESULT_NAME=SLOPE
/RESULT_TYPE=METRIC
/RESULT_FOLDER=RESIDUAL
;
!----------------------
! Calculate intercept
!----------------------
Evaluate_Expression
/EXPRESSION=(METRIC::RESIDUAL::SUMY-METRIC::RESIDUAL::SLOPE*METRIC::RESIDUAL::SUMX)/&::NUM_POINTS
/RESULT_NAME=INTERCEPT
/RESULT_TYPE=METRIC
/RESULT_FOLDER=RESIDUAL
;
!--------------------------------------------------------------------------
! create the resulting metric signal that contains the slope and intercept
!--------------------------------------------------------------------------
Evaluate_Expression
/EXPRESSION=VECTOR(METRIC::RESIDUAL::INTERCEPT,METRIC::RESIDUAL::SLOPE)
/RESULT_NAME=::NAME
/RESULT_TYPE=METRIC
/RESULT_FOLDER=::FOLDER
;
!-----------------------------------------------------------
! clean up the temporary signals
! if you want to see the temporary signals for debugging
! comment out this next command
!-----------------------------------------------------------
Remove_Signals
/SIGNAL_TYPES=DERIVED+DERIVED+DERIVED+DERIVED+METRIC+METRIC+METRIC+METRIC+METRIC+METRIC
/SIGNAL_NAMES=TEMPX+TEMPY+XX+XY+SLOPE+INTERCEPT+SUMX+SUMXX+SUMXY+SUMY
/SIGNAL_FOLDER=RESIDUAL+RESIDUAL+RESIDUAL+RESIDUAL+RESIDUAL+RESIDUAL+RESIDUAL+RESIDUAL+RESIDUAL+RESIDUAL
;