# HFMfit: Fit a HFM Model In treeHFM: Hidden Factor Graph Models

## Description

The functions fits an HFM (Hidden Factor Graph Model) to the (tree structured) data.

## Usage

 ```1 2``` ``` HFMfit(data,nHStates,dataNodeIndices,priorInit,transmatInit,transInitDiv, type,emissionProb=c(),SigmaInit=c(), muInit=c() ) ```

## Arguments

 `data` The observations. A list of measurements for every observation. An entry can be a matrix of size KxN, whereas K is the dimension of the data and N the lenght of the sequence or of size 1xK whereas the entries are discrete. `nHStates` Number of hidden states H `dataNodeIndices` The data structure. A list of the toplogy of every data entry of the format [parentIndex childIndex]. `priorInit` The inital state probabilities `transmatInit` The initial transtition probabilities for sequential transitions (H x H) `transInitDiv` The initial transtition probabilities for splitting events (H x (H*H) `type` Type of the data. ('d' discrete or 'c' continous) `emissionProb` Initial emission probilities for discrete data, a matrix of size E x H, whereas E are the number of discrete observable states. `SigmaInit` Initial covariance matrix for continous data `muInit` Initial means for continous data

## Details

The algorithm fits an HFM to sequential data (in this case essentially a Hidden Markov Model is fitted) or to data that is structured as a binary tree. An HFM consists of two transition probability matrices. One for the sequential transitions and one for transitions where a splitting happens.

## Value

An HFM:

 `\$initProb` The initial probabilities (prior). `\$transMatSeq` The transition matrix for sequential events `\$transMatDiv` The transition matrix for division events `\$ll` The log likelihood `\$emission` The emission probabilities of the hidden states

## Author(s)

 ``` 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49``` ```## Fit a discrete treeHFM ## nOStates = 3; nHStates = 2; # T = 1; nex = 10; obs1=sample(1:nOStates,5,replace=T) obs2=sample(1:nOStates,5,replace=T) data=list() data[]=obs1; # nodeIndices1=cbind(c(0,1,2,2,4),c(1,2,3,4,5)); dataNodeIndices=list() dataNodeIndices[]=nodeIndices1; prior1=array(1,nHStates); priorInit = array(1,nHStates)/nHStates; transmatInit = matrix(1,nHStates,nHStates)*(1/nHStates); transInitDiv= matrix(1,nHStates,nHStates*nHStates)*(1/(nHStates*nHStates)) obsmatInit = matrix(sample(1:nOStates,nHStates*nOStates,replace=T),nHStates,nOStates); obsmatInit=obsmatInit/rowSums(obsmatInit,1); hfm=HFMfit(data,nHStates,dataNodeIndices,priorInit,transmatInit,transInitDiv, 'd',emissionProb=obsmatInit); ## Fit a continous treeHFM ## nHStates = 2; ########create observation sequences######## obs1 <- rbind(runif(10,0,1),runif(10,0,1)) obs2 <- rbind(runif(8,0,1),runif(8,0,1)) data=list() data[]=obs1 data[]=obs2 ######### create guesses for gaussian covariance matrix and means ######### mc2 <- Mclust(t(cbind(obs1,obs2)), G=2) muInit=mc2\$parameters\$mean; SigmaInit=mc2\$parameters\$variance\$sigma #########create tree topology#################################### nodeIndices1=cbind(c(0,1,2,3,3,4,5,6,7,8),c(1,2,3,4,5,6,7,8,9,10)); nodeIndices2=cbind(c(0,1,2,3,4,4,5,6),c(1,2,3,4,5,6,7,8)); dataNodeIndices=list() dataNodeIndices[]=nodeIndices1; dataNodeIndices[]=nodeIndices2; ######### create guesses for prior and transition matrices######### prior1=array(1,nHStates); priorInit = array(1,nHStates)/nHStates; transmatInit = matrix(1,nHStates,nHStates)*(1/nHStates); transInitDiv= matrix(1,nHStates,nHStates*nHStates)*(1/(nHStates*nHStates)) # hfm=HFMfit(data,nHStates,dataNodeIndices,priorInit,transmatInit,transInitDiv,'c', SigmaInit=SigmaInit, muInit=muInit); # ```