R:绘制MASS polr序数模型的预测

时间:2015-10-24 09:40:06

标签: r categorical-data posthoc lsmeans

我使用MASS polr函数使用(在这种情况下,根据数据给出了对不同种类奶酪的偏好)对有序数据拟合比例赔率累积logit模型:

data=read.csv("https://www.dropbox.com/s/psj74dx8ohnrdlp/cheese.csv?dl=1")
data$response=factor(data$response, ordered=T) # make response into ordered factor
head(data)
  cheese response count
1      A        1     0
2      A        2     0
3      A        3     1
4      A        4     7
5      A        5     8
6      A        6     8
library(MASS)
fit=polr(response ~ cheese, weights=count, data=data, Hess=TRUE, method="logistic")

为了绘制模型的预测,我使用

制作了效果图
library(effects)
library(colorRamps)
plot(allEffects(fit),ylab="Response",type="probability",style="stacked",colors=colorRampPalette(c("white","red"))(9))

enter image description here

我想知道,如果根据effects包报告的预测方法,还可以绘制类似于每种奶酪的平均偏好以及95%conf间隔的内容吗?

编辑:最初我还询问了如何获得Tukey posthoc测试,但同时我发现那些可以使用

获得
library(multcomp)
summary(glht(fit, mcp(cheese = "Tukey")))

或使用包lsmeans作为

summary(lsmeans(fit, pairwise ~ cheese, adjust="tukey", mode = "linear.predictor"),type="response")

1 个答案:

答案 0 :(得分:2)

Russ Lenth刚刚向我指出,只需lsmeans option mode="mean" ?modelsclm)即可获得平均偏好和95%置信区间(同样适用于clmmordinal模型适合使用包df=summary(lsmeans(fit, pairwise ~ cheese, mode = "mean"),type="response")$lsmeans cheese mean.class SE df asymp.LCL asymp.UCL A 6.272828 0.1963144 NA 5.888058 6.657597 B 3.494899 0.2116926 NA 3.079989 3.909809 C 4.879440 0.2006915 NA 4.486091 5.272788 D 7.422159 0.1654718 NA 7.097840 7.746478 ):

library(ggplot2)
library(ggthemes)
ggplot(df, aes(cheese, mean.class)) + geom_bar(stat="identity", position="dodge", fill="steelblue", width=0.6) + 
     geom_errorbar(aes(ymin=asymp.LCL, ymax=asymp.UCL), width=0.15, position=position_dodge(width=0.9)) + 
     theme_few(base_size=18) + xlab("Type of cheese") + ylab("Mean preference") + 
     coord_cartesian(ylim = c(1, 9)) + scale_y_continuous(breaks=1:9)

给了我正在寻找的情节:

{{1}}

enter image description here