User Tools

Site Tools


visual3d:documentation:statistics:compute_linear_regression

Differences

This shows you the differences between two versions of the page.

Link to this comparison view

Both sides previous revisionPrevious revision
Next revision
Previous revision
visual3d:documentation:statistics:compute_linear_regression [2024/06/19 12:53] sgrangervisual3d:documentation:statistics:compute_linear_regression [2024/07/17 15:46] (current) – created sgranger
Line 1: Line 1:
-{{{{{{since this page was created a command has been added to visual3d.+====== Compute Linear Regression ======
  
-[[visual3d:documentation:pipeline:metric_commands:metric_to_fit_signal_to_line|metric_to_fit_signal_to_line]] +Since this page was created a command has been added to Visual3D.
-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:+[[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:
  
 <code> <code>
-regression equation: y = b + mx+Regression Equation: y = b + mx
  
-slope: m = (nσxy - (σx)(σy)) / (nσx^2 - (σx)^2) +Slope: m = (NΣXY - (ΣX)(ΣY)) / (NΣX^2 - (ΣX)^2) 
  
-intercept: b = (σy - m(σx)) / n+Intercept: b = (ΣY - m(ΣX)) / N
  
 where : where :
  
   x and y are the variables.   x and y are the variables.
-  m = the slope of the regression line +  m = The slope of the regression line 
-  b = the intercept point of the regression line and the y axis. +  b = The intercept point of the regression line and the y axis. 
-  number of values or elements +  Number of values or elements 
-  = array of x-components +  = array of x-components 
-  = array of y-components +  = array of y-components 
-  σx sum of x +  ΣX Sum of x 
-  σy sum of y +  ΣY Sum of y 
-  σxy sum of xy +  ΣXY Sum of xy 
-  σx^2 = sum of x^2+  ΣX^2 = Sum of x^2
 </code> </code>
  
-===== example data =====+==== Example Data ====
  
-given the following data:+Given the following data:
  
 |5 ||3.1| |5 ||3.1|
Line 38: Line 40:
 |11||4.7| |11||4.7|
  
-===== linear_regression_explicit pipeline script =====+==== Linear_Regression_Explicit Pipeline Script ====
  
-this pipeline command script creates the signals and calls the **linear_regression_explicit** meta command.+This pipeline command script creates the signals and calls the **linear_regression_explicit** meta command.
  
 <code> <code>
 !========================================================================= !=========================================================================
-this script will call the meta-command linear_regression_explicit that  +This script will call the meta-command linear_regression_explicit that  
-! will perform a linear regression of data that is placed in the global+! will perform a linear regression of data that is placed in the GLOBAL
  
-noteregression equation: y = b + mx+NoteRegression Equation: y = b + mx
  
-slope: m = (nσxy - (σx)(σy)) / (nσx^2 - (σx)^2) +Slope: m = (NΣXY - (ΣX)(ΣY)) / (NΣX^2 - (ΣX)^2) 
  
-intercept: b = (σy - m(σx)) / n+Intercept: b = (ΣY - m(ΣX)) / N
  
 ! where : ! where :
 ! !
 !  x and y are the variables. !  x and y are the variables.
-!  m = the slope of the regression line +!  m = The slope of the regression line 
-!  b = the intercept point of the regression line and the y axis. +!  b = The intercept point of the regression line and the y axis. 
-!  number of values or elements +!  Number of values or elements 
-!  = array of x-components +!  = array of x-components 
-!  = array of y-components +!  = array of y-components 
-!  σx sum of x +!  ΣX Sum of x 
-!  σy sum of y +!  ΣY Sum of y 
-!  σxy sum of xy +!  ΣXY Sum of xy 
-!  σx^2 = sum of x^2+!  ΣX^2 = Sum of x^2
 !=========================================================================  !========================================================================= 
  
-the global needs to be active +The GLOBAL needs to be active 
-select_active_file +Select_Active_File 
-/file_name=global +/FILE_NAME=GLOBAL 
-! /query=+! /QUERY=
 ; ;
  
 !------------------------------------------------------- !-------------------------------------------------------
-create a signal containing the number of data points +Create a signal containing the number of data points 
 !------------------------------------------------------- !-------------------------------------------------------
-metric_explicit  +Metric_Explicit  
-/result_metric_name= +/RESULT_METRIC_NAME= 
-/result_metric_folder=residual  +/RESULT_METRIC_FOLDER=RESIDUAL  
-/metric_value=6 +/METRIC_VALUE=6 
  
  
 ! create a signal containing the data  ! create a signal containing the data 
-evaluate_expression  +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))  +/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_NAME=TEST  
-/result_type=derived  +/RESULT_TYPE=DERIVED  
-/result_folder=residual +/RESULT_FOLDER=RESIDUAL 
  
  
 !-------------------------------------------------- !--------------------------------------------------
-call the meta command linear_regression_explicit+Call the meta command linear_regression_explicit
 !-------------------------------------------------- !--------------------------------------------------
 linear_regression_explicit  linear_regression_explicit 
-/signalx=derived::residual::test:: +/SIGNALX=DERIVED::RESIDUAL::TEST:: 
-/signaly=derived::residual::test:: +/SIGNALY=DERIVED::RESIDUAL::TEST:: 
-/num_pointsmetric::residual:: +/NUM_POINTSMETRIC::RESIDUAL:: 
-/result_name=result  +/RESULT_NAME=RESULT  
-/result_folder=residual +/RESULT_FOLDER=RESIDUAL 
  
  
 !-------------------------------------------------------------------------- !--------------------------------------------------------------------------
 ! create signal representing the linear regression  ! create signal representing the linear regression 
-! 4 temporary signals are created for clarity - startxendxstartyendy+! 4 temporary signals are created for clarity - STARTXENDXSTARTYENDY
 !-------------------------------------------------------------------------- !--------------------------------------------------------------------------
  
-create startx at the first datapoint +Create STARTX at the first datapoint 
-evaluate_expression  +Evaluate_Expression  
-/expression=derived::residual::test::x[1]  +/EXPRESSION=DERIVED::RESIDUAL::TEST::X[1]  
-/result_name=startx  +/RESULT_NAME=STARTX  
-/result_type=metric  +/RESULT_TYPE=METRIC  
-/result_folder=residual +/RESULT_FOLDER=RESIDUAL 
  
  
-create endx at the nth datapoint +Create ENDX at the Nth datapoint 
-evaluate_expression  +Evaluate_Expression  
-/expression=derived::residual::test::x[metric::residual::n]  +/EXPRESSION=DERIVED::RESIDUAL::TEST::X[METRIC::RESIDUAL::N]  
-/result_name=endx  +/RESULT_NAME=ENDX  
-/result_type=metric  +/RESULT_TYPE=METRIC  
-/result_folder=residual +/RESULT_FOLDER=RESIDUAL 
  
  
-create starty from the meta command regression results and startx (y = b + mx) +Create STARTY from the meta command regression results and STARTX (y = b + mx) 
-evaluate_expression  +Evaluate_Expression  
-/expression=metric::residual::result::x+metric::residual::result::y*metric::residual::startx  +/EXPRESSION=METRIC::RESIDUAL::RESULT::X+METRIC::RESIDUAL::RESULT::Y*METRIC::RESIDUAL::STARTX  
-/result_name=starty  +/RESULT_NAME=STARTY  
-/result_type=metric  +/RESULT_TYPE=METRIC  
-/result_folder=residual +/RESULT_FOLDER=RESIDUAL 
 ; ;
  
-create endy from the meta command regression results and endx (y = b + mx) +Create ENDY from the meta command regression results and ENDX (y = b + mx) 
-evaluate_expression  +Evaluate_Expression  
-/expression=metric::residual::result::x+metric::residual::result::y*metric::residual::endx  +/EXPRESSION=METRIC::RESIDUAL::RESULT::X+METRIC::RESIDUAL::RESULT::Y*METRIC::RESIDUAL::ENDX  
-/result_name=endy  +/RESULT_NAME=ENDY  
-/result_type=metric  +/RESULT_TYPE=METRIC  
-/result_folder=residual +/RESULT_FOLDER=RESIDUAL 
  
  
-create the line  +Create the line  
-evaluate_expression  +Evaluate_Expression  
-/expression=list(vector(metric::residual::startx,metric::residual::starty),vector(metric::residual::endx,metric::residual::endy))  +/EXPRESSION=LIST(VECTOR(METRIC::RESIDUAL::STARTX,METRIC::RESIDUAL::STARTY),VECTOR(METRIC::RESIDUAL::ENDX,METRIC::RESIDUAL::ENDY))  
-/result_name=result_line  +/RESULT_NAME=RESULT_LINE  
-/result_type=metric  +/RESULT_TYPE=METRIC  
-/result_folder=residual +/RESULT_FOLDER=RESIDUAL 
 ; ;
 </code> </code>
  
-===== graph the data and regression line =====+==== Graph the Data and Regression Line ====
  
-the resulting signal **metric::residual::result_line** can be graphed in a report graph with the original data.+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:+For example, create a report graph (2D Graph) as follows:
  
-linear_regression1.jpg+{{:Linear_regression1.jpg}}
  
-now create a second 2d graph at the same location containing the regression line+Now create a second 2D graph at the same location containing the regression line
  
-linear_regression2.jpg+{{:Linear_regression2.jpg}}
  
-the resulting report graph should appear as follows:+The resulting report graph should appear as follows:
  
-linear_regression3.jpg+{{:Linear_regression3.jpg}}
  
-===== linear_regression_explicit meta-command =====+==== Linear_Regression_Explicit Meta-Command ====
  
-below are the commands for the **linear_regression_explicit** meta-commandsgo to [[visual3d:documentation:pipeline:meta_commands:meta_commands_overview|pipeline commands:meta commands]] for details on how to use meta-commands.+Below are the commands for the **Linear_Regression_Explicit** Meta-CommandsGo to [[Visual3D:Documentation:Pipeline:Meta_Commands:Meta_Commands_Overview|Pipeline Commands:Meta Commands]] for details on how to use Meta-commands.
  
 <code> <code>
-begin_meta +BEGIN_META 
-meta_cmd_name=linear_regression_explicit  +META_CMD_NAME=Linear_Regression_Explicit  
-meta_paramsignalx  : string ::yes +META_PARAMSIGNALX  : string ::yes 
-meta_paramsignaly  : string ::yes +META_PARAMSIGNALY  : string ::yes 
-meta_paramnum_points  : string ::yes +META_PARAMNUM_POINTS  : string ::yes 
-meta_paramresult_name  : string ::yes +META_PARAMRESULT_NAME  : string ::yes 
-meta_paramresult_folder  : string ::yes +META_PARAMRESULT_FOLDER  : string ::yes 
-end_meta+END_META
  
 !======================================================================== !========================================================================
-this meta-command will calculate the linear regression of data +This meta-command will calculate the linear regression of data 
 ! that is passed to the meta-command ! that is passed to the meta-command
 ! !
-noteregression equation: y = b + mx+NoteRegression Equation: y = b + mx
  
-slope: m = (nσxy - (σx)(σy)) / (nσx^2 - (σx)^2) +Slope: m = (NΣXY - (ΣX)(ΣY)) / (NΣX^2 - (ΣX)^2) 
  
-intercept: b = (σy - m(σx)) / n+Intercept: b = (ΣY - m(ΣX)) / N
  
 ! where : ! where :
 ! !
 !  x and y are the variables. !  x and y are the variables.
-!  m = the slope of the regression line +!  m = The slope of the regression line 
-!  b = the intercept point of the regression line and the y axis. +!  b = The intercept point of the regression line and the y axis. 
-!  number of values or elements +!  Number of values or elements 
-!  = array of x-components +!  = array of x-components 
-!  = array of y-components +!  = array of y-components 
-!  σx sum of x +!  ΣX Sum of x 
-!  σy sum of y +!  ΣY Sum of y 
-!  σxy sum of xy +!  ΣXY Sum of xy 
-!  σx^2 = sum of x^2+!  ΣX^2 = Sum of x^2
 !========================================================================== !==========================================================================
  
 !--------------------------------------------------------------------------- !---------------------------------------------------------------------------
-create temporary variables tempx and tempy from signal::and signal::y+Create Temporary Variables TEMPX and TEMPY from SIGNAL::and SIGNAL::Y
 !--------------------------------------------------------------------------- !---------------------------------------------------------------------------
  
-set_pipeline_parameter  +Set_Pipeline_Parameter  
-/parameter_name=name  +/PARAMETER_NAME=NAME  
-/parameter_value=::result_name +/PARAMETER_VALUE=::RESULT_NAME 
 ; ;
  
-set_pipeline_parameter  +Set_Pipeline_Parameter  
-/parameter_name=folder  +/PARAMETER_NAME=FOLDER  
-/parameter_value=::result_folder +/PARAMETER_VALUE=::RESULT_FOLDER 
 ; ;
  
-create tempx +Create TEMPX 
-evaluate_expression  +Evaluate_Expression  
-/expression=::signalx  +/EXPRESSION=::SIGNALX  
-/result_name=tempx  +/RESULT_NAME=TEMPX  
-/result_type=derived  +/RESULT_TYPE=DERIVED  
-/result_folder=residual +/RESULT_FOLDER=RESIDUAL 
 ; ;
  
-create tempy +Create TEMPY 
-evaluate_expression  +Evaluate_Expression  
-/expression=::signaly  +/EXPRESSION=::SIGNALY  
-/result_name=tempy  +/RESULT_NAME=TEMPY  
-/result_type=derived  +/RESULT_TYPE=DERIVED  
-/result_folder=residual +/RESULT_FOLDER=RESIDUAL 
 ; ;
  
 !---------------------------- !----------------------------
-sum x and y+Sum X and Y
 !----------------------------  !---------------------------- 
  
-sum x +Sum X 
-metric_sum  +Metric_Sum  
-/result_metric_name=sumx  +/RESULT_METRIC_NAME=SUMX  
-/apply_as_suffix_to_signal_name=false  +/APPLY_AS_SUFFIX_TO_SIGNAL_NAME=FALSE  
-/result_metric_folder=residual  +/RESULT_METRIC_FOLDER=RESIDUAL  
-/signal_types=derived  +/SIGNAL_TYPES=DERIVED  
-/signal_names=tempx  +/SIGNAL_NAMES=TEMPX  
-/signal_folder=residual  +/SIGNAL_FOLDER=RESIDUAL  
-/signal_components= +/SIGNAL_COMPONENTS= 
-/event_sequence=  +/EVENT_SEQUENCE=  
-/exclude_events=  +/EXCLUDE_EVENTS=  
-/generate_mean_and_stddev=false  +/GENERATE_MEAN_AND_STDDEV=FALSE  
-/append_to_existing_values=false +/APPEND_TO_EXISTING_VALUES=FALSE 
 ; ;
  
-sum y +Sum Y 
-metric_sum  +Metric_Sum  
-/result_metric_name=sumy  +/RESULT_METRIC_NAME=SUMY  
-/apply_as_suffix_to_signal_name=false  +/APPLY_AS_SUFFIX_TO_SIGNAL_NAME=FALSE  
-/result_metric_folder=residual  +/RESULT_METRIC_FOLDER=RESIDUAL  
-/signal_types=derived  +/SIGNAL_TYPES=DERIVED  
-/signal_names=tempy  +/SIGNAL_NAMES=TEMPY  
-/signal_folder=residual  +/SIGNAL_FOLDER=RESIDUAL  
-/signal_components= +/SIGNAL_COMPONENTS= 
-/event_sequence=  +/EVENT_SEQUENCE=  
-/exclude_events=  +/EXCLUDE_EVENTS=  
-/generate_mean_and_stddev=false  +/GENERATE_MEAN_AND_STDDEV=FALSE  
-/append_to_existing_values=false +/APPEND_TO_EXISTING_VALUES=FALSE 
 ; ;
  
 !---------------------- !----------------------
-multiply x and y+Multiply X and Y
 !---------------------- !----------------------
-evaluate_expression  +Evaluate_Expression  
-/expression=derived::residual::tempx*derived::residual::tempy  +/EXPRESSION=DERIVED::RESIDUAL::TEMPX*DERIVED::RESIDUAL::TEMPY  
-/result_name=xy  +/RESULT_NAME=XY  
-/result_type=derived  +/RESULT_TYPE=DERIVED  
-/result_folder=residual +/RESULT_FOLDER=RESIDUAL 
 ; ;
  
 !---------------------- !----------------------
-sum xy+Sum XY
 !---------------------- !----------------------
-metric_sum  +Metric_Sum  
-/result_metric_name=sumxy  +/RESULT_METRIC_NAME=SUMXY  
-/apply_as_suffix_to_signal_name=false  +/APPLY_AS_SUFFIX_TO_SIGNAL_NAME=FALSE  
-/result_metric_folder=residual  +/RESULT_METRIC_FOLDER=RESIDUAL  
-/signal_types=derived  +/SIGNAL_TYPES=DERIVED  
-/signal_names=xy  +/SIGNAL_NAMES=XY  
-/signal_folder=residual  +/SIGNAL_FOLDER=RESIDUAL  
-/signal_components= +/SIGNAL_COMPONENTS= 
-/event_sequence=  +/EVENT_SEQUENCE=  
-/exclude_events=  +/EXCLUDE_EVENTS=  
-/generate_mean_and_stddev=false  +/GENERATE_MEAN_AND_STDDEV=FALSE  
-/append_to_existing_values=false +/APPEND_TO_EXISTING_VALUES=FALSE 
 ; ;
  
 !--------------- !---------------
-square x+Square X
 !--------------- !---------------
-evaluate_expression  +Evaluate_Expression  
-/expression=derived::residual::tempx*derived::residual::tempx  +/EXPRESSION=DERIVED::RESIDUAL::TEMPX*DERIVED::RESIDUAL::TEMPX  
-/result_name=xx  +/RESULT_NAME=XX  
-/result_type=derived  +/RESULT_TYPE=DERIVED  
-/result_folder=residual +/RESULT_FOLDER=RESIDUAL 
 ; ;
  
 !----------------- !-----------------
-sum square of x+Sum square of X
 !----------------- !-----------------
-metric_sum  +Metric_Sum  
-/result_metric_name=sumxx  +/RESULT_METRIC_NAME=SUMXX  
-/apply_as_suffix_to_signal_name=false  +/APPLY_AS_SUFFIX_TO_SIGNAL_NAME=FALSE  
-/result_metric_folder=residual  +/RESULT_METRIC_FOLDER=RESIDUAL  
-/signal_types=derived  +/SIGNAL_TYPES=DERIVED  
-/signal_names=xx  +/SIGNAL_NAMES=XX  
-/signal_folder=residual  +/SIGNAL_FOLDER=RESIDUAL  
-/signal_components= +/SIGNAL_COMPONENTS= 
-/event_sequence=  +/EVENT_SEQUENCE=  
-/exclude_events=  +/EXCLUDE_EVENTS=  
-/generate_mean_and_stddev=false  +/GENERATE_MEAN_AND_STDDEV=FALSE  
-/append_to_existing_values=false +/APPEND_TO_EXISTING_VALUES=FALSE 
 ; ;
  
 !------------------ !------------------
-calculate slope+Calculate slope
 !------------------ !------------------
-evaluate_expression  +Evaluate_Expression  
-/expression=(&::num_points&*metric::residual::sumxy-metric::residual::sumx*metric::residual::sumy)/(&::num_points&*metric::residual::sumxx-metric::residual::sumx^2)  +/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_NAME=SLOPE  
-/result_type=metric  +/RESULT_TYPE=METRIC  
-/result_folder=residual +/RESULT_FOLDER=RESIDUAL 
 ; ;
  
 !---------------------- !----------------------
-calculate intercept+Calculate intercept
 !---------------------- !----------------------
-evaluate_expression  +Evaluate_Expression  
-/expression=(metric::residual::sumy-metric::residual::slope*metric::residual::sumx)/&::num_points  +/EXPRESSION=(METRIC::RESIDUAL::SUMY-METRIC::RESIDUAL::SLOPE*METRIC::RESIDUAL::SUMX)/&::NUM_POINTS  
-/result_name=intercept  +/RESULT_NAME=INTERCEPT  
-/result_type=metric  +/RESULT_TYPE=METRIC  
-/result_folder=residual +/RESULT_FOLDER=RESIDUAL 
 ; ;
  
Line 342: Line 344:
 ! create the resulting metric signal that contains the slope and intercept ! create the resulting metric signal that contains the slope and intercept
 !-------------------------------------------------------------------------- !--------------------------------------------------------------------------
-evaluate_expression  +Evaluate_Expression  
-/expression=vector(metric::residual::intercept,metric::residual::slope)  +/EXPRESSION=VECTOR(METRIC::RESIDUAL::INTERCEPT,METRIC::RESIDUAL::SLOPE)  
-/result_name=::name  +/RESULT_NAME=::NAME  
-/result_type=metric  +/RESULT_TYPE=METRIC  
-/result_folder=::folder +/RESULT_FOLDER=::FOLDER 
 ; ;
  
Line 354: Line 356:
 ! comment out this next command  ! comment out this next command 
 !----------------------------------------------------------- !-----------------------------------------------------------
-remove_signals  +Remove_Signals  
-/signal_types=derived+derived+derived+derived+metric+metric+metric+metric+metric+metric  +/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_NAMES=TEMPX+TEMPY+XX+XY+SLOPE+INTERCEPT+SUMX+SUMXX+SUMXY+SUMY  
-/signal_folder=residual+residual+residual+residual+residual+residual+residual+residual+residual+residual +/SIGNAL_FOLDER=RESIDUAL+RESIDUAL+RESIDUAL+RESIDUAL+RESIDUAL+RESIDUAL+RESIDUAL+RESIDUAL+RESIDUAL+RESIDUAL 
 ; ;
 </code> </code>
  
  
-}}}}}}+
visual3d/documentation/statistics/compute_linear_regression.1718801621.txt.gz · Last modified: 2024/06/19 12:53 by sgranger