【问题标题】:How to create a loop that will make regression models in R? [closed]如何创建将在 R 中创建回归模型的循环? [关闭]
【发布时间】:2018-07-29 04:05:48
【问题描述】:

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

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

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

【问题讨论】:

  • 你在这个问题上问的有点太多了。如果你不能 1) 为数据的子集创建一个循环 2) 绘制模型 3) 找到趋势线的斜率 那么它们是三个不同的东西,我敢说你至少可以找到第 2 点和第 3 点很容易已经在堆栈溢出。

标签: r for-loop lm


【解决方案1】:

假设您只是使用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 获得了物种列表,然后逐个元素地遍历该元素。

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

【讨论】:

    【解决方案2】:

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

    # 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)
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2021-05-26
      • 2018-02-26
      • 1970-01-01
      • 2019-05-03
      • 2020-07-16
      • 1970-01-01
      • 2022-01-12
      • 1970-01-01
      相关资源
      最近更新 更多