Esempio n. 1
0
        public Model(MeasureList measures, bool outcomeIsLogNormallyDistributed, McmcParameters mcmcParams)
        {
            OelStdz  = new OelStandardizer(this);
            Measures = measures;

            this.ClassName = this.GetType().Name;
            if (mcmcParams == null)
            {
                mcmcParams = new McmcParameters(); // default values
                this.Messages.AddWarning("Mcmc parameters were undefined, using default values", this.ClassName);
            }

            this.Messages.Add(measures.Error);
            this.Messages.Add(mcmcParams.Error);
            this.NIter         = mcmcParams.NIter;
            this.NBurnin       = mcmcParams.NBurnin;
            this.NThin         = mcmcParams.NThin;
            this.MonitorBurnin = mcmcParams.MonitorBurnin;
            this.OutcomeIsLogNormallyDistributed = outcomeIsLogNormallyDistributed;
            if (this.OutcomeIsLogNormallyDistributed)
            {
                OelStdz.StandardizeObservations();
            }
            this.Data = new DataSummary(measures, this.OutcomeIsLogNormallyDistributed);
            this.ME   = measures.ME;
            this.OEL  = measures.OEL;
        }
Esempio n. 2
0
        public SEGUninformativeModel(MeasureList measures, UninformativeModelParameters specificParams, McmcParameters mcmcParams = null)
            : base(measures, specificParams.LogNormalDstrn, /*new Range(specificParams.MuLower, specificParams.MuUpper, specificParams.InitMu),*/ mcmcParams)
        {
            this.MuLower = specificParams.MuLower;
            this.MuUpper = specificParams.MuUpper;
            this.SDRange = specificParams.SDRange;
            this.InitMu  = specificParams.InitMu;
            this.InitSD  = specificParams.InitSD;
            this.Result  = new ModelResult(this, "mu", "sd");
            bool testSD = double.IsNaN(this.InitSD) || (this.InitSD == 0);

            if (Tools.IsND(this.InitMu) || testSD)
            {
                InitialValues initVals = WebExpoFunctions3.DefaultInits(this.Data, this.OutcomeIsLogNormallyDistributed, Tools.Combine(this.MuLower, this.MuUpper), this.SDRange, includeCensoredData: false);
                if (Tools.IsND(this.InitMu))
                {
                    this.InitMu = initVals.Mu;
                }

                if (testSD)
                {
                    this.InitSD = initVals.SigmaWithin;
                }
            }

            if (this.ME.ThroughCV)
            {
                this.Result.Chains.Add("cv");
            }
        }// end constructor
Esempio n. 3
0
        public virtual string ToString(MeasureList.FieldSeparator fs)
        {
            string fieldSeparator = MeasureList.GetFieldSeparator(fs);

            return(string.Format(CultureInfo.InvariantCulture, "{0}{2}{1}",
                                 A,
                                 Worker == null ? "" : Worker.Tag,
                                 fieldSeparator));
        }
Esempio n. 4
0
        public override string ToString(MeasureList.FieldSeparator fs)
        {
            string fieldSeparator = MeasureList.GetFieldSeparator(fs);

            return(string.Format(CultureInfo.InvariantCulture, "[{0},{1}]{3}{2}",
                                 A,
                                 B,
                                 Worker == null ? "" : Worker.Tag,
                                 fieldSeparator));
        }
Esempio n. 5
0
        public SEGInformedVarModel(MeasureList measures, SEGInformedVarModelParameters specificParams, McmcParameters mcmcParams = null, PastDataSummary pastDataSummary = null)
            : base(measures, specificParams.LogNormalDstrn, mcmcParams)
        {
            this.PastData = pastDataSummary == null ? PastDataSummary.EmptyObject : pastDataSummary;

            this.Messages.Add(this.PastData.Messages);
            this.LogSigmaMu   = specificParams.LogSigmaMu;
            this.LogSigmaPrec = specificParams.LogSigmaPrec;
            this.InitSigma    = specificParams.InitSigma;
            this.MuLower      = specificParams.MuLower;
            this.MuUpper      = specificParams.MuUpper;
            this.InitMu       = specificParams.InitMu;
            this.Result       = new ModelResult(this, "mu", "sd");

            if (this.ME.ThroughCV)
            {
                this.Result.Chains.Add("cv");
            }
        }// end constructor
Esempio n. 6
0
        internal DataSummary(MeasureList ml, bool logNormalDistrn)
        {
            this.MeasureList        = ml;
            this.UncensoredMeasures = MeasureList.measuresByCensoringType[Measure.MeasureType.Uncensored].ToArray();
            this.LTMeasures         = MeasureList.measuresByCensoringType[Measure.MeasureType.LessThan].ToArray();
            this.GTMeasures         = MeasureList.measuresByCensoringType[Measure.MeasureType.GreaterThan].ToArray();
            this.IntervalMeasures   = MeasureList.measuresByCensoringType[Measure.MeasureType.Interval].ToArray();

            this.logNormalDistrn = logNormalDistrn;
            this.N           = ml.N;
            this.MEAny       = ml.MEAny;
            this.METhroughCV = ml.METhroughCV;
            this.METhroughSD = ml.METhroughSD;

            this.raw                  = new RawData();
            this.raw.Y                = ml.measuresByCensoringType[Measure.MeasureType.Uncensored].Select(x => x.A).ToArray();
            this.raw.UncensoredSum    = this.raw.Y.Sum();
            this.raw.UncensoredSum2   = this.raw.Y.Sqr().Sum();
            this.raw.LT               = ml.measuresByCensoringType[Measure.MeasureType.LessThan].Select(x => x.A).ToArray();
            this.raw.GT               = ml.measuresByCensoringType[Measure.MeasureType.GreaterThan].Select(x => x.A).ToArray();
            this.raw.IntervalCensored = Tuple.Create(
                ml.measuresByCensoringType[Measure.MeasureType.Interval].Select(x => x.A).ToArray(),
                ml.measuresByCensoringType[Measure.MeasureType.Interval].Select(x => x.B).ToArray());
            this.AnyGT = GTLength > 0;
            this.AnyLT = LTLength > 0;
            this.AnyIntervalCensored = IntervalLength > 0;
            this.AnyCensored         = this.AnyGT || this.AnyLT || this.AnyIntervalCensored;

            if (ml.ME.ThroughCV || logNormalDistrn)
            {
                Func <double, bool> test;
                if (logNormalDistrn)
                {
                    test = delegate(double x)
                    {
                        return(x <= 0);
                    };
                }
                else
                {
                    test = delegate(double x)
                    {
                        return(x < 0);
                    };
                }

                string firstErrSrc = "";
                while (true)
                {
                    if (this.raw.Y.Any(xx => test(xx)))
                    {
                        firstErrSrc = "uncensored measure";
                        break;
                    }

                    if (this.raw.GT.Any(xx => test(xx)))
                    {
                        firstErrSrc = "'greater than' measure";
                        break;
                    }

                    if (this.raw.LT.Any(xx => test(xx)))
                    {
                        firstErrSrc = "'less than' measure";
                        break;
                    }

                    if (this.raw.IntervalCensored.Item1.Any(xx => test(xx)))
                    {
                        firstErrSrc = "'interval' measure";
                        break;
                    }

                    break;
                }

                if (firstErrSrc != string.Empty)
                {
                    //TODO:  Il faudra vérifier si cette exception peut ou non se produire.
                    string fmt = "{1} and negative data points (as found in {0}) are irreconciliable.";
                    firstErrSrc = string.Format(
                        fmt,
                        firstErrSrc,
                        logNormalDistrn ? "A log-normally distributed outcome" : "Measurement Error specified through a Coefficient of Variation");

                    throw new WEException(firstErrSrc);
                }
            }

            if (logNormalDistrn)
            {
                this.logOfRaw                  = new RawData();
                this.logOfRaw.Y                = this.raw.Y.Log();
                this.logOfRaw.UncensoredSum    = this.logOfRaw.Y.Sum();
                this.logOfRaw.UncensoredSum2   = this.logOfRaw.Y.Sqr().Sum();
                this.logOfRaw.GT               = this.raw.GT.Log();
                this.logOfRaw.LT               = this.raw.LT.Log();
                this.logOfRaw.IntervalCensored = Tuple.Create(
                    this.raw.IntervalCensored.Item1.Log(),
                    this.raw.IntervalCensored.Item2.Log());
            }

            this.WorkersByTag = MeasureList.WorkersByTag;
            for (int iw = 0; iw < this.WorkersByTag.Values.Count; iw++)
            {
                this.WorkersByTag.Values[iw].Ordinal = iw;
            }

            this.WorkerCounts = new int[this.MeasuresByWorker.Keys.Count];
            foreach (KeyValuePair <Worker, List <Measure> > kv in this.MeasuresByWorker)
            {
                this.WorkerCounts[kv.Key.Ordinal] = kv.Value.Count;
            }

            this.WorkersByOrdinal = this.WorkersByTag.Values.ToArray();
        }
Esempio n. 7
0
        public BetweenWorkerModel(MeasureList measures, AbstractBWModelParameters specificParams, McmcParameters mcmcParams = null)
            : base(measures, specificParams.LogNormalDstrn, mcmcParams)
        {
            this.Result = new ModelResult(this, "muOverall", "sigmaWithin", "sigmaBetween");
            if (!measures.IsWorkerInfoComplete)
            {
                this.Messages.AddError("Incomplete worker information.", this.ClassName);
            }

            if (this.Data.MEAny)
            {
                if (this.Data.METhroughSD && !MeasurementErrorSupport.MESD)
                {
                    this.Messages.AddError("No support for METhrougSD.", this.ClassName);
                }
                else if (this.Data.METhroughCV && !MeasurementErrorSupport.me_CV)
                {
                    this.Messages.AddError("No support for METhrougCV.", this.ClassName);
                }
            }

            if (this.Messages.Level == Message.Severity.Error)
            {
                return;
            }

            this.UseUniformPriorOnSds = specificParams.UseUniformPriorOnSds;
            if (this.UseUniformPriorOnSds)
            {
                this.SigmaBetweenRange = Tools.Copy(((BWModelParameters_UniformPriorOnSDs)specificParams).SigmaBetweenRange);
                this.SigmaWithinRange  = ((BWModelParameters_UniformPriorOnSDs)specificParams).SigmaWithinRange;
            }
            else
            {
                this.LogSigmaBetweenMu   = ((BWModelParameters)specificParams).LogSigmaBetweenMu;
                this.LogSigmaBetweenPrec = ((BWModelParameters)specificParams).LogSigmaBetweenPrec;
                this.LogSigmaWithinMu    = ((BWModelParameters)specificParams).LogSigmaWithinMu;
                this.LogSigmaWithinPrec  = ((BWModelParameters)specificParams).LogSigmaWithinPrec;
            }

            //4 paramètres communs aux 2 formes que prend le modèle.
            this.MuOverallLower  = specificParams.MuOverallLower;
            this.MuOverallUpper  = specificParams.MuOverallUpper;
            this.InitMuOverall   = specificParams.InitMuOverall;
            this.InitSigmaWithin = specificParams.InitSigmaWithin;

            this.MuOverallRange = Tools.Combine(this.MuOverallLower, this.MuOverallUpper);
            this.Result.AddWorkerMuChains(Data.WorkersByTag.Values);

            if (!Data.AnyCensored && UseUniformPriorOnSds)
            {
                //
                //# If there is no variation observed within subjects,
                //# then we require a non-null lower bound for sigma.within
                //
                if (SigmaWithinRange[0] == 0)
                {
                    bool problem = (Data.NWorkers == Data.N); //il devrait donc y avoir plus d'une mesure pour au moins un travailleur
                    if (!problem)
                    {
                        problem = true;
                        foreach (KeyValuePair <Worker, List <Measure> > kv in Data.MeasuresByWorker)
                        {
                            if (kv.Value.Count <= 1)
                            {
                                continue;
                            }
                            //at least two measures
                            double vari = kv.Value.Select(x => x.A).Variance();
                            if (double.IsNaN(vari) || (vari == 0))
                            {
                                continue;
                            }
                            else
                            {
                                problem = false;
                                break;
                            }
                        } //foreach
                    }

                    if (problem)
                    {
                        this.Messages.AddError("Lower bound for SigmaWithinRange must be > 0 due to observed null within-subjects variance.", this.ClassName);
                        return;
                    }
                }
            }

            if (specificParams.GetType() == typeof(BWModelParameters))// && (Tools.IsND(this.InitMuOverall) || Tools.IsND(this.InitSigmaWithin)))
            {
                InitialValues initValues = specificParams.DefaultInits(this.Data, includeCensoredData: false);

                if (Tools.IsND(this.InitMuOverall))
                {
                    this.InitMuOverall = initValues.Mu;
                }

                if (Tools.IsND(this.InitSigmaWithin))
                {
                    this.InitSigmaWithin = initValues.SigmaWithin;
                }

                this.NormalizedY = initValues.NormalizedY.ToArray();
            }
        }