# NOT RUN {
#
# Example 1: The refinery data
#
N <- 194
TimeData <- RefineryData[,1]
TrayData <- RefineryData[,2]
ValvData <- RefineryData[,3]
# Define the List array containing the tray data
TrayList <- list(argvals=RefineryData[,1], y=RefineryData[,2])
TrayDataList <- vector("list",1)
TrayDataList[[1]] <- TrayList
# construct the basis object for tray variable
# Order 5 spline basis with four knots at the 67
# to allow discontinuity in the first derivative
# and 15 knots between 67 and 193
Traynorder <- 5
Traybreaks <- c(0, rep(67,3), seq(67, 193, len=15))
Traynbasis <- 22
TrayBasis <- create.bspline.basis(c(0,193), Traynbasis, Traynorder,
Traybreaks)
# Set up the basis list for the tray variable
TrayBasisList <- vector("list",1)
TrayBasisList[[1]] <- TrayBasis
# Both alpha and beta coefficient functions will be constant.
# Define the constant basis
conbasis <- create.constant.basis(c(0,193))
betafdPar <- fdPar(conbasis)
alphafdPar <- fdPar(conbasis)
# Construct a step basis (order 1) and valve level
Valvbreaks <- c(0,67,193)
Valvnbasis <- 2
Valvnorder <- 1
Valvbasis <- create.bspline.basis(c(0,193), Valvnbasis, Valvnorder, Valvbreaks)
# smooth the valve data
Valvfd <- smooth.basis(TimeData, ValvData, Valvbasis)$fd
# Set up the model list for the tray variable
# Define single homogeneous term
# Xterm Fields: funobj parvec estimate variable deriv. factor
XTerm <- make.Xterm(betafdPar, 0.04, TRUE, 1, 0, -1)
XList <- vector("list", 1)
XList[[1]] <- XTerm
# Define the single forcing term
# Fterm Fields: funobj parvec estimate Ufd factor
FTerm <- make.Fterm(alphafdPar, 1.0, TRUE, Valvfd, 1)
FList <- vector("list", 1)
FList[[1]] <- FTerm
# Define the single differential equation in the model
TrayVariable <- make.Variable(XList=XList, FList=FList,
name="Tray level", order=1)
# Set up the model List array
TrayModelList <- vector("list",1)
TrayModelList[[1]] <- TrayVariable
# Run a check on TrayVariableList and make the modelList object
checkModelList <- checkModel(TrayBasisList, TrayModelList, summarywrd=TRUE)
TrayModelList <- checkModelList$model
nparam <- checkModelList$nparam
# Evaluate the fit to the data given the initial parameter estimates (0 and 0)
# This also initializes the four-way tensors so that they are not re-computed
# for subsquent analyses.
rhoVec <- 0.5
Data2LDList <- Data2LD(TrayDataList, TrayBasisList, TrayModelList, rhoVec)
MSE <- Data2LDList$MSE # Mean squared error for fit to data
DMSE <- Data2LDList$DpMSE # gradient with respect to parameter values
# These parameters control the optimization.
dbglev <- 1 # debugging level
iterlim <- 50 # maximum number of iterations
convrg <- c(1e-8, 1e-4) # convergence criterion
# This sets up an increasing sequence of rho values
gammavec <- 0:7
rhoMat <- as.matrix(exp(gammavec)/(1+exp(gammavec)),length(gammavec),1)
nrho <- length(rhoMat)
dfesave <- matrix(0,nrho,1)
gcvsave <- matrix(0,nrho,1)
MSEsave <- matrix(0,nrho,1)
thesave <- matrix(0,nrho,nparam)
# This initializes the list object containing coefficient estimates
TrayModelList.opt <- TrayModelList
# Loop through values of rho.
# for test purposes, only the first value rho = 0.5 is used here
for (irho in 1:1) {
rhoi <- rhoMat[irho,]
print(paste("rho <- ",round(rhoi,5)))
Data2LDResult <-
Data2LD.opt(TrayDataList, TrayBasisList, TrayModelList.opt,
rhoi, convrg, iterlim, dbglev)
theta.opti <- Data2LDResult$thetastore
TrayModelList.opt <- modelVec2List(TrayModelList.opt, theta.opti)
Data2LDList <- Data2LD(TrayDataList, TrayBasisList, TrayModelList.opt, rhoi)
MSE <- Data2LDList$MSE
df <- Data2LDList$df
gcv <- Data2LDList$gcv
ISE <- Data2LDList$ISE
Var.theta <- Data2LDList$Var.theta
thesave[irho,] <- theta.opti
dfesave[irho] <- df
gcvsave[irho] <- gcv
MSEsave[irho] <- MSE
}
# }
Run the code above in your browser using DataLab