2017-01-07 5 views
2

ggplot2のロジット回帰の予測確率をグラフ化する必要があります。本質的には、同じグラフ内の各治療条件によってglmをグラフ化しようとしています。しかし、私は非常に私のtreat変数(すなわち、私は興味がある)がカテゴリカルであることを見てこれを行う方法を混乱させる。これは、ggplotを使って治療効果をグラフ化しようとすると、 0、1、および2ですが、行はありません。グラフGLM in ggplot2

私の質問は...どのように私はこの場合ロジット予測線をグラフ化できますか?前もって感謝します!

set.seed(96) 
df <- data.frame(
    vote <- sample(0:1, 200, replace = T), 
    treat <- sample(0:3, 200, replace = T)) 

glm_output <- glm(vote ~ as.factor(treat), data = df, family ="binomial"(link = "logit")) 

predicted_vote <- data.frame(predict(glm_output, newdata = df, type = "link", interval = "confidence", se = T)) 
df <- cbind(df, predicted_vote) 

答えて

2

treat説明変数は、あなたの代わりに、以下のような箱ひげ図を使用している場合、それはより多くの意味を行います、カテゴリですので:

ggplot(df, aes(x = treat, y = predicted_prob)) + 
    geom_boxplot(aes(fill = factor(treat)), alpha = .2) 

enter image description here

あなたがによって予測確率を表示したい場合glmを試してみてください。

ggplot(df, aes(x = treat, y = predicted_prob)) + 
    geom_boxplot(aes(fill = factor(treat)), alpha = .2) + facet_wrap(~gender) 

enter image description here

# create age groups 
df$age_group <- cut(df$age, breaks=seq(0,100,20)) 

ggplot(df, aes(x = treat, y = predicted_prob)) + 
    geom_boxplot(aes(fill = factor(treat)), alpha = .2) + facet_grid(age_group~gender) 

enter image description here