应用popbio" projection.matrix"到多个fertilities并生成矩阵列表

时间:2015-06-07 12:09:19

标签: matrix transition modeling population

我通常会在这里找到问题的答案(我很高兴stackovergflow存在!),但我还没有找到答案...我希望你能帮助我: )

我正在使用" popbio"中的projection.matrix()函数。用于创建转换矩阵的包。在函数中,您必须指定" stage"和"命运" (两个分类变量)和" fertilities" (数字列)。

一切正常,但我想将该函数应用于数据框中的1:n生育列,并获得由具有不同生育率值的相同分类变量生成的矩阵列表。

这就是我的数据框架的样子(我只包含我用于此问题的变量):

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)

使用

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

返回单个转换矩阵,其中生育率值包含在矩阵中。

我的问题是我希望一次性生成具有不同生育率值的矩阵列表(实际上我的数据长度> = 300,并且四种不同处理中的每一种的生育率列为~100) ...)。

我将非常感谢您的帮助!

-W

PS这就是popbio中的功能:

    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 个答案:

答案 0 :(得分:0)

我的问题由Caner Aktas的ResearchGate回答

答案:

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 

将popbio“projection.matrix”应用于多个fertilities并生成矩阵列表?可从:https://www.researchgate.net/post/Applying_popbio_projectionmatrix_to_multiple_fertilities_and_generate_list_of_matrices#5578524f60614b1a438b459b [2015年6月10日访问]。