我有一个像这样的数据框:
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))。我想输出每个值的系数信息。具体而言,将beta Estimate
和Std. error
纳入数据集。然后计算标准化效果大小。输出应为:
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]]
答案 0 :(得分:2)
broom
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)