【问题标题】:propagating controls for a number of nested groups为多个嵌套组传播控件
【发布时间】:2020-12-26 03:33:47
【问题描述】:

这是对上一篇帖子here 的后续问题。

我从@akrun 那里得到了一组关于我的玩具问题的很好的答案,但是当我阅读答案时,我意识到它实际上还不适用于现实生活中的问题。这个问题的说明仍然是正确的::

之前:

之后:

进一步的挑战是“grp”和“治疗”变量只是我所描述的级别,因为这些是需要传播控制的地方。事实上,还有四个与组处于同一级别的额外分组变量,每个变量都有自己的一组阳性和阴性对照。

因此,此问题的解决方案不能是识别所有阴性/阳性对照并将它们附加到每个组。解决方案需要在考虑分组的情况下执行,然后在所有组中适当地传播。由于 dplyr 似乎非常适合这种方法,我认为这是要走的路,但我有点卡在中间。对我来说,这建议了一个 purrr 解决方案,但除了 reduce() 之外,我根本没有使用 purrr。或者可能是group_by() %>% nest %>% ... %>% unnest 的路要走?​​p>

library(tidyverse)

data_propagated_controls <- data %>%
# \\ group the data by all the grouping variables
# \\ exclude the 'treatment' variables
group_by(var1, var2, var3) %>%
# \\ split into individual dataframes
group_split() %>%
# \\ for each list item propagate controls
# \\ similar as the problem described below
# \\ steps to run in pseudocode

identify and extract controls
append controls to each treatment
add a column to distinguish treatment/controls
join all treatments/controls by rbind

# \\ reassemble the dataframe from the list
# \\ reduce with rbind or full_join should work
reduce(split, rbind)

上一篇文章中的问题说明:

librar(ggplot)

before <- structure(list(group = c("grp1", "grp1", "grp1", "grp1", 
"grp2", "grp2", "grp2", "grp2", "grp3", "grp3", "grp3", "grp3", 
"neg", "neg", "pos", "pos"), treatment = c("A", "B", "C", 
"D", "A", "B", "C", "D", "A", "B", "C", "D", "none", "none", 
"none", "none"), value = c(3L, 5L, 7L, 9L, 2L, 4L, 6L, 8L, 3L, 
4L, 6L, 9L, 12L, 10L, 1L, 2L)), class = "data.frame", row.names = c(NA, -16L))

ggplot(data = before, aes(x=treatment, y=value)) + geom_boxplot() + facet_wrap (~group)

after <- structure(list(group = c("grp1", "grp1", "grp1", "grp1", "grp1", "grp1", 
"grp1", "grp1", "grp2", "grp2", "grp2", "grp2", "grp2", "grp2", 
"grp2", "grp2", "grp3", "grp3", "grp3", "grp3", "grp3", "grp3", 
"grp3", "grp3"), treatment = c("A", "B", "C", "D", "neg", "neg", 
"pos", "pos", "A", "B", "C", "D", "neg", "neg", "pos", "pos", 
"A", "B", "C", "D", "neg", "neg", "pos", "pos"), value = c(3L, 
5L, 7L, 9L, 12L, 10L, 1L, 2L, 2L, 4L, 6L, 8L, 12L, 10L, 1L, 2L, 
3L, 4L, 6L, 9L, 12L, 10L, 1L, 2L)), class = "data.frame", row.names = c(NA, -24L))

ggplot(data = after, aes(x=treatment, y=value)) + geom_boxplot() + facet_wrap (~group)

【问题讨论】:

  • 想了想,我想我可以通过多种方式来解决这个问题。要么我只是在group_bygroup_split 之后获取列表,然后通过 lapply 运行一些附加功能,以按照概述添加到数据集。或者,我可以通过lapply 在拆分列表中的分组数据帧上生成我喜欢的图,甚至无需修改数据集。

标签: r dplyr purrr


【解决方案1】:

我想我找到了一个解决方案。它利用嵌套数据框和“full_join”的简洁功能,因为它将缺失值传播到适当的缺失点。

对于我的代码,Drug_ID == "DMSO" 表示应该在所有其他 Drug_ID 中传播的对照处理。并且 Cell_Line_ID、DOX_ID 和 time 列包含额外的分组变量,对于每个单独的条件,每个变量都有自己的控制值。

现在这很好地允许我在每个时间点和条件下将控件绘制到绘图的每个方面。我现在的最后一个问题是对控制值进行更多控制。如果它与一堆其他措施重叠,那真的很难看。 ggplot 需要针对特定​​元素的“bring_to_front”函数。

#// generate a list of lists that contains all relevant controls only
temp_ctrls <- data %>%
     #// group by variables with separate DMSO controls
     group_by(Cell_Line_ID, DOX_ID, time) %>%
     #// identify and filter all controls
     filter(Drug_control != "none") %>%
     #// remove column for Drug_ID
     select(-Drug_ID) %>%
     #// split groups into individual lists
     nest()
#// change names of data column to dmso
names(temp_ctrls)[[which(names(temp_ctrls)=="data")]] <- "ctrl"

#// generate a list of lists that contains all data that needs appending
data_list <- data %>%
     #// group by variables now including Drug_ID
     group_by(Cell_Line_ID, DOX_ID, time, Drug_ID) %>%
     #// split groups into individual lists
     nest()

#// merge two nested lists by Cell_Line, DOX, and time
data_m <- full_join(data_list, temp_ctrls)

#// remove all list items with Drug_ID DMOS
data_m <- filter(data_m, Drug_ID != "DMSO")

#// assemble control and data and unnest
data_m <- data_m %>%
     #// create new list column with merged data + ctrl
     mutate(merged = map2(data, ctrl, rbind)) %>%
     #// remove extraneous data columns
     select(-data, -ctrl) %>%
     #// unnest everything into a single dataframe
     unnest()

#// clean-up
rm(temp_ctrls, data_list)

【讨论】:

  • 很高兴你能弄明白
  • 我还通过绘制完全独立的geom_smooth 层完全控制了控制线与治疗线的设计。这一切都太强大了。让我真希望我十年前就学会了!
猜你喜欢
  • 1970-01-01
  • 2011-08-18
  • 2023-04-03
  • 1970-01-01
  • 2018-03-24
  • 2011-06-01
  • 2017-12-24
  • 2022-11-04
  • 2012-10-20
相关资源
最近更新 更多