1

我在三个s 中有一个data.frame每两个 s 的计数,我正在拟合一个逻辑回归(用 a ),并使用's和绘制它,并使用' s添加p值。groupclusterbinomial glmlogit link functionggplot2geom_bargeom_smoothggpmiscstat_fit_tidy

这是它的样子:

数据:

library(dplyr)

observed.probability.df <- data.frame(cluster = c("c1","c1","c2","c2","c3","c3"), group = rep(c("A","B"),3), p = c(0.4,0.6,0.5,0.5,0.6,0.4))
observed.data.df <- do.call(rbind,lapply(c("c1","c2","c3"), function(l){
  do.call(rbind,lapply(c("A","B"), function(g)
    data.frame(cluster = l, group = g, value = c(rep(0,1000*dplyr::filter(observed.probability.df, cluster == l & group != g)$p),rep(1,1000*dplyr::filter(observed.probability.df, cluster == l & group == g)$p)))
  ))
}))

observed.probability.df$cluster <- factor(observed.probability.df$cluster, levels = c("c1","c2","c3"))
observed.data.df$cluster <- factor(observed.data.df$cluster, levels = c("c1","c2","c3"))
observed.probability.df$group <- factor(observed.probability.df$group, levels = c("A","B"))
observed.data.df$group <- factor(observed.data.df$group, levels = c("A","B"))

阴谋:

library(ggplot2)
library(ggpmisc)

ggplot(observed.probability.df, aes(x = group, y = p, group = cluster, fill = group)) +
  geom_bar(stat = 'identity') +
  geom_smooth(data = observed.data.df, mapping = aes(x = group, y = value, group = cluster), color = "black", method = 'glm', method.args = list(family = binomial(link = 'logit'))) + 
  stat_fit_tidy(data = observed.data.df, mapping = aes(x = group, y = value, group = cluster, label = sprintf("P = %.3g", stat(x_p.value))), method = 'glm', method.args = list(formula = y ~ x, family = binomial(link = 'logit')), parse = T, label.x = "center", label.y = "top") +
  scale_x_discrete(name = NULL,labels = levels(observed.probability.df$group), breaks = sort(unique(observed.probability.df$group))) +
  facet_wrap(as.formula("~ cluster")) + theme_minimal() + theme(legend.title = element_blank()) + ylab("Fraction of cells")

在此处输入图像描述

假设我对每个都有预期的概率,group并且我想将其添加为offsettogeom_smoothstat_fit_tidy glms。我该怎么做呢?

此交叉验证帖子之后,我将这些偏移量添加到observed.data.df

observed.data.df <- observed.data.df %>% dplyr::left_join(data.frame(group = c("A","B"), p = qlogis(c(0.45,0.55))))

然后尝试将offset(p)表达式添加到geom_smoothand stat_fit_tidy

ggplot(observed.probability.df, aes(x = group, y = p, group = cluster, fill = group)) +
  geom_bar(stat = 'identity') +
  geom_smooth(data = observed.data.df, mapping = aes(x = group, y = value, group = cluster), color = "black", method = 'glm', method.args = list(formula = y ~ x + offset(p), family = binomial(link = 'logit'))) + 
  stat_fit_tidy(data = observed.data.df, mapping = aes(x = group, y = value, group = cluster, label = sprintf("P = %.3g", stat(x_p.value))), method = 'glm', method.args = list(formula = y ~ x + offset(p), family = binomial(link = 'logit')), parse = T, label.x = "center", label.y = "top") +
  scale_x_discrete(name = NULL,labels = levels(observed.probability.df$group), breaks = sort(unique(observed.probability.df$group))) +
  facet_wrap(as.formula("~ cluster")) + theme_minimal() + theme(legend.title = element_blank()) + ylab("Fraction of cells")

但我收到这些警告:

Warning messages:
1: Computation failed in `stat_smooth()`:
invalid type (closure) for variable 'offset(p)' 
2: Computation failed in `stat_smooth()`:
invalid type (closure) for variable 'offset(p)' 
3: Computation failed in `stat_smooth()`:
invalid type (closure) for variable 'offset(p)' 
4: Computation failed in `stat_fit_tidy()`:
invalid type (closure) for variable 'offset(p)' 
5: Computation failed in `stat_fit_tidy()`:
invalid type (closure) for variable 'offset(p)' 
6: Computation failed in `stat_fit_tidy()`:
invalid type (closure) for variable 'offset(p)' 

表示无法识别此添加,并且仅显示条形图: 在此处输入图像描述

知道如何将偏移项添加到geom_smoothstat_fit_tidy glms 中吗?或者甚至只是到geom_smoothglm (注释掉该stat_fit_tidy行)?

或者,是否可以将geom_bar预测回归线、SE 和通过拟合glm外部ggplot调用 ( fit <- glm(value ~ group + offset(p), data = observed.data.df, family = binomial(link = 'logit'))) 获得的 p 值相加?

4

1 回答 1

1

问题在于,在 ggplotxy模型中,公式代表美学,而不是 中的变量名称data,即在 ggplot 中,模型公式中的名称代表美学。没有p审美,所以当尝试适合时,p找不到。在这里不能传递数字向量,因为 ggplot 会将数据分成组并分别为每个组拟合模型,我们可以将单个数字向量作为常数值传递。我认为人们需要定义一种新的伪美学及其相应的尺度,才能以这种方式进行拟合。

于 2021-05-13T22:43:47.503 回答