示例#1
0
        protected virtual void SetPresText()
        {
            VariableRow mVariableRow = meta.MetaQuery.GetVariableRow(this.Name);

            foreach (string langCode in meta.LanguageCodes)
            {
                this.PresText[langCode] = mVariableRow.texts[langCode].PresText;
            }
            if (mVariableRow.Footnote != meta.Config.Codes.FootnoteN)
            {
                this.VarNoteExist = true;
            }
            else
            {
                this.VarNoteExist = false;
            }
        }
示例#2
0
        // Because two rows exist for different purposes, the first for headers and last for layout balance,
        // indexing became terrible.
        // I didn't want it to be spaghetti but I am italian, so...
        internal void SetRows(bool scriptLoaded)
        {
            _Updating = true;
            ClearRows();
            var cf = _Component.Scanner.CompiledFeatures;

            if (scriptLoaded && cf != null)
            {
                // Get the defined vars from the raw script.
                // The vars aren't added to the ExpandoObject until they're invoked. This gets around that.
                // There's probably a better solution but idk what it could be atm.
                var varsResults = Regex.Matches(_Component.GameProfile.RawScript, @"vars\.[a-z][0-9a-z]*", RegexOptions.IgnoreCase);

                _ValueQueues = new Queue <double> [cf.FeatureCount + varsResults.Count];
                _VarsNames   = new List <string>();
                int f = 0;

                var count = cf.CWatchImages.Count();

                if (count > 0)
                {
                    tlpFeatures.RowCount += count;
                    for (int i = 0; i < cf.CWatchZones.Length; i++)
                    {
                        var wz = cf.CWatchZones[i];

                        for (int n = 0; n < wz.CWatches.Length; n++)
                        {
                            var w = wz.CWatches[n];

                            for (int t = 0; t < w.CWatchImages.Length; t++)
                            {
                                var wi = w.CWatchImages[t];

                                var name = wz.Name + "/" + w.Name + " - " + wi.Name;

                                var valueQueue = new Queue <double>();
                                _ValueQueues[f] = valueQueue;
                                f++;

                                var featureRow = new VariableRow(name);

                                tlpFeatures.RowStyles.Insert(t, new RowStyle(SizeType.Absolute, 21F));
                                tlpFeatures.Controls.Add(featureRow, 0, wi.Index + 1);
                                // Todo: Add hook for manual enable/disable.
                            }
                        }
                    }
                }

                if (varsResults.Count > 0)
                {
                    string[] variables = new string[varsResults.Count];
                    for (int i = 0; i < varsResults.Count; i++)
                    {
                        variables[i] = varsResults[i].Value;
                    }

                    // What does this and the VASL script do with case-sensitivity?
                    variables = variables.Distinct().ToArray();

                    var offset = tlpFeatures.RowCount - 1;
                    tlpFeatures.RowCount += variables.Length;
                    for (int i = 0; i < variables.Length; i++)
                    {
                        var variable = variables[i];

                        var valueQueue = new Queue <double>();
                        _ValueQueues[f] = valueQueue;
                        f++;

                        _VarsNames.Add(variable.Substring(5));

                        var variableRow = new VariableRow(variable);

                        tlpFeatures.RowStyles.Insert(i + offset, new RowStyle(SizeType.Absolute, 21F));
                        tlpFeatures.Controls.Add(variableRow, 0, i + offset);
                    }
                }
            }
            _Updating = false;
        }
        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);
                }
            }
        }
示例#4
0
        public void LinearRegressionTest()
        {
            int iterations = 100;

            var reader      = new StreamReader(File.OpenRead(@"..\..\Data\LinearRegressionResults.csv"));
            int linesinfile = iterations;

            double[] constants    = new double[linesinfile];
            double[] constantlcls = new double[linesinfile];
            double[] constantucls = new double[linesinfile];
            double[] constantstes = new double[linesinfile];
            double[] i1s          = new double[linesinfile];
            double[] i1lcls       = new double[linesinfile];
            double[] i1ucls       = new double[linesinfile];
            double[] i1stes       = new double[linesinfile];
            double[] i2s          = new double[linesinfile];
            double[] i2lcls       = new double[linesinfile];
            double[] i2ucls       = new double[linesinfile];
            double[] i2stes       = new double[linesinfile];
            double[] i3s          = new double[linesinfile];
            double[] i3lcls       = new double[linesinfile];
            double[] i3ucls       = new double[linesinfile];
            double[] i3stes       = new double[linesinfile];
            double[] fconstants   = new double[linesinfile];
            double[] pfconstants  = new double[linesinfile];
            double[] f1s          = new double[linesinfile];
            double[] pf1s         = new double[linesinfile];
            double[] f2s          = new double[linesinfile];
            double[] pf2s         = new double[linesinfile];
            double[] f3s          = new double[linesinfile];
            double[] pf3s         = new double[linesinfile];
            double[] regdfs       = new double[linesinfile];
            double[] regsss       = new double[linesinfile];
            double[] regmss       = new double[linesinfile];
            double[] fstatistics  = new double[linesinfile];
            double[] pvalues      = new double[linesinfile];
            double[] resdfs       = new double[linesinfile];
            double[] ressss       = new double[linesinfile];
            double[] resmss       = new double[linesinfile];
            double[] totdfs       = new double[linesinfile];
            double[] totsss       = new double[linesinfile];
            double[] rsquares     = new double[linesinfile];
            int      i            = 0;

            while (!reader.EndOfStream)
            {
                var  line   = reader.ReadLine();
                var  values = line.Split(',');
                bool rv     = Double.TryParse(values[1], out constants[i]);
                rv = Double.TryParse(values[2], out constantlcls[i]);
                rv = Double.TryParse(values[3], out constantucls[i]);
                rv = Double.TryParse(values[4], out constantstes[i]);
                rv = Double.TryParse(values[5], out i1s[i]);
                rv = Double.TryParse(values[6], out i1lcls[i]);
                rv = Double.TryParse(values[7], out i1ucls[i]);
                rv = Double.TryParse(values[8], out i1stes[i]);
                rv = Double.TryParse(values[9], out i2s[i]);
                rv = Double.TryParse(values[10], out i2lcls[i]);
                rv = Double.TryParse(values[11], out i2ucls[i]);
                rv = Double.TryParse(values[12], out i2stes[i]);
                rv = Double.TryParse(values[13], out i3s[i]);
                rv = Double.TryParse(values[14], out i3lcls[i]);
                rv = Double.TryParse(values[15], out i3ucls[i]);
                rv = Double.TryParse(values[16], out i3stes[i]);
                rv = Double.TryParse(values[17], out fconstants[i]);
                rv = Double.TryParse(values[18], out pfconstants[i]);
                rv = Double.TryParse(values[19], out f1s[i]);
                rv = Double.TryParse(values[20], out pf1s[i]);
                rv = Double.TryParse(values[21], out f2s[i]);
                rv = Double.TryParse(values[22], out pf2s[i]);
                rv = Double.TryParse(values[23], out f3s[i]);
                rv = Double.TryParse(values[24], out pf3s[i]);
                rv = Double.TryParse(values[25], out regdfs[i]);
                rv = Double.TryParse(values[26], out regsss[i]);
                rv = Double.TryParse(values[27], out regmss[i]);
                rv = Double.TryParse(values[28], out fstatistics[i]);
                rv = Double.TryParse(values[29], out pvalues[i]);
                rv = Double.TryParse(values[30], out resdfs[i]);
                rv = Double.TryParse(values[31], out ressss[i]);
                rv = Double.TryParse(values[32], out resmss[i]);
                rv = Double.TryParse(values[33], out totdfs[i]);
                rv = Double.TryParse(values[34], out totsss[i]);
                rv = Double.TryParse(values[35], out rsquares[i]);
                i++;
                if (i == linesinfile)
                {
                    break;
                }
            }

            for (int j = 0; j < iterations; j++)
            {
                getData("LinearRegressionTestData", "iteration = " + j);

                Dictionary <string, string> inputVariableList = new Dictionary <string, string>();
                inputVariableList.Add("depvar", "dependvar");
                inputVariableList.Add("intercept", "true");
                inputVariableList.Add("includemissing", "false");
                inputVariableList.Add("P", "0.95");
                inputVariableList.Add("indepvar1", "unsorted");
                inputVariableList.Add("indepvar2", "unsorted");
                inputVariableList.Add("indepvar3", "unsorted");

                DataTable regressTable = new DataTable();

                LinearRegression.LinearRegressionResults results = new LinearRegression.LinearRegressionResults();

                StatisticsRepository.LinearRegression linearRegression = new StatisticsRepository.LinearRegression();
                results = linearRegression.LinearRegression(inputVariableList, mainTable);

                double tScore = 1.9;
                while (Epi.Statistics.SharedResources.PFromT(tScore, results.residualsDf) > 0.025)
                {
                    tScore += 0.000001;
                }
                List <VariableRow> moreStats = new List <VariableRow>();
                foreach (StatisticsRepository.LinearRegression.VariableRow vrow in results.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;
                    moreStats.Add(nrow);
                }
                double regressionFp = Epi.Statistics.SharedResources.PFromF(results.regressionF, results.regressionDf, results.residualsDf);

                Assert.AreEqual(results.variables[3].coefficient, constants[j], 0.0001);
                Assert.AreEqual(moreStats[3].coefficientLower, constantlcls[j], 0.0001);
                Assert.AreEqual(moreStats[3].coefficientUpper, constantucls[j], 0.0001);
                Assert.AreEqual(moreStats[3].stdError, constantstes[j], 0.0001);
                Assert.AreEqual(results.variables[0].coefficient, i1s[j], 0.0001);
                Assert.AreEqual(moreStats[0].coefficientLower, i1lcls[j], 0.0001);
                Assert.AreEqual(moreStats[0].coefficientUpper, i1ucls[j], 0.0001);
                Assert.AreEqual(moreStats[0].stdError, i1stes[j], 0.0001);
                Assert.AreEqual(results.variables[1].coefficient, i2s[j], 0.0001);
                Assert.AreEqual(moreStats[1].coefficientLower, i2lcls[j], 0.0001);
                Assert.AreEqual(moreStats[1].coefficientUpper, i2ucls[j], 0.0001);
                Assert.AreEqual(moreStats[1].stdError, i2stes[j], 0.0001);
                Assert.AreEqual(results.variables[2].coefficient, i3s[j], 0.0001);
                Assert.AreEqual(moreStats[2].coefficientLower, i3lcls[j], 0.0001);
                Assert.AreEqual(moreStats[2].coefficientUpper, i3ucls[j], 0.0001);
                Assert.AreEqual(moreStats[2].stdError, i3stes[j], 0.0001);
                Assert.AreEqual(results.variables[3].Ftest, fconstants[j], 0.0001);
                Assert.AreEqual(results.variables[3].P, pfconstants[j], 0.0001);
                Assert.AreEqual(results.variables[0].Ftest, f1s[j], 0.0001);
                Assert.AreEqual(results.variables[0].P, pf1s[j], 0.0001);
                Assert.AreEqual(results.variables[1].Ftest, f2s[j], 0.0001);
                Assert.AreEqual(results.variables[1].P, pf2s[j], 0.0001);
                Assert.AreEqual(results.variables[2].Ftest, f3s[j], 0.0001);
                Assert.AreEqual(results.variables[2].P, pf3s[j], 0.0001);
                Assert.AreEqual(results.regressionDf, regdfs[j], 0.0001);
                Assert.AreEqual(results.regressionSumOfSquares, regsss[j], 0.0001);
                Assert.AreEqual(results.regressionMeanSquare, regmss[j], 0.0001);
                Assert.AreEqual(results.regressionF, fstatistics[j], 0.0001);
                if (pvalues[j] < 0.001)
                {
                    Assert.AreEqual(regressionFp, pvalues[j], 0.0001);
                }
                else if (pvalues[j] < 0.01)
                {
                    Assert.AreEqual(regressionFp, pvalues[j], 0.001);
                }
                else if (pvalues[j] < 0.1)
                {
                    Assert.AreEqual(regressionFp, pvalues[j], 0.01);
                }
                else
                {
                    Assert.AreEqual(regressionFp, pvalues[j], 0.1);
                }
                Assert.AreEqual(results.residualsDf, resdfs[j], 0.0001);
                Assert.AreEqual(results.residualsSumOfSquares, ressss[j], 0.0001);
                Assert.AreEqual(results.residualsMeanSquare, resmss[j], 0.0001);
                Assert.AreEqual(results.totalDf, totdfs[j], 0.0001);
                Assert.AreEqual(results.totalSumOfSquares, totsss[j], 0.0001);
                Assert.AreEqual(results.correlationCoefficient, rsquares[j], 0.0001);
            }
        }
 public VariableRowChangeEvent(VariableRow row, global::System.Data.DataRowAction action)
 {
     this.eventRow = row;
     this.eventAction = action;
 }
 public void RemoveVariableRow(VariableRow row)
 {
     this.Rows.Remove(row);
 }
 public void AddVariableRow(VariableRow row)
 {
     this.Rows.Add(row);
 }
        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);
        }
示例#9
0
        /// <summary>
        /// Parses a WixStandardBootstrapperApplication element for Bundles.
        /// </summary>
        /// <param name="node">The element to parse.</param>
        private void ParseWixStandardBootstrapperApplicationElement(XElement node)
        {
            SourceLineNumber sourceLineNumbers        = Preprocessor.GetSourceLineNumbers(node);
            string           launchTarget             = null;
            string           licenseFile              = null;
            string           licenseUrl               = null;
            string           logoFile                 = null;
            string           logoSideFile             = null;
            string           themeFile                = null;
            string           localizationFile         = null;
            YesNoType        suppressOptionsUI        = YesNoType.NotSet;
            YesNoType        suppressDowngradeFailure = YesNoType.NotSet;
            YesNoType        suppressRepair           = YesNoType.NotSet;
            YesNoType        showVersion              = YesNoType.NotSet;

            foreach (XAttribute attrib in node.Attributes())
            {
                if (String.IsNullOrEmpty(attrib.Name.NamespaceName) || this.Namespace == attrib.Name.Namespace)
                {
                    switch (attrib.Name.LocalName)
                    {
                    case "LaunchTarget":
                        launchTarget = this.Core.GetAttributeValue(sourceLineNumbers, attrib);
                        break;

                    case "LicenseFile":
                        licenseFile = this.Core.GetAttributeValue(sourceLineNumbers, attrib);
                        break;

                    case "LicenseUrl":
                        licenseUrl = this.Core.GetAttributeValue(sourceLineNumbers, attrib, EmptyRule.CanBeEmpty);
                        break;

                    case "LogoFile":
                        logoFile = this.Core.GetAttributeValue(sourceLineNumbers, attrib);
                        break;

                    case "LogoSideFile":
                        logoSideFile = this.Core.GetAttributeValue(sourceLineNumbers, attrib);
                        break;

                    case "ThemeFile":
                        themeFile = this.Core.GetAttributeValue(sourceLineNumbers, attrib);
                        break;

                    case "LocalizationFile":
                        localizationFile = this.Core.GetAttributeValue(sourceLineNumbers, attrib);
                        break;

                    case "SuppressOptionsUI":
                        suppressOptionsUI = this.Core.GetAttributeYesNoValue(sourceLineNumbers, attrib);
                        break;

                    case "SuppressDowngradeFailure":
                        suppressDowngradeFailure = this.Core.GetAttributeYesNoValue(sourceLineNumbers, attrib);
                        break;

                    case "SuppressRepair":
                        suppressRepair = this.Core.GetAttributeYesNoValue(sourceLineNumbers, attrib);
                        break;

                    case "ShowVersion":
                        showVersion = this.Core.GetAttributeYesNoValue(sourceLineNumbers, attrib);
                        break;

                    default:
                        this.Core.UnexpectedAttribute(node, attrib);
                        break;
                    }
                }
                else
                {
                    this.Core.ParseExtensionAttribute(node, attrib);
                }
            }

            this.Core.ParseForExtensionElements(node);

            if (String.IsNullOrEmpty(licenseFile) && null == licenseUrl)
            {
                this.Core.OnMessage(WixErrors.ExpectedAttribute(sourceLineNumbers, node.Name.LocalName, "LicenseFile", "LicenseUrl", true));
            }

            if (!this.Core.EncounteredError)
            {
                if (!String.IsNullOrEmpty(launchTarget))
                {
                    VariableRow row = (VariableRow)this.Core.CreateRow(sourceLineNumbers, "Variable");
                    row.Id    = "LaunchTarget";
                    row.Value = launchTarget;
                    row.Type  = "string";
                }

                if (!String.IsNullOrEmpty(licenseFile))
                {
                    WixVariableRow wixVariableRow = (WixVariableRow)this.Core.CreateRow(sourceLineNumbers, "WixVariable");
                    wixVariableRow.Id    = "WixStdbaLicenseRtf";
                    wixVariableRow.Value = licenseFile;
                }

                if (null != licenseUrl)
                {
                    WixVariableRow wixVariableRow = (WixVariableRow)this.Core.CreateRow(sourceLineNumbers, "WixVariable");
                    wixVariableRow.Id    = "WixStdbaLicenseUrl";
                    wixVariableRow.Value = licenseUrl;
                }

                if (!String.IsNullOrEmpty(logoFile))
                {
                    WixVariableRow wixVariableRow = (WixVariableRow)this.Core.CreateRow(sourceLineNumbers, "WixVariable");
                    wixVariableRow.Id    = "WixStdbaLogo";
                    wixVariableRow.Value = logoFile;
                }

                if (!String.IsNullOrEmpty(logoSideFile))
                {
                    WixVariableRow wixVariableRow = (WixVariableRow)this.Core.CreateRow(sourceLineNumbers, "WixVariable");
                    wixVariableRow.Id    = "WixStdbaLogoSide";
                    wixVariableRow.Value = logoSideFile;
                }

                if (!String.IsNullOrEmpty(themeFile))
                {
                    WixVariableRow wixVariableRow = (WixVariableRow)this.Core.CreateRow(sourceLineNumbers, "WixVariable");
                    wixVariableRow.Id    = "WixStdbaThemeXml";
                    wixVariableRow.Value = themeFile;
                }

                if (!String.IsNullOrEmpty(localizationFile))
                {
                    WixVariableRow wixVariableRow = (WixVariableRow)this.Core.CreateRow(sourceLineNumbers, "WixVariable");
                    wixVariableRow.Id    = "WixStdbaThemeWxl";
                    wixVariableRow.Value = localizationFile;
                }

                if (YesNoType.Yes == suppressOptionsUI || YesNoType.Yes == suppressDowngradeFailure || YesNoType.Yes == suppressRepair || YesNoType.Yes == showVersion)
                {
                    Row row = this.Core.CreateRow(sourceLineNumbers, "WixStdbaOptions");
                    if (YesNoType.Yes == suppressOptionsUI)
                    {
                        row[0] = 1;
                    }

                    if (YesNoType.Yes == suppressDowngradeFailure)
                    {
                        row[1] = 1;
                    }

                    if (YesNoType.Yes == suppressRepair)
                    {
                        row[2] = 1;
                    }

                    if (YesNoType.Yes == showVersion)
                    {
                        row[3] = 1;
                    }
                }
            }
        }