[Traminer-users] Script for computing z-scores for transition probabilities

Robbie Aert, van r.c.m.vanaert at tilburguniversity.edu
Thu Mar 7 16:20:11 CET 2013


Dear TraMineR users,

A couple of days ago, I sent an email with a newly developed function for
computing z-scores for transition probabilities. However, there was a
problem with handling missing data in the function. I would like to
apologize for this and below you can find the adjusted script.

Kind regards,
Robbie van Aert


######################################################################
SCRIPT
#################################################################################

seqtrate.z <- function(seq_obj) {
  tr.rates <- suppressMessages(round(seqtrate(seq_obj), 2))
  matrix <- as.matrix(seq_obj)                            # reformat to
matrix #
  tr <- table(c(matrix[,-ncol(matrix)]), c(matrix[,-1]))

  ### Check whether there are missings in the matrix.
  if('%' %in% colnames(tr) == TRUE) {
    tr.rev <- tr[-1,-1]
  } else { tr.rev <- tr }
  if('*' %in% colnames(tr) == TRUE) {
    tr.rev <- tr.rev[-1,-1] }

  ### Number of loops are number of rows in matrix.
  n_loops <- nrow(tr.rev)

  ### Empty arrays in which the data is going to be stored.
  res.Xg <- array(NA, dim = c(nrow(tr.rev), ncol(tr.rev)))
  res.Xt <- array(NA, dim = c(nrow(tr.rev), ncol(tr.rev)))
  res.Xgt <- array(NA, dim = c(nrow(tr.rev), ncol(tr.rev)))
  res.Mgt <- array(NA, dim = c(nrow(tr.rev), ncol(tr.rev)))
  res.Pg <- array(NA, dim = c(nrow(tr.rev), ncol(tr.rev)))
  res.Pt <- array(NA, dim = c(nrow(tr.rev), ncol(tr.rev)))

  ### Loops that are used for filling the arrays.
  for (i in 1:n_loops) {
    for (k in 1:n_loops) {
    ########################################
      res.Xg[i,k] <- sum(tr.rev[i, ])
 # Xg = sum observations in Gth row                 #
      res.Xt[i,k] <- sum(tr.rev[ ,k])
 # Xt = sum observations in Tth column             #
      res.Xgt[i,k] <- tr.rev[i,k]
   # Xgt = observed freq                                      #
      res.Mgt[i,k] <- ((res.Xg[i,k] * res.Xt[i,k]) / sum(tr.rev))    # Mgt
= expected freq based on data               #
      res.Pg[i,k] <- res.Xg[i,k] / sum(tr.rev)                           #
Pg = Xg divided by total N                           #
      res.Pt[i,k] <- res.Xt[i,k] / sum(tr.rev)
# Pt = Xt divided by total N                             #
    }
          ########################################
  }

  ### Formula for z-values.
  res.Zgt <- round((res.Xgt - res.Mgt) / (sqrt(res.Mgt*(1 - res.Pg)*(1 -
res.Pt))),3)

  ### Assign column and row names to matrix.
  column <- colnames(tr.rates)
  colnames(res.Zgt) <- column
  row <- rownames(tr.rates)
  rownames(res.Zgt) <- row

  return(res.Zgt)
}
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.r-forge.r-project.org/pipermail/traminer-users/attachments/20130307/9588b699/attachment.html>


More information about the Traminer-users mailing list