使用purrr和函数对多个具有随机误差的变量执行线性回归

问题描述 投票:0回答:1

这是我的数据:

dataSet <- data.frame(study_id=c(1,1,1,1,2,2,2,2,3,3,3,3),
                      Timepoint=c(1,6,12,18,1,6,12,18,1,6,12,18),
                      Secretor=c(0,0,0,0,1,1,1,1,0,0,0,0),
                      Gene1=c(1,2,3,4,1,2,3,4,1,2,3,4),
                      Gene2=c(3,4,5,6,3,4,5,6,3,4,5,6),
                      Gene3=c(4,5,6,7,4,5,6,7,4,5,6,7),
                      Gene4=c(6,7,8,9,6,7,8,9,6,7,8,9))

我已成功使用purrr通过以下功能生成许多探索性ggplots:

library(tidyverse)

stat_sum_df_all <- function(fun, geom="pointrange", ...) {
  stat_summary(fun.data=fun, geom=geom, ...)
}

plot_fun = function(x, y) {
  ggplot(data = dataSet, aes(x = .data[[x]], y = .data[[y]], group = Secretor, colour = Secretor)) +
    stat_summary(geom = "line", fun.data = median_hilow) +
    stat_sum_df_all("median_hilow", fun.args=(conf.int = 0.5), linetype = "solid") +
    theme_bw()
} 

genelist = names(dataSet)[4:7]
Timepoint = names(dataSet)[2]

all_plots = map(genelist,
                ~map(Timepoint, plot_fun, y = .x) )

现在我想做的是将线性回归的p值放在图的标题中。我回归的公式是:

library(lmerTest)
fit <- lmer(genelist ~ Timepoint*Secretor + (1|study_id), data=dataSet)

但是,我无法弄清楚如何像我为每个基因进行回归分析的绘图一样创建一个函数。预先感谢您的任何建议。

r function regression purrr
1个回答
0
投票

我在reddit上获得了帮助,这是解决方案,谢谢大家的帮助。

my_fitting_function <- function(gene) {
  f <- paste0(gene, " ~ Timepoint*Secretor + (1|study_id)")
  fit <- lmer(f, data = dataSet)
  return(fit)
}
models <- purrr::map(genelist, my_fitting_function)
© www.soinside.com 2019 - 2024. All rights reserved.