#liberaries
#detach("package:remhmm", unload = TRUE)
#devtools::install_github("Mbehbahani/DigiPy/remhmm")
library(remhmm)
library(REHdynamics)
library(remstats)
## Warning: package 'remstats' was built under R version 4.3.2
library(remstimate)
## Warning: package 'remstimate' was built under R version 4.3.2
library("nloptr")
## Warning: package 'nloptr' was built under R version 4.3.2
Parameter settings for Scenario1
m=2 #Number States
q=1 #Number of variables (In REM it means the Number of statistics)
num_obs <- 100
N_relations<-200 #number of relational events in each state
# Hidden State
transition_probs <- matrix(c(0.2,0.8,0.8,0.2), nrow = 2,byrow=TRUE)
initial_probs <- c(0.1, 0.9)
# Initial Values for Optimization
gamma0=matrix(c(0.2,0.8,0.8,0.2), nrow = 2,byrow=TRUE)
mean0=c(0.1,0.5)
sd0=c(0.3,0.4)
delta0=c(0.3, 0.7)
vc= matrices_to_vector(gamma0, mean0, sd0, delta0)
Lgamma <- matrix(c(0,0,0,0), nrow = 2,byrow=TRUE)
Lmean0 <- c(-Inf,-Inf)
Lsd0 <- c(0,0)
Ldelta0 <- c(0, 0)
Lvc <- matrices_to_vector(Lgamma, Lmean0, Lsd0, Ldelta0)
Ugamma <- matrix(c(1,1,1,1), nrow = 2,byrow=TRUE)
Umean0 <- matrix(c(Inf,Inf))
Usd0 <- matrix(c(Inf,Inf))
Udelta0 <- c(1, 1)
Uvc <- matrices_to_vector(Ugamma, Umean0, Usd0, Udelta0)
lower_bounds <- Lvc
upper_bounds <- Uvc
## code to prepare `covar` dataset
# Generate the actors and their exogenous information for the
# generated datasets
set.seed(27613)
covar <- data.frame(id = 1:10, time = 0, z = rnorm(n = 10))
colnames(covar)[colnames(covar) == "id"] <- "name"
# usethis::use_data(covar,overwrite = TRUE)
## code to prepare `param` dataset
# Normal variable (will be add)
param1 <- list(
"baseline" = -4,
"inertia" = 0.1)
param2 <- list(
"baseline" = -4,
"inertia" = 0.5)
# Generate REM of the current state
#num_var<-1
#q<-num_var
#initial_probs
#transition_probs
hidden_states<-numeric(num_obs)
for (t in 1:num_obs) {
if (t == 1) {
# Initial state
hidden_states[t] <- sample(1:m, 1, prob = initial_probs)
} else {
# Transition to the next state
hidden_states[t] <- sample(1:m, 1, prob = transition_probs[hidden_states[t - 1], ])
}
}
## M<-200 specify M (N_relations at the beginning)
windows <- data.frame(
start = seq(1, by = N_relations, length.out = num_obs),
stop = seq(N_relations, by = N_relations, length.out = num_obs)
)
norm.HMM.mllk(vc, x, m,q)
## [1] 15.11015
Result<-norm.HMM.mle(x, m,q, vc,lower_bounds, upper_bounds)
## Warning in nloptr.add.default.options(opts.user = opts, x0 = x0,
## num_constraints_ineq = num_constraints_ineq, : No termination criterium
## specified, using default (relative x-tolerance = 1e-04)
Result
## $gamma
## [,1] [,2]
## [1,] 0.2268973 0.7731027
## [2,] 0.7725330 0.2274670
##
## $mean
## [,1] [,2]
## [1,] 0.09266093 0.5023897
##
## $sd
## [,1] [,2]
## [1,] 0.1990576 0.01363938
##
## $delta
## [1] 0.5713172 0.4286828
Fstates<-norm.HMM.viterbi(x,Result,m,q)
#hidden_states
#x
cbind(x,hidden_states,Fstates)
## hidden_states Fstates
## [1,] 0.49274652 2 2
## [2,] 0.03256410 1 1
## [3,] 0.50940753 2 2
## [4,] 0.06028542 1 1
## [5,] 0.51355557 2 2
## [6,] 0.08674986 1 1
## [7,] 0.50403092 2 2
## [8,] 0.51904986 2 2
## [9,] 0.49490613 2 2
## [10,] 0.13837724 1 1
## [11,] 0.48635982 2 2
## [12,] 0.06365644 1 1
## [13,] 0.48209168 2 2
## [14,] 0.06478355 1 1
## [15,] 0.49906671 2 2
## [16,] 0.10766241 1 1
## [17,] 0.50770941 2 2
## [18,] 0.15248797 1 1
## [19,] 0.51652893 2 2
## [20,] 0.06257603 1 1
## [21,] 0.51211759 2 2
## [22,] 0.51416828 2 2
## [23,] 0.48848176 2 2
## [24,] 0.10794500 1 1
## [25,] 0.08676196 1 1
## [26,] 0.48204893 2 2
## [27,] 0.51192392 2 2
## [28,] 0.06287079 1 1
## [29,] 0.02866174 1 1
## [30,] 0.10727773 1 1
## [31,] 0.51626549 2 2
## [32,] 0.13819422 1 1
## [33,] 0.48606101 2 2
## [34,] -0.08260028 1 1
## [35,] 0.51394992 2 2
## [36,] 0.13757479 1 1
## [37,] 0.51395826 2 2
## [38,] 0.52057586 2 2
## [39,] 0.50534270 2 2
## [40,] 0.10705506 1 1
## [41,] 0.48390016 2 2
## [42,] 0.51385478 2 2
## [43,] 0.49875251 2 2
## [44,] 0.12330244 1 1
## [45,] 0.51172535 2 2
## [46,] 0.08733404 1 1
## [47,] 0.52495422 2 2
## [48,] 0.03174733 1 1
## [49,] 0.49668453 2 2
## [50,] -0.01461943 1 1
## [51,] 0.52273581 2 2
## [52,] 0.13855165 1 1
## [53,] 0.51597690 2 2
## [54,] 0.08668094 1 1
## [55,] 0.49661313 2 2
## [56,] 0.13817610 1 1
## [57,] 0.49240050 2 2
## [58,] 0.06258792 1 1
## [59,] 0.49661979 2 2
## [60,] 0.51379834 2 2
## [61,] 0.08564502 1 1
## [62,] 0.49021609 2 2
## [63,] 0.08728806 1 1
## [64,] 0.50300235 2 2
## [65,] 0.12321235 1 1
## [66,] 0.47749465 2 2
## [67,] 0.10675151 1 1
## [68,] 0.52713244 2 2
## [69,] 0.51600528 2 2
## [70,] 0.49438373 2 2
## [71,] 0.08631044 1 1
## [72,] 0.51376953 2 2
## [73,] 0.08706425 1 1
## [74,] 0.47110787 2 2
## [75,] 0.12258366 1 1
## [76,] 0.51386866 2 2
## [77,] 0.10667489 1 1
## [78,] 0.50504478 2 2
## [79,] 0.52484056 2 2
## [80,] 0.50508365 2 2
## [81,] 0.50080411 2 2
## [82,] -0.01124111 1 1
## [83,] 0.49442367 2 2
## [84,] 0.10635817 1 1
## [85,] 0.48589748 2 2
## [86,] -0.81564415 1 1
## [87,] 0.49656001 2 2
## [88,] 0.08726232 1 1
## [89,] 0.47538349 2 2
## [90,] 0.13809183 1 1
## [91,] 0.48595070 2 2
## [92,] 0.06317193 1 1
## [93,] 0.49867832 2 2
## [94,] 0.06308124 1 1
## [95,] 0.47119219 2 2
## [96,] 0.08773070 1 1
## [97,] 0.51595027 2 2
## [98,] 0.15151744 1 1
## [99,] 0.03096112 1 1
## [100,] 0.48381489 2 2
sum(hidden_states==Fstates)
## [1] 100