Archive for December, 2012

December 16, 2012

Override a non-generic method in S3 OOP system

In R OOP, you can override a generic method for your custom class. For example, you can define print() and plot() methods for an class Foo. It is also easy to overrisk a non-generic method if you use S4 OOP system, through the S4 function setGeneric(). But S3 does not have a function equivalent to setGeneric().

Suppose you want to write a method sample() for class Foo. As the built-in sample() function is not generic, simply defining sample.Foo() will break the original sample(). For example, calling


will result in the following error message:

Error in UseMethod(“sample”) :
no applicable method for ‘sample’ applied to an object of class “c(‘double’, ‘numeric’)”

Here is the correct way to override a non-generic method in S3. First, you copy the non-generic method to the default method for the generic method you want to create. Then, you create the generic method and the method for class Foo.

sample.default = sample
sample <- function(obj, ...){
sample.Foo <- function(obj, ...){
December 6, 2012

Rank 1 update to Cholesky factorization in R

Rank 1 update can be achieved in Matlab with the built-in function cholupdate(). More details about the function can be found here:

In R, the recommended Matrix package has added a function updown() to handle rank 1 update. However, the documentation of that function is not easy to understand, and I find the example given is misleading. That prompted me to replicate the Matlab example with the updown() function. The resulting code snippet is shown below.

A <- symmetric.pascal.matrix(4)
R <- chol(A)

x <- c(0, 0, 0, 1)

A + x%*%t(x)

R <- Cholesky(Matrix(A, sparse=T))
x <- Matrix(x)
R1.sparse <- updown('+', x, R)
R1 <- as(R1.sparse, 'Matrix')

x <- Matrix(c(0, 0, 0, 1/sqrt(2)))
R1.sparse <- updown('-', x, R)
R1 <- as(R1.sparse, 'Matrix')

Note that matrices have to be in the sparse representation to be used in the updown() function.

December 4, 2012

Estimating the intensity of ward admission and its effect on emergency department access block


Emergency department access block is an urgent problem faced by many public hospitals today. When access block occurs, patients in need of acute care cannot access inpatient wards within an optimal time frame. A widely held belief is that access block is the end product of a long causal chain, which involves poor discharge planning, insufficient bed capacity, and inadequate admission intensity to the wards. This paper studies the last link of the causal chain—the effect of admission intensity on access block, using data from a metropolitan hospital in Australia. We applied several modern statistical methods to analyze the data. First, we modeled the admission events as a nonhomogeneous Poisson process and estimated time-varying admission intensity with penalized regression splines. Next, we established a functional linear model to investigate the effect of the time-varying admission intensity on emergency department access block. Finally, we used functional principal component analysis to explore the variation in the daily time-varying admission intensities. The analyses suggest that improving admission practice during off-peak hours may have most impact on reducing the number of ED access blocks.


Tags: ,