【问题标题】:Nested loop for regression over several columns in RR中多列回归的嵌套循环
【发布时间】:2021-03-11 22:09:39
【问题描述】:

我有两个数据框。一个我的数据:

test <- structure(list(IDcount = c(1, 1, 1, 1, 1, 2, 2, 2, 2, 2), year = c(1, 
2, 3, 4, 5, 1, 2, 3, 4, 5), Otminus1 = c(-0.28, -0.28, -0.44, 
-0.27, 0.23, -0.03, -0.06, -0.04, 0, 0.02), N.1 = c(-0.76, -0.1, 
0.01, 0.1, -0.04, -0.04, -0.04, -0.04, -0.05, -0.05), N.2 = c(NA, 
-0.86, -0.09, 0.11, 0.06, -0.05, -0.08, -0.08, -0.09, -0.09), 
    N.3 = c(NA, NA, -0.85, 0.01, 0.07, -0.04, -0.09, -0.12, -0.13, 
    -0.13)), row.names = c(NA, -10L), groups = structure(list(
    IDcount = c(1, 2), .rows = structure(list(1:5, 6:10), ptype = integer(0), class = c("vctrs_list_of", 
    "vctrs_vctr", "list"))), row.names = 1:2, class = c("tbl_df", 
"tbl", "data.frame"), .drop = TRUE), class = c("grouped_df", 
"tbl_df", "tbl", "data.frame"))

还有一个可以捕捉我的回归结果:

results <- structure(list(IDcount = c(1, 2), N.1 = c(NA, NA), N.2 = c(NA, 
NA), N.3 = c(NA, NA), N.4 = c(NA, NA), N.5 = c(NA, NA)), row.names = c(NA, 
-2L), class = "data.frame")

我正在对我的数据进行回归,其中每家公司都被分配了一个系数,代码如下:

betas <- matrix(nrow=2, ncol=2)

colnames(betas) <- c("Intercept", "beta")
  
    for (i in 1:2) {
    betas[i,] <- coef(lm(Otminus1~N.1, test[test$IDcount==i,]))
    }
  
    betas <- data.frame(betas)
    
    results$N.1 <- betas$beta

我现在想将此循环嵌套在一个循环中,以便回归和结果数据框中使用的列从 1 移动到 3。使用循环应该会导致列 N.1 到 N.5 中的值在数据框结果中。 这是我的错误做法:

for (j in 1:5) {
    
    for (i in 1:2) {
    betas[i,] <- coef(lm(Otminus1~N.j, test[test$IDcount==i,]))
    }
  
    betas <- data.frame(betas)
   
    results$N.j <- betas$beta
  }

但是这个循环无法将 N.j 中的 j 识别为 for 循环变量之一。

【问题讨论】:

    标签: r for-loop nested lm


    【解决方案1】:

    通过在 N.j 列名上迭代 j 来尝试此代码:

    library(dplyr)
    library(stringr)
    
    index <- colnames(test) %>% str_which("N.")
    
    for (j in colnames(test)[index]) {
      
      for (i in 1:2) {
        betas[i,] <- coef(lm(Otminus1~., test[test$IDcount==i, c("Otminus1", j)]))
      }
      
      betas <- data.frame(betas)
      
      results[[j]] <- betas$beta
    }
    

    【讨论】:

    • 非常感谢您的帮助!!!这行得通。但是,我的原始数据扩展了 40 多列,因此我更愿意通过迭代索引来调用这些列。
    • 我修改了代码以从您的数据框中提取 N. 列。
    【解决方案2】:

    您可以使用 dplyrtidyr 并摆脱 for 循环。

    library(dplyr) #dplyr > 1.0.0
    library(tidyr)
    
    test %>%
      pivot_longer(cols = starts_with('N')) %>%
      group_by(IDcount, name) %>%
      summarise(value = coef(lm(Otminus1~value, cur_data()))) %>%
      slice(2L) %>%
      pivot_wider()
    
    #  IDcount     N.1    N.2    N.3
    #    <dbl>   <dbl>  <dbl>  <dbl>
    #1       1  0.0756  0.190  0.499
    #2       2 -5.33   -0.815 -0.412
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2015-07-01
      • 2022-10-14
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多