在 R 中添加许多具有给定斜率和截距的直线(lmer 包的输出)

add many straight line with given slopes and intercepts in R (output of lmer package)

我有一个来自 glmer 函数的混合效果模型“logit_ri”(随机拦截)。我尝试将固定效应斜率和随机截距提取到数据框中,然后按组“Land”(德国各州)绘制它

# Extract out the fixed-effect slope 
slope <- fixef(logit_ri)['logASLr:Artl']

# Extract out the random-effect intercept for Land
Land_intercept <- ranef(logit_ri)$Land

# Create a new column for the slope
Land_intercept$slope <- slope

# Use the row names to create a county name column
Land_intercept$Land <- rownames(Land_intercept)

x <- c(seq(1:2),seq(1:2),seq(1:2),seq(1:2),seq(1:2),seq(1:2),seq(1:2),seq(1:2))
y <- c(seq(1:16))
Land_intercept <- data.frame(cbind(Land_intercept,x,y))
colnames(Land_intercept)[1]<- "intercept"


数据框如下所示:

Land_intercept
                          intercept     slope                   Land x  y
Baden-W¸rttemberg      -0.161346161 -2.366151      Baden-W¸rttemberg 1  1
Bayern                  0.008278875 -2.366151                 Bayern 2  2
Berlin                 -0.105067965 -2.366151                 Berlin 1  3
Brandenburg             0.507153607 -2.366151            Brandenburg 2  4
Bremen                 -0.217788246 -2.366151                 Bremen 1  5
Hamburg                -0.660789497 -2.366151                Hamburg 2  6
Hessen                 -0.217997505 -2.366151                 Hessen 1  7
Mecklenburg-Vorpommern  0.262814205 -2.366151 Mecklenburg-Vorpommern 2  8
Niedersachsen          -0.182076161 -2.366151          Niedersachsen 1  9
Nordrhein-Westfalen     0.015583050 -2.366151    Nordrhein-Westfalen 2 10
Rheinland-Pfalz        -0.239750889 -2.366151        Rheinland-Pfalz 1 11
Saarland               -0.164090824 -2.366151               Saarland 2 12
Sachsen                 0.373352938 -2.366151                Sachsen 1 13
Sachsen-Anhalt          0.443658186 -2.366151         Sachsen-Anhalt 2 14
Schleswig-Holstein     -0.098413476 -2.366151     Schleswig-Holstein 1 15
Th¸ringen               0.436479863 -2.366151              Th¸ringen 2 16

并通过 ggplot2(通过“Land”)绘制它

ggplot(data=Land_intercept,
   aes(x,y))+
  geom_point()+
  theme_bw() +
  scale_y_continuous(limits=c(-3,1))+
  geom_abline(data=Land_intercept,aes(slope=slope,intercept=intercept,color=factor(Land)))

但是,我的情节中的截距不正确。 我将不胜感激任何帮助。 由于我无法上传原始数据(它很大),我可能需要帮助绘制数据,包括 16 个斜率(它们都是相同的)和 16 个德国州的 16 个截距。 (正如我在上面发布的那样)

最好在这些行上也加上标签,以使其更清晰易读。

或者一些优雅的方法来绘制具有斜率和截距的随机截距模型也很棒。

提前感谢您的任何建议和帮助。

回复:评论

你不需要 x 和 y 美学来让情节发挥作用。要显示网格,您必须有轴限制。如果您考虑由 y = a + bx 延伸到无穷远的参数化线,这是有道理的。

library(ggplot2)

Land_intercept <- data.frame(
  intercept = c(-0.161346161,0.008278875,
                -0.105067965,0.507153607,-0.217788246,-0.660789497,
                -0.217997505,0.262814205,-0.182076161,0.01558305,
                -0.239750889,-0.164090824,0.373352938,0.443658186,-0.098413476,
                0.436479863),
  slope = c(-2.366151,-2.366151,-2.366151,
            -2.366151,-2.366151,-2.366151,-2.366151,-2.366151,
            -2.366151,-2.366151,-2.366151,-2.366151,-2.366151,
            -2.366151,-2.366151,-2.366151),
  Land = c("Baden-W¸rttemberg","Bayern",
           "Berlin","Brandenburg","Bremen","Hamburg","Hessen",
           "Mecklenburg-Vorpommern","Niedersachsen",
           "Nordrhein-Westfalen","Rheinland-Pfalz","Saarland","Sachsen",
           "Sachsen-Anhalt","Schleswig-Holstein","Th¸ringen")
)

ggplot(Land_intercept) +
  geom_abline(aes(slope = slope, intercept = intercept, colour = Land)) +
  xlim(c(-1, 1)) + ylim(c(-3,  1))

reprex package (v1.0.0)

于 2021-08-19 创建