R/seqientdiff.R

Defines functions seqientdiff

## =======================
## Within Sequence Entropy
## =======================

seqientdiff <- function(seqdata, norm=TRUE) {

	if (!inherits(seqdata, "stslist"))
		stop("data is NOT a sequence object, see seqdef function to create one")

    entropydiff <- function(dur, norm) {
		len <- sum(dur)
		## If we don't check, the funcion returns NA (division by 0)
		ent <- entropy(dur)
		if(ent>0 && norm) {
			## The maximum entropy is when length of the DSS=length of the sequence 
			p <- 1/len
			entmax <- (-len)*(p*log(p))
			ent <- ent/entmax
		}
		return(ent)
    }
	iseqtab <- seqdur(seqdata)
	iseqtab[is.na(iseqtab)] <- 0
	ient <- apply(iseqtab,1,entropydiff, norm=norm)
    
	ient <- as.matrix(ient)
	colnames(ient) <- "Hdss"
	rownames(ient) <- paste("[",seq(1:length(ient)),"]",sep="")
	return(ient)
}	

Try the TraMineR package in your browser

Any scripts or data that you put into this service are public.

TraMineR documentation built on Jan. 9, 2024, 3:02 p.m.