libraries

#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=2 #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 = m,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 = m,byrow=TRUE)
mean0=matrix(c(0.1,0.5,0.2,0.7) , nrow = q,byrow=TRUE)
sd0=matrix(c(0.3,0.4,0.1,0.2), nrow = q,byrow=TRUE)
delta0=c(0.3, 0.7)


vc= matrices_to_vector(gamma0, mean0, sd0, delta0)

Lgamma <- matrix(c(0,0,0,0), nrow = m,byrow=TRUE)
Lmean0 <- rep(-Inf, q * 2)
Lsd0 <- rep(0, q * 2)
Ldelta0 <- rep(0, m)
Lvc <- matrices_to_vector(Lgamma, Lmean0, Lsd0, Ldelta0)

Ugamma <- matrix(c(1,1,1,1), nrow = m,byrow=TRUE)
Umean0 <- rep(Inf, q * 2)
Usd0 <- rep(Inf, q * 2)
Udelta0 <- rep(1, m)
Uvc <- matrices_to_vector(Ugamma, Umean0, Usd0, Udelta0)


lower_bounds <- Lvc
upper_bounds <- Uvc

generate REH data using “generate_rem” function

## 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,
     "z_send"=0.2 )

param2 <- list(
    "baseline" = -4, 
    "inertia" = 0.5,
    "z_send"=0.7 )

REM data (MainTest(Event effect).Rmd)

# 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], ])
    }
}

preparation to generate datasets

windows defenition

##  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)
)

for calculating the effects. of the statistics
it based on the number of statistics. It should be modified

norm.HMM.mllk(vc, x, m,q)
## [1] -27.88027
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.2612493 0.7387507
## [2,] 0.8529362 0.1470638
## 
## $mean
##           [,1]      [,2]
## [1,] 0.1188562 0.6726295
## [2,] 0.1930384 0.7170707
## 
## $sd
##            [,1]       [,2]
## [1,] 0.07251008 0.45995266
## [2,] 0.05054450 0.07620702
## 
## $delta
## [1] 0.6637299 0.3362701
Fstates<-norm.HMM.viterbi(x,Result,m,q)
#hidden_states
#x
cbind(x,hidden_states,Fstates)
##                               hidden_states Fstates
##   [1,]  1.651446765 0.6468862             2       2
##   [2,]  0.067023823 0.1972435             1       1
##   [3,]  0.319174207 0.7596067             2       2
##   [4,]  0.048912816 0.1647565             1       1
##   [5,]  0.924011489 0.7201511             2       2
##   [6,]  0.091345815 0.1934655             1       1
##   [7,]  0.211713958 0.7620410             2       2
##   [8,]  0.745664162 0.7376323             2       2
##   [9,]  0.185323363 0.7085514             2       2
##  [10,]  0.099510572 0.2191988             1       1
##  [11,]  0.408163629 0.6691445             2       2
##  [12,]  0.066899457 0.2184403             1       1
##  [13,]  0.543791479 0.6592160             2       2
##  [14,]  0.112607332 0.1905198             1       1
##  [15,]  0.365665189 0.6492272             2       2
##  [16,]  0.188430071 0.1856045             1       1
##  [17,]  0.265863296 0.7044724             2       2
##  [18,]  0.217796095 0.2333650             1       1
##  [19,]  0.735315212 0.7218976             2       2
##  [20,]  0.157304619 0.1749077             1       1
##  [21,]  0.675010509 0.7140220             2       2
##  [22,]  0.560186432 0.7274596             2       2
##  [23,]  0.200348712 0.7764138             2       2
##  [24,]  0.215558690 0.1814441             1       1
##  [25,]  0.107497035 0.1831628             1       1
##  [26,]  0.876443939 0.6305120             2       2
##  [27,]  1.159033112 0.6437165             2       2
##  [28,]  0.111096487 0.1823991             1       1
##  [29,]  0.025122196 0.1583019             1       1
##  [30,]  0.151813812 0.1911574             1       1
##  [31,]  0.305499622 0.7364174             2       2
##  [32,]  0.149530812 0.1984807             1       1
##  [33,]  0.479399197 0.6530749             2       2
##  [34,]  0.153098616 0.1379333             1       1
##  [35,]  0.587742011 0.6889346             2       2
##  [36,]  0.013837892 0.2061985             1       1
##  [37,]  0.435279015 0.7304410             2       2
##  [38,]  0.876162853 0.6683150             2       2
##  [39,]  0.894991296 0.6558346             2       2
##  [40,]  0.129467720 0.1947648             1       1
##  [41,]  0.458407402 0.7549627             2       2
##  [42,]  0.154056289 0.7385279             2       2
##  [43,]  0.800374890 0.6651944             2       2
##  [44,]  0.061732999 0.2054986             1       1
##  [45,]  0.941808448 0.6620029             2       2
##  [46,]  0.127695989 0.1951152             1       1
##  [47,]  0.272353284 0.7658479             2       2
##  [48,]  0.196185757 0.0760059             1       1
##  [49,] -0.512827620 0.9554744             2       2
##  [50,] -0.035187805 0.1685775             1       1
##  [51,]  0.066545230 0.7621485             2       2
##  [52,]  0.213583740 0.1819388             1       1
##  [53,]  0.435218605 0.6892471             2       2
##  [54,]  0.008514712 0.2211902             1       1
##  [55,]  0.463518037 0.7148787             2       2
##  [56,]  0.025729170 0.2373265             1       1
##  [57,] -0.060756640 0.8185260             2       2
##  [58,]  0.023069327 0.2188862             1       1
##  [59,]  0.283285534 0.7641513             2       2
##  [60,]  0.549345002 0.6585590             2       2
##  [61,] -0.044263139 0.2159724             1       1
##  [62,]  0.132744594 0.7737801             2       2
##  [63,]  0.134524922 0.1939455             1       1
##  [64,]  0.657318713 0.6709126             2       2
##  [65,]  0.038003201 0.1943513             1       1
##  [66,]  0.116898605 0.7540525             2       2
##  [67,]  0.139102490 0.1930833             1       1
##  [68,]  0.286346673 0.7783457             2       2
##  [69,]  0.817792361 0.6986286             2       2
##  [70,]  1.142948316 0.6069873             2       2
##  [71,]  0.060949475 0.1901416             1       1
##  [72,]  1.040705201 0.6328036             2       2
##  [73,]  0.126509346 0.1799825             1       1
##  [74,]  0.812668219 0.6540862             2       2
##  [75,] -0.018746825 0.2033016             1       1
##  [76,]  0.224692652 0.8239120             2       2
##  [77,]  0.048125334 0.2214900             1       1
##  [78,]  0.562399158 0.6730493             2       2
##  [79,]  0.780090569 0.6483673             2       2
##  [80,]  0.734276627 0.6720325             2       2
##  [81,]  0.996044785 0.6569215             2       2
##  [82,]  0.150767675 0.1587435             1       1
##  [83,]  0.622330613 0.6423010             2       2
##  [84,] -0.009184101 0.2242269             1       1
##  [85,] -0.068652305 0.8068993             2       2
##  [86,]  0.180468131 0.1091016             1       1
##  [87,] -0.165890279 0.7920560             2       2
##  [88,]  0.150439388 0.1914129             1       1
##  [89,]  1.283597810 0.6141207             2       2
##  [90,]  0.034226004 0.2416539             1       1
##  [91,]  0.938828130 0.6418170             2       2
##  [92,]  0.033280984 0.2170993             1       1
##  [93,] -0.259998069 0.8190769             2       2
##  [94,]  0.113969217 0.1819611             1       1
##  [95,]  0.840929544 0.6610181             2       2
##  [96,]  0.127975410 0.2147438             1       1
##  [97,]  0.451227606 0.7057840             2       2
##  [98,]  0.145929302 0.2292715             1       1
##  [99,]  0.035067036 0.1675377             1       1
## [100,]  0.296801022 0.7375518             2       2
sum(hidden_states==Fstates)
## [1] 100