R Under development (unstable) (2025-01-27 r87654 ucrt) -- "Unsuffered Consequences" Copyright (C) 2025 The R Foundation for Statistical Computing Platform: x86_64-w64-mingw32/x64 R is free software and comes with ABSOLUTELY NO WARRANTY. You are welcome to redistribute it under certain conditions. Type 'license()' or 'licence()' for distribution details. R is a collaborative project with many contributors. Type 'contributors()' for more information and 'citation()' on how to cite R or R packages in publications. Type 'demo()' for some demos, 'help()' for on-line help, or 'help.start()' for an HTML browser interface to help. Type 'q()' to quit R. > # Compare mmglm0 and mmglm1 > # Gaussian with identity link function > # R CMD BATCH --no-save mmglm0-mmglm1-gaussian.R mmglm0-mmglm1-gaussian.Rout.save > > library(HiddenMarkov) > > > delta <- c(0,1) > > Pi <- matrix(c(0.8, 0.2, + 0.3, 0.7), + byrow=TRUE, nrow=2) > > beta <- matrix(c(0.1, -0.1, + 1.0, 5.0), + byrow=TRUE, nrow=2) > > sd <- c(1, 2) > > #-------------------------------------------------------- > # Gaussian with identity link function > # using mmglm0 > > x0 <- mmglm0(NULL, Pi, delta, family="gaussian", link="identity", + beta=beta, sigma=sd, msg=FALSE) > > x0 <- simulate(x0, nsim=5000, seed=10) > > x0 <- BaumWelch(x0, bwcontrol(prt=FALSE)) > > print(summary(x0)) $variable.names [1] "x1" "y" $delta [1] 0 1 $Pi [,1] [,2] [1,] 0.7866626 0.2133374 [2,] 0.2994035 0.7005965 $nonstat [1] TRUE $beta [,1] [,2] [1,] 0.1444775 -0.2273769 [2,] 0.9376238 5.0883638 $sigma [1] 0.9717685 2.0099827 $family [1] "gaussian" $glmformula y ~ x1 $link [1] "identity" $n [1] 5000 > > #-------------------------------------------------------- > # Now embed this data into a mmglm1 object > > glmformula <- formula(y ~ x1) > glmfamily <- gaussian(link="identity") > Xdesign <- model.matrix(glmformula, data=x0$x) > > x1 <- mmglm1(x0$x$y, Pi, delta, glmfamily, beta, Xdesign, sigma=sd, msg=FALSE) > > x1 <- BaumWelch(x1, bwcontrol(prt=FALSE)) > > print(summary(x1)) $delta [1] 0 1 $Pi [,1] [,2] [1,] 0.7866626 0.2133374 [2,] 0.2994035 0.7005965 $nonstat [1] TRUE $beta State 1 State 2 (Intercept) 0.1444775 -0.2273769 x1 0.9376238 5.0883638 $sigma [1] 0.9717685 2.0099827 $glmfamily Family: gaussian Link function: identity $n [1] 5000 > > #-------------------------------------------------------- > # Compare Models > > if (abs(logLik(x0)-logLik(x1)) > 1e-06) + warning("WARNING: See tests/mmglm0-mmglm1-gaussian.R, log-likelihoods are different") > > if (any(Viterbi(x0)!=Viterbi(x1))) + warning("WARNING: See tests/mmglm0-mmglm1-gaussian.R, Viterbi paths are different") > > if (any(abs(residuals(x0)-residuals(x1)) > 1e-06)) + warning("WARNING: See tests/mmglm0-mmglm1-gaussian.R, residuals are different") > > > > proc.time() user system elapsed 1.70 0.23 1.90