我在R中有以下输出:
> out
, , 1
Estimate Std. Error z value Pr(>|z|)
(Intercept) -0.8968692 0.1995274 -4.494968 6.958036e-06
X 0.5803202 0.2190155 2.649676 8.056901e-03
Z 0.2535625 0.2232445 1.135806 2.560376e-01
X:Z 0.3375088 0.2659863 1.268895 2.044784e-01
, , 2
Estimate Std. Error z value Pr(>|z|)
(Intercept) -0.8624345 0.1888867 -4.565884 4.973944e-06
X 0.2544430 0.1784808 1.425604 1.539826e-01
Z 0.4704287 0.2387451 1.970423 4.878996e-02
X:Z 0.4813516 0.2239360 2.149505 3.159440e-02
, , 3
Estimate Std. Error z value Pr(>|z|)
(Intercept) -1.0576301 0.1896436 -5.5769362 2.447916e-08
X 0.1515228 0.1917464 0.7902253 4.293962e-01
Z 0.1551839 0.2686790 0.5775810 5.635470e-01
X:Z 0.1673263 0.2872858 0.5824384 5.602714e-01
现在我想在这三个重复中计算相应的估计系数的平均值。也就是说,对于拦截,平均值为(-0.8968692-0.8624345-1.0576301)/ 3 = -0.9389779
所以我试过了:
> out[1,,]
[,1] [,2] [,3]
Estimate -8.968692e-01 -8.624345e-01 -1.057630e+00
Std. Error 1.995274e-01 1.888867e-01 1.896436e-01
z value -4.494968e+00 -4.565884e+00 -5.576936e+00
Pr(>|z|) 6.958036e-06 4.973944e-06 2.447916e-08
mean(out[1,,])
[1] -1.406388
这是不正确的。
然后我尝试了
mean(out[1,,])$estimates
Error in mean(out[1, , ])$estimates :
$ operator is invalid for atomic vectors
如何计算相应估算值的平均值?
答案 0 :(得分:1)
您可以使用apply
。这是一个可重复的例子。
set.seed(357)
xy <- array(runif(27), dim = c(3, 3, 3))
xy
, , 1
[,1] [,2] [,3]
[1,] 0.10728121 0.2248621 0.9337365
[2,] 0.05504568 0.6534852 0.7474587
[3,] 0.27987766 0.4634314 0.7783997
, , 2
[,1] [,2] [,3]
[1,] 0.9913409 0.4601817 0.1716801
[2,] 0.6245529 0.2254525 0.3195294
[3,] 0.5245744 0.5898001 0.8953055
, , 3
[,1] [,2] [,3]
[1,] 0.6831485 0.45645455 0.7077218
[2,] 0.4317410 0.36626006 0.6886439
[3,] 0.1809770 0.04334935 0.3717553
mean(c(0.107, 0.991, 0.683))
[1] 0.5936667
apply(xy, MARGIN = 1:2, FUN = mean)
[,1] [,2] [,3]
[1,] 0.5939235 0.3804995 0.6043794
[2,] 0.3704465 0.4150659 0.5852107
[3,] 0.3284764 0.3655270 0.6818202
或者您可以使用rowMeans
t(apply(xy, 1, FUN=rowMeans))
# [,1] [,2] [,3]
#[1,] 0.5939235 0.3804995 0.6043794
#[2,] 0.3704465 0.4150659 0.5852107
#[3,] 0.3284764 0.3655270 0.6818202