TheoMichelot / hmmTMB

Fit hidden Markov models using Template Model Builder (TMB): flexible state-dependent distributions, transition probability structures, random effects, and smoothing splines.
53 stars 7 forks source link

Persisting error "Log-likelihood is NaN or infinite at starting parameters" on dataset with many very small values #21

Closed orijitghosh closed 7 months ago

orijitghosh commented 7 months ago

Hi, I was trying to implement hmmTMB on my data (1440 timepoints for each minutes of the day) which has large number of zeroes or very small values (1e-03). By large numbers, I mean sometimes almost 80% of the timepoints. However, every time I have been getting this error:

Error in self$setup(silent = silent) : 
  Log-likelihood is NaN or infinite at starting parameters. Check that the data are within the domain of definition of the observation distributions, or try other starting parameters.

Data:

structure(list(t = c(86460, 86520, 86580, 86640, 86700, 86760, 86820, 86880, 86940, 87000, 87060, 87120, 87180, 87240, 87300, 87360, 87420, 87480, 87540, 87600, 87660, 87720, 87780, 87840, 87900, 87960, 88020, 88080, 88140, 88200, 88260, 88320, 88380, 88440, 88500, 88560, 88620, 88680, 88740, 88800, 88860, 88920, 88980, 89040, 89100, 89160, 89220, 89280, 89340, 89400, 89460, 89520, 89580, 89640, 89700, 89760, 89820, 89880, 89940, 90000, 90060, 90120, 90180, 90240, 90300, 90360, 90420, 90480, 90540, 90600, 90660, 90720, 90780, 90840, 90900, 90960, 91020, 91080, 91140, 91200, 91260, 91320, 91380, 91440, 91500, 91560, 91620, 91680, 91740, 91800, 91860, 91920, 91980, 92040, 92100, 92160, 92220, 92280, 92340, 92400, 92460, 92520, 92580, 92640, 92700, 92760, 92820, 92880, 92940, 93000, 93060, 93120, 93180, 93240, 93300, 93360, 93420, 93480, 93540, 93600, 93660, 93720, 93780, 93840, 93900, 93960, 94020, 94080, 94140, 94200, 94260, 94320, 94380, 94440, 94500, 94560, 94620, 94680, 94740, 94800, 94860, 94920, 94980, 95040, 95100, 95160, 95220, 95280, 95340, 95400, 95460, 95520, 95580, 95640, 95700, 95760, 95820, 95880, 95940, 96000, 96060, 96120, 96180, 96240, 96300, 96360, 96420, 96480, 96540, 96600, 96660, 96720, 96780, 96840, 96900, 96960, 97020, 97080, 97140, 97200, 97260, 97320, 97380, 97440, 97500, 97560, 97620, 97680, 97740, 97800, 97860, 97920, 97980, 98040, 98100, 98160, 98220, 98280, 98340, 98400, 98460, 98520, 98580, 98640, 98700, 98760, 98820, 98880, 98940, 99000, 99060, 99120, 99180, 99240, 99300, 99360, 99420, 99480, 99540, 99600, 99660, 99720, 99780, 99840, 99900, 99960, 100020, 100080, 100140, 100200, 100260, 100320, 100380, 100440, 100500, 100560, 100620, 100680, 100740, 100800, 100860, 100920, 100980, 101040, 101100, 101160, 101220, 101280, 101340, 101400, 101460, 101520, 101580, 101640, 101700, 101760, 101820, 101880, 101940, 102000, 102060, 102120, 102180, 102240, 102300, 102360, 102420, 102480, 102540, 102600, 102660, 102720, 102780, 102840, 102900, 102960, 103020, 103080, 103140, 103200, 103260, 103320, 103380, 103440, 103500, 103560, 103620, 103680, 103740, 103800, 103860, 103920, 103980, 104040, 104100, 104160, 104220, 104280, 104340, 104400, 104460, 104520, 104580, 104640, 104700, 104760, 104820, 104880, 104940, 105000, 105060, 105120, 105180, 105240, 105300, 105360, 105420, 105480, 105540, 105600, 105660, 105720, 105780, 105840, 105900, 105960, 106020, 106080, 106140, 106200, 106260, 106320, 106380, 106440, 106500, 106560, 106620, 106680, 106740, 106800, 106860, 106920, 106980, 107040, 107100, 107160, 107220, 107280, 107340, 107400, 107460, 107520, 107580, 107640, 107700, 107760, 107820, 107880, 107940, 108000, 108060, 108120, 108180, 108240, 108300, 108360, 108420, 108480, 108540, 108600, 108660, 108720, 108780, 108840, 108900, 108960, 109020, 109080, 109140, 109200, 109260, 109320, 109380, 109440, 109500, 109560, 109620, 109680, 109740, 109800, 109860, 109920, 109980, 110040, 110100, 110160, 110220, 110280, 110340, 110400, 110460, 110520, 110580, 110640, 110700, 110760, 110820, 110880, 110940, 111000, 111060, 111120, 111180, 111240, 111300, 111360, 111420, 111480, 111540, 111600, 111660, 111720, 111780, 111840, 111900, 111960, 112020, 112080, 112140, 112200, 112260, 112320, 112380, 112440, 112500, 112560, 112620, 112680, 112740, 112800, 112860, 112920, 112980, 113040, 113100, 113160, 113220, 113280, 113340, 113400, 113460, 113520, 113580, 113640, 113700, 113760, 113820, 113880, 113940, 114000, 114060, 114120, 114180, 114240, 114300, 114360, 114420, 114480, 114540, 114600, 114660, 114720, 114780, 114840, 114900, 114960, 115020, 115080, 115140, 115200, 115260, 115320, 115380, 115440, 115500, 115560, 115620, 115680, 115740, 115800, 115860, 115920, 115980, 116040, 116100, 116160, 116220, 116280, 116340, 116400, 116460, 116520, 116580, 116640, 116700, 116760, 116820, 116880, 116940, 117000, 117060, 117120, 117180, 117240, 117300, 117360, 117420, 117480, 117540, 117600, 117660, 117720, 117780, 117840, 117900, 117960, 118020, 118080, 118140, 118200, 118260, 118320, 118380, 118440, 118500, 118560, 118620, 118680, 118740, 118800, 118860, 118920, 118980, 119040, 119100, 119160, 119220, 119280, 119340, 119400, 119460, 119520, 119580, 119640, 119700, 119760, 119820, 119880, 119940, 120000, 120060, 120120, 120180, 120240, 120300, 120360, 120420, 120480, 120540, 120600, 120660, 120720, 120780, 120840, 120900, 120960, 121020, 121080, 121140, 121200, 121260, 121320, 121380, 121440, 121500, 121560, 121620, 121680, 121740, 121800, 121860, 121920, 121980, 122040, 122100, 122160, 122220, 122280, 122340, 122400, 122460, 122520, 122580, 122640, 122700, 122760, 122820, 122880, 122940, 123000, 123060, 123120, 123180, 123240, 123300, 123360, 123420, 123480, 123540, 123600, 123660, 123720, 123780, 123840, 123900, 123960, 124020, 124080, 124140, 124200, 124260, 124320, 124380, 124440, 124500, 124560, 124620, 124680, 124740, 124800, 124860, 124920, 124980, 125040, 125100, 125160, 125220, 125280, 125340, 125400, 125460, 125520, 125580, 125640, 125700, 125760, 125820, 125880, 125940, 126000, 126060, 126120, 126180, 126240, 126300, 126360, 126420, 126480, 126540, 126600, 126660, 126720, 126780, 126840, 126900, 126960, 127020, 127080, 127140, 127200, 127260, 127320, 127380, 127440, 127500, 127560, 127620, 127680, 127740, 127800, 127860, 127920, 127980, 128040, 128100, 128160, 128220, 128280, 128340, 128400, 128460, 128520, 128580, 128640, 128700, 128760, 128820, 128880, 128940, 129000, 129060, 129120, 129180, 129240, 129300, 129360, 129420, 129480, 129540, 129600, 129660, 129720, 129780, 129840, 129900, 129960, 130020, 130080, 130140, 130200, 130260, 130320, 130380, 130440, 130500, 130560, 130620, 130680, 130740, 130800, 130860, 130920, 130980, 131040, 131100, 131160, 131220, 131280, 131340, 131400, 131460, 131520, 131580, 131640, 131700, 131760, 131820, 131880, 131940, 132000, 132060, 132120, 132180, 132240, 132300, 132360, 132420, 132480, 132540, 132600, 132660, 132720, 132780, 132840, 132900, 132960, 133020, 133080, 133140, 133200, 133260, 133320, 133380, 133440, 133500, 133560, 133620, 133680, 133740, 133800, 133860, 133920, 133980, 134040, 134100, 134160, 134220, 134280, 134340, 134400, 134460, 134520, 134580, 134640, 134700, 134760, 134820, 134880, 134940, 135000, 135060, 135120, 135180, 135240, 135300, 135360, 135420, 135480, 135540, 135600, 135660, 135720, 135780, 135840, 135900, 135960, 136020, 136080, 136140, 136200, 136260, 136320, 136380, 136440, 136500, 136560, 136620, 136680, 136740, 136800, 136860, 136920, 136980, 137040, 137100, 137160, 137220, 137280, 137340, 137400, 137460, 137520, 137580, 137640, 137700, 137760, 137820, 137880, 137940, 138000, 138060, 138120, 138180, 138240, 138300, 138360, 138420, 138480, 138540, 138600, 138660, 138720, 138780, 138840, 138900, 138960, 139020, 139080, 139140, 139200, 139260, 139320, 139380, 139440, 139500, 139560, 139620, 139680, 139740, 139800, 139860, 139920, 139980, 140040, 140100, 140160, 140220, 140280, 140340, 140400, 140460, 140520, 140580, 140640, 140700, 140760, 140820, 140880, 140940, 141000, 141060, 141120, 141180, 141240, 141300, 141360, 141420, 141480, 141540, 141600, 141660, 141720, 141780, 141840, 141900, 141960, 142020, 142080, 142140, 142200, 142260, 142320, 142380, 142440, 142500, 142560, 142620, 142680, 142740, 142800, 142860, 142920, 142980, 143040, 143100, 143160, 143220, 143280, 143340, 143400, 143460, 143520, 143580, 143640, 143700, 143760, 143820, 143880, 143940, 144000, 144060, 144120, 144180, 144240, 144300, 144360, 144420, 144480, 144540, 144600, 144660, 144720, 144780, 144840, 144900, 144960, 145020, 145080, 145140, 145200, 145260, 145320, 145380, 145440, 145500, 145560, 145620, 145680, 145740, 145800, 145860, 145920, 145980, 146040, 146100, 146160, 146220, 146280, 146340, 146400, 146460, 146520, 146580, 146640, 146700, 146760, 146820, 146880, 146940, 147000, 147060, 147120, 147180, 147240, 147300, 147360, 147420, 147480, 147540, 147600, 147660, 147720, 147780, 147840, 147900, 147960, 148020, 148080, 148140, 148200, 148260, 148320, 148380, 148440, 148500, 148560, 148620, 148680, 148740, 148800, 148860, 148920, 148980, 149040, 149100, 149160, 149220, 149280, 149340, 149400, 149460, 149520, 149580, 149640, 149700, 149760, 149820, 149880, 149940, 150000, 150060, 150120, 150180, 150240, 150300, 150360, 150420, 150480, 150540, 150600, 150660, 150720, 150780, 150840, 150900, 150960, 151020, 151080, 151140, 151200, 151260, 151320, 151380, 151440, 151500, 151560, 151620, 151680, 151740, 151800, 151860, 151920, 151980, 152040, 152100, 152160, 152220, 152280, 152340, 152400, 152460, 152520, 152580, 152640, 152700, 152760, 152820, 152880, 152940, 153000, 153060, 153120, 153180, 153240, 153300, 153360, 153420, 153480, 153540, 153600, 153660, 153720, 153780, 153840, 153900, 153960, 154020, 154080, 154140, 154200, 154260, 154320, 154380, 154440, 154500, 154560, 154620, 154680, 154740, 154800, 154860, 154920, 154980, 155040, 155100, 155160, 155220, 155280, 155340, 155400, 155460, 155520, 155580, 155640, 155700, 155760, 155820, 155880, 155940, 156000, 156060, 156120, 156180, 156240, 156300, 156360, 156420, 156480, 156540, 156600, 156660, 156720, 156780, 156840, 156900, 156960, 157020, 157080, 157140, 157200, 157260, 157320, 157380, 157440, 157500, 157560, 157620, 157680, 157740, 157800, 157860, 157920, 157980, 158040, 158100, 158160, 158220, 158280, 158340, 158400, 158460, 158520, 158580, 158640, 158700, 158760, 158820, 158880, 158940, 159000, 159060, 159120, 159180, 159240, 159300, 159360, 159420, 159480, 159540, 159600, 159660, 159720, 159780, 159840, 159900, 159960, 160020, 160080, 160140, 160200, 160260, 160320, 160380, 160440, 160500, 160560, 160620, 160680, 160740, 160800, 160860, 160920, 160980, 161040, 161100, 161160, 161220, 161280, 161340, 161400, 161460, 161520, 161580, 161640, 161700, 161760, 161820, 161880, 161940, 162000, 162060, 162120, 162180, 162240, 162300, 162360, 162420, 162480, 162540, 162600, 162660, 162720, 162780, 162840, 162900, 162960, 163020, 163080, 163140, 163200, 163260, 163320, 163380, 163440, 163500, 163560, 163620, 163680, 163740, 163800, 163860, 163920, 163980, 164040, 164100, 164160, 164220, 164280, 164340, 164400, 164460, 164520, 164580, 164640, 164700, 164760, 164820, 164880, 164940, 165000, 165060, 165120, 165180, 165240, 165300, 165360, 165420, 165480, 165540, 165600, 165660, 165720, 165780, 165840, 165900, 165960, 166020, 166080, 166140, 166200, 166260, 166320, 166380, 166440, 166500, 166560, 166620, 166680, 166740, 166800, 166860, 166920, 166980, 167040, 167100, 167160, 167220, 167280, 167340, 167400, 167460, 167520, 167580, 167640, 167700, 167760, 167820, 167880, 167940, 168000, 168060, 168120, 168180, 168240, 168300, 168360, 168420, 168480, 168540, 168600, 168660, 168720, 168780, 168840, 168900, 168960, 169020, 169080, 169140, 169200, 169260, 169320, 169380, 169440, 169500, 169560, 169620, 169680, 169740, 169800, 169860, 169920, 169980, 170040, 170100, 170160, 170220, 170280, 170340, 170400, 170460, 170520, 170580, 170640, 170700, 170760, 170820, 170880, 170940, 171000, 171060, 171120, 171180, 171240, 171300, 171360, 171420, 171480, 171540, 171600, 171660, 171720, 171780, 171840, 171900, 171960, 172020, 172080, 172140, 172200, 172260, 172320, 172380, 172440, 172500, 172560, 172620, 172680, 172740, 172800), normact = c(0.29940119760479, 0.29940119760479, 0.29940119760479, 0.001, 0.898203592814371, 0.29940119760479, 0.001, 0.898203592814371, 0.29940119760479, 0.001, 0.598802395209581, 0.29940119760479, 0.598802395209581, 0.29940119760479, 0.29940119760479, 0.598802395209581, 0.598802395209581, 0.29940119760479, 0.598802395209581, 0.001, 0.898203592814371, 0.598802395209581, 0.598802395209581, 0.598802395209581, 0.29940119760479, 0.001, 0.598802395209581, 0.598802395209581, 0.598802395209581, 0.598802395209581, 0.29940119760479, 0.29940119760479, 0.001, 0.29940119760479, 0.898203592814371, 0.001, 0.29940119760479, 0.29940119760479, 0.001, 0.001, 0.29940119760479, 0.598802395209581, 0.598802395209581, 0.598802395209581, 0.29940119760479, 0.29940119760479, 0.29940119760479, 0.29940119760479, 0.29940119760479, 0.29940119760479, 0.598802395209581, 0.29940119760479, 0.001, 0.29940119760479, 0.001, 0.898203592814371, 0.001, 0.001, 0.001, 0.001, 0.29940119760479, 0.001, 0.29940119760479, 0.898203592814371, 0.598802395209581, 0.001, 0.001, 0.29940119760479, 0.29940119760479, 0.598802395209581, 0.598802395209581, 0.29940119760479, 0.001, 0.29940119760479, 0.598802395209581, 0.29940119760479, 0.29940119760479, 0.001, 0.29940119760479, 0.29940119760479, 0.001, 0.001, 0.001, 0.001, 0.001, 0.29940119760479, 0.29940119760479, 0.001, 0.001, 0.001, 0.001, 0.001, 0.001, 0.001, 0.001, 0.001, 0.001, 0.001, 0.001, 0.001, 0.001, 0.001, 0.001, 0.001, 0.001, 0.598802395209581, 0.001, 0.001, 0.001, 0.001, 0.001, 0.001, 0.001, 0.001, 0.001, 0.001, 0.001, 0.001, 0.001, 0.001, 0.598802395209581, 0.598802395209581, 0.598802395209581, 0.598802395209581, 0.29940119760479, 0.001, 0.001, 0.29940119760479, 0.29940119760479, 0.001, 0.001, 0.001, 0.001, 0.001, 0.001, 0.001, 0.001, 0.001, 0.001, 0.001, 0.001, 0.001, 0.001, 0.001, 0.001, 0.001, 0.001, 0.001, 0.001, 0.001, 0.001, 0.001, 0.001, 0.001, 0.001, 0.001, 0.001, 0.001, 0.001, 0.001, 0.001, 0.001, 0.001, 0.001, 0.001, 0.001, 0.001, 0.001, 0.001, 0.001, 0.001, 0.001, 0.001, 0.001, 0.001, 0.001, 0.001, 0.001, 0.001, 0.001, 0.001, 0.001, 0.001, 0.001, 0.001, 0.001, 0.001, 0.001, 0.001, 0.001, 0.001, 0.001, 0.001, 0.001, 0.001, 0.001, 0.001, 0.001, 0.001, 0.001, 0.001, 0.001, 0.001, 0.001, 0.001, 0.001, 0.001, 0.001, 0.001, 0.001, 0.001, 0.001, 0.001, 0.001, 0.001, 0.001, 0.001, 0.001, 0.001, 0.001, 0.001, 0.001, 0.001, 0.001, 0.001, 0.001, 0.001, 0.001, 0.001, 0.001, 0.001, 0.001, 0.001, 0.001, 0.001, 0.001, 0.001, 0.001, 0.001, 0.001, 0.001, 0.001, 0.001, 0.001, 0.001, 0.001, 0.001, 0.001, 0.001, 0.001, 0.001, 0.001, 0.001, 0.001, 0.001, 0.001, 0.001, 0.001, 0.001, 0.001, 0.001, 0.001, 0.001, 0.001, 0.001, 0.001, 0.001, 0.001, 0.001, 0.001, 0.001, 0.001, 0.001, 0.001, 0.001, 0.001, 0.001, 0.001, 0.001, 0.001, 0.001, 0.001, 0.001, 0.001, 0.001, 0.001, 0.001, 0.001, 0.001, 0.001, 0.001, 0.001, 0.001, 0.001, 0.001, 0.001, 0.001, 0.001, 0.001, 0.001, 0.001, 0.001, 0.001, 0.001, 0.001, 0.001, 0.001, 0.001, 0.001, 0.001, 0.001, 0.001, 0.001, 0.001, 0.001, 0.001, 0.001, 0.001, 0.001, 0.001, 0.001, 0.001, 0.001, 0.001, 0.001, 0.001, 0.001, 0.001, 0.001, 0.001, 0.001, 0.001, 0.001, 0.001, 0.001, 0.001, 0.001, 0.001, 0.001, 0.001, 0.001, 0.001, 0.001, 0.001, 0.001, 0.001, 0.001, 0.001, 0.001, 0.001, 0.001, 0.001, 0.001, 0.001, 0.001, 0.001, 0.001, 0.001, 0.001, 0.001, 0.001, 0.001, 0.001, 0.001, 0.001, 0.001, 0.001, 0.001, 0.001, 0.001, 0.001, 0.29940119760479, 0.001, 0.001, 0.001, 0.001, 0.001, 0.001, 0.001, 0.001, 0.001, 0.001, 0.001, 0.001, 0.001, 0.001, 0.001, 0.001, 0.001, 0.001, 0.001, 0.001, 0.001, 0.001, 0.001, 0.001, 0.29940119760479, 0.001, 0.001, 0.001, 0.001, 0.001, 0.001, 0.001, 0.001, 0.001, 0.001, 0.001, 0.001, 0.001, 0.001, 0.001, 0.001, 0.001, 0.001, 0.001, 0.001, 0.001, 0.001, 0.001, 0.001, 0.001, 0.001, 0.001, 0.001, 0.001, 0.001, 0.001, 0.001, 0.001, 0.001, 0.001, 0.001, 0.001, 0.001, 0.001, 0.001, 0.001, 0.001, 0.001, 0.001, 0.001, 0.001, 0.001, 0.001, 0.001, 0.001, 0.001, 0.001, 0.001, 0.001, 0.001, 0.001, 0.001, 0.001, 0.001, 0.001, 0.001, 0.001, 0.001, 0.001, 0.001, 0.001, 0.001, 0.001, 0.001, 0.001, 0.001, 0.001, 0.001, 0.001, 0.001, 0.001, 0.001, 0.001, 0.001, 0.001, 0.001, 0.001, 0.001, 0.001, 0.001, 0.001, 0.001, 0.001, 0.001, 0.001, 0.001, 0.001, 0.001, 0.001, 0.001, 0.001, 0.001, 0.001, 0.001, 0.001, 0.001, 0.001, 0.001, 0.001, 0.001, 0.001, 0.001, 0.001, 0.001, 0.001, 0.001, 0.001, 0.001, 0.001, 0.001, 0.001, 0.001, 0.001, 0.001, 0.001, 0.001, 0.001, 0.001, 0.001, 0.001, 0.001, 0.001, 0.001, 0.001, 0.001, 0.001, 0.001, 0.001, 0.001, 0.001, 0.001, 0.001, 0.001, 0.001, 0.001, 0.001, 0.001, 0.001, 0.001, 0.001, 0.001, 0.001, 0.001, 0.001, 0.001, 0.001, 0.001, 0.001, 0.001, 0.001, 0.001, 0.001, 0.001, 0.001, 0.001, 0.29940119760479, 0.001, 0.29940119760479, 0.001, 0.001, 0.001, 0.001, 0.001, 0.598802395209581, 0.598802395209581, 0.001, 0.001, 0.001, 0.001, 0.001, 0.001, 0.29940119760479, 0.29940119760479, 0.001, 0.29940119760479, 0.598802395209581, 0.29940119760479, 0.001, 0.29940119760479, 0.001, 0.001, 0.001, 0.29940119760479, 0.001, 0.001, 0.001, 0.001, 0.001, 0.001, 0.001, 0.001, 0.29940119760479, 0.001, 0.001, 0.598802395209581, 0.29940119760479, 0.001, 0.001, 0.001, 0.29940119760479, 0.001, 0.001, 0.001, 0.001, 0.001, 0.001, 0.001, 0.001, 0.001, 0.001, 0.001, 0.001, 0.29940119760479, 0.29940119760479, 0.001, 0.598802395209581, 0.29940119760479, 0.598802395209581, 0.898203592814371, 0.29940119760479, 0.598802395209581, 0.001, 0.29940119760479, 0.001, 0.001, 0.001, 0.598802395209581, 0.001, 0.001, 0.001, 0.29940119760479, 0.29940119760479, 0.001, 2.69461077844311, 2.69461077844311, 0.001, 0.001, 0.001, 0.001, 0.29940119760479, 0.001, 2.69461077844311, 2.69461077844311, 0.001, 0.598802395209581, 0.29940119760479, 0.001, 0.29940119760479, 0.001, 0.001, 0.001, 0.001, 0.001, 0.001, 0.001, 0.001, 0.001, 0.001, 0.001, 0.29940119760479, 0.001, 0.598802395209581, 0.29940119760479, 0.29940119760479, 0.29940119760479, 0.001, 0.598802395209581, 0.29940119760479, 0.598802395209581, 0.001, 0.29940119760479, 0.001, 0.001, 0.001, 0.001, 0.29940119760479, 0.001, 0.29940119760479, 0.001, 0.001, 0.001, 0.001, 0.001, 0.598802395209581, 0.29940119760479, 0.001, 0.29940119760479, 0.29940119760479, 0.001, 0.001, 0.598802395209581, 0.29940119760479, 0.29940119760479, 0.001, 0.29940119760479, 0.29940119760479, 0.29940119760479, 0.001, 0.598802395209581, 0.29940119760479, 0.001, 0.29940119760479, 0.29940119760479, 0.29940119760479, 1.79640718562874, 0.598802395209581, 0.001, 0.29940119760479, 0.898203592814371, 0.29940119760479, 0.29940119760479, 0.29940119760479, 0.29940119760479, 0.001, 0.001, 0.001, 0.001, 0.29940119760479, 0.598802395209581, 0.598802395209581, 0.598802395209581, 0.598802395209581, 0.598802395209581, 0.598802395209581, 0.598802395209581, 0.29940119760479, 1.49700598802395, 0.29940119760479, 0.598802395209581, 0.598802395209581, 0.29940119760479, 0.29940119760479, 0.001, 0.598802395209581, 0.898203592814371, 0.598802395209581, 0.001, 0.898203592814371, 0.29940119760479, 1.19760479041916, 0.29940119760479, 0.598802395209581, 0.598802395209581, 0.598802395209581, 0.29940119760479, 0.598802395209581, 0.898203592814371, 0.598802395209581, 0.29940119760479, 0.001, 0.29940119760479, 0.29940119760479, 0.598802395209581, 0.598802395209581, 1.49700598802395, 0.001, 0.598802395209581, 0.598802395209581, 0.29940119760479, 0.29940119760479, 0.001, 0.001, 0.598802395209581, 0.29940119760479, 0.29940119760479, 0.001, 0.001, 0.898203592814371, 0.29940119760479, 0.598802395209581, 0.001, 0.001, 0.598802395209581, 0.001, 0.001, 0.001, 0.001, 0.001, 0.001, 0.001, 0.001, 0.001, 0.001, 0.001, 0.001, 0.001, 0.001, 0.001, 0.001, 0.001, 0.001, 0.001, 0.001, 0.001, 0.001, 0.001, 0.001, 0.001, 0.001, 0.001, 0.001, 0.001, 0.001, 0.001, 0.001, 0.001, 0.001, 0.001, 0.001, 0.001, 0.001, 0.001, 0.001, 0.001, 0.001, 0.001, 0.001, 0.001, 0.001, 0.001, 0.001, 0.001, 0.001, 0.001, 0.001, 0.001, 0.001, 0.001, 0.001, 0.001, 0.001, 0.001, 0.001, 0.001, 0.001, 0.001, 0.001, 0.001, 0.001, 0.001, 0.001, 0.001, 0.001, 0.001, 0.001, 0.001, 0.001, 0.001, 0.001, 0.001, 0.001, 0.001, 0.001, 0.001, 0.001, 0.001, 0.001, 0.001, 0.001, 0.001, 0.001, 0.001, 0.001, 0.001, 0.001, 0.001, 0.001, 0.001, 0.001, 0.001, 0.001, 0.001, 0.001, 0.001, 0.001, 0.001, 0.001, 0.001, 0.001, 0.001, 0.001, 0.001, 0.001, 0.001, 0.001, 0.001, 0.001, 0.001, 0.001, 0.001, 0.001, 0.001, 0.001, 0.001, 0.001, 0.001, 0.001, 0.001, 0.001, 0.001, 0.001, 0.001, 0.001, 0.001, 0.001, 0.001, 0.001, 0.001, 0.001, 0.001, 0.001, 0.001, 0.001, 0.001, 0.001, 0.001, 0.001, 0.001, 0.001, 0.001, 0.001, 0.001, 0.001, 0.001, 0.001, 0.001, 0.001, 0.001, 0.001, 0.001, 0.001, 0.001, 0.001, 0.001, 0.001, 0.001, 0.001, 0.001, 0.001, 0.001, 0.001, 0.001, 0.001, 0.001, 0.001, 0.001, 0.001, 0.001, 0.001, 0.001, 0.001, 0.001, 0.001, 0.001, 0.001, 0.001, 0.001, 0.001, 0.001, 0.001, 0.001, 0.001, 0.001, 0.001, 0.001, 0.001, 0.001, 0.001, 0.001, 0.001, 0.001, 0.001, 0.001, 0.001, 0.001, 0.001, 0.001, 0.001, 0.001, 0.001, 0.001, 0.001, 0.001, 0.001, 0.001, 0.001, 0.001, 0.001, 0.001, 0.001, 0.001, 0.001, 0.001, 0.001, 0.001, 0.001, 0.001, 0.001, 0.001, 0.001, 0.001, 0.001, 0.001, 0.001, 0.001, 0.001, 0.001, 0.001, 0.001, 0.001, 0.001, 0.001, 0.001, 0.001, 0.001, 0.001, 0.001, 0.001, 0.001, 0.001, 0.001, 0.001, 0.001, 0.001, 0.001, 0.001, 0.001, 0.001, 0.001, 0.001, 0.001, 0.001, 0.001, 0.001, 0.001, 0.001, 0.001, 0.001, 0.001, 0.001, 0.001, 0.001, 0.001, 0.001, 0.001, 0.001, 0.001, 0.001, 0.001, 0.001, 0.001, 0.001, 0.001, 0.001, 0.001, 0.001, 0.001, 0.001, 0.001, 0.001, 0.001, 0.001, 0.001, 0.001, 0.001, 0.001, 0.001, 0.001, 0.001, 0.001, 0.001, 0.001, 0.001, 0.001, 0.001, 0.001, 0.001, 0.001, 0.001, 0.001, 0.001, 0.001, 0.001, 0.001, 0.001, 0.001, 0.001, 0.001, 0.001, 0.001, 0.001, 0.001, 0.001, 0.001, 0.001, 0.001, 0.001, 0.001, 0.001, 0.001, 0.001, 0.001, 0.001, 0.001, 0.001, 0.001, 0.001, 0.001, 0.001, 0.001, 0.001, 0.001, 0.001, 0.001, 0.001, 0.001, 0.001, 0.001, 0.001, 0.001, 0.001, 0.001, 0.001, 0.001, 0.001, 0.001, 0.001, 0.001, 0.001, 0.001, 0.001, 0.001, 0.001, 0.001, 0.001, 0.001, 0.001, 0.001, 0.001, 0.001, 0.001, 0.001, 0.001, 0.001, 0.001, 0.001, 0.001, 0.001, 0.001, 0.001, 0.001, 0.001, 0.001, 0.001, 0.001, 0.001, 0.001, 0.001, 0.001, 0.001, 0.001, 0.001, 0.001, 0.001, 0.001, 0.001, 0.001, 0.001, 0.001, 0.001, 0.001, 0.001, 0.001, 0.001, 0.001, 0.001, 0.001, 0.001, 0.001, 0.001, 0.001, 0.001, 0.001, 0.001, 0.001, 0.001, 0.001, 0.001, 0.001, 0.001, 0.001, 0.001, 0.001, 0.001, 0.001, 0.001, 0.001, 0.001, 0.001, 0.001, 0.001, 0.001, 0.001, 0.001, 0.001, 0.001, 0.001, 0.001, 0.001, 0.001, 0.001, 0.001, 0.001, 0.001, 0.001, 0.001, 0.001, 0.001, 0.001, 0.001, 0.001, 0.001, 0.001, 0.001, 0.001, 0.001, 0.001, 0.001, 0.001, 0.001, 0.001, 0.001, 0.001, 0.001, 0.001, 0.001, 0.001, 0.001, 0.001, 0.001, 0.001, 0.001, 0.001, 0.001, 0.001, 0.001, 0.001, 0.001, 0.001, 0.001, 0.001, 0.001, 0.001, 0.001, 0.001, 0.001, 0.001, 0.001, 0.001, 0.001, 0.001, 0.001, 0.001, 0.001, 0.001, 0.001, 0.001, 0.001, 0.001, 0.001, 0.001, 0.001, 0.001, 0.001, 0.001, 0.001, 0.001, 0.001, 0.001, 0.001, 0.001, 0.001, 0.001, 0.001, 0.001, 0.001, 0.001, 0.001, 0.001, 0.001, 0.001, 0.001, 0.001, 0.001, 0.001, 0.001, 0.001, 0.001, 0.001, 0.001, 0.001, 0.001, 0.001, 0.001, 0.001, 0.001, 0.001, 0.001, 0.001, 0.001, 0.001, 0.001, 0.001, 0.001, 0.001, 0.001, 0.001, 0.001, 0.001, 0.001, 0.001, 0.001, 0.001, 0.001, 0.001, 0.001, 0.001, 0.001, 0.001, 0.001, 0.001, 0.001, 0.001, 0.001, 0.001, 0.001, 0.001, 0.001, 0.001, 0.001, 0.001, 0.001, 0.001, 0.001, 0.001, 0.001, 0.001, 0.001, 0.001, 0.001, 0.001, 0.001, 0.001, 0.001, 0.001, 0.001, 0.001, 0.001, 0.001, 0.001, 0.001, 0.001, 0.001, 0.001, 0.001, 0.001, 0.001, 0.001, 0.001, 0.001, 0.001, 0.001, 0.001, 0.001, 0.001, 0.001, 0.001, 0.001, 0.001, 0.001, 0.001, 0.001, 0.001, 0.001, 0.001, 0.001, 0.001, 0.001, 0.001, 0.001, 0.001, 0.001, 0.001, 0.001, 0.001, 0.001, 0.001, 0.001, 0.001, 0.001, 0.001, 0.001, 0.001, 0.001, 0.001, 0.001, 0.001, 0.001, 0.29940119760479, 0.29940119760479, 0.29940119760479, 0.29940119760479, 0.001, 0.001, 0.001, 0.001, 0.001, 0.001, 0.001, 0.001, 0.001, 0.001, 0.001, 0.598802395209581, 0.001, 0.001, 0.001, 0.001, 0.001, 0.001, 0.001, 0.001, 0.001, 0.001, 0.001, 0.001, 0.29940119760479, 1.19760479041916)), row.names = c(NA, -1440L), class = "data.frame")

Code:

hid1 <- MarkovChain$new(data = data, n_states = 4)
dists <- list(normact = "norm")
par0_2s <- list(normact = list(mean = c(0.001, 0.375, 0.625, 875), sd = c(0.0001, 0.01, 0.01, 0.01)))
obs1 <- Observation$new(data = data, n_states = 4, dists = dists, par = par0_2s)
obs1$par()
par0_new <- obs1$suggest_initial()
par0_new
obs1$update_par(par = par0_new)
obs1$par()
hmm1 <- HMM$new(obs = obs1, hid = hid1)
hmm1
hmm1$fit(silent = F)

Any idea what may be causing this?

Thank you for the nice documentation and examples!

TheoMichelot commented 7 months ago

Hi, it looks like the problem is that the distribution of the data is very different from being normal (or a mixture of normal distributions).

As you pointed out, there are many observations that are equal to 0.001, but beyond this, the variable normact only have 8 unique values in the data set. I noticed that almost all observations are a multiple of 0.2994012 (except the observations equal to 0.001). For this reason, the data look a little bit like a time series of integers, so one thing you could do is scale the observations to be {0, 1, 2, ..., 9}, and then fit a Poisson HMM with zero inflation. This would treat the observations as discrete (as opposed to normal of gamma HMMs), but ordered (as opposed to a categorical HMM). The zero inflation will account for the high proportion of zeros. You will have to decide whether this makes sense or not for your application.

# Transform observations to integers
data$normact <- round(data$normact / 0.2994012)

hid <- MarkovChain$new(data = data, n_states = 2)

# Define zero-inflated Poisson model for observations
dists <- list(normact = "zipois")
par0 <- list(normact = list(rate = c(0.5, 1.5), z = c(0.9, 0.1)))
obs <- Observation$new(data = data, dists = dists, 
                       n_states = 2, par = par0)

hmm <- HMM$new(obs = obs, hid = hid)

hmm$fit()

It looks like this identifies one state that captures almost all the zeros (and has high zero inflation parameter z), and an other state that captures larger observations.

> lapply(hmm$par(), round, 3)
$obspar
, , 1

             state 1 state 2
normact.rate   0.790   1.215
normact.z      0.993   0.089

$tpm
, , 1

        state 1 state 2
state 1   0.993   0.007
state 2   0.026   0.974

I noticed that you tried to use four states in your code. The more states you use, the more unstable the model fitting will be. I would generally recommend starting from a simple 2-state model, and building complexity from there if possible. But there might not be clear enough clusters in the data to identify four states here.

orijitghosh commented 7 months ago

Hi Theo, Thank you for the detailed reply. This looks like a problem inherent to the data. Unfortunately, for this example, I took an individual which had really less activity throughout the day (>90% of timepoints are zero), so when the activity counts are normalized, the values look multiples of 0.2994012. I then replaced the zeroes with small values. Around 5% of my data will be individuals like this. For most of my data, I used weibull distribution with 4 states for, and they do work, and inferred states make sense. I understand the solution you are suggesting. Maybe for this edge cases, a zero-inflated poisson model will be better. Thank you!