在单个页面中通过变量绘制分位数回归 [英] Plotting quantile regression by variables in a single page
问题描述
我正在分别对几个自变量(相同依赖)运行分位数回归.我只想在单个图上绘制每个变量在多个分位数上的斜率估计值.
I am running quantile regressions for several independent variables separately (same dependent). I want to plot only the slope estimates over several quantiles of each variable in a single plot.
这是一个玩具数据:
set.seed(1988)
y <- rnorm(50, 5, 3)
x1 <- rnorm(50, 3, 1)
x2 <- rnorm(50, 1, 0.5)
# Running Quantile Regression
require(quantreg)
fit1 <- summary(rq(y~x1, tau=1:9/10), se="boot")
fit2 <- summary(rq(y~x2, tau=1:9/10), se="boot")
我只想在分位数上绘制斜率估计值.因此,我在plot
中给出了parm=2
.
I want to plot only the slope estimates over quantiles. Hence, I am giving parm=2
in plot
.
plot(fit1, parm=2)
plot(fit2, parm=2)
现在,我想将这两个图合并在一个页面中.
Now, I want to combine both these plots in a single page.
到目前为止我已经尝试过;
What I have tried so far;
- 我尝试设置
par(mfrow=c(2,2))
并绘制它们.但它产生了一个空白页. - 我尝试使用
gridExtra
和gridGraphics失败.尝试按照此处 所述将基本图转换为Grob对象
- 尝试使用功能
layout
功能,如此文档 - 我正在尝试研究
plot.rqs
的源代码.但是我无法理解它如何绘制置信带(我只能在分位数上绘制系数)或在那里更改mfrow
参数.
- I tried setting
par(mfrow=c(2,2))
and plotting them. But it's producing a blank page. - I have tried using
gridExtra
and gridGraphics without success. Tried to convert base graphs into Grob objects as stated here - Tried using function
layout
function as in this document - I am trying to look into the source code of
plot.rqs
. But I am unable to understand how it's plotting confidence bands (I'm able to plot only the coefficients over quantiles) or to changemfrow
parameter there.
有人可以指出我要去哪里了吗?我应该查看plot.rqs
的源代码并在那里更改任何参数吗?
Can anybody point out where am I going wrong? Should I look into the source code of plot.rqs
and change any parameters there?
推荐答案
虽然quantreg::plot.summary.rqs
有一个mfrow
参数,但它使用它来覆盖par('mfrow')
以便覆盖parm
值,这不是什么你想做.
While quantreg::plot.summary.rqs
has an mfrow
parameter, it uses it to override par('mfrow')
so as to facet over parm
values, which is not what you want to do.
一种替代方法是解析对象并手动绘制.您可以从fit1
和fit2
(它们只是每个tau的值列表)中拉出tau值和系数矩阵,因此在tidyverse语法中,
One alternative is to parse the objects and plot manually. You can pull the tau values and coefficient matrix out of fit1
and fit2
, which are just lists of values for each tau, so in tidyverse grammar,
library(tidyverse)
c(fit1, fit2) %>% # concatenate lists, flattening to one level
# iterate over list and rbind to data.frame
map_dfr(~cbind(tau = .x[['tau']], # from each list element, cbind the tau...
coef(.x) %>% # ...and the coefficient matrix,
data.frame(check.names = TRUE) %>% # cleaned a little
rownames_to_column('term'))) %>%
filter(term != '(Intercept)') %>% # drop intercept rows
# initialize plot and map variables to aesthetics (positions)
ggplot(aes(x = tau, y = Value,
ymin = Value - Std..Error,
ymax = Value + Std..Error)) +
geom_ribbon(alpha = 0.5) +
geom_line(color = 'blue') +
facet_wrap(~term, nrow = 2) # make a plot for each value of `term`
如果愿意,可以从对象中拉出更多东西,添加原始对象的水平线,否则会变得疯狂.
Pull more out of the objects if you like, add the horizontal lines of the original, and otherwise go wild.
另一种选择是使用 magick
来捕获原始图像(或保存它们)并重新读取它们)并手动组合它们:
Another option is to use magick
to capture the original images (or save them with any device and reread them) and manually combine them:
library(magick)
plots <- image_graph(height = 300) # graphics device to capture plots in image stack
plot(fit1, parm = 2)
plot(fit2, parm = 2)
dev.off()
im1 <- image_append(p1p2, stack = TRUE) # attach images in stack top to bottom
image_write(im1, 'rq.png')
这篇关于在单个页面中通过变量绘制分位数回归的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!