如何创建一个循环来在 R 中建立回归模型?

How to create a loop that will make regression models in R?

我有看起来像这样的数据,有许多物种多年的时间序列数据。

Species    year  x
species1   2000  56
species1   2001  12
species1   2002  40
species2   2000  30
species2   2001  40
species2   2002  50

对于每个物种,我想创建一个 x 与年份的回归模型,我还想绘制每个模型并找到每条趋势线的斜率。为此,我怀疑我应该使用某种类型的循环。

假设您只是使用 lm 诀窍是将数据参数更改为不同的子集。

speciesList <- unique(df$Species)

for(i in 1:length(speciesList){

    lmmodel <- lm(x ~ year, data = subset(df, Species == speciesList[i]))

    #Now do all the stuff you want with lmmodel, e.g. plot, find slope, etc
}

我不会为您编写一整段功能代码,但这是棘手的一点。关于如何从模型绘制数据(包括趋势线等)的资源很多。

使用 subset 函数可以让我们一次提取一个物种的数据子集。我使用 unique 获得了物种列表,然后逐个元素逐个浏览该元素。

我也不确定 x 还是 year 是你的自变量,所以我做出了合乎逻辑的假设它是 year

这是一个没有循环的解决方案。

# some artificial data
set.seed(1)
daf <- data.frame(species = factor(paste0("species", c(rep(1:3, 10)))), 
                  year = rep(2000:2009, 3), x = sample(1:100, 30))

library(dplyr)
library(broom)

lm_fit <- daf %>% group_by(species) %>% 
  do(fit = lm(x ~ year, .))

tidy(lm_fit, fit) # or as.data.frame(tidy(lm_fit, fit)) to get a data.frame

# # A tibble: 6 x 6
# # Groups:   species [3]
# species  term          estimate std.error statistic p.value
# <fct>    <chr>            <dbl>     <dbl>     <dbl>   <dbl>
# 1 species1 (Intercept)   2508       7132       0.352   0.734 
# 2 species1 year        -    1.23       3.56   -0.346   0.738 
# 3 species2 (Intercept) -11250       4128      -2.73    0.0260
# 4 species2 year             5.64       2.06    2.74    0.0256
# 5 species3 (Intercept)    461       7460       0.0618  0.952 
# 6 species3 year        -    0.206      3.72   -0.0554  0.957 

library(ggplot2)
ggplot(daf, aes(x = year, y = x)) + geom_smooth(method = "lm", se = FALSE) +
  facet_wrap(~species)