public MultivariateLinearRegressionController(MultivariateLinearFitParameters param)
    {
      _param = param;

      string[] names = new string[param.SelectedDataColumns.Count];
      for(int i=0;i<names.Length;i++)
        names[i] = param.Table[param.SelectedDataColumns[i]].Name;

     
      
       _ctrl0 = new SingleChoiceController(names,0);
       _ctrl1 = new BooleanValueController(_param.IncludeIntercept);
       _ctrl2 = new BooleanValueController(_param.GenerateRegressionValues);
       _ctrl3 = new BooleanValueController(_param.GenerateRegressionValues);

        _ctrl0.DescriptionText = "Choose the dependent variable:";
        _ctrl1.DescriptionText = "Include intercept";
        _ctrl2.DescriptionText = "Generate prediction values";
        _ctrl3.DescriptionText = "Generate residual values";

      _elements[0] = new ControlViewElement(null,_ctrl0);
      _elements[1] = new ControlViewElement(null, _ctrl1);
      _elements[2] = new ControlViewElement(null, _ctrl2);
      _elements[3] = new ControlViewElement(null, _ctrl3);

      for(int i=0;i<_elements.Length;i++)
      {
        Current.Gui.FindAndAttachControlTo((IMVCController)_elements[i].Controller);
        _elements[i].View = ((IMVCController)_elements[i].Controller).ViewObject;
      }


      base.Initialize(_elements,false);
    }
Exemple #2
0
        public static LinearFitBySvd Regress(MultivariateLinearFitParameters parameters, out string[] paramNames)
        {
            DataColumnCollection        table        = parameters.Table;
            IAscendingIntegerCollection selectedCols = parameters.SelectedDataColumns;
            var selectedColsWODependent = new AscendingIntegerCollection(selectedCols);

            selectedColsWODependent.RemoveAt(parameters.DependentColumnIndexIntoSelection);

            IAscendingIntegerCollection validRows = DataTableWrapper.GetCollectionOfValidNumericRows(parameters.Table, selectedCols);

            parameters.SelectedDataRows = validRows;

            IROMatrix <double> xbase;

            if (parameters.IncludeIntercept)
            {
                xbase = DataTableWrapper.ToROColumnMatrixWithIntercept(parameters.Table, selectedColsWODependent, validRows);
            }
            else
            {
                xbase = DataTableWrapper.ToROColumnMatrix(parameters.Table, selectedColsWODependent, validRows);
            }

            paramNames = new string[xbase.ColumnCount];
            if (parameters.IncludeIntercept)
            {
                paramNames[0] = "Intercept";
                for (int i = 0; i < selectedColsWODependent.Count; i++)
                {
                    paramNames[i + 1] = table[selectedColsWODependent[i]].Name;
                }
            }
            else
            {
                for (int i = 0; i < selectedColsWODependent.Count; i++)
                {
                    paramNames[i] = table[selectedColsWODependent[i]].Name;
                }
            }

            // Fill the y and the error array
            double[] yarr = new double[validRows.Count];
            double[] earr = new double[validRows.Count];

            var ycol = (Altaxo.Data.INumericColumn)table[selectedCols[parameters.DependentColumnIndexIntoSelection]];

            for (int i = 0; i < validRows.Count; i++)
            {
                yarr[i] = ycol[validRows[i]];
                earr[i] = 1;
            }

            var fit =
                new LinearFitBySvd(
                    xbase, yarr, earr, xbase.RowCount, xbase.ColumnCount, 1E-5);

            return(fit);
        }
Exemple #3
0
        public static LinearFitBySvd Regress(MultivariateLinearFitParameters parameters, bool outputResults)
        {
            LinearFitBySvd fit = Regress(parameters, out var paramNames);

            if (outputResults)
            {
                OutputFitResults(fit, paramNames);

                if (parameters.GenerateRegressionValues)
                {
                    GenerateValues(parameters, fit);
                }
            }
            return(fit);
        }
Exemple #4
0
        public static void GenerateValues(MultivariateLinearFitParameters parameters, LinearFitBySvd fit)
        {
            DataColumn dependentColumn = parameters.Table[parameters.SelectedDataColumns[parameters.DependentColumnIndexIntoSelection]];

            if (parameters.GenerateRegressionValues)
            {
                var col = new DoubleColumn();
                VectorMath.Copy(VectorMath.ToROVector(fit.PredictedValues), DataColumnWrapper.ToVector(col, parameters.SelectedDataRows));
                parameters.Table.Add(col, dependentColumn.Name + "(predicted)", ColumnKind.V, parameters.Table.GetColumnGroup(dependentColumn));
            }

            if (parameters.GenerateResidualValues)
            {
                var col = new DoubleColumn();
                VectorMath.Copy(VectorMath.ToROVector(fit.ResidualValues), DataColumnWrapper.ToVector(col, parameters.SelectedDataRows));
                parameters.Table.Add(col, dependentColumn.Name + "(residual)", ColumnKind.V, parameters.Table.GetColumnGroup(dependentColumn));
            }
        }
Exemple #5
0
        public static LinearFitBySvd ShowDialogAndRegress(DataColumnCollection table, IAscendingIntegerCollection selectedColumns)
        {
            if (selectedColumns.Count < 2)
            {
                return(null);
            }

            object paramobject = new MultivariateLinearFitParameters(table, selectedColumns);

            if (!Current.Gui.ShowDialog(ref paramobject, "Multivariate linear fit"))
            {
                return(null);
            }

            var parameters = (MultivariateLinearFitParameters)paramobject;

            LinearFitBySvd result = Regress(parameters, true);

            return(result);
        }
    public static void GenerateValues(MultivariateLinearFitParameters parameters, LinearFitBySvd fit)
    {
      DataColumn dependentColumn = parameters.Table[parameters.SelectedDataColumns[parameters.DependentColumnIndexIntoSelection]];

      if(parameters.GenerateRegressionValues)
      {
        DoubleColumn col = new DoubleColumn();
        VectorMath.Copy(VectorMath.ToROVector(fit.PredictedValues),DataColumnWrapper.ToVector(col,parameters.SelectedDataRows));
        parameters.Table.Add(col,dependentColumn.Name + "(predicted)",ColumnKind.V, parameters.Table.GetColumnGroup(dependentColumn));
      }

      if(parameters.GenerateResidualValues)
      {
        DoubleColumn col = new DoubleColumn();
        VectorMath.Copy(VectorMath.ToROVector(fit.ResidualValues),DataColumnWrapper.ToVector(col,parameters.SelectedDataRows));
        parameters.Table.Add(col,dependentColumn.Name + "(residual)",ColumnKind.V, parameters.Table.GetColumnGroup(dependentColumn));
      }




    }
    public static LinearFitBySvd Regress(MultivariateLinearFitParameters parameters, bool outputResults)
    {
      string[] paramNames;
      LinearFitBySvd fit = Regress(parameters, out paramNames);

      if(outputResults)
      {
        OutputFitResults(fit,paramNames);

        if(parameters.GenerateRegressionValues)
        {
          GenerateValues(parameters,fit);
        }
      }
      return fit;
    }
    public static LinearFitBySvd Regress(MultivariateLinearFitParameters parameters, out string[] paramNames)
    {
      DataColumnCollection table = parameters.Table;
      IAscendingIntegerCollection selectedCols = parameters.SelectedDataColumns;
      AscendingIntegerCollection selectedColsWODependent = new AscendingIntegerCollection(selectedCols);
      selectedColsWODependent.RemoveAt(parameters.DependentColumnIndexIntoSelection);


      IAscendingIntegerCollection validRows = DataTableWrapper.GetCollectionOfValidNumericRows(parameters.Table,selectedCols);
      parameters.SelectedDataRows = validRows;
      
      IROMatrix xbase;
    
      if(parameters.IncludeIntercept)
      {
        xbase = DataTableWrapper.ToROColumnMatrixWithIntercept(parameters.Table,selectedColsWODependent,validRows);
      }
      else
      {
        xbase = DataTableWrapper.ToROColumnMatrix(parameters.Table,selectedColsWODependent,validRows);
      }

      paramNames = new string[xbase.Columns];
      if(parameters.IncludeIntercept)
      {
        paramNames[0] = "Intercept";
        for(int i=0;i<selectedColsWODependent.Count;i++)
          paramNames[i+1]=table[selectedColsWODependent[i]].Name;
      }
      else
      {
        for(int i=0;i<selectedColsWODependent.Count;i++)
          paramNames[i]=table[selectedColsWODependent[i]].Name;
      }


      // Fill the y and the error array
      double[] yarr = new double[validRows.Count];
      double[] earr = new double[validRows.Count];

      Altaxo.Data.INumericColumn ycol = (Altaxo.Data.INumericColumn)table[selectedCols[parameters.DependentColumnIndexIntoSelection]];

    
      for(int i=0;i<validRows.Count;i++)
      {
        yarr[i] = ycol[validRows[i]];
        earr[i] = 1;
      }

      LinearFitBySvd fit = 
        new LinearFitBySvd(
        xbase,yarr,earr, xbase.Rows, xbase.Columns, 1E-5);

      return fit;

    }
    public static LinearFitBySvd ShowDialogAndRegress(DataColumnCollection table, IAscendingIntegerCollection selectedColumns)
    {
      if(selectedColumns.Count<2)
        return null;

      object paramobject = new MultivariateLinearFitParameters(table,selectedColumns);

      if(!Current.Gui.ShowDialog(ref paramobject,"Multivariate linear fit"))
        return null;

      MultivariateLinearFitParameters parameters = (MultivariateLinearFitParameters)paramobject;

      LinearFitBySvd result =  Regress(parameters, true);

      return result;
    }