首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >ggplot中GLM的多次比较的输出

ggplot中GLM的多次比较的输出
EN

Stack Overflow用户
提问于 2019-02-14 11:23:31
回答 1查看 351关注 0票数 0

我想在ggplot中可视化GLM的多个比较的输出。在我的示例中:

代码语言:javascript
复制
#Simulation data set
set.seed(123)
d<-NULL
N<-54
d$Production <- rgamma(N,10)
d$Feature <- ifelse(d$Production >7 & d$Production<10, c("A"),ifelse(d$Production>11, 
c("B"), c("C")))  
#d$Temp<-rnorm(N,20,5)
d$Temp<-rep(1:3,18)
d<-as.data.frame(d)
#

# Gamma glm model
mG<- glm(Production~ Feature + Temp, family= Gamma, data = d)
summary(mG)
anova(mG,test="Chi")

#If for example a have interaction between Temp and Feature
d$BV <- interaction(d$Feature, d$Temp)

# GLM fit
mI  <- glm(Production ~ -1 + BV, data = d, family = "Gamma")
#summary(glht(mI, linfct = mcp(BV = "Tukey")))
cld(glht(mI, linfct = mcp(BV = "Tukey")))


# Prediction values
pred.data = data.frame(
Feature<-d$Feature,
Temp<-d$Temp
)  
pred.data$Production = predict(mG, newdata=pred.data, type="response")

# Select means
library(dplyr)
d2<-d %>%
  group_by(Feature, Temp) %>%
  summarize(Production = mean(Production, na.rm = TRUE))

#Final plot
library("ggplot2")
ggplot(d2, aes(Temp, Production, colour = Feature)) +
  geom_point() +
  stat_smooth(method = "glm", formula = y ~ x, family = Gamma)
#

现在我想在ggplot中可视化cld(glht(mI,linfct = mcp(BV =“Tukey”)的字母作为输出:

这是可能的吗?谢谢

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2019-02-14 16:24:49

试试这个:

代码语言:javascript
复制
library(tidyverse)

res <- cld(glht(mI, linfct = mcp(BV = "Tukey")))

d2 <-
  res$mcletters$Letters %>%
  tibble(
    reg = names(.),
    lett = .
  ) %>%
  mutate(
    Feature = substr(reg, 1, 1),
    Temp = substr(reg, 3, 3) %>% as.integer()
  ) %>%
  left_join(d2) %>%
  mutate(lett_pos = if_else(Feature == 'C', Production - .5, Production + .5))

ggplot(d2, aes(Temp, Production, colour = Feature)) +
  geom_point() +
  stat_smooth(method = "glm", formula = y ~ x) +
  geom_text(aes(y = lett_pos, label = lett), color = 'black')

票数 2
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/54682750

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档