首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >使用函数()生成df的多个子集的线性回归

使用函数()生成df的多个子集的线性回归
EN

Stack Overflow用户
提问于 2021-07-22 14:03:57
回答 2查看 56关注 0票数 0

作为R的新手,我很难为多个子集分配一个函数并执行它。(我试着解决这个问题已经有三天了,甚至在线程的帮助下,我也无法抓住它。)

设定:随着时间的推移,我需要对不同浓度的多种抗生素("MIC")的杀菌量进行线性回归。由于循环和map对我来说太高级了,所以我想通过给每个子集分配一个函数来解决这个问题。最后,我想要一个数据框架,显示抗生素,MIC,系数,p值.

问题:下面这两种方法都会导致错误消息,我没有完全掌握。

我希望你能和我分享你的编码智慧!

代码语言:javascript
复制
library(tidyverse)
library(ggplot2)
library(drc)

# calucalting linear regressions
## exlcude bacteria("CFU") <=50, it is under detection limit of assay

rawdata2 <- structure(
        list(
                antibiotic = c(
                        "CHX",
                        "CHX",
                        "CHX",
                        "CHX",
                        "CHX",
                        "CHX",
                        "CHX",
                        "CHX",
                        "CHX",
                        "CHX"
                ),
                MIC = structure(
                        c(1L, 1L,
                          1L, 1L, 1L, 1L, 2L, 2L, 2L, 2L),
                        .Label = c("0", "0.25", "0.5",
                                   "1", "2", "4"),
                        class = "factor"
                ),
                minutes = c(0L, 10L, 30L,
                            60L, 120L, 300L, 10L, 30L, 60L, 120L),
                CFU_mean = c(
                        1044444.44444444,
                        1050000,
                        1141666.66666667,
                        2425000,
                        16916666.6666667,
                        157500000,
                        883333.333333333,
                        1175000,
                        1758333.33333333,
                        12408333.3333333
                )
        ),
        row.names = c(NA,-10L),
        groups = structure(
                list(
                        antibiotic = c("CHX",
                                       "CHX"),
                        MIC = structure(
                                1:2,
                                .Label = c("0", "0.25", "0.5", "1",
                                           "2", "4"),
                                class = "factor"
                        ),
                        .rows = structure(
                                list(1:6, 7:10),
                                ptype = integer(0),
                                class = c("vctrs_list_of",
                                          "vctrs_vctr", "list")
                        )
                ),
                row.names = c(NA,-2L),
                class = c("tbl_df",
                          "tbl", "data.frame"),
                .drop = TRUE
        ),
        class = c("grouped_df",
                  "tbl_df", "tbl", "data.frame")
)


regressions <- function(x) {
        model <- lm(CFU ~ minutes, data = .)
        coeff <- coef(model)
        return(coeff)
}

lm_abx_by_MIC <- rawdata2 %>% 
        group_by(antibiotic, MIC) %>% 
        filter(CFU >50) %>% 
        do(regressions(.))

is.data.frame中的错误(数据):对象'.‘未找到

代码语言:javascript
复制
> regressions <- function(x) {
+         model <- lm(CFU_mean ~ minutes, data = x)
+         coeff <- coef(model)
+         return(coeff)
+ }
> lm_abx_by_MIC <- rawdata2 %>% 
+         group_by(antibiotic, MIC) %>% 
+         filter(CFU_mean >50) %>% 
+         do(regressions(.))

错误:结果1,2,3,4,5,必须是数据帧,而不是数字运行rlang::last_error()以查看错误发生的位置。

EN

回答 2

Stack Overflow用户

发布于 2021-07-22 14:53:56

不知道这是否正是你要找的。使用broom::tidy(),我们可以实现这样的解决方案(尽管它没有像您那样定义函数):

代码语言:javascript
复制
rawdata2 %>% 
  ungroup() %>% 
  filter(CFU_mean > 50) %>% 
  nest_by(antibiotic, MIC) %>% 
  mutate(model = list(lm(CFU_mean ~ minutes, data = data))) %>% 
  summarise(broom::tidy(model))
#> # A tibble: 4 x 7
#> # Groups:   antibiotic, MIC [2]
#>  antibiotic MIC   term          estimate std.error statistic p.value
#>  <chr>      <fct> <chr>            <dbl>     <dbl>     <dbl>   <dbl>
#> 1 CHX        0     (Intercept) -15891690. 11183665.    -1.42  0.228  
#> 2 CHX        0     minutes        529669.    82975.     6.38  0.00309
#> 3 CHX        0.25  (Intercept)  -1891787.  2091374.    -0.905 0.461  
#> 4 CHX        0.25  minutes        108146.    30345.     3.56  0.0705 

这对你有帮助吗?

票数 1
EN

Stack Overflow用户

发布于 2021-07-22 14:59:04

使用原始regressions函数的细微变化

代码语言:javascript
复制
rawdata2 %>% 
  group_by(antibiotic, MIC) %>% 
  filter(CFU_mean >50) %>%
  nest() %>%
  mutate(coeff = map(data, regressions)) %>%
  unnest(coeff)

#------
# A tibble: 4 x 4
# Groups:   antibiotic, MIC [2]
  antibiotic MIC   data                       coeff
  <chr>      <fct> <list>                     <dbl>
1 CHX        0     <tibble[,2] [6 x 2]>  -15891690.
2 CHX        0     <tibble[,2] [6 x 2]>     529669.
3 CHX        0.25  <tibble[,2] [4 x 2]>   -1891787.
4 CHX        0.25  <tibble[,2] [4 x 2]>     108146.
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/68486231

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档