##
## 1. generate 50 random curves with some covariance structure
## model 1 without outliers
##
cov.fun=function(d,k,c,mu){
k*exp(-c*d^mu)
}
n=50
p=30
t=seq(0,1,len=p)
d=dist(t,upper=TRUE,diag=TRUE)
d.matrix=as.matrix(d)
#covariance function in time
t.cov=cov.fun(d.matrix,1,1,1)
# Cholesky Decomposition
L=chol(t.cov)
mu=4*t
e=matrix(rnorm(n*p),p,n)
ydata = mu+t(L)%*%e
#functional boxplot
oldpar <- par(no.readonly=TRUE)
fbplot(ydata,method='MBD',ylim=c(-11,15))
# The same using boxplot.fd
boxplot.fd(ydata, method='MBD', ylim=c(-11, 15))
# same with default ylim
boxplot.fd(ydata)
##
## 2. as an fd object
##
T = dim(ydata)[1]
time = seq(0,T,len=T)
ybasis = create.bspline.basis(c(0,T), 23)
Yfd = smooth.basis(time, ydata, ybasis)$fd
boxplot(Yfd)
##
## 3. as an fdPar object
##
Ypar <- fdPar(Yfd)
boxplot(Ypar)
##
## 4. Smoothed version
##
Ysmooth <- smooth.fdPar(Yfd)
boxplot(Ysmooth)
##
## 5. model 2 with outliers
##
#magnitude
k=6
#randomly introduce outliers
C=rbinom(n,1,0.1)
s=2*rbinom(n,1,0.5)-1
cs.m=matrix(C*s,p,n,byrow=TRUE)
e=matrix(rnorm(n*p),p,n)
y=mu+t(L)%*%e+k*cs.m
#functional boxplot
fbplot(y,method='MBD',ylim=c(-11,15))
par(oldpar)
Run the code above in your browser using DataLab