简体   繁体   English

如何将所有MCMC后验分配的平局保存到R中的文件中

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

I'm running a Hierarchical Lin Regr model using bayesm package in R. I have a data set with one dependent and 6 predictors. 我正在R中使用bayesm包运行Hierarchical Lin bayesm模型。我有一个数据集,其中有一个依赖项和6个预测变量。 There are 207 unique respondents with 35 observations for each. 有207个独特的受访者,每个受访者有35个观察值。

I began by using 我开始使用

print(out$betadraw)

Then I read about the sink function to output the out$betadraw to a file. 然后,我了解了接收sink功能,以将out$betadraw输出到文件。 I thought that sink function would capture all the draws. 我认为接收sink功能将捕获所有平局。 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? 另外,是否可以将对象从bayesm包传递到coda包以进行收敛诊断? Any help would be greatly appreciated. 任何帮助将不胜感激。

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 . 您应该能够使用?file并将open参数设置为write来打开文本连接。 Then, you can capture output and write it to your file using ?write with the append argument set to TRUE . 然后,您可以捕获输出,并使用?writeappend参数设置为TRUE append 写入文件。 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. ) 请注意,我不会尝试运行它;它花了将近一个半小时并创建了一个962 MB的文件,该文件只能使用EditPad打开。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM