Exemplo n.º 1
0
        public LogRegressionResults GetRegressionResult(GadgetParameters gadgetOptions, List <string> columnNames, List <DictionaryDTO> inputDtoList, IEnumerable <EwavDataFilterCondition> ewavDataFilters, List <EwavRule_Base> rules, string filterString = "", string customFilter = "")
        {
            if (gadgetOptions.UseAdvancedDataFilter)
            {
                dh = new DashboardHelper(gadgetOptions, filterString, rules);
                gadgetOptions.UseAdvancedDataFilter  = true;
                gadgetOptions.AdvancedDataFilterText = filterString;
            }
            else
            {
                dh = new DashboardHelper(gadgetOptions, ewavDataFilters, rules);
            }
            DataTable dt;
            Dictionary <string, string> inputVariableList = RegressionManager.ConvertDtoToDic(inputDtoList);

            //LogRegressionResults results = new LogRegressionResults();
            StatisticsRepository.LogisticRegression logisticRegression = new StatisticsRepository.LogisticRegression();
            try
            {
                dt = dh.GenerateTable(columnNames, gadgetOptions, customFilter);
            }
            catch (System.Exception e)
            {
                throw new Exception("Error retrieving data from Dashboard Helper");
                //results.ErrorMessage = e.Message;
                //return results;
            }



            StatisticsRepository.LogisticRegression.LogisticRegressionResults regressionResults = logisticRegression.LogisticRegression(inputVariableList, dt);

            LogRegressionResults results = RegressionManager.ConvertToLogRegResults(regressionResults);

            //results.CasesIncluded = results.RegressionResults1.CasesIncluded;
            //results.Convergence = results.RegressionResults1.Convergence;
            //results.FinalLikelihood = results.RegressionResults1.FinalLikelihood;
            //results.Iterations = results.RegressionResults1.Iterations;
            //results.LRDF = results.RegressionResults1.LRDF;
            //results.LRP = results.RegressionResults1.LRP;
            //results.LRStatistic = results.RegressionResults1.LRStatistic;
            //results.ScoreDF = results.RegressionResults1.ScoreDF;
            //results.ScoreP = results.RegressionResults1.ScoreP;
            //results.ScoreStatistic = results.RegressionResults1.ScoreStatistic;
            //if (results.RegressionResults1.ErrorMessage != null)
            //{
            //    results.ErrorMessage = results.RegressionResults1.ErrorMessage.Replace("<tlt>", string.Empty).Replace("</tlt>", string.Empty);
            //}

            //results.Variables = results.RegressionResults1.Variables;


            return(results);
        }
Exemplo n.º 2
0
 public LogisticRegression(IAnalysisStatisticContext AnalysisStatisticContext)
 {
     LogisticRegress = new StatisticsRepository.LogisticRegression();
     this.Construct(AnalysisStatisticContext);
 }
        protected override void worker_DoWork(object sender, System.ComponentModel.DoWorkEventArgs e)
        {
            Configuration config = dashboardHelper.Config;
            Dictionary<string, string> setProperties = new Dictionary<string, string>();
            setProperties.Add("BLabels", config.Settings.RepresentationOfYes + ";" + config.Settings.RepresentationOfNo + ";" + config.Settings.RepresentationOfMissing); // TODO: Replace Yes, No, Missing with global vars

            bool includeMissing = true;

            Dictionary<string, string> inputVariableList = GadgetOptions.InputVariableList;

            lock (syncLock)
            {
                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);

                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 inputVariableList)
                {
                    if (kvp.Key.ToLower().Equals("includemissing"))
                    {
                        includeMissing = bool.Parse(kvp.Value);
                    }
                }

                foreach (KeyValuePair<string, string> kvp in 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);
                        if (!kvp.Value.ToLower().Equals("dependvar"))
                        {
                            customFilter = customFilter + StringLiterals.PARANTHESES_OPEN + StringLiterals.LEFT_SQUARE_BRACKET + kvp.Key + StringLiterals.RIGHT_SQUARE_BRACKET + StringLiterals.SPACE + "is not null" + StringLiterals.PARANTHESES_CLOSE + " AND ";
                        }
                    }
                    else if (kvp.Value.ToLower().Equals("discrete"))
                    {
                        columnNames.Add(kvp.Key);
                        customFilter = customFilter + StringLiterals.PARANTHESES_OPEN + StringLiterals.LEFT_SQUARE_BRACKET + kvp.Key + StringLiterals.RIGHT_SQUARE_BRACKET + StringLiterals.SPACE + "is not null" + StringLiterals.PARANTHESES_CLOSE + " AND ";
                    }
                }

                if (includeMissing)
                {
                    customFilter = string.Empty;
                }
                else
                {
                    customFilter = customFilter.Remove(customFilter.Length - 4, 4);
                }

                try
                {
                    lock (staticSyncLock)
                    {
                        DataTable regressTable = dashboardHelper.GenerateTable(columnNames, customFilter);
                        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.LogisticRegression logisticRegression = new StatisticsRepository.LogisticRegression();

                            results.regressionResults = logisticRegression.LogisticRegression(inputVariableList, regressTable);

                            results.casesIncluded = results.regressionResults.casesIncluded;
                            results.convergence = results.regressionResults.convergence;
                            results.finalLikelihood = results.regressionResults.finalLikelihood;
                            results.iterations = results.regressionResults.iterations;
                            results.LRDF = results.regressionResults.LRDF;
                            results.LRP = results.regressionResults.LRP;
                            results.LRStatistic = results.regressionResults.LRStatistic;
                            results.scoreDF = results.regressionResults.scoreDF;
                            results.scoreP = results.regressionResults.scoreP;
                            results.scoreStatistic = results.regressionResults.scoreStatistic;
                            results.errorMessage = results.regressionResults.errorMessage.Replace("<tlt>", string.Empty).Replace("</tlt>", string.Empty);
                            results.variables = new List<VariableRow>();

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

                            }
                            if (results.regressionResults.variables != null)
                            {
                                foreach (StatisticsRepository.LogisticRegression.VariableRow vrow in results.regressionResults.variables)
                                {
                                    VariableRow nrow = new VariableRow();
                                    nrow.coefficient = vrow.coefficient;
                                    nrow.ci = vrow.ci;
                                    nrow.P = vrow.P;
                                    nrow.ninetyFivePercent = vrow.ninetyFivePercent;
                                    nrow.oddsRatio = vrow.oddsRatio;
                                    nrow.se = vrow.se;
                                    nrow.variableName = vrow.variableName;
                                    nrow.Z = vrow.Z;
                                    results.variables.Add(nrow);
                                }

                                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(displayValue, new Thickness(4, 0, 4, 0), VerticalAlignment.Center, HorizontalAlignment.Left, TextAlignment.Left, rowCount, 0, Visibility.Visible));
                                    if (row.oddsRatio <= -9999)
                                    {
                                        this.Dispatcher.BeginInvoke(setText, grdRegress, new TextBlockConfig("*", new Thickness(4, 0, 4, 0), VerticalAlignment.Center, HorizontalAlignment.Right, TextAlignment.Right, rowCount, 1, Visibility.Visible));
                                        this.Dispatcher.BeginInvoke(setText, grdRegress, new TextBlockConfig("*", new Thickness(4, 0, 4, 0), VerticalAlignment.Center, HorizontalAlignment.Right, TextAlignment.Right, rowCount, 2, Visibility.Visible));
                                        this.Dispatcher.BeginInvoke(setText, grdRegress, new TextBlockConfig("*", new Thickness(4, 0, 4, 0), VerticalAlignment.Center, HorizontalAlignment.Right, TextAlignment.Right, rowCount, 3, Visibility.Visible));
                                    }
                                    else
                                    {
                                        this.Dispatcher.BeginInvoke(setText, grdRegress, new TextBlockConfig(row.oddsRatio.ToString("F4"), new Thickness(4, 0, 4, 0), VerticalAlignment.Center, HorizontalAlignment.Right, TextAlignment.Right, rowCount, 1, Visibility.Visible));
                                        this.Dispatcher.BeginInvoke(setText, grdRegress, new TextBlockConfig(row.ninetyFivePercent.ToString("F4"), new Thickness(4, 0, 4, 0), VerticalAlignment.Center, HorizontalAlignment.Right, TextAlignment.Right, rowCount, 2, Visibility.Visible));
                                        if (row.ci > 1.0E12)
                                        {
                                            this.Dispatcher.BeginInvoke(setText, grdRegress, new TextBlockConfig(">1.0E12", new Thickness(4, 0, 4, 0), VerticalAlignment.Center, HorizontalAlignment.Right, TextAlignment.Right, rowCount, 3, Visibility.Visible));
                                        }
                                        else
                                        {
                                            this.Dispatcher.BeginInvoke(setText, grdRegress, new TextBlockConfig(row.ci.ToString("F4"), new Thickness(4, 0, 4, 0), VerticalAlignment.Center, HorizontalAlignment.Right, TextAlignment.Right, rowCount, 3, Visibility.Visible));
                                        }
                                    }
                                    this.Dispatcher.BeginInvoke(setText, grdRegress, new TextBlockConfig(row.coefficient.ToString("F4"), new Thickness(4, 0, 4, 0), VerticalAlignment.Center, HorizontalAlignment.Right, TextAlignment.Right, rowCount, 4, Visibility.Visible));
                                    this.Dispatcher.BeginInvoke(setText, grdRegress, new TextBlockConfig(row.se.ToString("F4"), new Thickness(4, 0, 4, 0), VerticalAlignment.Center, HorizontalAlignment.Right, TextAlignment.Right, rowCount, 5, Visibility.Visible));
                                    this.Dispatcher.BeginInvoke(setText, grdRegress, new TextBlockConfig(row.Z.ToString("F4"), new Thickness(4, 0, 4, 0), VerticalAlignment.Center, HorizontalAlignment.Right, TextAlignment.Right, rowCount, 6, Visibility.Visible));
                                    this.Dispatcher.BeginInvoke(setText, grdRegress, new TextBlockConfig(row.P.ToString("F4"), new Thickness(4, 0, 4, 0), VerticalAlignment.Center, HorizontalAlignment.Right, TextAlignment.Right, rowCount, 7, Visibility.Visible));

                                    rowCount++;
                                }

                                if (results.regressionResults.interactionOddsRatios != null)
                                {
                                    this.Dispatcher.BeginInvoke(new SimpleCallback(RenderIORHeader));
                                    rowCount = 1;
                                    foreach (StatisticsRepository.LogisticRegression.InteractionRow ir in results.regressionResults.interactionOddsRatios)
                                    {
                                        this.Dispatcher.Invoke(addRow, grdIOR, 30);
                                        this.Dispatcher.BeginInvoke(setText, grdIOR, new TextBlockConfig(ir.interactionName.ToString(), new Thickness(4, 0, 4, 0), VerticalAlignment.Center, HorizontalAlignment.Left, TextAlignment.Right, rowCount, 0, Visibility.Visible));
                                        this.Dispatcher.BeginInvoke(setText, grdIOR, new TextBlockConfig(ir.oddsRatio.ToString(), new Thickness(4, 0, 4, 0), VerticalAlignment.Center, HorizontalAlignment.Left, TextAlignment.Right, rowCount, 1, Visibility.Visible));
                                        this.Dispatcher.BeginInvoke(setText, grdIOR, new TextBlockConfig(ir.ninetyFivePercent.ToString(), new Thickness(4, 0, 4, 0), VerticalAlignment.Center, HorizontalAlignment.Left, TextAlignment.Right, rowCount, 2, Visibility.Visible));
                                        this.Dispatcher.BeginInvoke(setText, grdIOR, new TextBlockConfig(ir.ci.ToString(), new Thickness(4, 0, 4, 0), VerticalAlignment.Center, HorizontalAlignment.Left, TextAlignment.Right, rowCount, 3, Visibility.Visible));
                                        rowCount++;
                                    }
                                }

                                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("Logistic 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);
                }
            }
        }
Exemplo n.º 4
0
 public LogisticRegression(IAnalysisStatisticContext AnalysisStatisticContext)
 {
     LogisticRegress = new StatisticsRepository.LogisticRegression();
     this.Construct(AnalysisStatisticContext);
 }