| Table of Contents | Data and model | MLE | REMLE | References |
Authors: Andrej Gajdoš, Jozef Hanč, Martina Hančová
Faculty of Science, P. J. Šafárik University in Košice, Slovakia
email: andrej.gajdos@student.upjs.sk
MLE, REMLE for electricity consumption 2
R-based computational tools - fdslrm, nlme, MMEinR(mixed), sommer
To get back to the table of contents, use the Home key.
In this FDSLRM application, we model the econometric time series data set, representing the hours observations of the consumption of the electric energy in some department store. The number of time series observations is $n=24$. The data and model were adapted from Štulajter & Witkovský, 2004.
The consumption data can be fitted by the following Gaussian orthogonal FDSLRM:
$ \begin{array}{rl} & X(t) & \! = \! & \beta_1+\beta_2\cos\left(\tfrac{2\pi t}{24}\right)+\beta_3\sin\left(\tfrac{2\pi t}{24}\right) +\\ & & & +Y_1\cos\left(\tfrac{2\pi t\cdot 2}{24}\right)+Y_2\sin\left(\tfrac{2\pi t\cdot 2}{24}\right)+\\ & & & +Y_3\cos\left(\tfrac{2\pi t\cdot 3}{24}\right)+Y_4\sin\left(\tfrac{2\pi t\cdot 3}{24}\right) +w(t), \, t\in \mathbb{N}, \end{array} $
where $\boldsymbol{\beta}=(\beta_1,\,\beta_2,\,\beta_3)' \in \mathbb{R}^3\,,\mathbf{Y} = (Y_1, Y_2, Y_3, Y_4)' \sim \mathcal{N}_4(\boldsymbol{0}, \mathrm{D})\,, w(t) \sim \mathcal{iid}\, \mathcal{N} (0, \sigma_0^2)\,, \boldsymbol{\nu}= (\sigma_0^2, \sigma_1^2, \sigma_2^2, \sigma_3^2, \sigma_4^2) \in \mathbb{R}_{+}^5.$
# data - time series observation
x <- c(40.3,40.7,38.5,37.9,38.6,41.1,45.2,45.7,46.7,46.5,
45.2,45.1,45.8,46.3,47.5,48.5,49.1,51.7,50.6,48.0,
44.7,41.2,40.0,40.3)
t <- 1:length(x)
# auxilliary functions to create design matrices F, V and the projection matrix M_F
makeF <- function(times, freqs) {
n <- length(times)
f <- length(freqs)
c1 <- matrix(1, n)
F <- matrix(c1, n, 1)
for (i in 1:f) {
F <- cbind(F, cos(2 * pi * freqs[i] * times))
F <- cbind(F, sin(2 * pi * freqs[i] * times))
}
return(F)
}
makeV <- function(times, freqs) {
V <- makeF(times, freqs)
V <- V[, -1]
return(V)
}
makeM_F <- function(F) {
n <- nrow(F)
c1 <- rep(1, n)
I <- diag(c1)
M_F <- I - F %*% solve((t(F) %*% F)) %*% t(F)
return(M_F)
}
# model parameters
n <- 24
k <- 3
l <- 4
# model - design matrices F, V
F <- makeF(t, c(1/24))
V <- makeV(t, c(2/24, 3/24))
lme
function for fitting LMMlme
:
maxIter
- maximum number of iterations (default: 50).
* `msMaxIter` - maximum number of iterations for the optimization step (default: 50).
* `tolerance` - tolerance for the convergence criterion (default: 1e-6).
* `niterEM` - number of iterations for the EM algorithm used to refine the initial estimates of the random effects (default: 25).
* `msMaxEval` - maximum number of evaluations of the objective function permitted for optimizer nlminb (default: 200).
* `msTol` - tolerance for the convergence criterion on the first iteration when optim is used (default is 1e-7).
* `.relStep` - relative step for numerical derivatives calculations. Default is `.Machine$double.eps^(1/3)` while `.Machine$double.eps = 2.220446e-16`.
* `opt` - the optimizer to be used, either "nlminb" (the default) or "optim".
* `optimMethod` - the optimization method, a version of quasi-Newton method to be used with the optim optimizer (default:"BFGS", alternative: "L-BFGS-B")
* `minAbsParApVar` - numeric value - minimum absolute parameter value in the approximate variance calculation (default: 0.05). <font>
library(nlme)
REMLE_nlme <- function(X, F, V, method) {
g <- rep(1,length(X))
d <- data.frame(g,F,V,X)
colnames(d) <- c("g","f1","f2","f3","v1","v2","v3","v4","X")
result <- NULL
if(method == "ML") {
result <- lme(fixed=X~f2+f3,random=list(g=pdDiag(~-1+v1+v2+v3+v4)),data=d,method="ML")
} else {
result <- lme(fixed=X~f2+f3,random=list(g=pdDiag(~-1+v1+v2+v3+v4)),data=d,method="REML")
}
return(as.vector(c(result$sigma^2, diag(getVarCov(result)))))
}
# auxilliary function to calculate the norm of a vector
norm_vec <- function(x) sqrt(sum(x^2))
MLEnlme <- REMLE_nlme(x, F, V, "ML")
print(MLEnlme)
norm_vec(MLEnlme)
[1] 0.929088 2.888293 1.684435 0.294511 1.786055
00 installation fdslrm.ipynb
once before the first run of any R-based Jupyter notebook.function fitDiagFDSLRM
via parameter "lme"
implements lme
function from nlme
Important note: A brief help on R functions of our fdslrm package designed to work with FDSLRM is in the modelling notebooks. After our testing, the most reliable way to install our fdslrm package in a Binder repository is its direct loading from GitHub. The standard installation of our fdslrm package as in the case of any R package on GitHub works without any problems in a local installation using Anaconda R distribution or CRAN distribution.
# loading all fdslrm functions as an R script from GiHub
devtools::source_url("https://github.com/fdslrm/fdslrmAllinOne/blob/master/fdslrmAllinOne.R?raw=TRUE")
initialFDSLRM()
SHA-1 hash of file is f56b9d53e72a8575947a467930a2bdddb5b500ad
# fit particular FDSLRM employing ML estimation of variances (by 'lme' function)
MLEfitnlme <- fitDiagFDSLRM(x, t, freq_mean = c(1/24), freq_random = c(2/24, 3/24), fit_function = "lme",
var_estim_method = "ML")
print(as.vector(c(MLEfitnlme$error_variance, diag(MLEfitnlme$rand_eff_variance))))
norm_vec(as.vector(c(MLEfitnlme$error_variance, diag(MLEfitnlme$rand_eff_variance))))
[1] 0.929088 2.888293 1.684435 0.294511 1.786055
R version of Witkovský's MATLAB function mixed
https://www.mathworks.com/matlabcentral/fileexchange/200
MMEinR (mixed)
:
- tolerance for the convergence criterion (
epss = 1e-8
)- iterative method solving Henderson's mixed model equations
- function return the estimates of variance parameters in a different order: $(\tilde{\nu}_1,\ldots,\tilde{\nu}_l,\tilde{\nu}_0)'$ in comparison with other tools
# load function 'MMEinR'
devtools::source_url("https://github.com/fdslrm/MMEinR/blob/master/MMEinR.R?raw=TRUE")
SHA-1 hash of file is 36b3b4cd1a1f98c5fb36b541fc6b3956f6663aba
MLEmixed <- mixed(x, F, V, c(1,1,1,1), c(1,1,1,1,1), 1)
print(MLEmixed$s2)
print(norm_vec(MLEmixed$s2))
[1] 2.888293 1.684435 0.294511 1.786055 0.929088 [1] 3.914013
function fitDiagFDSLRM
via parameter "mixed"
implements R version of MATLAB mixed
function
# fit particular FDSLRM employing ML estimation of variances (by 'MMEinR (mixed)' function)
MLEfitmixed <- fitDiagFDSLRM(x, t, freq_mean = c(1/24), freq_random = c(2/24, 3/24), fit_function = "mixed",
var_estim_method = "ML")
print(as.vector(c(MLEfitmixed$error_variance, MLEfitmixed$rand_eff_variance)))
norm_vec(as.vector(c(MLEfitmixed$error_variance, MLEfitmixed$rand_eff_variance)))
[1] 0.929088 2.888293 1.684435 0.294511 1.786055
REMLEnlme <- REMLE_nlme(x, F, V, "REML")
print(REMLEnlme)
norm_vec(REMLEnlme)
[1] 1.0930447 2.8746303 1.6707717 0.2808479 1.7723924
# fit particular FDSLRM employing REML estimation of variances (by 'lme' function)
REMLEfitnlme <- fitDiagFDSLRM(x, t, freq_mean = c(1/24), freq_random = c(2/24, 3/24), fit_function = "lme",
var_estim_method = "REML")
print(as.vector(c(REMLEfitnlme$error_variance, diag(REMLEfitnlme$rand_eff_variance))))
norm_vec(as.vector(c(REMLEfitnlme$error_variance, diag(REMLEfitnlme$rand_eff_variance))))
[1] 1.0930447 2.8746303 1.6707717 0.2808479 1.7723924
REMLEmixed <- mixed(x, F, V, c(1,1,1,1), c(1,1,1,1,1), 2)
print(REMLEmixed$s2)
print(norm_vec(REMLEmixed$s2))
[1] 2.8746303 1.6707717 0.2808479 1.7723924 1.0930447 [1] 3.933189
# fit particular FDSLRM employing REML estimation of variances (by 'MMEinR (mixed)' function)
REMLEfitmixed <- fitDiagFDSLRM(x, t, freq_mean = c(1/24), freq_random = c(2/24, 3/24), fit_function = "mixed",
var_estim_method = "REML")
print(as.vector(c(REMLEfitmixed$error_variance, REMLEfitmixed$rand_eff_variance)))
norm_vec(as.vector(c(REMLEfitmixed$error_variance, REMLEfitmixed$rand_eff_variance)))
[1] 1.0930447 2.8746303 1.6707717 0.2808479 1.7723924
Designed for genomic prediction and genome wide association studies (GWAS), particularly focused in the $p$ > $n$ problem (more coefficients than observations) to include multiple known relationship matrices and estimate complex unknown covariance structures. Spatial models can be fitted using the two-dimensional spline functionality in sommer.
mmer
function for fittingmmer
:
iters
- maximum number of iterations (default: 20).
* `tolpar` - tolerance for the convergence criterion for the change in loglikelihood (default: 1e-03)
* `tolparinv` - tolerance parameter for matrix inverse used when singularities are encountered (default:1e-06).
* `method` - this refers to the method or algorithm to be used for estimating variance components (default: NR, direct-inversion Newton-Raphson method, alternative: AI, Average Information)
* function return the estimates of variance parameters in a different order: $(\tilde{\nu}_1,\ldots,\tilde{\nu}_l,\tilde{\nu}_0)'$ in comparison with other tools<font>
library(sommer)
REMLE_sommer <- function(X, F, V) {
f1 <- F[,1]
f2 <- F[,2]
f3 <- F[,3]
v1 <- V[,1]
v2 <- V[,2]
v3 <- V[,3]
v4 <- V[,4]
DT <- data.frame(X, f1, f2, f3, v1, v2, v3, v4)
suppressWarnings(result_new <- mmer(fixed = X~f2+f3, random = ~ vs(ds(v1),1)+vs(ds(v2),1)+vs(ds(v3),1)+vs(ds(v4),1),
data = DT, verbose = FALSE))
output <- as.vector(unlist(result_new$sigma))
return(output)
}
REMLEsommer <- REMLE_sommer(x, F, V)
print(REMLEsommer)
print(norm_vec(REMLEsommer))
[1] 2.8741111 1.6704907 0.2808421 1.7720913 1.0930769 [1] 3.932563
function fitDiagFDSLRM
via parameter "mmer"
implements mmer
function from sommer
# fit particular FDSLRM employing REML estimation of variances (by 'mmer' function)
REMLEfitsommer <- suppressWarnings(fitDiagFDSLRM(x, t, freq_mean = c(1/24), freq_random = c(2/24, 3/24),
fit_function = "mmer", var_estim_method = "REML"))
fixed-effect model matrix is rank deficient so dropping 1 columns / coefficients
print(as.vector(c(REMLEfitsommer$error_variance, REMLEfitsommer$rand_eff_variance)))
print(norm_vec(c(REMLEfitsommer$error_variance, REMLEfitsommer$rand_eff_variance)))
[1] 1.0930769 2.8741111 1.6704907 0.2808421 1.7720913 [1] 3.932563
This notebook belongs to suplementary materials of the paper submitted to Statistical Papers and available at https://arxiv.org/abs/1905.07771.
We propose a two-stage estimation method of variance components in time series models known as FDSLRMs, whose observations can be described by a linear mixed model (LMM). We based estimating variances, fundamental quantities in a time series forecasting approach called kriging, on the empirical (plug-in) best linear unbiased predictions of unobservable random components in FDSLRM.
The method, providing invariant non-negative quadratic estimators, can be used for any absolutely continuous probability distribution of time series data. As a result of applying the convex optimization and the LMM methodology, we resolved two problems $-$ theoretical existence and equivalence between least squares estimators, non-negative (M)DOOLSE, and maximum likelihood estimators, (RE)MLE, as possible starting points of our method and a practical lack of computational implementation for FDSLRM. As for computing (RE)MLE in the case of $ n $ observed time series values, we also discovered a new algorithm of order $\mathcal{O}(n)$, which at the default precision is $10^7$ times more accurate and $n^2$ times faster than the best current Python(or R)-based computational packages, namely CVXPY, CVXR, nlme, sommer and mixed.
We illustrate our results on three real data sets $-$ electricity consumption, tourism and cyber security $-$ which are easily available, reproducible, sharable and modifiable in the form of interactive Jupyter notebooks.
Discrete Spectrum Linear Regression Models](https://link.springer.com/article/10.1007/s001840300299). Metrika, 2004, Vol. 60, No. 2, pp. 105–118
| Table of Contents | Data and model | MLE | REMLE | References |