【问题标题】:Options for caterpillar plots in lme4, grouping by factor to visually identify temporal trendslme4中的毛毛虫图选项,按因子分组以直观地识别时间趋势
【发布时间】:2015-01-05 21:04:42
【问题描述】:

我正在使用 lme4 中的 lmer 函数分析一个庞大而复杂的数据集。我正在使用 lattice 和 dotplot 来生成随机效应的毛毛虫图。有没有办法通过我的数据集中的因子或连续变量对我的毛毛虫图进行颜色编码?也用过qqmath,我可能只是需要帮助理解如何使用“groups”参数。这将有助于讨论点。

library("lme4")
data(package = "lme4")

summary(grouseticks)

fit1<-lmer(TICKS~1+(1|LOCATION), grouseticks)
rr1<-ranef(fit1, condVar = TRUE)
dotplot(rr1)
#We get a nice caterpillar plot of intecepts and variances by location, is there any way to color code those
#blue intercept points by another factor, such as year? 

【问题讨论】:

    标签: r lattice lme4


    【解决方案1】:

    做你想做的事情的问题容易是ranef()结果不包含你想要的信息,dotplot.ranef.merMod()方法有点太硬编码,无法修改很容易......我将展示一个ggplot 解决方案。如果您坚持使用lattice 解决方案,请尝试检查lme4:::dotplot.ranef.merMod,看看您是否可以按照以下解决方案进行调整。

    library("lme4")
    
    fit1 <- lmer(TICKS~1+(1|LOCATION), grouseticks)
    rr1 <- ranef(fit1, condVar = TRUE)
    lattice::dotplot(rr1)
    

    这个问题对于这个特定的数据集没有多大意义,因为这些位置是在多年间不定期采样的:

    yrtab <- with(grouseticks,table(LOCATION,YEAR))
    head(yrtab)
        YEAR
    ## LOCATION 95 96 97
    ##        1  0  5  3
    ##        2  0  0  3
    ##        3  0  7  0
    ##        4  3  6 11
    ##        5  0  3  0
    ##        6  0  9  0
    

    ...但是为了继续这个例子,让我们计算每个位置的 modal 采样年份(即,采集最大样本数的年份 - 对于平局,我们将取第一年,因为它是最简单的,这只是一个例子)

    yrvec <- 95:97
    yrmode <- with(grouseticks,yrvec[apply(yrtab,1,which.max)])
    dd <- data.frame(LOCATION=rownames(yrtab),yrmode)
    

    现在我们需要得到正确形状的随机效应数据,并提取标准误:

    ## extract conditional mode and square root
    ## (c() works on simple attr(.,"postVar") -- would have
    ##  to be more careful with vector-valued random effects
    rr2 <- data.frame(LOCATION=rownames(rr1[[1]]),
                      int=unname(rr1[[1]]),
                      se=sqrt(c(attr(rr1[[1]],"postVar"))))
    ## combine with other variables
    rr3 <- merge(rr2,dd)
    ## prepare for caterpillar by ordering locations by est. value
    rr4 <- transform(rr3,LOCATION=reorder(LOCATION,int))
    library("ggplot2"); theme_set(theme_bw())
    ggplot(rr4,aes(LOCATION,int,ymin=int-1.96*se,ymax=int+1.96*se))+
        geom_pointrange(aes(colour=factor(yrmode)))+coord_flip()+
            scale_colour_discrete(name="year")
    

    这必须概括一下以处理向量值随机效应...

    【讨论】:

    • 嗨,本,感谢您的反馈!只有一个问题,我以前从未使用过转换,但是当我运行您的代码时,我最终得到了这个:
    • 抱歉,图片没有复制。当我运行你的代码时,我最终得到一个仍然按 LOCATION 排序的图(不是截距)。我从未使用过转换,但我怀疑代码在那里崩溃了。尝试使用安排重新排序 dd 框架,但没有帮助。
    • LOCATION=reorder(LOCATION,int) 是关键一步。
    • 从基础而不是其他包转换和重新排序?
    • 是的......虽然我不认为如果你要拉入其他版本应该是一个问题(过去有一个问题(我认为)gdata 包被拉在reorder.factor 的不兼容版本中;我认为在这种情况下,您需要明确指定reorder(LOCATION,int,FUN=mean)
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2011-04-04
    • 2015-02-13
    • 2014-07-30
    • 2014-05-27
    • 2016-03-24
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多