qqline 连接第一和第三四分位数.如何在不同的分位数(即 30% 和 70%)之间画一条线? [英] qqline connects the first and third quartiles. How do I draw a line between different quantiles (ie 30% and 70%)?

查看:60
本文介绍了qqline 连接第一和第三四分位数.如何在不同的分位数(即 30% 和 70%)之间画一条线?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个包含 1000 多个点的数据集的 qqnorm 图.我想一次在两个分位数之间划一条线(比如 30% 和 70%),就像 qqline 对 25% 和 75% 所做的那样,但可以自由选择要使用的分位数.我在 ggplot 上闲逛了一下,但似乎无法得到一些有用的东西.这样做的最佳方法是什么?

我的数据是Ecdat的Garch数据集中美元兑英镑汇率的变化:

diffbp = diff(Garch[,5])

在那之后,我要做的就是

qqnorm(diffbp)qqline(diffbp)

但我还需要 qqline 可以通过不同分位数的图,以便进行比较.

我尝试了以下方法:

<代码>>qq范数(diffbp)>分位数(diffbp,probs=c(0.01,0.99))1% 99%-0.029850 0.032675>qqline(c(-0.029850,0.032675))

我不认为结果行正是我想要的.也就是说,当我希望它通过适当的分位数时,我相信这条线总是在 x 轴上通过 -1 和 1.

编辑 3:想通了!手工计算样本和理论分位数,通过它们计算线的方程,然后使用abline()绘制线.不过,这样做相当耗时.

解决方案

以下信息适用于 R(和 stats 包)2.15.1 版 - 显然更高的版本将包含绘制任意分位数的内置功能.<​​/p><小时>

qqline 似乎是硬编码来绘制 .25 和 .75 分位数.但是,如果您不介意创建自己的函数,则可以这样做:

myQqplot <- function(data, probs){qq范数(数据)theQuants <- 分位数(数据,概率 = 概率)lm1 <- lm(theQuants~qnorm(probs))abline(lm1)无形的()}myQQplot(diffbp, c(.5,.99))

没有检查以确保您只向 probs 参数提供两个分位数,但您可以根据需要添加它.

I have a qqnorm plot of a data set with 1000+ points. I want to draw a line between two quantiles at a time (say 30% and 70%) just as qqline does with 25% and 75%, but with the freedom of choosing which quantiles to use. I've poked around with ggplot a bit but can't seem to get something that works. What's the best way to do this?

edit: My data are the changes in the dollar to pound exchange rate in the Garch data set of Ecdat:

diffbp = diff(Garch[,5])

After that, all I'm doing is

qqnorm(diffbp)

qqline(diffbp)

but I also need plots where qqline can go through different quantiles, for comparison.

edit 2: I've tried the following:

> qqnorm(diffbp)

> quantile(diffbp,probs=c(0.01,0.99))
       1%       99% 
-0.029850  0.032675 

> qqline(c(-0.029850,0.032675))

I don't think the resulting lines are exactly what I'm going for. Namely, I believe that the line always goes through -1 and 1 on the x axis, when I want it to go through the appropriate quantiles.

edit 3: Figured it out! Computed the sample and theoretical quantiles by hand, computed the equation of the line through them, and then drew the line using abline(). Rather time-consuming way of going about it, though.

解决方案

EDIT:

The information below is valid for R (and stats package) version 2.15.1 - apparently later versions will incorporate the built-in capability of plotting arbitrary quantiles.


qqline seems to be hard-coded to plot the .25 and .75 quantiles. But if you don't mind creating your own function, something like this might do:

myQqplot <- function(data, probs){
  qqnorm(data)
  theQuants <- quantile(data, probs = probs)
  lm1 <- lm(theQuants~qnorm(probs))
  abline(lm1)
  invisible()
}

myQqplot(diffbp, c(.5,.99))  

There's no check to make sure that you supply only two quantiles to the probs argument, but you could add it if you want.

这篇关于qqline 连接第一和第三四分位数.如何在不同的分位数(即 30% 和 70%)之间画一条线?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆