【问题标题】:How to get a collection of p-values for linear regression? [duplicate]如何获得线性回归的 p 值集合? [复制]
【发布时间】:2018-08-28 09:58:57
【问题描述】:

我有 131 列的数据。第一列是我的 Y。我有 130 个 X。我想要 130 个线性回归,它们是 lm(y ~ x1)、lm(y ~ x2)、lm(y ~ x3) ....lm(y ~x130)。然后得到每个拟合的 p 值。我怎样才能让它更快? for循环还是应用?

【问题讨论】:

    标签: r lm p-value


    【解决方案1】:

    仅使用 base R 可以通过一系列*apply 指令来完成。

    首先,由于您没有发布,我将弥补一些数据。

    set.seed(7637)    # Make the results reproducible
    
    n <- 100
    dat <- as.data.frame(replicate(11, rnorm(n)))
    names(dat) <- c("Y", paste0("X", 1:10))
    

    现在,对于回归。

    lm_list <- lapply(dat[-1], function(x) lm(Y ~ x, dat))
    lm_smry <- lapply(lm_list, summary)
    lm_pval <- sapply(lm_smry, function(x) x$coefficients[, "Pr(>|t|)"])
    

    【讨论】:

      【解决方案2】:

      如果您的数据看起来像这样(只是更大)

      > library(dplyr)
      > tbl <- data.frame(
      +     A = rnorm(10),
      +     B = rnorm(10),
      +     C = rnorm(10)
      + ) %>% mutate(
      +     y = 2 * A + rnorm(10, .1)
      + )
      > tbl
                  A           B           C           y
      1  -1.3430281  0.06457155 -0.31477796 -3.54276780
      2  -0.8045598  0.55160502 -0.04486946 -0.17595827
      3   0.6432380 -0.38036302  0.30313165  2.71317260
      4   0.9282322  0.92453929  1.52828109  1.41677569
      5  -0.2104841 -0.31510189 -1.32938820 -0.02714028
      6  -1.8264372  0.92910256  0.16072524 -5.09970701
      7   0.9568248  0.42829255 -0.28423084  1.58072449
      8  -1.2061661 -1.10672961  0.69626390 -3.19605711
      9   0.6173230  2.74964116  0.67350556  1.78849532
      10 -1.1575590 -0.01747244 -0.10611764 -3.09733526
      

      您可以使用tidyr 将其制成更易于使用的表单

      > tidy_tbl <- tbl %>% tidyr::gather(var, x, -y)
      > head(tidy_tbl)
                  y var          x
      1 -3.54276780   A -1.3430281
      2 -0.17595827   A -0.8045598
      3  2.71317260   A  0.6432380
      4  1.41677569   A  0.9282322
      5 -0.02714028   A -0.2104841
      6 -5.09970701   A -1.8264372
      

      然后,您可以使用 broom 为每个 var 组拟合模型

      > library(broom)
      > fitted <- tidy_tbl %>% 
      +     group_by(var) %>% 
      +     do(model = lm(y ~ x, data = .))
      > fitted
      Source: local data frame [3 x 2]
      Groups: <by row>
      
      # A tibble: 3 x 2
        var   model   
      * <chr> <list>  
      1 A     <S3: lm>
      2 B     <S3: lm>
      3 C     <S3: lm>
      

      您可以使用tidy 将拟合模型从数据框中的嵌套列表移动到它们的摘要:

      > fitted %>% tidy(model)
      # A tibble: 6 x 6
      # Groups:   var [3]
        var   term        estimate std.error statistic   p.value
        <chr> <chr>          <dbl>     <dbl>     <dbl>     <dbl>
      1 A     (Intercept)   0.0744     0.305     0.244 0.814    
      2 A     x             2.46       0.288     8.54  0.0000271
      3 B     (Intercept)  -1.05       0.945    -1.11  0.298    
      4 B     x             0.750      0.891     0.842 0.424    
      5 C     (Intercept)  -0.842      0.920    -0.915 0.387    
      6 C     x             0.610      1.26      0.485 0.641  
      

      【讨论】:

        猜你喜欢
        • 2021-11-24
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2019-01-14
        • 2021-02-09
        • 1970-01-01
        • 2013-09-27
        • 2017-01-24
        相关资源
        最近更新 更多