简体   繁体   English

计算数据帧 R 中多列的 95% 可信区间

[英]Calculate 95 % credible interval for multiple columns in data frame R

I need to get/calculate the 95 % credible interval for my data.我需要为我的数据获取/计算 95% 的可信区间。 My data consists of ten columns and over 5000 rows.我的数据由十列和超过 5000 行组成。 Here is some example data.这是一些示例数据。

data <- data.frame(A = c(-7.595932, -6.451768, -4.682111, -8.781488, -4.251690), 
                   B = c(0.8324450, 0.9451657, 0.8773759, 0.6044753, 0.6553995),
                   C = c(22.747480, 15.477470, 18.745407, 9.622865, 21.137619), 
                   D = c(-11.684762, -13.474299, -9.783277, -7.747501, -12.352081))

I am just not sure which function to use since I get different results each time and it only works with one column at a time.我只是不确定要使用哪个 function,因为我每次都会得到不同的结果,而且它一次只能处理一列。 I have tried the following functions:我尝试了以下功能:

ci(data$`A`, confidence = 0.95)  ## R package gmodels

and

CI(data$`A`, confidence = 0.95) ##R package Rmisc

Have anyone else experienced the same problem?有没有其他人遇到过同样的问题?

The two functions give you actually the same thing:这两个函数实际上给了你同样的东西:

library(gmodels)
library(Rmisc)
 CI(data$A)
    upper      mean     lower 
-3.975568 -6.352598 -8.729627 
 ci(data$A, confidence = 0.95)
  Estimate   CI lower   CI upper Std. Error 
-6.3525978 -8.7296274 -3.9755682  0.8561414 

To apply it on all columns, use lapply or sapply :要将其应用于所有列,请使用lapplysapply

> sapply(data,CI)
              A         B        C          D
upper -3.975568 0.9648266 24.01143  -8.198957
mean  -6.352598 0.7829723 17.54617 -11.008384
lower -8.729627 0.6011180 11.08091 -13.817811

It's not clear if this is what you are looking for, but you can get a print-out of the mean of each variable with the 95% confidence interval for the mean like this:目前尚不清楚这是否是您要查找的内容,但您可以打印出每个变量的平均值,平均值为 95% 置信区间,如下所示:

lapply(data, function(x) {
   paste0(round(mean(x), 2), " (95% CI: ",
   paste(round(sort(mean(x) + c(1.96, -1.96) * sd(x)/sqrt(length(x))), 2),
         collapse = " to "), ")")
 } )

#> $A
#> [1] "-6.35 (95% CI: -8.03 to -4.67)"
#>
#> $B
#> [1] "0.78 (95% CI: 0.65 to 0.91)"
#>
#> $C
#> [1] "17.55 (95% CI: 12.98 to 22.11)"
#>
#> $D
#> [1] "-11.01 (95% CI: -12.99 to -9.03)"

If you want a credible interval (from Bayesian statistics) this requires some additional tuning, choice of prior and likelihood.如果你想要一个可信的区间(来自贝叶斯统计),这需要一些额外的调整、先验和可能性的选择。 There are some defaults already in some functions, so you may get away with it, but you should really know what you are doing, before blindly applying such concepts.在某些函数中已经存在一些默认值,因此您可能会侥幸逃脱,但在盲目应用这些概念之前,您应该真正知道自己在做什么。 Here is an example for demonstration purposes.这是一个用于演示目的的示例。

library(bayestestR)

data <- data.frame(A = c(-7.595932, -6.451768, -4.682111, -8.781488, -4.251690), 
                   B = c(0.8324450, 0.9451657, 0.8773759, 0.6044753, 0.6553995),
                   C = c(22.747480, 15.477470, 18.745407, 9.622865, 21.137619), 
                   D = c(-11.684762, -13.474299, -9.783277, -7.747501, -12.352081))

sapply(data,ci,ci=0.95)

        A         B         C        D        
CI      95        95        95       95       
CI_low  -8.662932 0.6095677 10.20833 -13.36208
CI_high -4.294732 0.9383867 22.58649 -7.951079

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

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