User Tools

Site Tools


visual3d:documentation:statistics:compute_linear_regression

This is an old revision of the document!


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> 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 </code> ===== 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. <code> !========================================================================= ! 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 ; </code> ===== 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. <code> ! 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 ; </code> }}}}

visual3d/documentation/statistics/compute_linear_regression.1718804356.txt.gz · Last modified: 2024/06/19 13:39 by sgranger