我有一个像这样的数据框:
set.seed(560)
df<-data.frame(lag= rep(1:40, each=228), psit= rep(rnorm(228, 20,
10)),var=rnorm(9120, 50, 10))
对于lag
的每个子集,我想运行一个线性回归,其中psit
由var
预测(例如lm(psit~var ,df))。我想输出每个值的系数信息。具体来说,测试版估计
和标准。错误
进入数据帧。然后计算标准化效应大小。输出应该是:
output<-data.frame(lag= rep(1:40, each=1), estimate= rep(rnorm(40,
.5, 0.01)),std.error=rnorm(40,0.01, 0.01))
output$strd.effect <- output$estimate /output$std.error
我已经尝试过:
models <-
df %>%
group_by(lag) %>%
do(model = lm(psit ~ var,data = .))
coeff<-
models %>%
ungroup()%>%
group_by(variable) %>%
do(glance(estimate=summary(model[i]$coeff[,1],
std.error=summary(model[i]$coeff[,2])
coeff<-
coeff %>%
group_by(variable) %>%
mutate(std.effect=estimate[[i]]/coeff[[i]]
最佳答案
扫帚
pacakge 可以在这里提供帮助。尝试一下
models %>%
rowwise() %>%
do({cbind(broom::tidy(.$model), lag=.$lag)}) %>%
filter(term=="var") %>%
mutate(std.effect=estimate/std.error ) %>%
select(lag, estimate, std.error, std.effect)
关于r - 使用 tidyverse 提取估计系数和标准误差,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46532490/