使用tidyverse提取估计的系数和标准误差

时间:2017-10-02 19:33:15

标签: r linear-regression tidyverse

我有一个像这样的数据框:

set.seed(560)
df<-data.frame(lag= rep(1:40, each=228), psit= rep(rnorm(228, 20, 
10)),var=rnorm(9120, 50, 10))

对于lag的每个子集,我想运行线性回归,其中psitvar预测(例如lm(psit~var,df))。我想输出每个值的系数信息。具体而言,将beta EstimateStd. 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]]

1 个答案:

答案 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)