【问题标题】:R issues returning output from function using loopR问题使用循环从函数返回输出
【发布时间】:2017-09-02 23:07:26
【问题描述】:

当我在循环中使用我在 R 中创建的函数返回输出时遇到问题。我正在尝试将多个 MCMC 模型的输出组合到一个 R 对象中。

功能:

    get_scrUN_output <- function(filename){
    out <- filename
    nam<-c("sigma","lam0","psi", "N")
    nam<-match(nam,dimnames(out[[1]]$sims)[[2]])

    out.lst<-mcmc.list(
        as.mcmc(out[[1]]$sims[200001:300000,nam]),
        as.mcmc(out[[2]]$sims[200001:300000,nam]),
        as.mcmc(out[[3]]$sims[200001:300000,nam]))

    s <- summary(out.lst)
    gd <- gelman.diag(out.lst,multivariate = FALSE)

    output_table <- rbind(as.data.frame(t(s$statistics)),
                    as.data.frame(t(s$quantiles)),
                    as.data.frame(t(gd$psrf)))
    return(output_table)    }

我用来创建 RData mcmc 输出列表以运行函数的代码:

    scrUN.ET <- list.files(getwd(),"out.*ET.RData")
    scrUN.lst <- as.vector(substring(scrUN.ET,1))
    scrUN.lst <- str_sub(scrUN.lst, 1, str_length(scrUN.lst)-3)

    >scrUN.lst
    [1] "BBout11FL"  "BBout11TL"  "BBout12TL"  "BBout13FL"  "BBout13TL"  

当我在单个输出文件上使用该函数时,它可以工作:

    get_scrUN_output(BBout11FL)

    sigma       lam0          psi           N
    Mean           130.43594323 14.5319368 0.3361405211 335.8042733
    SD               7.28386725  9.7311139 0.2743725813 274.6828277
    Naive SE         0.01329846  0.0177665 0.0005009335   0.5014999
    Time-series SE   1.28032869  1.3886577 0.0360607870  36.5692414
    2.5%           118.37718370  0.6129902 0.0300165600  30.0000000
    25%            124.29743884  5.7535456 0.0958156210  95.0000000
    50%            130.40628214 15.1264454 0.2426328827 242.0000000
    75%            135.99836262 19.9685209 0.5403864215 541.0000000
    97.5%          145.11615201 34.9438198 0.9298185748 930.0000000
    Point est.       1.59559993  4.4590599 1.0677998255   1.0678381
    Upper C.I.       2.56854388  9.5792520 1.2186078069   1.2186933

但是当我尝试使用循环通过函数运行所有输出文件时,我得到一个 NULL 输出。

    scrUN.output <- rbind(
      for (i in seq_along(scrUN.lst)){
        get_scrUN_output(get(scrUN.lst[i]))
        }
      )


    >scrUN.output
    NULL

谢谢!

【问题讨论】:

    标签: r function loops null


    【解决方案1】:

    原因是你rbind-ing 什么都没有。

    这是一个简化的示例,展示了您上面的代码在做什么——for 循环没有为变量分配任何东西,这就是为什么最后会得到NULL

    xx <- rbind( 
        for(i in c(1,2)){
          i
        }
      )
    print(xx)  # NULL
    

    试试这个:

    scrUN.output <- list() # initialize a list
    for (i in seq_along(scrUN.lst)){
        # update the list contents
        scrUN.output[[i]] <- get_scrUN_output(get(scrUN.lst[i]))
    }
    # finally, rbind eveything
    scrUN.output <- do.call(rbind, scrUN.output)
    

    或者更好,使用lapply

    scrUN.output <- lapply(scrUN.lst, get_scrUN_output)
    scrUN.output <- do.call(rbind, scrUN.output)
    

    【讨论】:

      【解决方案2】:

      我认为这就是您所要求的。这是对最终代码部分的编辑。您没有使用 rbind,因为 for 循环没有返回任何内容。

      scrUN.output <- lapply(scrUN.lst, function(i) get_scrUN_output(get(i)))
      scrUN.output <- do.call(rbind, scrUN.output)
      scrUN.output
      

      【讨论】:

      • 完美运行!谢谢。
      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-01-06
      • 1970-01-01
      • 1970-01-01
      • 2019-09-06
      • 2016-03-02
      • 1970-01-01
      相关资源
      最近更新 更多