Example #1
0
        public LinRegressionResults GetRegressionResult(GadgetParameters gadgetOptions, List <string> columnNames, List <DictionaryDTO> inputDtoList, IEnumerable <EwavDataFilterCondition> ewavDataFilters, List <EwavRule_Base> rules, string filterString = "")
        {
            if (gadgetOptions.UseAdvancedDataFilter)
            {
                dh = new DashboardHelper(gadgetOptions, filterString, rules);
                gadgetOptions.UseAdvancedDataFilter  = true;
                gadgetOptions.AdvancedDataFilterText = filterString;
            }
            else
            {
                dh = new DashboardHelper(gadgetOptions, ewavDataFilters, rules);
            }
            DataTable            dt;
            LinRegressionResults results = new LinRegressionResults();

            try
            {
                dt = dh.GenerateTable(columnNames, gadgetOptions);
            }
            catch (Exception e)
            {
                results.ErrorMessage = e.Message;
                return(results);
            }
            Dictionary <string, string> inputVariableList = RegressionManager.ConvertDtoToDic(inputDtoList);


            StatisticsRepository.LinearRegression linearRegression = new StatisticsRepository.LinearRegression();

            StatisticsRepository.LinearRegression.LinearRegressionResults regressionResults = linearRegression.LinearRegression(inputVariableList, dt);

            results.RegressionResults = RegressionManager.ConvertToLinRegResults(regressionResults);

            results.CorrelationCoefficient = results.RegressionResults.CorrelationCoefficient;
            results.RegressionDf           = results.RegressionResults.RegressionDf;
            results.RegressionF            = results.RegressionResults.RegressionF;
            results.RegressionMeanSquare   = results.RegressionResults.RegressionMeanSquare;
            results.RegressionSumOfSquares = results.RegressionResults.RegressionSumOfSquares;
            results.ResidualsDf            = results.RegressionResults.ResidualsDf;
            results.ResidualsMeanSquare    = results.RegressionResults.ResidualsMeanSquare;
            results.ResidualsSumOfSquares  = results.RegressionResults.ResidualsSumOfSquares;
            results.TotalDf           = results.RegressionResults.TotalDf;
            results.TotalSumOfSquares = results.RegressionResults.TotalSumOfSquares;
            results.Variables         = results.RegressionResults.Variables;
            if (results.RegressionResults.ErrorMessage != null)
            {
                results.ErrorMessage = results.RegressionResults.ErrorMessage.Replace("<tlt>", string.Empty).Replace("</tlt>", string.Empty);
            }

            return(results);
        }
Example #2
0
        public string BuildInputParams(
            string chartType, 
            string chartTitle, 
            string independentTitle, 
            string dependentTitle, 
            string independentValueFormat,
            string dependentValueFormat,
            string interval,
            string intervalUnit,
            object startFrom,
            DataTable regressionTable)
        {
            StringBuilder chartLineSeries = new StringBuilder();

            string independentValueType = "";
            string dependentValueType = "";

            chartType = chartType.Replace("\"", "");
            chartTitle = chartTitle.Replace("\"", "");
            independentTitle = independentTitle.Replace("\"", "");
            dependentTitle = dependentTitle.Replace("\"", "");

            switch (chartType.ToUpper().Replace(" ", ""))
            {
                case SilverlightStatics.Area:
                    chartType = SilverlightStatics.Area;
                    break;

                case SilverlightStatics.Column:
                    chartType = SilverlightStatics.Bar;
                    break;

                case SilverlightStatics.Bubble:
                    chartType = SilverlightStatics.Bubble;
                    break;

                case SilverlightStatics.EpiCurve:
                    chartType = SilverlightStatics.Histogram;
                    break;

                case SilverlightStatics.Bar:
                    chartType = SilverlightStatics.RotatedBar;
                    break;

                case SilverlightStatics.Line:
                    chartType = SilverlightStatics.Line;
                    break;

                case SilverlightStatics.Pie:
                    chartType = SilverlightStatics.Pie;
                    break;

                case SilverlightStatics.Scatter:
                    chartType = SilverlightStatics.Scatter;
                    break;

                case SilverlightStatics.Stacked:
                    chartType = SilverlightStatics.Stacked;
                    break;

                case SilverlightStatics.TreeMap:
                    chartType = SilverlightStatics.TreeMap;
                    break;

                default:
                    chartType = SilverlightStatics.Bar;
                    break;
            }

            chartLineSeries.Append("chartLineSeries=");

            DataTable distinct = regressionTable.DefaultView.ToTable(true, "SeriesName");

            if (regressionTable.Rows.Count == 0)
            {
                return string.Empty;
            }

            foreach (DataRow row in distinct.Rows)
            {
                string seriesName = row["SeriesName"].ToString();
                string filter = string.Format("SeriesName = '{0}'", seriesName);

                DataRow[] regressionData = regressionTable.Select(filter);

                if (chartLineSeries.ToString().EndsWith("chartLineSeries=") == false)
                {
                    chartLineSeries.Append(SilverlightStatics.SeparateLineSeries);
                }

                chartLineSeries.Append("(");
                chartLineSeries.Append("LineSeriesTitle");
                chartLineSeries.Append(seriesName);
                chartLineSeries.Append("LineSeriesTitle");

                Type indepValueType = typeof(string);
                indepValueType = regressionData[0]["Predictor"].GetType();
                independentValueType = indepValueType.ToString();

                chartLineSeries.Append(SilverlightStatics.IndependentValueFormat);
                chartLineSeries.Append(independentValueFormat);
                chartLineSeries.Append(SilverlightStatics.IndependentValueFormat);

                chartLineSeries.Append(SilverlightStatics.DependentValueFormat);
                chartLineSeries.Append(dependentValueFormat);
                chartLineSeries.Append(SilverlightStatics.DependentValueFormat);

                chartLineSeries.Append(SilverlightStatics.LineSeriesDataString);

                foreach (DataRow regression in regressionData)
                {
                    if (regression["Predictor"] is DateTime)
                    {
                        string dateTimeString = ((DateTime)regression["Predictor"]).ToString(_dateTimeFormat);
                        chartLineSeries.Append(dateTimeString);
                    }
                    else
                    {
                        chartLineSeries.Append(regression["Predictor"].ToString());
                    }

                    chartLineSeries.Append(SilverlightStatics.SeparateIndDepValues);
                    chartLineSeries.Append(regression["Response"].ToString());
                    chartLineSeries.Append(SilverlightStatics.SeparateDataPoints);
                }

                if (chartLineSeries.ToString().EndsWith(SilverlightStatics.SeparateDataPoints))
                {
                    string series = chartLineSeries.ToString().Substring(0, chartLineSeries.Length - SilverlightStatics.SeparateDataPoints.Length);
                    chartLineSeries = new StringBuilder();
                    chartLineSeries.Append(series);
                }

                chartLineSeries.Append(SilverlightStatics.LineSeriesDataString);
            }

            if (chartType == SilverlightStatics.Scatter)
            {
                List<NumericDataValue> dataValues = new List<NumericDataValue>();
                NumericDataValue minValue = null;
                NumericDataValue maxValue = null;
                foreach (DataRow row in regressionTable.Rows)
                {
                    if (row["Response"].Equals(DBNull.Value) || row["Predictor"].Equals(DBNull.Value))
                    {
                        continue;
                    }
                    NumericDataValue currentValue = new NumericDataValue() { DependentValue = Convert.ToDecimal(row["Response"]), IndependentValue = Convert.ToDecimal(row["Predictor"]) };
                    dataValues.Add(currentValue);
                    if (minValue == null)
                    {
                        minValue = currentValue;
                    }
                    else
                    {
                        if (currentValue.IndependentValue < minValue.IndependentValue)
                        {
                            minValue = currentValue;
                        }
                    }
                    if (maxValue == null)
                    {
                        maxValue = currentValue;
                    }
                    else
                    {
                        if (currentValue.IndependentValue > maxValue.IndependentValue)
                        {
                            maxValue = currentValue;
                        }
                    }
                }

                StatisticsRepository.LinearRegression linearRegression = new StatisticsRepository.LinearRegression();
                Dictionary<string, string> inputVariableList = new Dictionary<string, string>();
                inputVariableList.Add("Response", "dependvar");
                inputVariableList.Add("intercept", "true");
                inputVariableList.Add("includemissing", "false");
                inputVariableList.Add("p", "0.95");
                inputVariableList.Add("Predictor", "unsorted");

                StatisticsRepository.LinearRegression.LinearRegressionResults regresResults = linearRegression.LinearRegression(inputVariableList, regressionTable);

                object[] result = new object[] { dataValues, regresResults, maxValue, minValue };

                decimal coefficient = Convert.ToDecimal(regresResults.variables[0].coefficient);
                decimal constant = Convert.ToDecimal(regresResults.variables[1].coefficient);

                NumericDataValue newMaxValue = new NumericDataValue();
                newMaxValue.IndependentValue = maxValue.IndependentValue + 1;
                newMaxValue.DependentValue = (coefficient * maxValue.IndependentValue) + constant;
                NumericDataValue newMinValue = new NumericDataValue();
                newMinValue.IndependentValue = minValue.IndependentValue - 1;
                newMinValue.DependentValue = (coefficient * minValue.IndependentValue) + constant;

                chartLineSeries.Append("LineSeriesDataString)^((LineSeriesTitleLinear RegressionLineSeriesTitleIndependentValueFormatIndependentValueFormatDependentValueFormatDependentValueFormatLineSeriesDataString");
                chartLineSeries.Append(newMinValue.IndependentValue.ToString());
                chartLineSeries.Append("^sidv^");
                chartLineSeries.Append(newMinValue.DependentValue.ToString());
                chartLineSeries.Append("^&^");
                chartLineSeries.Append(newMaxValue.IndependentValue.ToString());
                chartLineSeries.Append("^sidv^");
                chartLineSeries.Append(newMaxValue.DependentValue.ToString());
                chartLineSeries.Append("LineSeriesDataString");
            }

            chartLineSeries.Append(")");

            string inputParams = string.Empty;

            if (chartType.Length > 0) inputParams += string.Format("chartType={0},", chartType);
            if (chartTitle.Length > 0) inputParams += string.Format("chartTitle={0},", chartTitle);

            if (independentTitle.Length > 0) inputParams += string.Format("independentLabel={0},", independentTitle);
            if (dependentTitle.Length > 0) inputParams += string.Format("dependentLabel={0},", dependentTitle);

            if (independentValueFormat.Length > 0) inputParams += string.Format("independentValueFormat={0},", independentValueFormat);
            if (dependentValueFormat.Length > 0) inputParams += string.Format("dependentValueFormat={0},", dependentValueFormat);

            if (independentValueType.Length > 0) inputParams += string.Format("independentValueType={0},", independentValueType);
            if (dependentValueType.Length > 0) inputParams += string.Format("dependentValueType={0},", dependentValueType);

            if (interval.Length > 0) inputParams += string.Format("interval={0},", interval);
            if (intervalUnit.Length > 0) inputParams += string.Format("intervalUnit={0},", intervalUnit);

            if (startFrom is DateTime)
            {
                startFrom = ((DateTime)startFrom).ToString(_dateTimeFormat);
            }
            else
            {
                startFrom = startFrom.ToString();
            }

            if (((string)startFrom).Length > 0) inputParams += string.Format("startFrom={0},", ((string)startFrom));

            inputParams += chartLineSeries.Replace(",", SilverlightStatics.Comma);
            return inputParams;
        }
        protected override void worker_DoWork(object sender, System.ComponentModel.DoWorkEventArgs e)
        {
            lock (syncLock)
            {
                Stopwatch stopwatch = new Stopwatch();
                stopwatch.Start();

                this.Dispatcher.BeginInvoke(new SimpleCallback(SetGadgetToProcessingState));
                this.Dispatcher.BeginInvoke(new SimpleCallback(ClearResults));

                string freqVar = GadgetOptions.MainVariableName;
                string crosstabVar = GadgetOptions.CrosstabVariableName;
                string weightVar = GadgetOptions.WeightVariableName;
                string strataVar = string.Empty;
                bool includeMissing = GadgetOptions.ShouldIncludeMissing;

                List<string> stratas = new List<string>();
                if (!string.IsNullOrEmpty(strataVar))
                {
                    stratas.Add(strataVar);
                }

                try
                {
                    RequestUpdateStatusDelegate requestUpdateStatus = new RequestUpdateStatusDelegate(RequestUpdateStatusMessage);
                    CheckForCancellationDelegate checkForCancellation = new CheckForCancellationDelegate(IsCancelled);

                    GadgetOptions.GadgetStatusUpdate += new GadgetStatusUpdateHandler(requestUpdateStatus);
                    GadgetOptions.GadgetCheckForCancellation += new GadgetCheckForCancellationHandler(checkForCancellation);

                    if (this.DataFilters != null && this.DataFilters.Count > 0)
                    {
                        GadgetOptions.CustomFilter = this.DataFilters.GenerateDataFilterString(false);
                    }
                    else
                    {
                        GadgetOptions.CustomFilter = string.Empty;
                    }

                    DataView dv = DashboardHelper.GenerateView(GadgetOptions);

                    List<XYChartData> dataList = new List<XYChartData>();

                    NumericDataValue minValue = null;
                    NumericDataValue maxValue = null;

                    foreach (DataRowView drv in dv)
                    {
                        DataRow row = drv.Row;

                        if (row[freqVar] != DBNull.Value && row[freqVar] != null && row[crosstabVar] != DBNull.Value && row[crosstabVar] != null)
                        {
                            XYChartData chartData = new XYChartData();
                            chartData.X = Convert.ToDouble(row[freqVar]);
                            chartData.Y = Convert.ToDouble(row[crosstabVar]);
                            dataList.Add(chartData);

                            NumericDataValue currentValue = new NumericDataValue() { DependentValue = Convert.ToDecimal(row[crosstabVar]), IndependentValue = Convert.ToDecimal(row[freqVar]) };
                            if (minValue == null)
                            {
                                minValue = currentValue;
                            }
                            else
                            {
                                if (currentValue.IndependentValue < minValue.IndependentValue)
                                {
                                    minValue = currentValue;
                                }
                            }
                            if (maxValue == null)
                            {
                                maxValue = currentValue;
                            }
                            else
                            {
                                if (currentValue.IndependentValue > maxValue.IndependentValue)
                                {
                                    maxValue = currentValue;
                                }
                            }
                        }
                    }

                    StatisticsRepository.LinearRegression linearRegression = new StatisticsRepository.LinearRegression();
                    Dictionary<string, string> inputVariableList = new Dictionary<string, string>();
                    inputVariableList.Add(crosstabVar, "dependvar");
                    inputVariableList.Add("intercept", "true");
                    inputVariableList.Add("includemissing", "false");
                    inputVariableList.Add("p", "0.95");
                    inputVariableList.Add(freqVar, "unsorted");

                    StatisticsRepository.LinearRegression.LinearRegressionResults regresResults = linearRegression.LinearRegression(inputVariableList, dv.Table);

                    this.Dispatcher.BeginInvoke(new SetChartDataDelegate(SetChartData), dataList, regresResults, maxValue, minValue);
                    this.Dispatcher.BeginInvoke(new SimpleCallback(RenderFinish));

                }
                catch (Exception ex)
                {
                    this.Dispatcher.BeginInvoke(new RenderFinishWithErrorDelegate(RenderFinishWithError), ex.Message);
                }
                finally
                {
                    stopwatch.Stop();
                    Debug.Print("Column chart gadget took " + stopwatch.Elapsed.ToString() + " seconds to complete.");
                    Debug.Print(DashboardHelper.DataFilters.GenerateDataFilterString());
                }
            }
        }
 public LinearRegression(IAnalysisStatisticContext AnalysisStatisticContext)
 {
     LinearRegress = new StatisticsRepository.LinearRegression();
     this.Construct(AnalysisStatisticContext);
 }
        protected override void worker_DoWork(object sender, System.ComponentModel.DoWorkEventArgs e)
        {
            lock (syncLock)
            {
                Configuration config = DashboardHelper.Config;
                Dictionary<string, string> setProperties = new Dictionary<string, string>();
                setProperties.Add("Intercept", "true");
                setProperties.Add("P", 0.95.ToString());
                setProperties.Add("BLabels", config.Settings.RepresentationOfYes + ";" + config.Settings.RepresentationOfNo + ";" + config.Settings.RepresentationOfMissing); // TODO: Replace Yes, No, Missing with global vars

                //Dictionary<string, string> inputVariableList = (Dictionary<string, string>)e.Argument;

                Stopwatch stopwatch = new Stopwatch();
                stopwatch.Start();

                this.Dispatcher.BeginInvoke(new SimpleCallback(SetGadgetToProcessingState));
                this.Dispatcher.BeginInvoke(new SimpleCallback(ClearResults));

                SetGridTextDelegate setText = new SetGridTextDelegate(SetGridText);
                AddGridRowDelegate addRow = new AddGridRowDelegate(AddGridRow);
                //SetGridBarDelegate setBar = new SetGridBarDelegate(SetGridBar);

                System.Collections.Generic.Dictionary<string, System.Data.DataTable> Freq_ListSet = new Dictionary<string, System.Data.DataTable>();

                //string customFilter = string.Empty;
                List<string> columnNames = new List<string>();

                foreach (KeyValuePair<string, string> kvp in GadgetOptions.InputVariableList)
                {
                    if (kvp.Value.ToLower().Equals("unsorted") || kvp.Value.ToLower().Equals("dependvar") || kvp.Value.ToLower().Equals("weightvar") || kvp.Value.ToLower().Equals("matchvar"))
                    {
                        columnNames.Add(kvp.Key);
                    }
                    else if (kvp.Value.ToLower().Equals("discrete"))
                    {
                        columnNames.Add(kvp.Key);
                    }
                }

                try
                {
                    lock (staticSyncLock)
                    {
                        RequestUpdateStatusDelegate requestUpdateStatus = new RequestUpdateStatusDelegate(RequestUpdateStatusMessage);
                        CheckForCancellationDelegate checkForCancellation = new CheckForCancellationDelegate(IsCancelled);

                        GadgetOptions.GadgetStatusUpdate += new GadgetStatusUpdateHandler(requestUpdateStatus);
                        GadgetOptions.GadgetCheckForCancellation += new GadgetCheckForCancellationHandler(checkForCancellation);

                        DataTable regressTable = DashboardHelper.GenerateTable(columnNames);
                        RegressionResults results = new RegressionResults();

                        if (regressTable == null || regressTable.Rows.Count <= 0)
                        {
                            this.Dispatcher.BeginInvoke(new RenderFinishWithErrorDelegate(RenderFinishWithError), SharedStrings.NO_RECORDS_SELECTED);
                            this.Dispatcher.BeginInvoke(new SimpleCallback(SetGadgetToFinishedState));
                            Debug.Print("Thread stopped due to errors.");
                            return;
                        }
                        else if (worker.CancellationPending)
                        {
                            this.Dispatcher.BeginInvoke(new RenderFinishWithErrorDelegate(RenderFinishWithError), SharedStrings.DASHBOARD_GADGET_STATUS_OPERATION_CANCELLED);
                            this.Dispatcher.BeginInvoke(new SimpleCallback(SetGadgetToFinishedState));
                            Debug.Print("Thread cancelled");
                            return;
                        }
                        else
                        {
                            StatisticsRepository.LinearRegression linearRegression = new StatisticsRepository.LinearRegression();

                            results.regressionResults = linearRegression.LinearRegression(GadgetOptions.InputVariableList, regressTable);

                            results.correlationCoefficient = results.regressionResults.correlationCoefficient;
                            results.regressionDf = results.regressionResults.regressionDf;
                            results.regressionF = results.regressionResults.regressionF;
                            results.regressionMeanSquare = results.regressionResults.regressionMeanSquare;
                            results.regressionSumOfSquares = results.regressionResults.regressionSumOfSquares;
                            results.residualsDf = results.regressionResults.residualsDf;
                            results.residualsMeanSquare = results.regressionResults.residualsMeanSquare;
                            results.residualsSumOfSquares = results.regressionResults.residualsSumOfSquares;
                            results.totalDf = results.regressionResults.totalDf;
                            results.totalSumOfSquares = results.regressionResults.totalSumOfSquares;
                            results.errorMessage = results.regressionResults.errorMessage.Replace("<tlt>", string.Empty).Replace("</tlt>", string.Empty);

                            if (!string.IsNullOrEmpty(results.errorMessage))
                            {
                                throw new ApplicationException(results.errorMessage);
                            }

                            results.variables = new List<VariableRow>();

                            if (results.regressionResults.variables != null)
                            {
                                double tScore = 1.9;
                                while (Epi.Statistics.SharedResources.PFromT(tScore, results.residualsDf) > 0.025)
                                {
                                    tScore += 0.000001;
                                }
                                foreach (StatisticsRepository.LinearRegression.VariableRow vrow in results.regressionResults.variables)
                                {
                                    VariableRow nrow = new VariableRow();
                                    nrow.coefficient = vrow.coefficient;
                                    nrow.Ftest = vrow.Ftest;
                                    nrow.P = vrow.P;
                                    nrow.stdError = vrow.stdError;
                                    nrow.variableName = vrow.variableName;
                                    nrow.coefficientLower = nrow.coefficient - tScore * nrow.stdError;
                                    nrow.coefficientUpper = nrow.coefficient + tScore * nrow.stdError;
                                    results.variables.Add(nrow);
                                }

                                results.regressionFp = Epi.Statistics.SharedResources.PFromF(results.regressionF, results.regressionDf, results.residualsDf);

                                this.Dispatcher.BeginInvoke(new SimpleCallback(RenderRegressionHeader));

                                int rowCount = 1;
                                foreach (VariableRow row in results.variables)
                                {
                                    this.Dispatcher.Invoke(addRow, grdRegress, 30);

                                    string displayValue = row.variableName;

                                    this.Dispatcher.BeginInvoke(setText, grdRegress, new TextBlockConfig(StringLiterals.SPACE + displayValue + StringLiterals.SPACE, new Thickness(2, 0, 2, 0), VerticalAlignment.Center, HorizontalAlignment.Left, TextAlignment.Left, rowCount, 0, Visibility.Visible));
                                    this.Dispatcher.BeginInvoke(setText, grdRegress, new TextBlockConfig(StringLiterals.SPACE + row.coefficient.ToString("F3") + StringLiterals.SPACE, new Thickness(2, 0, 2, 0), VerticalAlignment.Center, HorizontalAlignment.Right, TextAlignment.Right, rowCount, 1, Visibility.Visible));
                                    this.Dispatcher.BeginInvoke(setText, grdRegress, new TextBlockConfig(StringLiterals.SPACE + row.coefficientLower.ToString("F3") + StringLiterals.SPACE, new Thickness(2, 0, 2, 0), VerticalAlignment.Center, HorizontalAlignment.Right, TextAlignment.Right, rowCount, 2, Visibility.Visible));
                                    this.Dispatcher.BeginInvoke(setText, grdRegress, new TextBlockConfig(StringLiterals.SPACE + row.coefficientUpper.ToString("F3") + StringLiterals.SPACE, new Thickness(2, 0, 2, 0), VerticalAlignment.Center, HorizontalAlignment.Right, TextAlignment.Right, rowCount, 3, Visibility.Visible));

                                    this.Dispatcher.BeginInvoke(setText, grdRegress, new TextBlockConfig(StringLiterals.SPACE + row.stdError.ToString("F3") + StringLiterals.SPACE, new Thickness(2, 0, 2, 0), VerticalAlignment.Center, HorizontalAlignment.Right, TextAlignment.Right, rowCount, 4, Visibility.Visible));
                                    this.Dispatcher.BeginInvoke(setText, grdRegress, new TextBlockConfig(StringLiterals.SPACE + row.Ftest.ToString("F4") + StringLiterals.SPACE, new Thickness(2, 0, 2, 0), VerticalAlignment.Center, HorizontalAlignment.Right, TextAlignment.Right, rowCount, 5, Visibility.Visible));
                                    this.Dispatcher.BeginInvoke(setText, grdRegress, new TextBlockConfig(StringLiterals.SPACE + row.P.ToString("F6") + StringLiterals.SPACE, new Thickness(2, 0, 2, 0), VerticalAlignment.Center, HorizontalAlignment.Right, TextAlignment.Right, rowCount, 6, Visibility.Visible));

                                    rowCount++;
                                }

                                //this.Dispatcher.BeginInvoke(new AddGridFooterDelegate(RenderFrequencyFooter), rowCount, count);
                                this.Dispatcher.BeginInvoke(new SimpleCallback(DrawRegressionBorders));
                            }
                        }

                        this.Dispatcher.BeginInvoke(new RenderGridDelegate(RenderRegressionResults), results);
                        this.Dispatcher.BeginInvoke(new SimpleCallback(SetGadgetToFinishedState));
                    }
                }
                catch (Exception ex)
                {
                    this.Dispatcher.BeginInvoke(new RenderFinishWithErrorDelegate(RenderFinishWithError), ex.Message);
                    this.Dispatcher.BeginInvoke(new SimpleCallback(SetGadgetToFinishedState));
                }
                finally
                {
                    stopwatch.Stop();
                    Debug.Print("Linear regression gadget took " + stopwatch.Elapsed.ToString() + " seconds to complete with " + DashboardHelper.RecordCount.ToString() + " records and the following filters:");
                    Debug.Print(DashboardHelper.DataFilters.GenerateDataFilterString());
                }
            }
            //gridCells.singleTableResults = new StatisticsRepository.cTable().SigTable((double)gridCells.yyVal, (double)gridCells.ynVal, (double)gridCells.nyVal, (double)gridCells.nnVal, 0.95);
        }
        public string BuildInputParams(
            string chartType,
            string chartTitle,
            string independentTitle,
            string dependentTitle,
            string independentValueFormat,
            string dependentValueFormat,
            string interval,
            string intervalUnit,
            object startFrom,
            DataTable regressionTable)
        {
            StringBuilder chartLineSeries = new StringBuilder();

            string independentValueType = "";
            string dependentValueType   = "";

            chartType        = chartType.Replace("\"", "");
            chartTitle       = chartTitle.Replace("\"", "");
            independentTitle = independentTitle.Replace("\"", "");
            dependentTitle   = dependentTitle.Replace("\"", "");

            switch (chartType.ToUpperInvariant().Replace(" ", ""))
            {
            case SilverlightStatics.Area:
                chartType = SilverlightStatics.Area;
                break;

            case SilverlightStatics.Column:
                chartType = SilverlightStatics.Bar;
                break;

            case SilverlightStatics.Bubble:
                chartType = SilverlightStatics.Bubble;
                break;

            case SilverlightStatics.EpiCurve:
                chartType = SilverlightStatics.Histogram;
                break;

            case SilverlightStatics.Bar:
                chartType = SilverlightStatics.RotatedBar;
                break;

            case SilverlightStatics.Line:
                chartType = SilverlightStatics.Line;
                break;

            case SilverlightStatics.Pie:
                chartType = SilverlightStatics.Pie;
                break;

            case SilverlightStatics.Scatter:
                chartType = SilverlightStatics.Scatter;
                break;

            case SilverlightStatics.Stacked:
                chartType = SilverlightStatics.Stacked;
                break;

            case SilverlightStatics.TreeMap:
                chartType = SilverlightStatics.TreeMap;
                break;

            default:
                chartType = SilverlightStatics.Bar;
                break;
            }

            chartLineSeries.Append("chartLineSeries=");

            DataTable distinct = regressionTable.DefaultView.ToTable(true, "SeriesName");

            if (regressionTable.Rows.Count == 0)
            {
                return(string.Empty);
            }

            foreach (DataRow row in distinct.Rows)
            {
                string seriesName = row["SeriesName"].ToString();
                string filter     = string.Format("SeriesName = '{0}'", seriesName);

                DataRow[] regressionData = regressionTable.Select(filter);

                if (chartLineSeries.ToString().EndsWith("chartLineSeries=") == false)
                {
                    chartLineSeries.Append(SilverlightStatics.SeparateLineSeries);
                }

                chartLineSeries.Append("(");
                chartLineSeries.Append("LineSeriesTitle");
                chartLineSeries.Append(seriesName);
                chartLineSeries.Append("LineSeriesTitle");

                Type indepValueType = typeof(string);
                indepValueType       = regressionData[0]["Predictor"].GetType();
                independentValueType = indepValueType.ToString();

                chartLineSeries.Append(SilverlightStatics.IndependentValueFormat);
                chartLineSeries.Append(independentValueFormat);
                chartLineSeries.Append(SilverlightStatics.IndependentValueFormat);

                chartLineSeries.Append(SilverlightStatics.DependentValueFormat);
                chartLineSeries.Append(dependentValueFormat);
                chartLineSeries.Append(SilverlightStatics.DependentValueFormat);

                chartLineSeries.Append(SilverlightStatics.LineSeriesDataString);

                foreach (DataRow regression in regressionData)
                {
                    if (regression["Predictor"] is DateTime)
                    {
                        string dateTimeString = ((DateTime)regression["Predictor"]).ToString(_dateTimeFormat);
                        chartLineSeries.Append(dateTimeString);
                    }
                    else
                    {
                        chartLineSeries.Append(regression["Predictor"].ToString());
                    }

                    chartLineSeries.Append(SilverlightStatics.SeparateIndDepValues);
                    chartLineSeries.Append(regression["Response"].ToString());
                    chartLineSeries.Append(SilverlightStatics.SeparateDataPoints);
                }

                if (chartLineSeries.ToString().EndsWith(SilverlightStatics.SeparateDataPoints))
                {
                    string series = chartLineSeries.ToString().Substring(0, chartLineSeries.Length - SilverlightStatics.SeparateDataPoints.Length);
                    chartLineSeries = new StringBuilder();
                    chartLineSeries.Append(series);
                }

                chartLineSeries.Append(SilverlightStatics.LineSeriesDataString);
            }

            if (chartType == SilverlightStatics.Scatter)
            {
                List <NumericDataValue> dataValues = new List <NumericDataValue>();
                NumericDataValue        minValue   = null;
                NumericDataValue        maxValue   = null;
                foreach (DataRow row in regressionTable.Rows)
                {
                    if (row["Response"].Equals(DBNull.Value) || row["Predictor"].Equals(DBNull.Value))
                    {
                        continue;
                    }
                    NumericDataValue currentValue = new NumericDataValue()
                    {
                        DependentValue = Convert.ToDecimal(row["Response"]), IndependentValue = Convert.ToDecimal(row["Predictor"])
                    };
                    dataValues.Add(currentValue);
                    if (minValue == null)
                    {
                        minValue = currentValue;
                    }
                    else
                    {
                        if (currentValue.IndependentValue < minValue.IndependentValue)
                        {
                            minValue = currentValue;
                        }
                    }
                    if (maxValue == null)
                    {
                        maxValue = currentValue;
                    }
                    else
                    {
                        if (currentValue.IndependentValue > maxValue.IndependentValue)
                        {
                            maxValue = currentValue;
                        }
                    }
                }

                StatisticsRepository.LinearRegression linearRegression  = new StatisticsRepository.LinearRegression();
                Dictionary <string, string>           inputVariableList = new Dictionary <string, string>();
                inputVariableList.Add("Response", "dependvar");
                inputVariableList.Add("intercept", "true");
                inputVariableList.Add("includemissing", "false");
                inputVariableList.Add("p", "0.95");
                inputVariableList.Add("Predictor", "unsorted");

                StatisticsRepository.LinearRegression.LinearRegressionResults regresResults = linearRegression.LinearRegression(inputVariableList, regressionTable);

                object[] result = new object[] { dataValues, regresResults, maxValue, minValue };

                decimal coefficient = Convert.ToDecimal(regresResults.variables[0].coefficient);
                decimal constant    = Convert.ToDecimal(regresResults.variables[1].coefficient);

                NumericDataValue newMaxValue = new NumericDataValue();
                newMaxValue.IndependentValue = maxValue.IndependentValue + 1;
                newMaxValue.DependentValue   = (coefficient * maxValue.IndependentValue) + constant;
                NumericDataValue newMinValue = new NumericDataValue();
                newMinValue.IndependentValue = minValue.IndependentValue - 1;
                newMinValue.DependentValue   = (coefficient * minValue.IndependentValue) + constant;

                chartLineSeries.Append("LineSeriesDataString)^((LineSeriesTitleLinear RegressionLineSeriesTitleIndependentValueFormatIndependentValueFormatDependentValueFormatDependentValueFormatLineSeriesDataString");
                chartLineSeries.Append(newMinValue.IndependentValue.ToString());
                chartLineSeries.Append("^sidv^");
                chartLineSeries.Append(newMinValue.DependentValue.ToString());
                chartLineSeries.Append("^&^");
                chartLineSeries.Append(newMaxValue.IndependentValue.ToString());
                chartLineSeries.Append("^sidv^");
                chartLineSeries.Append(newMaxValue.DependentValue.ToString());
                chartLineSeries.Append("LineSeriesDataString");
            }

            chartLineSeries.Append(")");

            string inputParams = string.Empty;

            if (chartType.Length > 0)
            {
                inputParams += string.Format("chartType={0},", chartType);
            }
            if (chartTitle.Length > 0)
            {
                inputParams += string.Format("chartTitle={0},", chartTitle);
            }

            if (independentTitle.Length > 0)
            {
                inputParams += string.Format("independentLabel={0},", independentTitle);
            }
            if (dependentTitle.Length > 0)
            {
                inputParams += string.Format("dependentLabel={0},", dependentTitle);
            }

            if (independentValueFormat.Length > 0)
            {
                inputParams += string.Format("independentValueFormat={0},", independentValueFormat);
            }
            if (dependentValueFormat.Length > 0)
            {
                inputParams += string.Format("dependentValueFormat={0},", dependentValueFormat);
            }

            if (independentValueType.Length > 0)
            {
                inputParams += string.Format("independentValueType={0},", independentValueType);
            }
            if (dependentValueType.Length > 0)
            {
                inputParams += string.Format("dependentValueType={0},", dependentValueType);
            }

            if (interval.Length > 0)
            {
                inputParams += string.Format("interval={0},", interval);
            }
            if (intervalUnit.Length > 0)
            {
                inputParams += string.Format("intervalUnit={0},", intervalUnit);
            }

            if (startFrom is DateTime)
            {
                startFrom = ((DateTime)startFrom).ToString(_dateTimeFormat);
            }
            else
            {
                startFrom = startFrom.ToString();
            }


            if (((string)startFrom).Length > 0)
            {
                inputParams += string.Format("startFrom={0},", ((string)startFrom));
            }

            inputParams += chartLineSeries.Replace(",", SilverlightStatics.Comma);
            return(inputParams);
        }
Example #7
0
        void scatterChartWorker_DoWork(object sender, System.ComponentModel.DoWorkEventArgs e)
        {
            try
            {
                this.Dispatcher.BeginInvoke(new SimpleCallback(SetGadgetToProcessingState));
                this.Dispatcher.BeginInvoke(new SimpleCallback(ClearResults));

                RequestUpdateStatusDelegate requestUpdateStatus = new RequestUpdateStatusDelegate(RequestUpdateStatusMessage);
                CheckForCancellationDelegate checkForCancellation = new CheckForCancellationDelegate(IsCancelled);

                GadgetOptions.GadgetStatusUpdate += new GadgetStatusUpdateHandler(requestUpdateStatus);
                GadgetOptions.GadgetCheckForCancellation += new GadgetCheckForCancellationHandler(checkForCancellation);

                string xAxisVar = GadgetOptions.MainVariableName;
                string yAxisVar = GadgetOptions.CrosstabVariableName;

                List<string> columnNames = new List<string>();
                columnNames.Add(xAxisVar);
                columnNames.Add(yAxisVar);

                DataTable regressTable = DashboardHelper.GenerateTable(columnNames);

                if (regressTable == null || regressTable.Rows.Count == 0)
                {
                    this.Dispatcher.BeginInvoke(new RenderFinishWithErrorDelegate(RenderFinishWithError), SharedStrings.NO_RECORDS_SELECTED);
                    this.Dispatcher.BeginInvoke(new SimpleCallback(SetGadgetToFinishedState));
                    return;
                }
                else if (worker.CancellationPending)
                {
                    this.Dispatcher.BeginInvoke(new RenderFinishWithErrorDelegate(RenderFinishWithError), SharedStrings.DASHBOARD_GADGET_STATUS_OPERATION_CANCELLED);
                    this.Dispatcher.BeginInvoke(new SimpleCallback(SetGadgetToFinishedState));
                    System.Diagnostics.Debug.Print("Single chart thread was cancelled");
                    return;
                }
                else
                {
                    List<NumericDataValue> dataValues = new List<NumericDataValue>();
                    NumericDataValue minValue = null;
                    NumericDataValue maxValue = null;
                    foreach (DataRow row in regressTable.Rows)
                    {
                        if (row[yAxisVar].Equals(DBNull.Value) || row[xAxisVar].Equals(DBNull.Value))
                        {
                            continue;
                        }
                        NumericDataValue currentValue = new NumericDataValue() { DependentValue = Convert.ToDecimal(row[yAxisVar]), IndependentValue = Convert.ToDecimal(row[xAxisVar]) };
                        dataValues.Add(currentValue);
                        if (minValue == null)
                        {
                            minValue = currentValue;
                        }
                        else
                        {
                            if (currentValue.IndependentValue < minValue.IndependentValue)
                            {
                                minValue = currentValue;
                            }
                        }
                        if (maxValue == null)
                        {
                            maxValue = currentValue;
                        }
                        else
                        {
                            if (currentValue.IndependentValue > maxValue.IndependentValue)
                            {
                                maxValue = currentValue;
                            }
                        }
                    }

                    StatisticsRepository.LinearRegression linearRegression = new StatisticsRepository.LinearRegression();
                    Dictionary<string, string> inputVariableList = new Dictionary<string, string>();
                    inputVariableList.Add(yAxisVar, "dependvar");
                    inputVariableList.Add("intercept", "true");
                    inputVariableList.Add("includemissing", "false");
                    inputVariableList.Add("p", "0.95");
                    inputVariableList.Add(xAxisVar, "unsorted");

                    StatisticsRepository.LinearRegression.LinearRegressionResults regresResults = linearRegression.LinearRegression(inputVariableList, regressTable);

                    object[] result = new object[] { dataValues, regresResults, maxValue, minValue };
                    e.Result = result;

                    this.Dispatcher.BeginInvoke(new SimpleCallback(RenderFinish));
                }
            }
            catch (Exception ex)
            {
                this.Dispatcher.BeginInvoke(new RenderFinishWithErrorDelegate(RenderFinishWithError), ex.Message);
            }
        }
Example #8
0
        public ScatterDataDTO GenerateTable(GadgetParameters gadgetOptions,
                                            IEnumerable <EwavDataFilterCondition> ewavDataFilters, List <EwavRule_Base> rules, string filterString = "")
        {
            List <ListOfStringClass> lls = new List <ListOfStringClass>();

            if (gadgetOptions.UseAdvancedDataFilter)
            {
                dh = new DashboardHelper(gadgetOptions, filterString, rules);
                gadgetOptions.UseAdvancedDataFilter  = true;
                gadgetOptions.AdvancedDataFilterText = filterString;
            }
            else
            {
                dh = new DashboardHelper(gadgetOptions, ewavDataFilters, rules);
            }

            MyString ms       = new MyString();
            string   xAxisVar = gadgetOptions.MainVariableName;
            string   yAxisVar = gadgetOptions.CrosstabVariableName;

            List <string> columns = new List <string>();

            columns.Add(xAxisVar);
            columns.Add(yAxisVar);


            DataTable dt = dh.GenerateTable(columns, gadgetOptions);



            List <NumericDataValue> dataValues = new List <NumericDataValue>();
            NumericDataValue        minValue   = null;
            NumericDataValue        maxValue   = null;

            //regressTable.FieldsList.Fields

            //foreach (DataRow row in regressTable.Rows)
            foreach (DataRow row in dt.Rows)
            {
                //if (regressTable.GetValueAtRow(yAxisVar, row).Equals(DBNull.Value) || regressTable.GetValueAtRow(xAxisVar, row).Equals(DBNull.Value))
                if (row[yAxisVar].Equals(DBNull.Value) || row[xAxisVar].Equals(DBNull.Value))
                {
                    continue;
                }
                NumericDataValue currentValue = new NumericDataValue()
                {
                    DependentValue = Convert.ToDecimal(row[yAxisVar]), IndependentValue = Convert.ToDecimal(row[xAxisVar])
                };
                //NumericDataValue currentValue = new NumericDataValue() { DependentValue = Convert.ToDecimal(regressTable.GetValueAtRow(yAxisVar, row)), IndependentValue = Convert.ToDecimal(regressTable.GetValueAtRow(xAxisVar, row)) };
                dataValues.Add(currentValue);
                if (minValue == null)
                {
                    minValue = currentValue;
                }
                else
                {
                    if (currentValue.IndependentValue < minValue.IndependentValue)
                    {
                        minValue = currentValue;
                    }
                }
                if (maxValue == null)
                {
                    maxValue = currentValue;
                }
                else
                {
                    if (currentValue.IndependentValue > maxValue.IndependentValue)
                    {
                        maxValue = currentValue;
                    }
                }
            }

            StatisticsRepository.LinearRegression linearRegression  = new StatisticsRepository.LinearRegression();
            Dictionary <string, string>           inputVariableList = new Dictionary <string, string>();

            inputVariableList.Add(yAxisVar, "dependvar");
            inputVariableList.Add("intercept", "true");
            inputVariableList.Add("includemissing", "false");
            inputVariableList.Add("p", "0.95");
            inputVariableList.Add(xAxisVar, "unsorted");

            StatisticsRepository.LinearRegression.LinearRegressionResults regresResults = linearRegression.LinearRegression(inputVariableList, dt);
            LinRegressionResults results = new LinRegressionResults();

            results = RegressionManager.ConvertToLinRegResults(regresResults);

            //results.CorrelationCoefficient = results.RegressionResults.CorrelationCoefficient;
            //results.RegressionDf = results.RegressionResults.RegressionDf;
            //results.RegressionF = results.RegressionResults.RegressionF;
            //results.RegressionMeanSquare = results.RegressionResults.RegressionMeanSquare;
            //results.RegressionSumOfSquares = results.RegressionResults.RegressionSumOfSquares;
            //results.ResidualsDf = results.RegressionResults.ResidualsDf;
            //results.ResidualsMeanSquare = results.RegressionResults.ResidualsMeanSquare;
            //results.ResidualsSumOfSquares = results.RegressionResults.ResidualsSumOfSquares;
            //results.TotalDf = results.RegressionResults.TotalDf;
            //results.TotalSumOfSquares = results.RegressionResults.TotalSumOfSquares;
            //results.Variables = results.RegressionResults.Variables;
            if (results.ErrorMessage != null)
            {
                results.ErrorMessage = results.ErrorMessage.Replace("<tlt>", string.Empty).Replace("</tlt>", string.Empty);
            }


            ScatterDataDTO scatterDTO = new ScatterDataDTO();

            scatterDTO.DataValues    = dataValues;
            scatterDTO.RegresResults = results;
            scatterDTO.MinValue      = minValue;
            scatterDTO.MaxValue      = maxValue;

            //RegressionManager lrm = new RegressionManager();
            //lls = lrm.ConvertDataTableToList(dt);
            //return new List<ListOfStringClass>();

            return(scatterDTO);
        }
Example #9
0
 public LinearRegression(IAnalysisStatisticContext AnalysisStatisticContext)
 {
     LinearRegress = new StatisticsRepository.LinearRegression();
     this.Construct(AnalysisStatisticContext);
 }