R 使用每个预测器列使用多个响应拟合模型,并将结果单独存储在数据帧中

R 使用每个预测器列使用多个响应拟合模型,并将结果单独存储在数据帧中,r,dplyr,tidyr,purrr,broom,R,Dplyr,Tidyr,Purrr,Broom,参考,其中数据帧由一列响应变量和几列预测变量组成。作者希望分别使用每个预测变量为响应变量拟合模型,最后创建一个包含模型系数的数据帧。我感兴趣的原始问题下面有一个答案(抄写在下面) 您可以使用多响应线性模型,其中每个响应分别对每个预测值进行回归,例如: lm(cbind(Sepal.Length,Sepal.Width) ~ Species,data=iris) Call: lm(formula = cbind(Sepal.Length, Sepal.Width) ~ Species, data

参考,其中数据帧由一列响应变量和几列预测变量组成。作者希望分别使用每个预测变量为响应变量拟合模型,最后创建一个包含模型系数的数据帧。我感兴趣的原始问题下面有一个答案(抄写在下面)


您可以使用多响应线性模型,其中每个响应分别对每个预测值进行回归,例如:

lm(cbind(Sepal.Length,Sepal.Width) ~ Species,data=iris)

Call:
lm(formula = cbind(Sepal.Length, Sepal.Width) ~ Species, data = iris)

Coefficients:
                   Sepal.Length  Sepal.Width
(Intercept)         5.006         3.428     
Speciesversicolor   0.930        -0.658     
Speciesvirginica    1.582        -0.454 
reformulate(termlabels="Species",response="cbind(Sepal.Length,Sepal.Width)")

cbind(Sepal.Length, Sepal.Width) ~ Species
您得到两组预测值,tidy在这方面做得很好:

tidy(lm(cbind(Sepal.Length,Sepal.Width) ~ Species,data=iris))
# A tibble: 6 x 6
  response     term              estimate std.error statistic   p.value
  <chr>        <chr>                <dbl>     <dbl>     <dbl>     <dbl>
1 Sepal.Length (Intercept)          5.01     0.0728     68.8  1.13e-113
2 Sepal.Length Speciesversicolor    0.93     0.103       9.03 8.77e- 16
3 Sepal.Length Speciesvirginica     1.58     0.103      15.4  2.21e- 32
4 Sepal.Width  (Intercept)          3.43     0.0480     71.4  5.71e-116
5 Sepal.Width  Speciesversicolor   -0.658    0.0679     -9.69 1.83e- 17
6 Sepal.Width  Speciesvirginica    -0.454    0.0679     -6.68 4.54e- 10  
为此,我们编写了一个函数,并为预测器添加了一个附加列:

library(purrr)
library(dplyr)

tidyMLM = function(iv,dat){

    f = reformulate(termlabels=iv,
    response="cbind(Sepal.Length,Sepal.Width)")
    
    tidy(lm(f,data=dat)) %>% mutate(predictor=iv,.after=response)
}
使用令人惊异的呼噜声(拉普利的日子一去不复返了):

predictors=setdiff(colnames(iris),c(“萼片长度”,“萼片宽度”))
map_dfr(预测因子,tidyMLM,dat=iris)
#A tibble:14x7
响应预测项估计标准误差统计p值
萼片长…花瓣长(截距)4.31 0.0784 54.9 2.43e-100
萼片长…花瓣长花瓣长0.409 0.0189 21.6 1.04e-47
3萼片宽花瓣长(截距)3.45 0.0761 45.4 9.02e-89
4萼片宽花瓣长花瓣长-0.106 0.0183-5.77 4.51e-8
萼片长…花瓣宽(截距)4.78 0.0729 65.5 3.34e-111
6萼片长…花瓣宽花瓣宽0.889 0.0514 17.3 2.33e-37
7萼片宽花瓣宽(截距)3.31 0.0621 53.3 1.84e-98
8萼片宽花瓣宽花瓣宽-0.209 0.0437-4.79 4.07e-6
9萼片棱…种(截距)5.01 0.0728 68.8 1.13e-113
10萼片长…种变种…0.93 0.103 9.03 8.77e-16
11萼片长…种属维吉…1.58 0.103 15.4 2.21e-32
12萼片宽度种(截距)3.43 0.0480 71.4 5.71e-116
13萼片宽种变种…-0.658 0.0679-9.69 1.83e-17
14萼片宽度物种物种Virgi…-0.454 0.0679-6.68 4.54e-10
如果你有80个回答,120个预测因素:

df = data.frame(matrix(rnorm(100*200),ncol=200))
colnames(df) = c(paste0("r",1:80),paste0("p",1:120))
responses = as.matrix(df[,grep("r",colnames(df))])
predictors = grep("p",colnames(df),value=TRUE)

tidyMLM = function(iv,dat){
    
        f = reformulate(termlabels=iv,
        response="responses")
        
        tidy(lm(f,data=dat)) %>% mutate(predictor=iv,.after=response)
    }

map_dfr(predictors,tidyMLM,dat=df)

# A tibble: 19,200 x 7
   response predictor term        estimate std.error statistic p.value
   <chr>    <chr>     <chr>          <dbl>     <dbl>     <dbl>   <dbl>
 1 r1       p1        (Intercept)  -0.0959    0.0882    -1.09   0.280 
 2 r1       p1        p1            0.0217    0.0879     0.247  0.806 
 3 r2       p1        (Intercept)  -0.0174    0.101     -0.172  0.864 
 4 r2       p1        p1           -0.186     0.101     -1.84   0.0685
 5 r3       p1        (Intercept)   0.0214    0.0947     0.226  0.822 
 6 r3       p1        p1            0.0651    0.0944     0.690  0.492 
 7 r4       p1        (Intercept)   0.0708    0.103      0.686  0.495 
 8 r4       p1        p1           -0.142     0.103     -1.38   0.169 
 9 r5       p1        (Intercept)   0.134     0.101      1.33   0.186 
10 r5       p1        p1            0.0248    0.100      0.247  0.805 
# … with 19,190 more rows
df=data.frame(矩阵(rnorm(100*200),ncol=200))
colnames(df)=c(paste0(“r”,1:80),paste0(“p”,1:120))
responses=as.matrix(df[,grep(“r”,colnames(df)))
预测值=grep(“p”,colnames(df),value=TRUE)
tidyMLM=功能(iv,dat){
f=重新格式化(术语标签=iv,
response=“responses”)
tidy(lm(f,数据=dat))%>%突变(预测值=iv,.after=应答)
}
map_dfr(预测因子,tidyMLM,dat=df)
#一个tibble:19200 x 7
响应预测项估计标准误差统计p值
1 r1 p1(截距)-0.0959 0.0882-1.09 0.280
2 r1 p1 p1 0.0217 0.0879 0.247 0.806
3 r2 p1(截距)-0.0174 0.101-0.172 0.864
4 r2 p1 p1-0.186 0.101-1.84 0.0685
5 r3 p1(截距)0.0214 0.0947 0.226 0.822
6 r3 p1 p1 0.0651 0.0944 0.690 0.492
7 r4 p1(截距)0.0708 0.103 0.686 0.495
8 r4 p1-0.142 0.103-1.38 0.169
9 r5 p1(截距)0.134 0.101 1.33 0.186
10 r5 p1 p1 0.0248 0.100 0.247 0.805
#…还有19190行

我希望这现在有意义。

您可以使用多响应线性模型,其中每个响应分别与每个预测值回归,例如:

lm(cbind(Sepal.Length,Sepal.Width) ~ Species,data=iris)

Call:
lm(formula = cbind(Sepal.Length, Sepal.Width) ~ Species, data = iris)

Coefficients:
                   Sepal.Length  Sepal.Width
(Intercept)         5.006         3.428     
Speciesversicolor   0.930        -0.658     
Speciesvirginica    1.582        -0.454 
reformulate(termlabels="Species",response="cbind(Sepal.Length,Sepal.Width)")

cbind(Sepal.Length, Sepal.Width) ~ Species
您得到两组预测值,tidy在这方面做得很好:

tidy(lm(cbind(Sepal.Length,Sepal.Width) ~ Species,data=iris))
# A tibble: 6 x 6
  response     term              estimate std.error statistic   p.value
  <chr>        <chr>                <dbl>     <dbl>     <dbl>     <dbl>
1 Sepal.Length (Intercept)          5.01     0.0728     68.8  1.13e-113
2 Sepal.Length Speciesversicolor    0.93     0.103       9.03 8.77e- 16
3 Sepal.Length Speciesvirginica     1.58     0.103      15.4  2.21e- 32
4 Sepal.Width  (Intercept)          3.43     0.0480     71.4  5.71e-116
5 Sepal.Width  Speciesversicolor   -0.658    0.0679     -9.69 1.83e- 17
6 Sepal.Width  Speciesvirginica    -0.454    0.0679     -6.68 4.54e- 10  
为此,我们编写了一个函数,并为预测器添加了一个附加列:

library(purrr)
library(dplyr)

tidyMLM = function(iv,dat){

    f = reformulate(termlabels=iv,
    response="cbind(Sepal.Length,Sepal.Width)")
    
    tidy(lm(f,data=dat)) %>% mutate(predictor=iv,.after=response)
}
使用令人惊异的呼噜声(拉普利的日子一去不复返了):

predictors=setdiff(colnames(iris),c(“萼片长度”,“萼片宽度”))
map_dfr(预测因子,tidyMLM,dat=iris)
#A tibble:14x7
响应预测项估计标准误差统计p值
萼片长…花瓣长(截距)4.31 0.0784 54.9 2.43e-100
萼片长…花瓣长花瓣长0.409 0.0189 21.6 1.04e-47
3萼片宽花瓣长(截距)3.45 0.0761 45.4 9.02e-89
4萼片宽花瓣长花瓣长-0.106 0.0183-5.77 4.51e-8
萼片长…花瓣宽(截距)4.78 0.0729 65.5 3.34e-111
6萼片长…花瓣宽花瓣宽0.889 0.0514 17.3 2.33e-37
7萼片宽花瓣宽(截距)3.31 0.0621 53.3 1.84e-98
8萼片宽花瓣宽花瓣宽-0.209 0.0437-4.79 4.07e-6
9萼片棱…种(截距)5.01 0.0728 68.8 1.13e-113
10萼片长…种变种…0.93 0.103 9.03 8.77e-16
11萼片长…种属维吉…1.58 0.103 15.4 2.21e-32
12萼片宽度种(截距)3.43 0.0480 71.4 5.71e-116
13萼片宽种变种…-0.658 0.0679-9.69 1.83e-17
14萼片宽度物种物种Virgi…-0.454 0.0679-6.68 4.54e-10
如果你有80个回答,120个预测因素:

df = data.frame(matrix(rnorm(100*200),ncol=200))
colnames(df) = c(paste0("r",1:80),paste0("p",1:120))
responses = as.matrix(df[,grep("r",colnames(df))])
predictors = grep("p",colnames(df),value=TRUE)

tidyMLM = function(iv,dat){
    
        f = reformulate(termlabels=iv,
        response="responses")
        
        tidy(lm(f,data=dat)) %>% mutate(predictor=iv,.after=response)
    }

map_dfr(predictors,tidyMLM,dat=df)

# A tibble: 19,200 x 7
   response predictor term        estimate std.error statistic p.value
   <chr>    <chr>     <chr>          <dbl>     <dbl>     <dbl>   <dbl>
 1 r1       p1        (Intercept)  -0.0959    0.0882    -1.09   0.280 
 2 r1       p1        p1            0.0217    0.0879     0.247  0.806 
 3 r2       p1        (Intercept)  -0.0174    0.101     -0.172  0.864 
 4 r2       p1        p1           -0.186     0.101     -1.84   0.0685
 5 r3       p1        (Intercept)   0.0214    0.0947     0.226  0.822 
 6 r3       p1        p1            0.0651    0.0944     0.690  0.492 
 7 r4       p1        (Intercept)   0.0708    0.103      0.686  0.495 
 8 r4       p1        p1           -0.142     0.103     -1.38   0.169 
 9 r5       p1        (Intercept)   0.134     0.101      1.33   0.186 
10 r5       p1        p1            0.0248    0.100      0.247  0.805 
# … with 19,190 more rows
df=data.frame(矩阵(rnorm(100*200),ncol=200))
colnames(df)=c(paste0(“r”,1:80),paste0(“p”,1:120))
responses=as.matrix(df[,grep(“r”,colnames(df)))
预测值=grep(“p”,colnames(df),value=TRUE)
tidyMLM=功能(iv,dat){
f=重新格式化(术语标签=iv,
response=“responses”)
tidy(lm(f,数据=dat))%>%突变(预测值=iv,.after=应答)
}
map_dfr(预测因子,tidyMLM,dat=df)
#一个tibble:19200 x 7
响应预测项