首页 > 解决方案 > 如何在 (...) 中获取值并取平均值?

问题描述

我正在使用包R0。我正在尝试复制 CRAN pdf 中提供的示例,但没有得到与 pdf 中提供的完全相同的结果。

代码:

library(R0)
data(Germany.1918)
mGT <- generation.time("gamma", c(3,1.5))
SB <- est.R0.SB(Germany.1918, mGT)
##Results will include "most likely R(t)" (ie. the R(t) value for which the computed probability
##is the highest), along with 95% CI, in a data.frame object
SB
# Reproduction number estimate using Real Time Bayesian method.
# 0 0 2.02 0.71 1.17 1.7 1.36 1.53 1.28 1.43 ...
SB$Rt.quant
# Date R.t. CI.lower. CI.upper.
# 1 1918-09-29 0.00 0.01 1.44
# 2 1918-09-30 0.00 0.01 1.42
# 3 1918-10-01 2.02 0.97 2.88
# 4 1918-10-02 0.71 0.07 1.51
# 5 1918-10-03 1.17 0.40 1.84
# 6 1918-10-04 1.70 1.09 2.24
# 7 1918-10-05 1.36 0.84 1.83
# 8 1918-10-06 1.53 1.08 1.94
# 9 1918-10-07 1.28 0.88 1.66
# 10 1918-10-08 1.43 1.08 1.77
# ...

我有三个疑问。

  1. 最后一个命令SB$Rt.quant在输出中显示 NULL。我怎样才能得到与示例中完全相同的结果?

  2. 如何获取隐藏在 (...) 中的值,用于命令:SBSB$Rt.quant

  3. 如何获取 SB 的均值和中位数并获得 SB 的相关均值和中位数的置信区间?

标签: rmeanmedianconfidence-intervalellipsis

解决方案


现在争论已经改变了。您必须使用SB$conf.int才能获得置信区间,例如

library(R0)
## Data is taken from the paper by Nishiura for key transmission parameters of an institutional
## outbreak during 1918 influenza pandemic in Germany)
data(Germany.1918)
mGT <- generation.time("gamma", c(3,1.5))
SB <- est.R0.SB(Germany.1918, mGT)
SB
SB$conf.int

然后您可以使用write.csv(SB$conf.int)打印所有值。如果您想同时拥有 R 和置信区间,就像R0pdf 文档中的示例一样,您可以使用以下代码

cbind(Rt=SB$R, SB$conf.int)

阅读第三个问题,我认为您可以通过使用来获得平均值, SB$R并且包中没有中值函数。置信区间已经可以通过SB$conf.int.


推荐阅读