Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/loops/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
R 从多个回归中提取系数值并存储在新矩阵中_R_Loops_For Loop_Statistics - Fatal编程技术网

R 从多个回归中提取系数值并存储在新矩阵中

R 从多个回归中提取系数值并存储在新矩阵中,r,loops,for-loop,statistics,R,Loops,For Loop,Statistics,我已经运行了一系列86个回归(编号83-168),并将它们存储为“reg_83”、“reg_84”等等。我现在尝试提取每个变量的系数值,并将它们输入到新的数据框中进行分析,以查看系数值随时间的变化。我有一个新的矩阵(“gencoef”),有12列86行。我有一个专栏专门讨论每个独立变量。我正在尝试运行一个循环,该循环将每个回归的系数值存储在变量列的相应单元格中。我使用了以下代码,但没有用。我不太擅长循环,因此这可能是一个相对简单的解决方案: for(i in c(83:168)){ for(

我已经运行了一系列86个回归(编号83-168),并将它们存储为“reg_83”、“reg_84”等等。我现在尝试提取每个变量的系数值,并将它们输入到新的数据框中进行分析,以查看系数值随时间的变化。我有一个新的矩阵(“gencoef”),有12列86行。我有一个专栏专门讨论每个独立变量。我正在尝试运行一个循环,该循环将每个回归的系数值存储在变量列的相应单元格中。我使用了以下代码,但没有用。我不太擅长循环,因此这可能是一个相对简单的解决方案:

for(i in c(83:168)){
  for(j in c(1:86)){
   eval(parse(text=paste(
"gencoef[",j,",2] <- summary(reg_",i,")$coefficients[1,1]"),sep==""))
}
}
(c(83:168)中的i){ 对于(c中的j(1:86)){ eval(解析(文本=粘贴(
“gencoef[”,j,,,2]可能有很多方法可以做到这一点,但我很快想到了一个方法。它使用了上面评论过的
broom

首先,让我们列出一个模型列表:

# make a response variable and a matrix of predictors
set.seed(111)
response <- rnorm(10)
predictors <- matrix(rnorm(100), nrow = 10)

# model response using each predictor to give a list of 10 model outputs
mods <- apply(predictors, 2, function(x) lm(response ~ x))
给出了相同的结果

# A tibble: 20 x 5
#   term        estimate std.error statistic p.value
# * <chr>          <dbl>     <dbl>     <dbl>   <dbl>
# 1 (Intercept)  0.0643      0.564    0.114   0.912 
# 2 x            0.0851      0.454    0.187   0.856 
# 3 (Intercept)  0.0256      0.511    0.0501  0.961 
# 4 x           -0.0763      0.567   -0.135   0.896 
# 5 (Intercept)  0.113       0.514    0.220   0.832 
# 6 x           -0.310       0.458   -0.677   0.518 
# 7 (Intercept) -0.448       0.562   -0.797   0.448 
# etc

这是一项用于和的作业,而不是用于循环和求值(解析(…)的作业,您需要
sep=“”
才能在
粘贴中没有空间(而不是
=
)但是我同意alistaire的观点,首先,使用
Tridy
一次完成这一切,更容易将所有回归存储在一个列表中。要回答您的问题,您可以使用
列表上的
sapply
并使用
coef(列表[[回归编号])
而不是按
摘要$coefficients
的方式。
library(purrr)
map_df(mods, tidy)
# A tibble: 20 x 5
#   term        estimate std.error statistic p.value
# * <chr>          <dbl>     <dbl>     <dbl>   <dbl>
# 1 (Intercept)  0.0643      0.564    0.114   0.912 
# 2 x            0.0851      0.454    0.187   0.856 
# 3 (Intercept)  0.0256      0.511    0.0501  0.961 
# 4 x           -0.0763      0.567   -0.135   0.896 
# 5 (Intercept)  0.113       0.514    0.220   0.832 
# 6 x           -0.310       0.458   -0.677   0.518 
# 7 (Intercept) -0.448       0.562   -0.797   0.448 
# etc
map_dfr(mods, tidy, .id = "model")

# A tibble: 20 x 6
#   model term        estimate std.error statistic p.value
#   <chr> <chr>          <dbl>     <dbl>     <dbl>   <dbl>
# 1 1     (Intercept) -0.672       0.263   -2.56    0.0338
# 2 1     x           -0.0655      0.284   -0.230   0.824 
# 3 2     (Intercept) -0.688       0.260   -2.65    0.0293
# 4 2     x            0.133       0.225    0.589   0.572 
# etc