JoB
JoB

Reputation: 15

R issues returning output from function using loop

I am having issues returning output from a function I created in R when I use it in a loop. I am trying to combine the output form multiple MCMC models into one R object.

The function:

    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)    }

The code I use to create a list of RData mcmc outputs to run through the function:

    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"  

When I use the function on an individual output file, it works:

    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

But when I try to run all output files through the function using a loop I get a NULL output.

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


    >scrUN.output
    NULL

Thanks!

Upvotes: 0

Views: 124

Answers (2)

adilapapaya
adilapapaya

Reputation: 4795

The reason is you're rbind-ing nothing.

Here's a simplified example demonstrating what your code above is doing –– the for loop isn't assigning anything to a variable which is why you're getting NULL at the end.

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

Try this instead:

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)

Or better yet, use lapply:

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

Upvotes: 2

dvantwisk
dvantwisk

Reputation: 561

I think this is what you're asking for. This is an edit of the final code section. Your were using rbind on nothing since nothing is being returned by the for-loop.

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

Upvotes: 0

Related Questions