如何正确索引列表项以在for循环内返回行而不是列

时间:2016-11-18 01:01:33

标签: r loops indexing

我试图在另一个for循环中写一个for循环。第一个循环从不同大小的矩阵列表(下面的vcmats)中抓取第i个vcov矩阵,并抓取一个具有适当维度的24个预测变量模型的帧,以便与来自不同模型的帧列表(下面的jacobians)的当前vcov矩阵相乘。第二个循环应从所选预测器帧中拉出第j个记录(行),正确格式化它,然后使用vcov矩阵运行计算,并将后处理所需的指示变量和计算结果输出到保持表(holdtab)。 / p>

当我运行下面的代码时,我得到以下错误:jjacob [,1:4]中的错误:维度错误,因为R返回1s的列(即jacobs的拦截列),而不是完整的第一列记录(即jjacob = jacobs [1,])。我已经大大简化了这个例子,但是留下了足够的复杂性来证明这个问题。我将非常感谢您解决此问题的任何帮助。

vcmats <- list(structure(c(0.67553, -0.1932, -0.00878, -0.00295, -0.00262, 
-0.00637, -0.1932, 0.19988, 0.00331, -0.00159, 0.00149, 2e-05, 
-0.00878, 0.00331, 0.00047, -6e-05, 3e-05, 3e-05, -0.00295, -0.00159, 
-6e-05, 0.00013, -2e-05, 6e-05, -0.00262, 0.00149, 3e-05, -2e-05, 
2e-05, 0, -0.00637, 2e-05, 3e-05, 6e-05, 0, 0.00026), .Dim = c(6L, 
6L)), structure(c(0.38399, -0.03572, -0.00543, -0.00453, -0.00634, 
-0.03572, 0.10912, 0.00118, -0.00044, 0.00016, -0.00543, 0.00118, 
0.00042, -3e-05, 4e-05, -0.00453, -0.00044, -3e-05, 0.00011, 
5e-05, -0.00634, 0.00016, 4e-05, 5e-05, 0.00025), .Dim = c(5L, 
5L)))

jacobians <- list(structure(list(intcpt = c(1, 1, 1, 1), species = c(1, 1, 
0, 0), nage = c(6, 6, 6, 6), T = c(12, 50, 12, 50), hgt = c(90, 
90, 90, 90), moon = c(7, 7, 7, 7), hXm = c(0, 0, 0, 0), covr = c(0, 
0, 0, 0), het = c(0, 0, 0, 0)), .Names = c("intcpt", "species", 
"nage", "T", "hgt", "moon", "hXm", "covr", "het"), row.names = c("1", 
"1.4", "1.12", "1.16"), class = "data.frame"), structure(list(
    intcpt = c(1, 1, 1, 1), species = c(1, 1, 0, 0), nage = c(6, 
    6, 6, 6), T = c(12, 50, 12, 50), hgt = c(0, 0, 0, 0), moon = c(7, 
    7, 7, 7), hXm = c(0, 0, 0, 0), covr = c(0, 0, 0, 0), het = c(0, 
    0, 0, 0)), .Names = c("intcpt", "species", "nage", "T", "hgt", 
"moon", "hXm", "covr", "het"), row.names = c("2", "2.4", "2.12", 
"2.16"), class = "data.frame"))

holdtab <- structure(list(model = structure(c(4L, 4L, 4L, 4L, 5L, 5L, 5L, 
5L), .Label = c("M.1.BaseCov", "M.2.Height", "M.5.Height.X.LastNewMoon", 
"M.6.Height.plus.LastNew", "M.7.LastNewMoon", "M.G.Global"), class = "factor"), 
    aicc = c(341.317, 341.317, 341.317, 341.317, 342.1412, 342.1412, 
    342.1412, 342.1412), species = c(NA, NA, NA, NA, NA, NA, 
    NA, NA), condVar = c(NA, NA, NA, NA, NA, NA, NA, NA)), .Names = c("model", 
"aicc", "species", "condVar"), row.names = c(1L, 2L, 3L, 4L, 
25L, 26L, 27L, 28L), class = "data.frame")

jloop <- 1
for (imat in vcmats) {                      # Call the outside loop of vcov matrices
    jacobs = jacobians[[jloop]]             # Set tempvar jacobs as the jth member of the jacobians frame (n/24)
    for (jjacob in jacobs) {                # Call inside loop of lines in jacob (each individual set of predictor levels)
                                        # I need to reduce the vector length to match my vcov matrix so
    pt1 = jjacob[,1:4]                      # Separate Core columns from variable columns (because I don't want to drop species when ==0)
    pt2 = jjacob[,5:9]                      # Pull out variable columns for next step
    pt2 = pt2[,!apply(pt2 == 0, 2, all)]    # Drop any variable columns that ==0
    jjacob = cbind(pt1, pt2)                    # Reconstruct the record now of correct dimensions for the relevant vcov matrix
    jjacob = as.matrix(jjacob)              # Explicitly convert jjmod - I was having trouble with this previously
    tj = (t(jjacob))                            # Transpose the vector
    condvar = jjacob %*% imat %*% tj            # run the calculation
    condVarTab[record,3] = jjacob[2]            # Write species 0 or 1 to the output table
    condVarTab[record,4] = condvar          # Write the conditional variance to the table
    record = record+1                       # Iterate the record number for the next output run
        }
    jloop = jloop+1                         # Once all 24 models in a frame are calculated iterate to the next frame of models which will be associated with a new vcv matrix
    }

0 个答案:

没有答案