簡體   English   中英

兩次回歸的系數 plot

[英]Coefficient plot of two regressions

我正在嘗試使用ggplot創建一個系數 plot ,它結合了兩個不同回歸的結果。 每個回歸的系數、標准誤差和置信區間界限存儲為以下形式的數據框:

## regression1: 
   var     betas       crse       upper       lower
1  x1  0.517251974 0.58176862  0.37751553  0.05698842
2  x2 -0.260210445 0.03521915 -0.12118217 -0.25923872
3  x3  0.680752318 0.08844444  0.75410023  0.40740441
4  x4  0.663395004 0.05090350  0.26316403  0.06362598
5  x5 -0.551992451 0.03289870 -0.08751219 -0.21647271

## regression2:
   var     betas       crse       upper       lower
1  x1 -0.343254719 0.05498965  0.01451302 -0.20104246
2  x2  0.126434568 0.02243139  0.17040108  0.08247165
3  x3 -0.178460203 0.06215729 -0.05663415 -0.30028625
4  x4  0.301058265 0.03737595  0.37431378  0.22780275
5  x5 -0.054594805 0.02037967 -0.01465139 -0.09453822

我的方法是使用以下方法將兩種回歸組合到一個數據框中:

combined <- rbind(regression1, regression2)

然后我使用ggplot

ggplot(combined, aes(x=var, y=betas)) +
  geom_point(aes(x=var, y=betas), 
             color="red", 
             shape=15) + 
  geom_errorbar(aes(ymin=lower, ymax=upper), 
                width=.25, 
                size=.65)

但是,在 plot 中,具有系數/置信區間的兩個模型的線條相互重疊,很難區分哪個是哪個。 有沒有辦法將線條分開以便區分? 也許我使用了錯誤的方法,我不應該rbind這兩個情節。

您只需要創建一個新變量來跟蹤兩個回歸並允許點/誤差線的顏色隨該變量而變化。 閃避允許將兩個回歸分開。 所以:

regression1$Reg <- "Regression 1"
regression2$Reg <- "Regression 2"

combined <- rbind(regression1, regression2)

ggplot(combined, aes(x=var, y=betas)) +
  geom_point(aes(x=var, y=betas, colour = Reg), 
             shape=15, position = position_dodge(width = 0.25)) + 
  geom_errorbar(aes(ymin=lower, ymax=upper, colour = Reg), 
                width=.25, position = position_dodge(width = 0.25), 
                size=.65)

您可以使用matplot 但是,您的回歸似乎有缺陷,因為 beta 不在 CI 之間。

matplot(regression1[, c(2, 4:5)], type='l', lty=c(1, 2, 2), col=1)
matlines(regression2[, c(2, 4:5)], type='l', lty=c(1, 2, 2), col=2)
legend('topright', legend=paste('r', 1:2), text.col=1:2, bty='n')

在此處輸入圖像描述

@user2602640 的回答得到了要點,但是您可以使用一些額外的快捷方式。

  • dplyr::bind_rows()將自動設置一個“id”列(您需要命名 arguments,這有點煩人;否則 id 將恢復為數字索引)
  • 如果您對誤差線沒有端蓋的線條感到滿意(無論如何,極簡主義/塔夫特定量圖形學派推薦),那么geom_pointrange()將同時處理點和 CI(也許有一個類似的組合-在擴展的 ggplot-verse 中某處的點和誤差線幾何?)
  • 您可能對broomdotwhisker包感興趣,它們可以進一步簡化類似的任務(例如,如果您有兩個回歸dotwhisker::dwplot(r1, r2)將自動構建組合系數圖)

position_dodge()選擇的寬度 (0.25) 是您可能想要調整的美學選擇(ggplot 會自動避開箱線圖等幾何圖形,但考慮零寬度的點,因此您需要指定位移量)

library(tidyverse)
combined <- bind_rows(reg1=reg1, reg2=reg2, .id="model")
ggplot(combined, aes(x=var, y=betas, ymin=lower, ymax=upper, colour=model)) +
   geom_pointrange(position=position_dodge(width=0.25))

數據設置

reg1 <- read.table(header=TRUE, text="
   var     betas       crse       upper       lower
 x1  0.517251974 0.58176862  0.37751553  0.05698842
 x2 -0.260210445 0.03521915 -0.12118217 -0.25923872
 x3  0.680752318 0.08844444  0.75410023  0.40740441
 x4  0.663395004 0.05090350  0.26316403  0.06362598
 x5 -0.551992451 0.03289870 -0.08751219 -0.21647271
")

reg2 <- read.table(header=TRUE, text="
   var     betas       crse       upper       lower
 x1 -0.343254719 0.05498965  0.01451302 -0.20104246
 x2  0.126434568 0.02243139  0.17040108  0.08247165
 x3 -0.178460203 0.06215729 -0.05663415 -0.30028625
 x4  0.301058265 0.03737595  0.37431378  0.22780275
 x5 -0.054594805 0.02037967 -0.01465139 -0.09453822
")

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM