Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/r/74.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 使用apply()通过多个因变量迭代线性回归模型_R_Loops_Dplyr_Apply - Fatal编程技术网

R 使用apply()通过多个因变量迭代线性回归模型

R 使用apply()通过多个因变量迭代线性回归模型,r,loops,dplyr,apply,R,Loops,Dplyr,Apply,我正在计算45个不同id值的因变量的线性回归的模型输出。如何使用tidy(dplyr、apply等)代码来实现这一点 我有一个包含三个变量的数据集,data=c(id,distance,actPct),这样id==1:45-10您可以使用tidyverse中的map使用groupby、nest和mutate来完成以下操作: data01 %>% group_by(id) %>% nest() %>% mutate(models = map(data, ~ lm

我正在计算45个不同id值的因变量的线性回归的模型输出。如何使用tidy(dplyr、apply等)代码来实现这一点


我有一个包含三个变量的数据集,
data
=
c(id
distance
actPct)
,这样
id
==1:45-10您可以使用
tidyverse
中的
map
使用
groupby
nest
mutate
来完成以下操作:

data01 %>% 
  group_by(id) %>% 
  nest() %>% 
  mutate(models = map(data, ~ lm(actPct ~ distance, data = .x)))

# A tibble: 41 x 3
#       id data             models  
#    <int> <list>           <list>  
#  1    42 <tibble [3 x 2]> <S3: lm>
#  2    43 <tibble [4 x 2]> <S3: lm>
#  3    13 <tibble [2 x 2]> <S3: lm>
#  4    38 <tibble [4 x 2]> <S3: lm>
#  5    29 <tibble [2 x 2]> <S3: lm>
#  6    24 <tibble [5 x 2]> <S3: lm>
#  7    34 <tibble [5 x 2]> <S3: lm>
#  8     7 <tibble [3 x 2]> <S3: lm>
#  9    30 <tibble [2 x 2]> <S3: lm>
# 10    32 <tibble [2 x 2]> <S3: lm>
# ... with 31 more rows
data01%>%
分组依据(id)%>%
嵌套()%>%
变异(模型=地图(数据,~lm(actPct~距离,数据=.x)))
#A tibble:41 x 3
#id数据模型
#                  
#  1    42  
#  2    43  
#  3    13  
#  4    38  
#  5    29  
#  6    24  
#  7    34  
#  8     7  
#  9    30  
# 10    32  
# ... 还有31行
另请参见R for R中关于许多模型的数据科学章节:

数据

set.seed(42)
id <- sample(1:45, 100, replace = T)
distance <- sample(-4:4, 100, replace = T)
actPct <- runif(100, min = 0, max = 1)
data01 <- tibble(id = id, distance = distance, actPct = actPct)
set.seed(42)

id您可以使用
tidyverse
中的
group\u by
nest
mutate
map
来完成以下操作:

data01 %>% 
  group_by(id) %>% 
  nest() %>% 
  mutate(models = map(data, ~ lm(actPct ~ distance, data = .x)))

# A tibble: 41 x 3
#       id data             models  
#    <int> <list>           <list>  
#  1    42 <tibble [3 x 2]> <S3: lm>
#  2    43 <tibble [4 x 2]> <S3: lm>
#  3    13 <tibble [2 x 2]> <S3: lm>
#  4    38 <tibble [4 x 2]> <S3: lm>
#  5    29 <tibble [2 x 2]> <S3: lm>
#  6    24 <tibble [5 x 2]> <S3: lm>
#  7    34 <tibble [5 x 2]> <S3: lm>
#  8     7 <tibble [3 x 2]> <S3: lm>
#  9    30 <tibble [2 x 2]> <S3: lm>
# 10    32 <tibble [2 x 2]> <S3: lm>
# ... with 31 more rows
data01%>%
分组依据(id)%>%
嵌套()%>%
变异(模型=地图(数据,~lm(actPct~距离,数据=.x)))
#A tibble:41 x 3
#id数据模型
#                  
#  1    42  
#  2    43  
#  3    13  
#  4    38  
#  5    29  
#  6    24  
#  7    34  
#  8     7  
#  9    30  
# 10    32  
# ... 还有31行
另请参见R for R中关于许多模型的数据科学章节:

数据

set.seed(42)
id <- sample(1:45, 100, replace = T)
distance <- sample(-4:4, 100, replace = T)
actPct <- runif(100, min = 0, max = 1)
data01 <- tibble(id = id, distance = distance, actPct = actPct)
set.seed(42)

根据我的经验,id
apply
通常速度很慢。@NelsonGon,谢谢,我以前没有体验过缓慢的
apply
功能。还有什么你更喜欢的吗?我读到的所有内容都表明
apply
for loop
快,所以我想尝试一下。你能看看这个问题是否有帮助吗?似乎与您的类似,可能使用
apply
家族的其他成员,但不使用
apply
(个人意见)。
apply
从我的经验来看通常是缓慢的。@NelsonGon,谢谢,我以前没有体验过缓慢的
apply
功能。还有什么你更喜欢的吗?我读到的所有内容都表明
apply
for loop
快,所以我想尝试一下。你能看看这个问题是否有帮助吗?似乎与您的类似,可能使用
apply
家族的其他成员,但不使用
apply
(个人意见)。