gpt4 book ai didi

r - 在R中的列表列表中获取2个变量的平均值

转载 作者:行者123 更新时间:2023-12-04 01:21:04 25 4
gpt4 key购买 nike

在下面的 replicate() 调用之后,我想知道如何获得 'Estimate'' 的 mean标准。列表中 x1x2 的错误

foo <- function(){
x1 <- rnorm(20) ; x2 <- rnorm(20,2); y <- rnorm(20,3)
coef(summary(lm(y~x1+x2)))[,1:2] }

# Now replicate foo():

replicate(2, foo(), simplify = F) # How to get the mean of 'Estimate' and 'Std. Error' for 'x1' and 'x2' after loop

### OUTPUT:

[[1]]
Estimate Std. Error
(Intercept) 3.1356507 0.4874282
x1 -0.3408958 0.2033072
x2 -0.1335315 0.2513356

[[2]]
Estimate Std. Error
(Intercept) 2.5574090 0.4354596
x1 0.0850336 0.3101341
x2 0.2348902 0.1754149

最佳答案

首先,我们修复列表以便我们可以看到不同方法的结果:

set.seed(111)
dat = replicate(2, foo(), simplify = F)

dat
[[1]]
Estimate Std. Error
(Intercept) 3.47644151 0.4100162
x1 -0.04314488 0.2769444
x2 -0.04946731 0.1860911

[[2]]
Estimate Std. Error
(Intercept) 2.96378594 0.5265807
x1 0.22268874 0.3517643
x2 -0.04437585 0.2468499

使用基础 R“减少”:

Reduce(`+`,dat)/length(dat)
Estimate Std. Error
(Intercept) 3.22011373 0.4682984
x1 0.08977193 0.3143544
x2 -0.04692158 0.2164705

使用呼噜声:

library(purrr)
reduce(dat,`+`)/length(dat)

Estimate Std. Error
(Intercept) 3.22011373 0.4682984
x1 0.08977193 0.3143544
x2 -0.04692158 0.2164705

关于r - 在R中的列表列表中获取2个变量的平均值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/62661747/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com