Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/r/77.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
R 是否有一种简洁的方法,可以用geom_quantile()中的方程式和其他统计数据标记ggplot?_R_Ggplot2_Label_Quantile Regression_Ggpmisc - Fatal编程技术网

R 是否有一种简洁的方法,可以用geom_quantile()中的方程式和其他统计数据标记ggplot?

R 是否有一种简洁的方法,可以用geom_quantile()中的方程式和其他统计数据标记ggplot?,r,ggplot2,label,quantile-regression,ggpmisc,R,Ggplot2,Label,Quantile Regression,Ggpmisc,我想以类似于我对geom_平滑(method=“lm”)拟合线性回归(我以前使用过ggpmisc的方式,包括geom_分位数()拟合线的相关统计数据。例如,此代码: # quantile regression example with ggpmisc equation # basic quantile code from here: # https://ggplot2.tidyverse.org/reference/geom_quantile.html library(tidyverse) l

我想以类似于我对
geom_平滑(method=“lm”)
拟合线性回归(我以前使用过ggpmisc的方式,包括
geom_分位数()拟合线的相关统计数据。例如,此代码:

# quantile regression example with ggpmisc equation
# basic quantile code from here:
# https://ggplot2.tidyverse.org/reference/geom_quantile.html

library(tidyverse)
library(ggpmisc)
# see ggpmisc vignette for stat_poly_eq() code below:
# https://cran.r-project.org/web/packages/ggpmisc/vignettes/user-guide.html#stat_poly_eq

my_formula <- y ~ x
#my_formula <- y ~ poly(x, 3, raw = TRUE)

# linear ols regression with equation labelled
m <- ggplot(mpg, aes(displ, 1 / hwy)) +
  geom_point()

m + 
  geom_smooth(method = "lm", formula = my_formula) +
  stat_poly_eq(aes(label =  paste(stat(eq.label), "*\" with \"*", 
                                  stat(rr.label), "*\", \"*", 
                                  stat(f.value.label), "*\", and \"*",
                                  stat(p.value.label), "*\".\"",
                                  sep = "")),
               formula = my_formula, parse = TRUE, size = 3)  


您将如何将摘要统计信息输出到标签,或在移动中重新创建它们?(即,除了在调用ggplot之前进行回归,然后将其传递给注释(例如,类似于所做的事情或线性回归?

@mark neal
stat_-fit_-glance()
quantreg::rq()
一起工作外,使用
stat_-fit_-glance()
更为复杂。该统计数据不“知道”从
glance()
可以得到什么,因此必须手动组装
标签

你需要知道什么是可用的。你可以在ggplot外运行fit模型并使用
glance()
来找出它返回的列,或者在ggplot中借助包“gginnards”来执行此操作。我将展示此替代方法,继续上面的代码示例

库(gginnards)
m+
几何分位数(分位数=0.5)+
统计拟合(method=“rq”、method.args=list(formula=y~x)、geom=“debug”)
默认情况下,
geom_debug()

# A tibble: 1 x 11
   npcx  npcy   tau logLik    AIC    BIC df.residual     x      y PANEL group
  <dbl> <dbl> <dbl>  <dbl>  <dbl>  <dbl>       <int> <dbl>  <dbl> <fct> <int>
1    NA    NA   0.5   816. -1628. -1621.         232  1.87 0.0803 1        -1
此示例生成以下曲线图。

使用
stat\u fit\u tidy()
同样的方法也应该有效。但是,在“ggpmisc”(=0.3.8)中,现在在CRAN中

以下示例仅适用于“ggpmisc”(>=0.3.8)

剩下的问题是
tible
glance()
tidy()
返回是否包含要添加到绘图中的信息,至少在默认情况下,
tidy.qr()
似乎不是这样。但是,
tidy.rq()
有一个参数
se.type
,用于确定在
tible
中返回的值。修改后的
stat\u fit\u tidy()
接受要传递给
tidy()
的命名参数,从而可以执行以下操作

m+
几何分位数(分位数=0.5)+
stat_fit_tidy(method=“rq”,
method.args=list(公式=y~x),
tidy.args=list(se.type=“nid”),
mapping=aes(label=sprintf('y~“=”~%.3g~+~%.3g~x*”,带“*斜体(P)~”=”~%.3f',
在统计(截距估计)之后,
统计后(x估计),
在_stat(x_p.value))之后,
parse=TRUE)
此示例生成以下曲线图

定义一个新的stat
stat\u rq\u eq()
会使这更简单:


<代码> STATORQRQEQNP>请考虑这是佩德罗优秀答案的附录,在那里他做了大部分重举重-这增加了一些演示调整(颜色和线型)和代码来简化多个分位数,产生下面的情节:

库(tidyverse)
库(ggpmisc)#确保版本为0.3.8或更高
图书馆(量子力学)
库(泛型)

my_公式您希望在绘图上打印的具体“相关统计数据”是什么?当然,最好适合您自己的模型,以获得重要的拟合统计数据,而不是在绘图函数上进行响应。这些其他函数基本上只是为您做这件事的包装器(最终要多次拟合模型,这有点浪费)。如果你真的想隐藏工作,你可以编写自己的geom来实现这一点,但这基本上就是需要发生的事情。说得对。我认为可能有一个解决方案,使用ggpmisc的
stat_fit_glance
,尽管我只需要了解它如何处理多个分位数。@MarkNeal我提出了一个问题来提醒自己如果实现了
glance()@MarkNeal新的“ggpmisc”0.3.8现在已经在CRAN中了。我已经根据这个版本更新了我的答案。现在可以使用
stat\u fit\u tidy()
@MarkNeal添加方程和装有
qr()的模型的p值。下一个版本的“ggpmisc”,0.4.0正在GitHub中形成。我已经添加了
stat\u quant\u eq()
。它试图为你的问题提供一个优雅的答案,包括对分位数向量的支持。如果你能抽出时间,你能检查一下并给出反馈吗?你需要从GitHub软件包“ggpp”和“gppmisc”安装:
remotes::install_GitHub(“aphalo/ggpp”)
remotes::install_GitHub(“aphalo/ggpmisc”,ref=“stat.quant.eq”)
ref
参数指向实现新stat的分支。对于那些新的分支,在
stat\u fit\u tidy()中添加
tau=0.9
将第90个分位数方程提供给绘图以匹配
geom\u分位数(分位数=0.9)
将产生该行。如果未定义tau值,则默认为0.5(即“中值”回归)。多个分位数方程的“联合”处理可允许标签“步进”垂直,这样就不需要指定手动y轴位置来避免重叠。我将考虑合并一些代码来打印
P@MarkNealP值的代码位于
stat\u poly\u eq()的源代码中
并且可以从那里复制。考虑到我很可能也会使用它,我将在下一个版本之前尝试添加对多个tau值的支持。考虑我自己将如何使用它,也可以使用stat\u quantile\u band()来绘制分位数,就像stat\u smooth()绘制c一样
# A tibble: 1 x 11
   npcx  npcy   tau logLik    AIC    BIC df.residual     x      y PANEL group
  <dbl> <dbl> <dbl>  <dbl>  <dbl>  <dbl>       <int> <dbl>  <dbl> <fct> <int>
1    NA    NA   0.5   816. -1628. -1621.         232  1.87 0.0803 1        -1
library(tidyverse)
library(ggpmisc) #ensure version 0.3.8 or greater
library(quantreg)
library(generics)

my_formula <- y ~ x
#my_formula <- y ~ poly(x, 3, raw = TRUE)

# base plot
m <- ggplot(mpg, aes(displ, 1 / hwy)) +
  geom_point()

# function for labelling
# Doesn't neatly handle P values (e.g return "P<0.001 where appropriate)

stat_rq_eqn <- function(formula = y ~ x, tau = 0.5, colour = "red", label.y = 0.9, ...) {
  stat_fit_tidy(method = "rq",
                method.args = list(formula = formula, tau = tau), 
                tidy.args = list(se.type = "nid"),
                mapping = aes(label = sprintf('italic(tau)~"="~%.3f~";"~y~"="~%.3g~+~%.3g~x*", with "~italic(P)~"="~%.3g',
                                              after_stat(x_tau),
                                              after_stat(Intercept_estimate), 
                                              after_stat(x_estimate),
                                              after_stat(x_p.value))),
                parse = TRUE,
                colour = colour,
                label.y = label.y,
                ...)
}

# This works, though with double entry of plot specs
# custom colours and linetype
# https://stackoverflow.com/a/44383810/4927395
# https://stackoverflow.com/a/64518380/4927395


m + 
  geom_quantile(quantiles = c(0.1, 0.5, 0.9), 
                aes(colour = as.factor(..quantile..),
                    linetype = as.factor(..quantile..))
                )+
  scale_color_manual(values = c("red","purple","darkgreen"))+
  scale_linetype_manual(values = c("dotted", "dashed", "solid"))+
  stat_rq_eqn(tau = 0.1, colour = "red", label.y = 0.9)+
  stat_rq_eqn(tau = 0.5, colour = "purple", label.y = 0.95)+
  stat_rq_eqn(tau = 0.9, colour = "darkgreen", label.y = 1.0)+
  theme(legend.position = "none") # suppress legend


# not a good habit to have double entry above
# modified with reference to tibble for plot specs, 
# though still a stat_rq_eqn call for each quantile and manual vertical placement
# https://www.r-bloggers.com/2019/06/curly-curly-the-successor-of-bang-bang/

my_tau = c(0.1, 0.5, 0.9)
my_colours = c("red","purple","darkgreen")
my_linetype = c("dotted", "dashed", "solid")

quantile_plot_specs <- tibble(my_tau, my_colours, my_linetype)

m + 
  geom_quantile(quantiles = {{quantile_plot_specs$my_tau}}, 
                aes(colour = as.factor(..quantile..),
                    linetype = as.factor(..quantile..))
  )+
  scale_color_manual(values = {{quantile_plot_specs$my_colours}})+
  scale_linetype_manual(values = {{quantile_plot_specs$my_linetype}})+
  stat_rq_eqn(tau = {{quantile_plot_specs$my_tau[1]}}, colour = {{quantile_plot_specs$my_colours[1]}}, label.y = 0.9)+
  stat_rq_eqn(tau = {{quantile_plot_specs$my_tau[2]}}, colour = {{quantile_plot_specs$my_colours[2]}}, label.y = 0.95)+
  stat_rq_eqn(tau = {{quantile_plot_specs$my_tau[3]}}, colour = {{quantile_plot_specs$my_colours[3]}}, label.y = 1.0)+
  theme(legend.position = "none")