How do I save all the draws from a MCMC posterior distribution to a file in R

StackOverflow https://stackoverflow.com/questions/19601034

  •  01-07-2022
  •  | 
  •  

Question

I'm running a Hierarchical Lin Regr model using bayesm package in R. I have a data set with one dependent and 6 predictors. There are 207 unique respondents with 35 observations for each.

I began by using

print(out$betadraw)

Then I read about the sink function to output the out$betadraw to a file. I thought that sink function would capture all the draws. Instead the draws were truncated after certain number of draws.

I need to capture all the draws. In addition, is it possible to pass objects from bayesm package to coda package for convergence diagnostics? Any help would be greatly appreciated.

Était-ce utile?

La solution

Without a reproducible example, it's hard to know for sure what is going on.

You should be able to open a text connection using ?file with the open argument set to write. Then, you can capture output and write it to your file using ?write with the append argument set to TRUE. The following worked fine on my machine:

> zz <- file(description="some name.txt", open="w")
> isOpen(zz)
[1] TRUE
> for(i in 1:100000){
+   x <- rbeta(1000, shape1=10, shape2=10)
+   write(x, file=zz, append=TRUE)
+ }
> close(zz)

(Note, I wouldn't try running that; it took nearly a half an hour and created a 962 MB file that could only be opened with EditPad.)

Licencié sous: CC-BY-SA avec attribution
Non affilié à StackOverflow
scroll top