简体   繁体   English

如何从 R 输出绘制直方图?

[英]How to plot histogram from the R output?

I am trying to plot histogram from the R output which is not a data frame.我试图从不是数据框的 R 输出绘制直方图。 Below are my codes and the output.下面是我的代码和输出。

x <- replicate(1000, 
               {y <- rpois(200, 1)
               {lambda0 <- 1
for(i in 1:1) 
  {
  if( i == 1 ) cat( sprintf("%15s %15s %15s %15s\n", "LogL", "Score", "Information", "New Estimate"))
  logL        <- sum((-lambda0) + y*(log(lambda0)))
  score       <- sum((y/lambda0)-1)
  information <- sum(y/(lambda0)^2)
  lambda1      <- lambda0 + score/information
  cat( sprintf("%15.4f %15.4f %15.4f %15.5f\n", logL, score, information, lambda1))
  lambda0 <- lambda1
}  
} 
               })

Below is my output下面是我的输出

在此处输入图片说明

I'm trying to take the new estimate from the output and create histogram.我正在尝试从输出中获取新的估计值并创建直方图。 Can you please help?你能帮忙吗?

Thank you.谢谢你。

You need to store the value for New Estimate during your loop.您需要在循环期间存储 New Estimate 的值。 This way you can retrieve your results after the loop is finished.这样您就可以在循环完成后检索结果。 Normally when using a loop, you specify a variable in advance in which you can save the result for each iteration.通常在使用循环时,您预先指定一个变量,您可以在其中保存每次迭代的结果。 Eg:例如:

numbers <- 1:3

result <- list(length = length(numbers)

for (i in seq_along(numbers){

result[[i]] <- numbers[[i]] + 1

}

In this example there is a vector of three numbers, you want to add one to each number and save the result.在这个例子中,有一个由三个数字组成的向量,你想给每个数字加一个并保存结果。 You can do this by creating a list of length 3 (adding length is better, but not necessary) and for each i th iteration, you save the result in the i th element of the list.您可以通过创建长度为 3 的列表(添加长度更好,但不是必需的)来完成此操作,并且对于每个第i次迭代,您将结果保存在列表的第i个元素中。

After finishing the loop you can retrieve the results from the result variable.完成循环后,您可以从result变量中检索结果。 And you can retrieve the i th result by using square brackets: result[[i]] .您可以使用方括号检索第i个结果: result[[i]]

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

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