繁体   English   中英

编写 function 以运行多个回归模型,其中自变量和因变量在 R 中发生变化

[英]Write a function to run multiple regression models with changing independent variables and changing dependent variables in R

以数据集mtcars为例:目标是写一个 function 来运行自变量变化因变量变化的多个回归模型。

在我编写的代码(如下)中, var是自变量, mpg是自变量。 我使用map反复运行回归,使用vsam作为每次变化的自变量。

var = c("vs", "am")

mtcars %>% select(all_of(var)) %>% 
  map(~ glm(mpg ~ .x + cyl + disp + splines::ns(wt, 2) + hp, 
            family = gaussian(link = "identity"), 
            data = mtcars)) %>% 
  map_dfr(tidy, conf.int = T, .id = 'source') %>% 
  select(source, source, term, estimate, std.error, conf.low, conf.high, p.value) 

我想使用一组不同的自变量以及我可以指定的 y 运行相同的回归(例如,我使用上面的mpg运行,我想将其更改为qsec或其他一些变量)。 所以我设想这样一个 function :

function_name <- function(x, y, dataset){
  dataset %>% select(all_of(x)) %>%
    map(~ glm(y ~ .x + cyl + disp + splines::ns(wt, 2) + hp, 
              family = gaussian(link = "identity"), 
              data = dataset)) %>%
    map_dfr(tidy, conf.int = T, .id = 'source') %>% 
    select(source, source, term, estimate, std.error, conf.low, conf.high, p.value) 
}

但是这个 function 没有用。 有什么建议么?

你可以像这样达到你想要的结果:

  1. 您的代码的问题是y ~...不起作用。 相反,您可以使用reformulate (或as.formula )为您的回归 model 动态创建公式。
  2. 为了使这项工作直接在字符向量x上循环或更精确地setNames(x, x)而不是在dataset %>% select(all_of(x))上循环。
library(dplyr)
library(purrr)
library(broom)

function_name <- function(x, y, dataset) {
  map(setNames(x, x), ~ glm(reformulate(
    termlabels = c(.x, "cyl", "disp", "splines::ns(wt, 2)", "hp"),
    response = y
  ),
  family = gaussian(link = "identity"),
  data = dataset
  )) %>%
    map_dfr(tidy, conf.int = T, .id = "source") %>%
    select(source, source, term, estimate, std.error, conf.low, conf.high, p.value)
}

var <- c("vs", "am")

function_name(x = var, y = "mpg", mtcars)
#> # A tibble: 14 × 7
#>    source term                  estimate std.error conf.low conf.high  p.value
#>    <chr>  <chr>                    <dbl>     <dbl>    <dbl>     <dbl>    <dbl>
#>  1 vs     (Intercept)          32.7         3.49    25.8     39.5     1.24e- 9
#>  2 vs     vs                    1.03        1.52    -1.95     4.01    5.05e- 1
#>  3 vs     cyl                  -0.187       0.821   -1.80     1.42    8.21e- 1
#>  4 vs     disp                  0.000545    0.0119  -0.0228   0.0239  9.64e- 1
#>  5 vs     splines::ns(wt, 2)1 -22.4         4.82   -31.9    -13.0     9.02e- 5
#>  6 vs     splines::ns(wt, 2)2  -9.48        3.16   -15.7     -3.28    6.09e- 3
#>  7 vs     hp                   -0.0202      0.0115  -0.0427   0.00226 9.02e- 2
#>  8 am     (Intercept)          34.6         2.65    29.4     39.8     1.15e-12
#>  9 am     am                    0.0113      1.57    -3.06     3.08    9.94e- 1
#> 10 am     cyl                  -0.470       0.714   -1.87     0.931   5.17e- 1
#> 11 am     disp                  0.000796    0.0125  -0.0236   0.0252  9.50e- 1
#> 12 am     splines::ns(wt, 2)1 -21.5         5.86   -33.0    -10.0     1.14e- 3
#> 13 am     splines::ns(wt, 2)2  -9.21        3.34   -15.8     -2.66    1.07e- 2
#> 14 am     hp                   -0.0214      0.0136  -0.0480   0.00527 1.28e- 1

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM