Exemplo n.º 1
0
 /// <summary>
 ///   Initializes a new instance of the <see cref="EmpiricalHazardOptions"/> class.
 /// </summary>
 /// 
 public EmpiricalHazardOptions()
 {
     Estimator = HazardEstimator.BreslowNelsonAalen;
 }
Exemplo n.º 2
0
        /// <summary>
        ///   Estimates an Empirical Hazards distribution considering event times and the outcome of the
        ///   observed sample at the time of event, plus additional parameters for the hazard estimation.
        /// </summary>
        ///
        /// <param name="time">The time of occurrence for the event.</param>
        /// <param name="outcome">The outcome at the time of event (failure or censored).</param>
        /// <param name="weights">The weights associated with each event.</param>
        /// <param name="hazard">The hazard estimator to use. Default is <see cref="HazardEstimator.BreslowNelsonAalen"/>.</param>
        /// <param name="survival">The survival estimator to use. Default is <see cref="SurvivalEstimator.FlemingHarrington"/>.</param>
        /// <param name="ties">The method for handling event ties. Default is <see cref="HazardTiesMethod.Efron"/>.</param>
        ///
        /// <returns>The <see cref="EmpiricalHazardDistribution"/> estimated from the given data.</returns>
        ///
        public static EmpiricalHazardDistribution Estimate(double[] time, SurvivalOutcome[] outcome,
                                                           double[] weights, SurvivalEstimator survival, HazardEstimator hazard, HazardTiesMethod ties)
        {
            var dist = new EmpiricalHazardDistribution(survival);

            dist.Fit(time, weights, new EmpiricalHazardOptions
            {
                Outcome   = outcome,
                Estimator = hazard,
                Ties      = ties
            });

            return(dist);
        }
Exemplo n.º 3
0
 /// <summary>
 ///   Estimates an Empirical Hazards distribution considering event times and the outcome of the
 ///   observed sample at the time of event, plus additional parameters for the hazard estimation.
 /// </summary>
 ///
 /// <param name="time">The time of occurrence for the event.</param>
 /// <param name="outcome">The outcome at the time of event (failure or censored).</param>
 /// <param name="hazard">The hazard estimator to use. Default is <see cref="HazardEstimator.BreslowNelsonAalen"/>.</param>
 /// <param name="survival">The survival estimator to use. Default is <see cref="SurvivalEstimator.FlemingHarrington"/>.</param>
 ///
 /// <returns>The <see cref="EmpiricalHazardDistribution"/> estimated from the given data.</returns>
 ///
 public static EmpiricalHazardDistribution Estimate(double[] time, int[] outcome,
                                                    SurvivalEstimator survival, HazardEstimator hazard)
 {
     return(Estimate(time, outcome, survival, hazard, EmpiricalHazardOptions.DefaultTies));
 }
Exemplo n.º 4
0
 /// <summary>
 ///   Estimates an Empirical Hazards distribution considering event times and the outcome of the
 ///   observed sample at the time of event, plus additional parameters for the hazard estimation.
 /// </summary>
 ///
 /// <param name="time">The time of occurrence for the event.</param>
 /// <param name="outcome">The outcome at the time of event (failure or censored).</param>
 /// <param name="weights">The weights associated with each event.</param>
 /// <param name="hazard">The hazard estimator to use. Default is <see cref="HazardEstimator.BreslowNelsonAalen"/>.</param>
 /// <param name="survival">The survival estimator to use. Default is <see cref="SurvivalEstimator.FlemingHarrington"/>.</param>
 /// <param name="ties">The method for handling event ties. Default is <see cref="HazardTiesMethod.Efron"/>.</param>
 ///
 /// <returns>The <see cref="EmpiricalHazardDistribution"/> estimated from the given data.</returns>
 ///
 public static EmpiricalHazardDistribution Estimate(double[] time, int[] outcome,
                                                    double[] weights, SurvivalEstimator survival, HazardEstimator hazard, HazardTiesMethod ties)
 {
     return(Estimate(time, outcome.To <SurvivalOutcome[]>(), weights, survival, hazard, ties));
 }
Exemplo n.º 5
0
 /// <summary>
 ///   Estimates an Empirical Hazards distribution considering event times and the outcome of the
 ///   observed sample at the time of event, plus additional parameters for the hazard estimation.
 /// </summary>
 ///
 /// <param name="time">The time of occurrence for the event.</param>
 /// <param name="outcome">The outcome at the time of event (failure or censored).</param>
 /// <param name="hazard">The hazard estimator to use. Default is <see cref="HazardEstimator.BreslowNelsonAalen"/>.</param>
 /// <param name="survival">The survival estimator to use. Default is <see cref="SurvivalEstimator.FlemingHarrington"/>.</param>
 /// <param name="ties">The method for handling event ties. Default is <see cref="HazardTiesMethod.Efron"/>.</param>
 ///
 /// <returns>The <see cref="EmpiricalHazardDistribution"/> estimated from the given data.</returns>
 ///
 public static EmpiricalHazardDistribution Estimate(double[] time, SurvivalOutcome[] outcome,
                                                    SurvivalEstimator survival, HazardEstimator hazard, HazardTiesMethod ties)
 {
     return(Estimate(time, outcome, null, survival, hazard, ties));
 }
        /// <summary>
        ///   Fits the underlying distribution to a given set of observations.
        /// </summary>
        ///
        /// <param name="observations">The array of observations to fit the model against. The array
        /// elements can be either of type double (for univariate data) or
        /// type double[] (for multivariate data).</param>
        /// <param name="weights">The weight vector containing the weight for each of the samples.</param>
        /// <param name="options">Optional arguments which may be used during fitting, such
        /// as regularization constants and additional parameters.</param>
        ///
        public void Fit(double[] observations, double[] weights, SurvivalOptions options)
        {
            SurvivalOutcome[] outcome         = null;
            double[]          output          = weights;
            HazardEstimator   hazardEstimator = HazardEstimator.BreslowNelsonAalen;
            HazardTiesMethod  ties            = HazardTiesMethod.Breslow;

            if (options != null)
            {
                outcome = options.Outcome;
                var hazardOps = options as EmpiricalHazardOptions;
                if (hazardOps != null)
                {
                    hazardEstimator = hazardOps.Estimator;
                    ties            = hazardOps.Ties;
                }
            }

            if (outcome == null)
            {
                outcome = Vector.Create(observations.Length, SurvivalOutcome.Failed);
            }

            if (output == null)
            {
                output = Vector.Create(observations.Length, 1.0);
            }



            if (hazardEstimator == HazardEstimator.KaplanMeier)
            {
                double[] times   = observations.Distinct();
                double[] hazards = new double[times.Length];
                hazards[hazards.Length - 1] = 1;

                Array.Sort(times);

                // Compute an estimate of the hazard function using
                // the Kaplan-Meier estimator for the survival function

                double r = observations.Length;
                double d = 0; // Number of deaths
                double c = 0; // Number of censor

                for (int j = 0; j < times.Length; j++)
                {
                    double t = times[j];

                    // Survivals until time t
                    r = r - d - c;
                    d = 0;
                    c = 0;

                    for (int i = 0; i < observations.Length; i++)
                    {
                        if (observations[i] == t)
                        {
                            if (outcome[i] == SurvivalOutcome.Failed)
                            {
                                d++; // Failure at time t
                            }
                            else
                            {
                                c++; // Censored at time t
                            }
                        }
                    }

                    hazards[j] = d / r;
                }

                this.init(times, hazards, this.estimator);
                return;
            }
            else if (hazardEstimator == HazardEstimator.BreslowNelsonAalen)
            {
                double[] survivals = new double[observations.Length];

                Sort(ref observations, ref outcome, ref output);

                // Compute an estimate of the cumulative Hazard
                // function using the Breslow-Nelson-Aalen estimator

                double sum = 0;
                double d   = 0;
                for (int i = 0; i < observations.Length; i++)
                {
                    survivals[i] = 1;
                    double t = observations[i];
                    double v = output[i];
                    var    o = outcome[i];

                    sum += v;

                    if (o == SurvivalOutcome.Censored)
                    {
                        d = 0;
                        continue;
                    }

                    if (i > 0 && t != observations[i - 1])
                    {
                        d = 0;
                    }

                    // Count the number of deaths at t
                    if (o == SurvivalOutcome.Failed)
                    {
                        d++; // Deaths at time t
                    }
                    if (i < observations.Length - 1 && t == observations[i + 1] && outcome[i + 1] == SurvivalOutcome.Failed)
                    {
                        continue;
                    }

                    if (ties == HazardTiesMethod.Breslow)
                    {
                        survivals[i] = Math.Exp(-d / sum);
                    }
                    else
                    {
                        if (d == 1)
                        {
                            survivals[i] = Math.Pow(1 - v / sum, 1 / v);
                        }
                        else
                        {
                            survivals[i] = Math.Exp(-d / sum);
                        }
                    }
                }

                // Transform to hazards
                double[] hazards = new double[observations.Length];
                for (int i = 0; i < hazards.Length; i++)
                {
                    hazards[i] = -Math.Log(survivals[i]);
                }

                Array.Sort(observations, hazards);

                this.init(observations, hazards, this.estimator);
                return;
            }

            throw new ArgumentException();
        }
 /// <summary>
 ///   Estimates an Empirical Hazards distribution considering event times and the outcome of the
 ///   observed sample at the time of event, plus additional parameters for the hazard estimation.
 /// </summary>
 ///
 /// <param name="time">The time of occurrence for the event.</param>
 /// <param name="weights">The weights associated with each event.</param>
 /// <param name="hazard">The hazard estimator to use. Default is <see cref="HazardEstimator.BreslowNelsonAalen"/>.</param>
 /// <param name="survival">The survival estimator to use. Default is <see cref="SurvivalEstimator.FlemingHarrington"/>.</param>
 /// <param name="ties">The method for handling event ties. Default is <see cref="HazardTiesMethod.Efron"/>.</param>
 ///
 /// <returns>The <see cref="EmpiricalHazardDistribution"/> estimated from the given data.</returns>
 ///
 public static EmpiricalHazardDistribution Estimate(double[] time, double[] weights = null,
                                                    SurvivalEstimator survival      = EmpiricalHazardOptions.DefaultSurvival,
                                                    HazardEstimator hazard          = EmpiricalHazardOptions.DefaultEstimator, HazardTiesMethod ties = EmpiricalHazardOptions.DefaultTies)
 {
     return(Estimate(time, (SurvivalOutcome[])null, weights, survival, hazard, ties));
 }
Exemplo n.º 8
0
 /// <summary>
 ///   Initializes a new instance of the <see cref="EmpiricalHazardOptions"/> class.
 /// </summary>
 /// 
 public EmpiricalHazardOptions(HazardEstimator estimator, HazardTiesMethod ties, SurvivalOutcome[] outcome)
 {
     Estimator = estimator;
     Outcome = outcome;
     Ties = ties;
 }
Exemplo n.º 9
0
 /// <summary>
 ///   Initializes a new instance of the <see cref="EmpiricalHazardOptions"/> class.
 /// </summary>
 /// 
 public EmpiricalHazardOptions(HazardEstimator estimator, HazardTiesMethod ties, int[] output)
 {
     Estimator = estimator;
     Outcome = output.To<SurvivalOutcome[]>();
     Ties = ties;
 }
Exemplo n.º 10
0
 /// <summary>
 ///   Initializes a new instance of the <see cref="EmpiricalHazardOptions"/> class.
 /// </summary>
 /// 
 public EmpiricalHazardOptions(HazardEstimator estimator, SurvivalOutcome[] output)
 {
     Estimator = estimator;
     Outcome = output;
     Ties = DefaultTies;
 }
Exemplo n.º 11
0
 /// <summary>
 ///   Initializes a new instance of the <see cref="EmpiricalHazardOptions"/> class.
 /// </summary>
 ///
 public EmpiricalHazardOptions()
 {
     Estimator = HazardEstimator.BreslowNelsonAalen;
 }
Exemplo n.º 12
0
 /// <summary>
 ///   Initializes a new instance of the <see cref="EmpiricalHazardOptions"/> class.
 /// </summary>
 ///
 public EmpiricalHazardOptions(HazardEstimator estimator, HazardTiesMethod ties, int[] output)
 {
     Estimator = estimator;
     Outcome   = output.To <SurvivalOutcome[]>();
     Ties      = ties;
 }
Exemplo n.º 13
0
 /// <summary>
 ///   Initializes a new instance of the <see cref="EmpiricalHazardOptions"/> class.
 /// </summary>
 ///
 public EmpiricalHazardOptions(HazardEstimator estimator, HazardTiesMethod ties, SurvivalOutcome[] outcome)
 {
     Estimator = estimator;
     Outcome   = outcome;
     Ties      = ties;
 }
Exemplo n.º 14
0
 /// <summary>
 ///   Initializes a new instance of the <see cref="EmpiricalHazardOptions"/> class.
 /// </summary>
 ///
 public EmpiricalHazardOptions(HazardEstimator estimator, SurvivalOutcome[] output)
 {
     Estimator = estimator;
     Outcome   = output;
     Ties      = DefaultTies;
 }