R 在geom_smooth and stat_fit_tidy中向glm公式添加偏移项

R 在geom_smooth and stat_fit_tidy中向glm公式添加偏移项,r,ggplot2,formula,glm,ggpmisc,R,Ggplot2,Formula,Glm,Ggpmisc,我有一个data.frame,在三个clusters中每两个groups的计数,我正在对其进行逻辑回归(binomialglm,带有logit链接函数),并使用ggplot2的geom\u-bar和geom\u平滑绘制,并使用ggpmisc的stat\u fit\u tidy添加p值 下面是它的样子: 数据: 假设我有每个组的预期概率,我想将其作为偏移量添加到geom\u smooth和stat\u fit\u tidyglms。我该怎么做 接下来,我将这些偏移量添加到observed.dat

我有一个
data.frame
,在三个
cluster
s中每两个
group
s的计数,我正在对其进行逻辑回归(
binomial
glm
,带有
logit
链接函数
),并使用
ggplot2
geom\u-bar
geom\u平滑绘制,并使用
ggpmisc
stat\u fit\u tidy
添加p值

下面是它的样子:

数据:

假设我有每个
组的预期概率,我想将其作为
偏移量添加到
geom\u smooth
stat\u fit\u tidy
glm
s。我该怎么做

接下来,我将这些偏移量添加到
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))))
但我得到这些警告:

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\u smooth
stat\u fit\u tidy
glm
s中吗?甚至仅仅是对
geom\u smooth
glm(注释
stat\u fit\u tidy
行)


或者,是否可以将通过在
ggplot
调用外部拟合
glm
获得的预测回归线、SE和p值添加到
geom\u条中(
fit问题是,在ggplot中,模型公式中的
x
y
代表美学,而不是
数据中的变量名称,即,在ggplot中,模型公式中的名称代表美学。没有
p
美学,因此尝试拟合时,找不到
p
。无法传递数字v这里的ector作为ggplot将数据分成几组,并分别为每组拟合模型,我们可以将单个数值向量作为常量值传递。我认为需要定义一个新的伪美学及其相应的比例,才能以这种方式进行拟合

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