Applying popbio "projection.matrix" to multiple fertilities and generate list of matrices

165 views Asked by At

I usually find the answers to my questions by looking around here (I'm glad stackovergflow exists!), but I haven't found the answer to this one... I hope you can help me :)

I am using the projection.matrix() function from the "popbio" package to create transition matrices. In the function, you have to specify the "stage" and "fate" (both categorical variables), and the "fertilities" (a numeric column).

Everything works fine, but I would like to apply the function to 1:n fertility columns within the data frame, and get a list of matrices generated from the same categorical variables with the different fertility values.

This is how my data frame looks like (I only include the variables I am using for this question):

stage.fate = data.frame(replicate(2, sample(0:6,40,rep=TRUE)))
stage.fate$X1 = as.factor(stage.fate$X1)
stage.fate$X2 = as.factor(stage.fate$X2)
fertilities = data.frame(replicate(10,rnorm(40, .145, .045)))
df = cbind(stage.fate, fertilities)
colnames(df)[1:2]=c("stage", "fate")
prefix = "control"
suffix = seq(1:10)
fer.names = (paste(prefix ,suffix , sep="."))
colnames(df)[3:12] = c(fer.names)

Using

library(popbio)
projection.matrix(df, fertility=control.1)

returns a single transition matrix with the fertility values incorporated into the matrix.

My problem is that I would like to generate a list of matrices with the different fertility values in one go (in reality the length of my data is >=300, and the fertility columns ~100 for each of four different treatments...).

I will appreciate your help!

-W

PS This is how the function in popbio looks like:

    projection.matrix =
function (transitions, stage = NULL, fate = NULL, fertility = NULL, 
    sort = NULL, add = NULL, TF = FALSE) 
{
    if (missing(stage)) {
        stage <- "stage"
    }
    if (missing(fate)) {
        fate <- "fate"
    }
    nl <- as.list(1:ncol(transitions))
    names(nl) <- names(transitions)
    stage <- eval(substitute(stage), nl, parent.frame())
    fate <- eval(substitute(fate), nl, parent.frame())
    if (is.null(transitions[, stage])) {
        stop("No stage column matching ", stage)
    }
    if (is.null(transitions[, fate])) {
        stop("No fate column matching ", fate)
    }
    if (missing(sort)) {
        sort <- levels(transitions[, stage])
    }
    if (missing(fertility)) {
        fertility <- intersect(sort, names(transitions))
    }
    fertility <- eval(substitute(fertility), nl, parent.frame())
    tf <- table(transitions[, fate], transitions[, stage])
    T_matrix <- try(prop.table(tf, 2)[sort, sort], silent = TRUE)
    if (class(T_matrix) == "try-error") {
        warning(paste("Error sorting matrix.\n  Make sure that levels in stage and fate columns\n  match stages listed in sort option above.\n Printing unsorted matrix instead!\n"), 
            call. = FALSE)
        sort <- TRUE
        T_matrix <- prop.table(tf, 2)
    }
    T_matrix[is.nan(T_matrix)] <- 0
    if (length(add) > 0) {
        for (i in seq(1, length(add), 3)) {
            T_matrix[add[i + 0], add[i + 1]] <- as.numeric(add[i + 
                2])
        }
    }
    n <- length(fertility)
    F_matrix <- T_matrix * 0
    if (n == 0) {
        warning("Missing a fertility column with individual fertility rates\n", 
            call. = FALSE)
    }
    else {
        for (i in 1:n) {
            fert <- tapply(transitions[, fertility[i]], transitions[, 
                stage], mean, na.rm = TRUE)[sort]
            F_matrix[i, ] <- fert
        }
    }
    F_matrix[is.na(F_matrix)] <- 0
    if (TF) {
        list(T = T_matrix, F = F_matrix)
    }
    else {
        T_matrix + F_matrix
    }
}
<environment: namespace:popbio>
1

There are 1 answers

0
WFalcon On

My question was answered via ResearchGate by Caner Aktas

Answer:

fertility.list<-vector("list",length(suffix))

names(fertility.list)<-fer.names

for(i in suffix) fertility.list[[i]]<-projection.matrix(df,fertility=fer.names[i])

fertility.list 

Applying popbio “projection.matrix” to multiple fertilities and generate list of matrices?. Available from: https://www.researchgate.net/post/Applying_popbio_projectionmatrix_to_multiple_fertilities_and_generate_list_of_matrices#5578524f60614b1a438b459b [accessed Jun 10, 2015].