【发布时间】:2021-03-24 10:23:24
【问题描述】:
我正在尝试将一些统计信息的置信区间放入表格中,但结构对我来说很棘手。我希望它看起来像这样
| Level | Percentile | Student's |
|---|---|---|
| 95% | [p1, p2] | [t1, t2] |
其中 p1、p2 和 t1、t2 是部分使用 quantile 函数计算得出的数字。
示例代码,我刚刚生成了一些用于计算的随机数。
set.seed(42) #Arbitrary random seed
alk <- rexp(800,rate=1) #some random numbers
N <- 10^4
#Reserving space
Tstar <- numeric(N)
xbarstar <- numeric(N)
xbar <- mean(alk)
n <- length(alk)
for (i in 1:N)
{
bootsamp <- sample(alk,size=n,replace=T)
Tstar[i] <- (mean(bootsamp) - xbar)/(sd(bootsamp)/sqrt(n)) #for t-confidence
xbarstar[i] <- mean(bootsamp) #For the bootstrap percentile calc.
}
alpha<-0.05
#bootstrap t Confidence interval
boot.t <-xbar - quantile(Tstar,c(1-alpha/2,alpha/2))*sd(alk)/sqrt(n)
#bootstrap percentile
boot.p <- quantile(xbarstar,c(alpha/2,1-alpha/2))
nams<-c("level","percentile","student")
A <- matrix(data=nams,nrow=1,ncol=3)
A <- rbind(A,c("95%","[1.004, 1.1536]","[1.00131, 1.148672]"))
print(A)
这当然很糟糕,因为我手动输入区间值,但这只是为了让您了解我想要什么 - 我知道使用矩阵也可能不是正确的方法。
特别是,我对如何将[1.004, 1.1536] 放入单个单元格感到困惑。我知道我可以通过使用boot.t[[1]] 和boot.t[[2]] 单独引用这两个数字,或者通过使用unname(boot.t) 将它们都作为向量获取 - 但这无助于我将它们排列到一个单元格中,如上所述。
此外,打印矩阵或执行as.table(A) 会为我不想要的列/行提供 [1]、[2]、[3] 或 A、B、C 的这些烦人的标题。
附:这里的数学并不是很重要,我只需要帮助将结果安排在一个漂亮的表格中。
【问题讨论】: