以下是模拟简单线性回归数据和计算每个参数的非覆盖概率的函数。
var countries = ["UK", "INDIA", "FRANCE", "CHINA"];
var colors = ["#807dba", "#41ab5d", "#DECF3F", "#FAA43A"];
var result = [];
$.each(countries, function(index, value){
result[value] = colors[index];
});
console.info(result);
// Result : [UK: "#807dba", INDIA: "#41ab5d", FRANCE: "#DECF3F", CHINA: "#FAA43A"]
,输出为:
simfun <- function(n,b0,b1,sig){
x <- runif(n)
y <- b0+b1*x+ rnorm(n,0,sig)
data <- data.frame(y=y,x=x)
}
noncoverage <- function(n,b0,b1,sig){
dat <- simfun(n,b0,b1,sig)
fit <- lm(y~x,data=dat)
res=summary(fit)
estim = coefficients(res)[,1]
se= coefficients(res)[,2]
ci1 = estim[1] + qnorm(c(.025,.975))*se[1]
nc1 = ifelse((ci1[1]<b0 & ci1[2]>b0),0,1)
ci2 = estim[2] + qnorm(c(.025,.975))*se[2]
nc2 = ifelse((ci2[1]<b1 & ci2[2]>b1),0,1)
nc = data.frame(nc1=nc1,nc2=nc2)
}
set.seed(494590)
com=replicate(4,noncoverage(200,1,2,.5))
现在我想计算nc1和nc2的平均值,即
nc1的平均值为(c(0,1,0,0)),nc2的平均值为(c(0,0,0,0))。
如何通过R命令计算均值?
答案 0 :(得分:1)
作为替代方案,您可能希望简化函数noncoverage
的输出,即nc
。如果该函数始终用于输出2个数字的向量,则可以替换:
nc = data.frame(nc1=nc1,nc2=nc2)
带
nc = c(nc1=nc1, nc2=nc2)
然后com
变为:
[,1] [,2] [,3] [,4]
nc1 0 1 0 0
nc2 0 0 0 0
和rowMeans
会给出您想要的结果。
答案 1 :(得分:1)
将我的评论作为解决方案发布
colMeans(do.call(rbind, com))
# nc1 nc2
#0.25 0.00
或者
library(data.table)
rbindlist(com)[,lapply(.SD, mean)]
# nc1 nc2
#1: 0.25 0
set.seed(494590)
com <- replicate(4,noncoverage(200,1,2,.5), simplify=FALSE)