Learn R Programming

SpatioTemporal (version 1.1.2)

SVDsmooth: Smooth Basis Functions for Data Matrix with Missing Values

Description

Function that computes smooth functions for a data matrix with missing values, as described in Fuentes et. al. (2006), or does cross validation to determine a suitable number of basis functions. The function uses SVDmiss to complete the matrix and then computes smooth basis functions by applying smooth.spline to the SVD of the completed data matrix.

Usage

SVDsmooth(X, n.basis = min(2, dim(X)[2]),
    date.ind = NULL, scale = TRUE, niter = 100,
    conv.reldiff = 0.001, df = NULL, spar = NULL)

SVDsmoothCV(X, n.basis, ...)

Arguments

X
Data matrix, with missing values marked by NA. Rows and/or columns that are completely missing will be dropped (with a message), for the rows the smooths will be interpolated using
n.basis
Number of smooth basis functions to compute, will be passed as ncomp to SVDmiss; for SVDsmoothCV a vector with the different number of basis functions to evaluate.
date.ind
Vector giving the observation time of each row in X, used as x in smooth.spline when computing the smooth basis functions. If missing
scale
If TRUE, will use scale to scale X before calling SVDmiss.
niter,conv.reldiff
Controls convergence, passed to SVDmiss.
df,spar
The desired degrees of freedom/smoothing parameter for the spline, see smooth.spline
...
Additional parameters passed to SVDsmooth; i.e. date.ind, scale, niter, conv.reldiff, df, and spar.

Value

  • Depends on the function:
  • SVDsmoothA matrix where each column is a smooth basis function based on the SVD of the completed data matrix. The left most column contains the smooth of the most important SVD.
  • SVDsmoothCVA list of class SVDcv with components: [object Object],[object Object],[object Object]

Details

SVDsmoothCV uses leave-one-column-out cross-validation; holding one column out from X, calling SVDsmooth, and then regressing the held out column on the resulting smooth functions. Cross-validation statistics computed include RMSE, R-squared and BIC.

References

M. Fuentes, P. Guttorp, and P. D. Sampson. (2006) Using Transforms to Analyze Space-Time Processes in Statistical methods for spatio-temporal systems (B. Finkenst�dt, L. Held, V. Isham eds.) 77-150

See Also

Other data matrix: createDataMatrix, mesa.data.raw, SVDmiss

Other SVDcv methods: plot.SVDcv, print.SVDcv

Other SVD for missing data: calcSmoothTrends, plot.SVDcv, print.SVDcv, SVDmiss, updateSTdataTrend

Examples

Run this code
##create a data matrix
t <- seq(0,4*pi,len=50)
X.org <- cbind(cos(t),sin(2*t)) %*% matrix(rnorm(20),2,10)

##add some normal errors
X <- X.org + .25*rnorm(length(X.org))
##and mark some data as missing
X[runif(length(X))<.25] <- NA

##Ensure that we have complet columns/rows
while( any(rowSums(is.na(X))==dim(X)[2]) || any(colSums(is.na(X))==dim(X)[1]) ){
  X <- X.org + .25*rnorm(length(X.org))
  X[runif(length(X))<.25] <- NA
}

##compute two smooth basis functions
res <- SVDsmooth(X, n.basis=2, niter=100)

##plot the two smooth basis functions
par(mfcol=c(3,2), mar=c(4,4,.5,.5))
plot(t, res[,1], ylim=range(res), type="l")
lines(t, res[,2], col=2)
##and some of the data fitted to the smooths
for(i in 1:5){
  plot(t, X[,i])
  lines(t, predict.lm(lm(X[,i]~res), data.frame(res)) )
  lines(t, X.org[,i], col=2)
}

##compute cross-validation for 1 to 4 basis functions
res.cv <- SVDsmoothCV(X, n.basis=1:4, niter=100)

##study cross-validation results
print(res.cv)

##plot cross-validation statistics
plot(res.cv)
##plot the BIC for each column
plot(res.cv, pairs=TRUE)

Run the code above in your browser using DataLab