Kernel density estimation in R

Similar documents
Kernel density estimation in R

Right-truncated data. STAT474/STAT574 February 7, / 44

Chapter 7: Hypothesis testing

Introduction to BGPhazard

Multistate Modeling and Applications

Lecture 1: Probability Fundamentals

Estimation for Modified Data

Statistical Inference and Methods

Bayesian inference. Fredrik Ronquist and Peter Beerli. October 3, 2007

Textbook: Survivial Analysis Techniques for Censored and Truncated Data 2nd edition, by Klein and Moeschberger

Practice Exam 1. (A) (B) (C) (D) (E) You are given the following data on loss sizes:

Nonparametric Model Construction

Physics 509: Bootstrap and Robust Parameter Estimation

Exam 2 Practice Questions, 18.05, Spring 2014

COS513 LECTURE 8 STATISTICAL CONCEPTS

Bayesian Methods for Machine Learning

Kaplan-Meier in SAS. filename foo url " data small; infile foo firstobs=2; input time censor; run;

Spatially Adaptive Smoothing Splines

Survival Analysis. Stat 526. April 13, 2018

Modelling geoadditive survival data

Statistics 262: Intermediate Biostatistics Non-parametric Survival Analysis

27 : Distributed Monte Carlo Markov Chain. 1 Recap of MCMC and Naive Parallel Gibbs Sampling

ST745: Survival Analysis: Nonparametric methods

Bayesian Regression Linear and Logistic Regression

Statistics: Learning models from data

Markov Chain Monte Carlo methods

Statistical Methods in Particle Physics

Machine Learning 4771

Cox s proportional hazards model and Cox s partial likelihood

β j = coefficient of x j in the model; β = ( β1, β2,

PhD course in Advanced survival analysis. One-sample tests. Properties. Idea: (ABGK, sect. V.1.1) Counting process N(t)

Pattern Recognition and Machine Learning. Bishop Chapter 2: Probability Distributions

Non-Parametric Bayes

Survival Analysis Math 434 Fall 2011

STA 4273H: Sta-s-cal Machine Learning

1 Probabilities. 1.1 Basics 1 PROBABILITIES

Physics 403. Segev BenZvi. Credible Intervals, Confidence Intervals, and Limits. Department of Physics and Astronomy University of Rochester

Step-Stress Models and Associated Inference

In contrast, parametric techniques (fitting exponential or Weibull, for example) are more focussed, can handle general covariates, but require

Probability calculus and statistics

Bayesian Methods in Multilevel Regression

More on Estimation. Maximum Likelihood Estimation.

Econ 582 Nonparametric Regression

12 - Nonparametric Density Estimation

CPSC 540: Machine Learning

Gaussian processes. Chuong B. Do (updated by Honglak Lee) November 22, 2008

Lecture 22 Survival Analysis: An Introduction

Chapter 4 Fall Notations: t 1 < t 2 < < t D, D unique death times. d j = # deaths at t j = n. Y j = # at risk /alive at t j = n

Conditional probabilities and graphical models

Analysis of Time-to-Event Data: Chapter 6 - Regression diagnostics

Introduction to Probabilistic Machine Learning

CONTENTS OF DAY 2. II. Why Random Sampling is Important 10 A myth, an urban legend, and the real reason NOTES FOR SUMMER STATISTICS INSTITUTE COURSE

Censoring and Truncation - Highlighting the Differences

Introduction to Bayesian Statistics 1

2. A Basic Statistical Toolbox

One-parameter models

Introduction to Bayesian Methods. Introduction to Bayesian Methods p.1/??

Lecture 6 PREDICTING SURVIVAL UNDER THE PH MODEL

Bayesian Inference: Concept and Practice

Nonparametric Density Estimation

Frailty Modeling for Spatially Correlated Survival Data, with Application to Infant Mortality in Minnesota By: Sudipto Banerjee, Mela. P.

LECTURE NOTE #3 PROF. ALAN YUILLE

Bayesian Estimation An Informal Introduction

The Weibull in R is actually parameterized a fair bit differently from the book. In R, the density for x > 0 is

Uniformly Most Powerful Bayesian Tests and Standards for Statistical Evidence

Machine Learning Linear Classification. Prof. Matteo Matteucci

Learning Sequence Motif Models Using Expectation Maximization (EM) and Gibbs Sampling

Investigation of Possible Biases in Tau Neutrino Mass Limits

A Bayesian Approach to Phylogenetics

Survival analysis in R

Continuous random variables

6.867 Machine Learning

The posterior - the goal of Bayesian inference

MACHINE LEARNING INTRODUCTION: STRING CLASSIFICATION

Bayesian Inference and MCMC

Survival Analysis: Weeks 2-3. Lu Tian and Richard Olshen Stanford University

Inference for a Population Proportion

A.I. in health informatics lecture 2 clinical reasoning & probabilistic inference, I. kevin small & byron wallace

Introduction to Statistical Analysis

Lies, damned lies, and statistics in Astronomy

Lecture 5: Bayes pt. 1

36-463/663: Hierarchical Linear Models

Reading for Lecture 6 Release v10

Lecture 5. G. Cowan Lectures on Statistical Data Analysis Lecture 5 page 1

Bayesian Inference. Chapter 2: Conjugate models

6.867 Machine Learning

Introduc)on to Bayesian Methods

Bayesian Models in Machine Learning

Bayesian Regularization

Empirical Risk Minimization is an incomplete inductive principle Thomas P. Minka

Quantitative Understanding in Biology 1.7 Bayesian Methods

STAT Sample Problem: General Asymptotic Results

Hypothesis Testing. Part I. James J. Heckman University of Chicago. Econ 312 This draft, April 20, 2006

Exam C Solutions Spring 2005

Principles of Bayesian Inference

Whaddya know? Bayesian and Frequentist approaches to inverse problems

DS-GA 1003: Machine Learning and Computational Statistics Homework 7: Bayesian Modeling

Introduction to Bayesian Methods

Markov Chain Monte Carlo (MCMC)

Introduction to Applied Bayesian Modeling. ICPSR Day 4

Transcription:

Kernel density estimation in R Kernel density estimation can be done in R using the density() function in R. The default is a Guassian kernel, but others are possible also. It uses it s own algorithm to determine the bin width, but you can override and choose your own. If you rely on the density() function, you are limited to the built-in kernels. If you want to try a different one, you have to write the code yourself. STAT474/STAT574 February 25, 2015 1 / 49

Kernel density estimation in R: effect of bandwidth for rectangular kernel STAT474/STAT574 February 25, 2015 2 / 49

Kernel density estimation in R Note that exponential densities are a bit tricky to estimate to using kernel methods. Here is the default behavior estimating the density for exponential data. > x <- rexp(100) > plot(density(x)) STAT474/STAT574 February 25, 2015 3 / 49

Kernel density estimation in R: exponential data with Gaussian kernel STAT474/STAT574 February 25, 2015 4 / 49

Violin plots: a nice application of kernel density estimation Violin plots are an alternative to boxplots that show nonparametric density estimates of the distribution in addition to the median and interquartile range. The densities are rotated sideways to have a similar orientation as a box plot. > x <- rexp(100) > install.packages("vioplot") > library(vioplot) > x <- vioplot(x) STAT474/STAT574 February 25, 2015 5 / 49

Kernel density estimation in R: violin plot STAT474/STAT574 February 25, 2015 6 / 49

Kernel density estimation R: violin plot The violin plot uses the function sm.density() rather than density() for the nonparametric density estimate, and this leads to smoother density estimates. If you want to modify the behavior of the violin plot, you can copy the original code to your own function and change how the nonparametric density estimate is done (e.g., replacing sm.density with density, or changing the kernel used). STAT474/STAT574 February 25, 2015 7 / 49

Kernel density estimation in R: violin plot STAT474/STAT574 February 25, 2015 8 / 49

Kernel density estimation in R: violin plot STAT474/STAT574 February 25, 2015 9 / 49

Kernel density estimation in R: violin plot > vioplot function (x,..., range = 1.5, h = NULL, ylim = NULL, names = NULL, horizontal = FALSE, col = "magenta", border = "black", lty = 1, lwd = 1, rectcol = "black", colmed = "white", pchmed = 19, at, add = FALSE, wex = 1, drawrect = TRUE) { datas <- list(x,...) n <- length(datas) if (missing(at)) at <- 1:n upper <- vector(mode = "numeric", length = n) lower <- vector(mode = "numeric", length = n) q1 <- vector(mode = "numeric", length = n) q3 <- vector(mode = "numeric", length = n)... args <- list(display = "none") if (!(is.null(h))) args <- c(args, h = h) for (i in 1:n) {... smout <- do.call("sm.density", c(list(data, xlim = est.xlim), args)) STAT474/STAT574 February 25, 2015 10 / 49

Kernel density estimation There are lots of popular Kernel density estimates, and statisticians have put a lot of work into establishing their properties, showing when some Kernels work better than others (for example, using mean integrated square error as a criterion), determining how to choose bandwidths, and so on. In addition to the Guassian, common choices for the hazard function include Uniform, K(u) = 1/2 I ( 1 u 1) Epanechnikov, K(u) =.75(1 u 2 )I ( 1 u 1) biweight, K(u) = 15 16 (1 x 2 ) 2 I ( 1 u 1) STAT474/STAT574 February 25, 2015 11 / 49

Kernel-smoothed hazard estimation To estimate a smoothed version of the hazard function using a kernal method, first pick a kernel, then use ĥ = 1 b D ( t ti K b i=1 ) H(t i ) where D is the number of death times and b is the babdwidth (instead of h). A common notation for bandwidth is h, but we use b because h is used for the hazard function. Also Ĥ(t) is the Nelson-Aalen estimator of the cumulative hazard function: { 0, if t t 1 H(t) = t i t d i Y i, if t > t 1 STAT474/STAT574 February 25, 2015 12 / 49

Kernel-smoothed hazard estimation The variance of the smoothed hazard is D 2 σ [ĥ(t)] = b 2 i=1 [ K ( )] t 2 ti V [ H(t)] b STAT474/STAT574 February 25, 2015 13 / 49

Asymmetric kernels A difficulty that we saw with the exponential also can occur here, the estimated hazard can give negative values. Consequently, you can use an asymmetric kernel instead for small t. For t < b, let q = t/b. A similar approach can be used for large t, when t D b < t < t D. In this case, you can use q = (t D t)/b and replace x with x in the kernal density estimate for these larger times. STAT474/STAT574 February 25, 2015 14 / 49

Asymmetric kernels STAT474/STAT574 February 25, 2015 15 / 49

Asymmetric kernels STAT474/STAT574 February 25, 2015 16 / 49

Confidence intervals A pointwise confidence interval can be obtained with lower and upper limits ( [ ĥ(t) exp Z1 α/2σ(ĥ(t)) ] [ Z1 α/2, ĥ(t) ĥ(t) exp σ(ĥ(t)) ]) ĥ(t) Note that the confidence interfal is really a confidence interval for the smoothed hazard function and not a confidence interval for the actual hazard function, making it difficult to interpret. In particular, the confidence interval will depend on the both the kernel and the bandwidth. Coverage probabilities for smoothed hazard estimates (the proportion of times the confidence interval includes the true hazard rate) appears to have ongoing research. STAT474/STAT574 February 25, 2015 17 / 49

Asymmetric kernels STAT474/STAT574 February 25, 2015 18 / 49

Effect of bandwidth STAT474/STAT574 February 25, 2015 19 / 49

Effect of bandwidth Because the bandwidth has a big impact, we somehow want to pick the optimal bandwidth. An idea is to minimize the squared area between the true hazard function and estimated hazard function. This squared area between the two functions is called the Mean Integrated Squared Error (MISE): MISE(b) = E = E τu τ L τu τ L [ĥ(u) h(u)]2 du ĥ 2 (u) du 2E τu τ L ĥ(u)h(u) du + f (h(u)) The last term doesn t depend on b so it is sufficient to minimize the function ignoring the last term. The first term can be estimated by τu τ L ĥ 2 (u) du, which can be estimated using the trapezoid rule from calculus. STAT474/STAT574 February 25, 2015 20 / 49

Effect of bandwidth The second term can be approximated by 1 ( ) t ti K H(t i ) H(t j ) b b i j summing over event times between τ L and τ U. Minimizing MISE is can be done approximately by minimizing g(b) = ( ) ui+1 u i 2 [ĥ2 (u i ) ĥ2 (u i+1 )] 2 ( ) t ti K H(t i ) H(t j ) b b i The minimization can be done numerically by plugging in different values of b and evaluating. i j STAT474/STAT574 February 25, 2015 21 / 49

Effect of bandwidth STAT474/STAT574 February 25, 2015 22 / 49

Effect of bandwidth For this example, the minimum occurs around b = 0.17 to b = 0.23 depending on the kernel. Generally, there is a trade-off with smaller bandwidths having smaller bias but higher variance, and larger bandwidths (more smoothing) having less variance but greater bias. Measuring the quality of bandwidths and kernels using MISE is standard in kernel density estimation (not just survival analysis). Bias here means that E[ĥ(t)] h(t). STAT474/STAT574 February 25, 2015 23 / 49

Section 6.3: Estimation of Excess Mortality The idea for this topic is to compare the survival curve or hazard rate for one group against a reference group, particular if the non-reference group is thought to have higher risk. The reference group might come from a much larger sample, so that its survival curve can be considered to be known. An example is to compare the mortality for psychiatric patients against the general population. You could use census data to get the lifetable for the general population, and determine the excess mortality for the psychiatric patients. Two approaches are: a multiplicative model, and an additive model. In the multiplicative model, belonging to a particular group multiplies the hazard rate by a factor. In the additive model, belonging to a particular group adds a factor to the hazard rate. STAT474/STAT574 February 25, 2015 24 / 49

Excess mortality For the multiplicative model, if there is a reference hazard rate of θ j (t) for the jth individual in a study (based on sex, age, ethnicity, etc.), then due to other risk factors, the hazard rate for the jth individual is h j (t) = β(t)θ j (t) where β(t) 1 implies that the hazard rate is higher than the reference hazard. We define B(t) = t 0 β(u) du as the cumulative relative excess mortality. STAT474/STAT574 February 25, 2015 25 / 49

Excess mortality Note that d dt = β(t). To estimate B(t), let Y j(t) = 1 if the jth individual is at risk at time t. Otherwise, let Y j (t) = 0. Here Y j (t) is defined for left-truncated and right-censored data. Let Q(t) = n θ j (t)y j (t) j=1 where n is the sample size. Then we estimate B(t) by B(t) = ti t d i Q(t i ) This value is comparing the actual number of deaths that have occurred by time t i with the expected number of deaths based on the hazard rate and number of patients available to have died. STAT474/STAT574 February 25, 2015 26 / 49

Excess mortality The variance is estimated by V [ B(t)] = ti t d i Q(t i ) 2 β(t) can be estimated by slope of B(t), which can be improved by using kernel-smoothing methods on B(t). STAT474/STAT574 February 25, 2015 27 / 49

Excess mortality For the additive model, the hazard is h j (t) = α(t) + θ j (t) Similarly to the multiplicative model, we estimate the cumulative excess mortality A(t) = t 0 α(u) du In this case the expected cumulative hazard rate is n t Θ(t) = θ j (u) Y j(u) Y (u) du where is the number at risk at time u. j=1 0 Y (u) = n Y j (u) j=1 STAT474/STAT574 February 25, 2015 28 / 49

Excess mortality The estimated excess mortality is Â(t) = t i t d i Y i Θ(t) where the first term is the Nelson-Aalen estimator of the cumulative hazard. The variance is V [Â(t)] = ti t d i Y (t) 2 STAT474/STAT574 February 25, 2015 29 / 49

Excess mortality For a lifetable where times are every year, you can also compute Θ(t) = Θ(t 1) + t λ(a j + t 1) Y (t) where a j is the age at the beginning of the study for patient j and λ is the reference hazard. Note that Θ(t) is a smooth function of t while Â(t) is has jumps. STAT474/STAT574 February 25, 2015 30 / 49

Excess mortality A more general model is to combine multiplicative and additive components, using h j (t) = β(t)θ j (t) + α(t) which is done in chapter 10. STAT474/STAT574 February 25, 2015 31 / 49

Example: Iowa psychiatric patients As an example, starting with the multiplicative model, consider 26 psychiatric patients from Iowa, where we compare to census data. STAT474/STAT574 February 25, 2015 32 / 49

Iowa psychiatric patients STAT474/STAT574 February 25, 2015 33 / 49

Census data for Iowa STAT474/STAT574 February 25, 2015 34 / 49

Excess mortality for Iowa psychiatric patients STAT474/STAT574 February 25, 2015 35 / 49

Excess mortality for Iowa psychiatric patients STAT474/STAT574 February 25, 2015 36 / 49

Excess mortality The cumulative excess mortality is difficult to interpret. The slope of the curve is more meaningful. The curve is relatively linear. If we consider age 10 to age 30, the curve goes from roughly 50 to 100, suggesting a slope of (100 50)/(30 10) = 2.5, so that patients aged 10 to 30 had a roughly 2.5 times higher chance of dying. This is a fairly low-risk age group, for which suicide is high risk factor. Note that the census data might include psychiatric patients who have committed suicide, so we might be comparing psychiatric patients to the general population which includes psychiatric patients, as opposed to psychiatric patients compared to people who have not been psychiatric patients, so this might bias results. STAT474/STAT574 February 25, 2015 37 / 49

Survival curves You can use the reference distribution to inform the survival curve instead of just relying on the data. This results in an adjusted or corrected survival curve. Let S (t) = exp[ Θ(t)] (or use the cumulative hazard based on multiplying the reference hazard by the excess harzard) and let Ŝ(t) be the standard Kaplan-Meier survival curve (using only the data, not the reference survival data). Then S c (t) = Ŝ(t)/S (t) is the corrected survival function. The estimate can be greater than 1, in which case the estimate can be set to 1. Typically, S (t) is less than 1, so that dividing by this quantity increases the estimated survival probabilities. This is somewhat similar in Bayesian statististics to the use of the prior, using the reference survival times as a prior for what the psychiatric patients are likely to experience. Consequently, the adjusted survival curve is in between the kaplan-meier (data only) estimate, and the reference survival times. STAT474/STAT574 February 25, 2015 38 / 49

Survival curves STAT474/STAT574 February 25, 2015 39 / 49

Survival curves STAT474/STAT574 February 25, 2015 40 / 49

Bayesian nonparametric survival analysis The previous example leads naturally to Bayesian nonparametric survival analysis. Here we have prior information (or prior beliefs) about the shape of the survival curve (such as based on a reference survival function). The survival curve based on this previous information is combined with the likelihood of the survival data to produce a posterior estimate of the survival function. Reasons for using a prior are: (1) to take advantage of prior information or expertise of someone familiar with the type of data, (2) to get a reasonable estimate when the sample size is small. STAT474/STAT574 February 25, 2015 41 / 49

Bayesian survival analysis In frequentist statistical methods, parameters are treated as fixed, but unknown, and an estimator is chosen to estimate the parameters based on the data and a model (including model assumptions). Parameters are unknown, but are treated as not being random. Philosophically, the Bayesian approach is to try to model all uncertainty using random variables. Uncertainty exists both in the form of the data that would arise from a probability model as well as the parameters of the model itself, so both observations and parameters are treated as random. Typically, the observations have a distribution that depends on the parameters, and the parameters themselves come from some other distribution. Bayesian models are therefore often hierarchical, often with multiple levels in the hierarchy. STAT474/STAT574 February 25, 2015 42 / 49

Bayesian survival analysis For survival analysis, we think of the (unknown) survival curve as the parameter. From a frequentist point of view, survival probabilities determine the probabilities of observing different death times, but there are no probabilities of the survival function itself. From a Bayesian point of view, you can imagine that there was some stochastic process generating survival curves according to some distribution on the space of survival curves. One of the survival curves happened to occur for the population we are studying. Once that survival function was chosen, event times could occur according to that survival curve. STAT474/STAT574 February 25, 2015 43 / 49

Bayesian survival analysis STAT474/STAT574 February 25, 2015 44 / 49

Bayesian survival analysis We imagine that there is a true survival curve S(t), and an estimated survival curve, Ŝ(t). We define a loss function as L(S, Ŝ) = [Ŝ(t) S(t)]2 dt 0 The function Ŝ that minimizes the expected value of the loss function is called the posterior mean, which is used to estimate the survival function. STAT474/STAT574 February 25, 2015 45 / 49

A prior for survival curves A typical way to assign a prior on the survival function is to use a Dirichlet process prior. For a Dirichlet process, we partition the real line into intervals A 1,..., A k, so that P(X A i ) = W i. The numbers (W 1,..., W k ) have a k-dimension Dirichlet distribution with parameters α 1,..., α k. For this to be a Dirichlet distribution, we must have Z i, i = 1,..., k are independent gamma random variables with shape parameter α i and W i = W i Z i k i=1 Z i. By construction, the random numbers s are between 0 and 1 and sum to 1, so when interpreted as probabilities, they form a discrete probability distribution. Essentially, we can think of a Dirichlet distribution as a distribution on unfair dice with k sides. We want to make a die that has k sides, and we want the probabilities of each side to be randomly determined. How fair or unfair the die is partly depends on the α parameters and partly depends on chance itself. STAT474/STAT574 February 25, 2015 46 / 49

A prior for survival curves We can also think of the Dirichlet distrbibution as generalizing the beta distribution. A beta random variable is a number between 0 and 1. This number partitions the interval [0,1] into two pieces, [0, x) and [x, 1]. A Dirichlet random variable partitions the interval into k regions, using k 1 values between 0 and 1. The joint density for these k 1 values is f (w 1,..., w k 1 ) = Γ[α 1 + + α k ) Γ(α 1 ) Γ(α k ) [ k 1 i=1 w α i 1 i ] [ ] k 1 αk 1 1 w i which reduces to a beta density with parameters (α 1, α 2 ) when k = 2. i=1 STAT474/STAT574 February 25, 2015 47 / 49

Assigning a prior To assign a prior on the space of survival curves, first assume an average survival function, S 0 (t). The Dirichlet prior determines when the jumps occur, and the exponential curve gives the decay of the curve between jumps. Simulated survival curves when S 0 (t) = e 0.1t and α = 5S 0 (t) are given below. STAT474/STAT574 February 25, 2015 48 / 49

Bayesian survival analysis STAT474/STAT574 February 25, 2015 49 / 49

Bayesian survival analysis Other approaches are to have a prior for the cumulative hazard function and to use Gibb s sampling or Markov chain Monte Carlo. These topics would be more appropriate to cover after a class in Bayes methods. STAT474/STAT574 February 25, 2015 50 / 49