2017-01-07 2 views
2

ggplot2에서 로짓 회귀의 예상 확률을 그래프로 나타낼 필요가 있습니다. 본질적으로, 나는 같은 그래프 내에서 각 치료 조건에 따라 glm을 그래프로 표시하려고 노력하고있다. 그러나, 나는 이것을 이해하는 방법에 대해 매우 혼란스러워진다. 나의 treat 변수 (즉, 관심이있는 x)는 범주 형이다. 이것은 ggplot을 사용하여 치료 효과를 그래프로 표시하려고 할 때 나는 0, 1 W 2이지만 행은 없습니다.그래프 GLM in ggplot2

내 질문은 ... 어떻게이 경우에 로그 예측 선을 그래프로 표시 할 수 있습니까? 미리 감사드립니다!

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

enter image description here

당신이에 의해 예측 확률을보고 싶은 경우 : 설명 변수 treat이 범주이므로 대신 다음과 같이 상자 그림 사용하는 경우

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

, 그것은 더 이해가됩니다 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