Learn R Programming

CepLDA (version 1.0.0)

cep.lda: Discriminant Analysis of Time Series in the Presence of Within-Group Spectral Variability

Description

The main program.

Usage

cep.lda(y,x,xNew,L,mcep,nw,k,cv,tol)

Arguments

y
n-vector indicating group membership of training time series.
x
N by n matrix containing n training time series each with length N.
xNew
N by nNew matrix, containing nNew time series whose memberships are predicted.
L
Number of cepstral coefficients used in the lda. If FALSE, cross-validation is used for the data driven selection of L. Default is FALSE.
mcep
Maximum number of cepstral coefficients considerd. Default is set to 10.
nw
Width of tapers used in multitaper spectral estimation. Default is set to 4.
k
Number of tapers used in multitaper spectral estimation. Default is set to 7.
cv
If TRUE, returns results (classes and posterior probabilities) for leave-one-out cross-validation. Note that, if the prior is estimated, the proportions in the whole dataset are used. As with the standard lda function, if used, prediction on a test data set cannot be done and weight functions are not produced (simular to the predict.lda). Default is FALSE.
tol
Tolerance to decide if a matrix is singular; it will reject variables and linear combinations of unit-variance variables whose variance is less than $tol^2$.

Value

List with 5 elements
C.lda
lda output on the cepstral scale. Similar to output of lda(MASS) function.
cep.data
Data frame containing cepstral coefficients and group information from training data.
Lopt
Number of cepstral coefficients used.
lspec
Estimated log-spectral weight functions.
predict
Results of classification. If external data xNew is supplied, these data are classified. If not, biased classification of the training data x is returned. For unbiased leave-out-one cross-validated classification of training data, use cv=TRUE.

References

Krafty, RT (2016) Discriminant Analysis of Time Series in the Presence of Within-Group Spectral Variability. Journal of Time Series Analysis

See Also

predict.ceplda, plot.ceplda, print.ceplda, Lopt.get

Examples

Run this code
## Simulate training data
nj = 50  #number of series in training data
N = 500  #length of time series
traindata1 <- r.cond.ar2(N=N,nj=nj,r.phi1=c(.01,.7),r.phi2=c(-.12,-.06),r.sig2=c(.3,3))
traindata2 <- r.cond.ar2(N=N,nj=nj,r.phi1=c(.5,1.2),r.phi2=c(-.36,-.25),r.sig2=c(.3,3))
traindata3 <- r.cond.ar2(N=N,nj=nj,r.phi1=c(.9,1.5),r.phi2=c(-.56,-.75),r.sig2=c(.3,3))
train <- cbind(traindata1$X,traindata2$X,traindata3$X)
y <- c(rep(1,nj),rep(2,nj),rep(3,nj))

## Fit the discriminant analysis
fit <- cep.lda(y,train)
fit  #displays group means and cepstral weight functions

## Discriminant plot
plot(fit)

## Plot log-spectral weights
par(mfrow=c(1,2))
plot(fit$lspec$frq, fit$lspec$dsc[,1],type='l',xlab="frequency", ylab="log-spectral weights")
plot(fit$lspec$frq, fit$lspec$dsc[,2],type='l',xlab="frequency", ylab="log-spectral weights")

## Bias classification of training data
mean(fit$predict$class == y) #classifictaion rate
table(y,fit$predict$class)

## Fit the discriminant analysis while classifing training data via cross-validation
fit.cv <- cep.lda(y,train, cv=TRUE)
mean(fit.cv$predict$class == y) #classifictaion rate
table(y,fit.cv$predict$class)

## Simulate test data
testdata1 <- r.cond.ar2(N=N,nj=nj,r.phi1=c(.01,.7),r.phi2=c(-.12,-.06),r.sig2=c(.3,3))
testdata2 <- r.cond.ar2(N=N,nj=nj,r.phi1=c(.5,1.2),r.phi2=c(-.36,-.25),r.sig2=c(.3,3))
testdata3 <- r.cond.ar2(N=N,nj=nj,r.phi1=c(.9,1.5),r.phi2=c(-.56,-.75),r.sig2=c(.3,3))
test <- cbind(testdata1$X,testdata2$X,testdata3$X)
yTest <- c(rep(1,nj),rep(2,nj),rep(3,nj))

## Fit discriminant analysis and classify new data
fit.pre <- cep.lda(y,train,test)
mean(fit.pre$predict$class == y)
table(yTest,fit.pre$predict$class)

Run the code above in your browser using DataLab