Contact
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutSign UpSign In
| Download

a research paper about FDSLRM modeling with supplementary materials - software, notebooks

Project: fdslrm
Views: 1041
Kernel: R (R-Project)

Authors: Andrej Gajdoš, Jozef Hanč, Martina Hančová
Faculty of Science, P. J. Šafárik University in Košice, Slovakia
email: [email protected]


MLE, REMLE for electricity consumption 1

R-based computational tools - fdslrm, nlme, MMEinR(mixed), sommer

Table of Contents

  • Data and model - data and model description of empirical data

  • MLE - computation by standard packages nlme, MMEinR(mixed) and fdslrm

  • REMLE - computation by standard package nlme, MMEinR(mixed), sommer and fdslrm

  • References

To get back to the table of contents, use the Home key.


Data and Toy model 1

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=24n=24. The data was adapted from Štulajter & Witkovský, 2004 and the FDSLRM model from Gajdoš et al 2017.

The consumption data can be fitted by the following Gaussian orthogonal FDSLRM:

X(t) ⁣= ⁣β1+β2cos(2πt24)+β3sin(2πt24)++Y1cos(2πt324)+Y2sin(2πt324)++Y3cos(2πt424)+Y4sin(2πt424)+w(t),tN, \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 3}{24}\right)+Y_2\sin\left(\tfrac{2\pi t\cdot 3}{24}\right)+\\ & & & +Y_3\cos\left(\tfrac{2\pi t\cdot 4}{24}\right)+Y_4\sin\left(\tfrac{2\pi t\cdot 4}{24}\right) +w(t), \, t\in \mathbb{N}, \end{array}

where β=(β1,β2,β3)R3,Y=(Y1,Y2,Y3,Y4)N4(0,D),w(t)iidN(0,σ02),ν=(σ02,σ12,σ22,σ32,σ42)R+5.\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(3/24, 4/24))

MLE

nlme: Linear and Nonlinear Mixed Effects Models

  • Purpose: R package to fit and compare Gaussian linear and nonlinear mixed-effects models

  • Version: 3.1-140, 2019

  • URL: https://CRAN.R-project.org/package=nlme]

  • Key tool: lme function for fitting LMM

  • Computational parameters of lme:

  • 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).

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] 2.862040e+00 1.334114e-01 1.624973e+00 5.330684e-09 1.028996e+00
[1] 3.450861

fdslrm: Time series analysis and forecasting using LMM

  • Purpose: R package for modeling and prediction of time series using linear mixed models.

  • Version: 0.1.0, 2019

  • Depends: kableExtra, IRdisplay, MASS, Matrix, car, nlme, stats, forecast, fpp2, matrixcalc, sommer, gnm, pracma, CVXR

  • Maintainer: Andrej Gajdoš

  • Authors: Andrej Gajdoš, Jozef Hanč, Martina Hančová

  • URL: https://github.com/fdslrm/R-package

  • Installation: Run jupyter notebook 00 installation fdslrm.ipynb once before the first run of any R-based Jupyter notebook.

fdslrm (nlme)

function fitDiagFDSLRM via parameter "lme" implements lme function from nlme

library(fdslrm) initialFDSLRM()
Attaching package: ‘fdslrm’ The following objects are masked _by_ ‘.GlobalEnv’: makeF, makeM_F, makeV
# fit particular FDSLRM employing ML estimation of variances (by 'lme' function) MLEfitnlme <- fitDiagFDSLRM(x, t, freq_mean = c(1/24), freq_random = c(3/24, 4/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] 2.862040e+00 1.334114e-01 1.624973e+00 5.330684e-09 1.028996e+00
[1] 3.450861

MMEinR

R version of Witkovský's MATLAB function mixed https://www.mathworks.com/matlabcentral/fileexchange/200

  • Purpose: R function to estimate parameters of a linear mixed model (LMM) with a simple variance components structure

  • URL: https://github.com/fdslrm/MMEinR

  • Computational parameters of 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: (ν~1,,ν~l,ν~0)(\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))
Warning message in mixed(x, F, V, c(1, 1, 1, 1), c(1, 1, 1, 1, 1), 1): “Estimated variance components are negative or zeros!”
[1] 1.334323e-01 1.624977e+00 4.956820e-18 1.028997e+00 2.862032e+00 [1] 3.450857

fdslrm (MMEinR)

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(3/24, 4/24), fit_function = "mixed", var_estim_method = "ML")
Warning message in mixed(x, F, V, dimensions, variances_start, var_est_met): “Estimated variance components are negative or zeros!”
print(as.vector(c(MLEfitmixed$error_variance, MLEfitmixed$rand_eff_variance))) norm_vec(as.vector(c(MLEfitmixed$error_variance, MLEfitmixed$rand_eff_variance)))
[1] 2.862032e+00 1.334323e-01 1.624977e+00 4.956820e-18 1.028997e+00
[1] 3.450857

REMLE

nlme

REMLEnlme <- REMLE_nlme(x, F, V, "REML") print(REMLEnlme) norm_vec(REMLEnlme)
[1] 3.339037e+00 9.368183e-02 1.585227e+00 2.257708e-09 9.892468e-01
[1] 3.827466

fdslrm (nlme)

# fit particular FDSLRM employing REML estimation of variances (by 'lme' function) REMLEfitnlme <- fitDiagFDSLRM(x, t, freq_mean = c(1/24), freq_random = c(3/24, 4/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] 3.339037e+00 9.368183e-02 1.585227e+00 2.257708e-09 9.892468e-01
[1] 3.827466

MMEinR

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))
Warning message in mixed(x, F, V, c(1, 1, 1, 1), c(1, 1, 1, 1, 1), 2): “Estimated variance components are negative or zeros!”
[1] 9.368188e-02 1.585226e+00 2.220446e-16 9.892469e-01 3.339037e+00 [1] 3.827466

fdslrm (MMEinR)

# fit particular FDSLRM employing REML estimation of variances (by 'MMEinR (mixed)' function) REMLEfitmixed <- fitDiagFDSLRM(x, t, freq_mean = c(1/24), freq_random = c(3/24, 4/24), fit_function = "mixed", var_estim_method = "REML")
Warning message in mixed(x, F, V, dimensions, variances_start, var_est_met): “Estimated variance components are negative or zeros!”
print(as.vector(c(REMLEfitmixed$error_variance, REMLEfitmixed$rand_eff_variance))) norm_vec(as.vector(c(REMLEfitmixed$error_variance, REMLEfitmixed$rand_eff_variance)))
[1] 3.339037e+00 9.368188e-02 1.585226e+00 2.220446e-16 9.892469e-01
[1] 3.827466

sommer: Solving Mixed Model Equations in R

  • Purpose: R package - structural multivariate-univariate linear mixed model solver for multiple random effects and estimation of unknown variance-covariance structures (i.e. heterogeneous and unstructured variance models). Designed for genomic prediction and genome wide association studies (GWAS), particularly focused in the pp > nn 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.

  • Version: 3.9.3, 2019

  • URL: https://CRAN.R-project.org/package=sommer

  • Key tool: mmer function for fitting

  • computational parameters of mmer:

  • 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: (ν~1,,ν~l,ν~0)(\tilde{\nu}_1,\ldots,\tilde{\nu}_l,\tilde{\nu}_0)' in comparison with other tools

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] 0.09375687 1.58504405 0.00000000 0.98916742 3.33903029 [1] 3.827366

fdslrm (sommer)

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(3/24, 4/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] 3.33903029 0.09375687 1.58504405 0.00000000 0.98916742 [1] 3.827366

References

This notebook belongs to suplementary materials of the paper submitted to Statistical Papers and available at https://arxiv.org/abs/1905.07771.

Abstract of the paper

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, one of the steps 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 n observed time series values, we also discovered a new algorithm of order O(n)\mathcal{O}(n), which at the default precision is 10710^7 times more accurate and n2n^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.