Hidden Markov models package in R

后端 未结 4 1003
感动是毒
感动是毒 2021-02-04 20:35

I need some help implementing a HMM module in R. I\'m new to R and don\'t have a lot of knowledge on it. So i have to implement an IE using HMM, i have 2 folders with files, on

4条回答
  •  滥情空心
    2021-02-04 21:01

    DepmixS4 is what you are looking for.

    First of all, you need to identify best number of hidden states for your model. This can be done by taking model with least value of AIC for different hidden states.

    I have created a function HMM_model_execution which will return the model variable and number of states for the model.

      library(depmixS4)
    
    • first column should be visible state and remaining external variable in doc_data

      HMM_model_execution<-function( doc_data, k) 
      
    • k number of total hidden state to compare

      {
           aic_values <- vector(mode="numeric", length=k-1) # to store AIC values
      
           for( i in 2:k)
           {
                 print(paste("loop counter",i))
                 mod <- depmix(response = doc_data$numpresc ~ 1, data = doc_data, nstates = i)
                 fm <- fit(mod, verbose = FALSE)
                 aic_values[i-1]<- AIC(fm)
                 #print(paste("Aic value at this index",aic_values[i-1]))
                 #writeLines("\n")
           }
      
           min_index<-which.min(aic_values)     
      
    • no of hidden states for best model

      #print(paste("index of minimum AIC",min_index))
      mod <- depmix(response = doc_data$numpresc ~ 1, data = doc_data, nstates = (min_index+1))
      fm <- fit(mod, verbose = FALSE)                                         
      
    • best model execution

      print(paste("best model with number of hidden states", min_index+1))
      return(c(fm, min_index+1))
      writeLines("\n")
      writeLines("\n")
      

    External variables( co-variates can be passed in function depmix ). summary (fm) will give you all model parameters.

提交回复
热议问题