Dear TraMineR users,<div><br></div><div>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.</div>
<div><br></div><div>Kind regards,</div><div>Robbie van Aert</div><div><br></div><div><br></div><div>###################################################################### SCRIPT #################################################################################</div>
<div><br></div><div><div>seqtrate.z <- function(seq_obj) {</div><div> tr.rates <- suppressMessages(round(seqtrate(seq_obj), 2))</div><div> matrix <- as.matrix(seq_obj) # reformat to matrix #</div>
<div> tr <- table(c(matrix[,-ncol(matrix)]), c(matrix[,-1]))</div><div> </div><div> ### Check whether there are missings in the matrix.</div><div> if('%' %in% colnames(tr) == TRUE) {</div><div> tr.rev <- tr[-1,-1] </div>
<div> } else { tr.rev <- tr }</div><div> if('*' %in% colnames(tr) == TRUE) {</div><div> tr.rev <- tr.rev[-1,-1] } </div><div> </div><div> ### Number of loops are number of rows in matrix.</div><div> n_loops <- nrow(tr.rev)</div>
<div> </div><div> ### Empty arrays in which the data is going to be stored.</div><div> res.Xg <- array(NA, dim = c(nrow(tr.rev), ncol(tr.rev)))</div><div> res.Xt <- array(NA, dim = c(nrow(tr.rev), ncol(tr.rev)))</div>
<div> res.Xgt <- array(NA, dim = c(nrow(tr.rev), ncol(tr.rev)))</div><div> res.Mgt <- array(NA, dim = c(nrow(tr.rev), ncol(tr.rev)))</div><div> res.Pg <- array(NA, dim = c(nrow(tr.rev), ncol(tr.rev)))</div><div>
res.Pt <- array(NA, dim = c(nrow(tr.rev), ncol(tr.rev)))</div><div> </div><div> ### Loops that are used for filling the arrays.</div><div> for (i in 1:n_loops) {</div><div> for (k in 1:n_loops) { ########################################</div>
<div> res.Xg[i,k] <- sum(tr.rev[i, ]) # Xg = sum observations in Gth row #</div><div> res.Xt[i,k] <- sum(tr.rev[ ,k]) # Xt = sum observations in Tth column #</div>
<div> res.Xgt[i,k] <- tr.rev[i,k] # Xgt = observed freq #</div><div> res.Mgt[i,k] <- ((res.Xg[i,k] * res.Xt[i,k]) / sum(tr.rev)) # Mgt = expected freq based on data #</div>
<div> res.Pg[i,k] <- res.Xg[i,k] / sum(tr.rev) # Pg = Xg divided by total N #</div><div> res.Pt[i,k] <- res.Xt[i,k] / sum(tr.rev) # Pt = Xt divided by total N #</div>
<div> } ########################################</div><div> }</div><div> </div><div> ### Formula for z-values.</div><div> res.Zgt <- round((res.Xgt - res.Mgt) / (sqrt(res.Mgt*(1 - res.Pg)*(1 - res.Pt))),3)</div>
<div> </div><div> ### Assign column and row names to matrix.</div><div> column <- colnames(tr.rates)</div><div> colnames(res.Zgt) <- column</div><div> row <- rownames(tr.rates)</div><div> rownames(res.Zgt) <- row</div>
<div> </div><div> return(res.Zgt)</div><div>}</div></div>