我有一个
data.frame
group
三分之一
cluster
binomial
glm
用一个
logit
link function
),并使用
ggplot2
的
geom_bar
和
geom_smooth
,并使用
ggpmisc
的
stat_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")
假设我有每一个的预期概率
组
offset
到
几何平滑
和
保持健康整洁
s。我该怎么做?
跟随
this Cross Validated post
,我将这些偏移量添加到
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)
表达式到
几何平滑
和
保持健康整洁
:
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)'
表示此添加未被识别,并且绘图仅显示条形图:
你知道怎么把抵消项加到
几何平滑
和
保持健康整洁
glm公司
什么?甚至只是为了
几何平滑
glm(注释
保持健康整洁
行)?
几何钢筋
通过拟合得到的预测回归线、SE和p值
外面
ggplot
呼叫(
fit <- glm(value ~ group + offset(p), data = observed.data.df, family = binomial(link = 'logit'))