2015-11-24 78 views
-1

我有一个变量,其中有学生的分数。我正在寻找最好的方式来绘制分数的百分位数。为了让我的数据的一瞥,钟形百分位曲线在R

[1] 26 30 27 28 27 27 29 28 3 12 27 24 29 25 26 30 25 27 28 27 25 14 30 28 24 28 27 
    [28] 19 18 25 28 24 24 6 20 28 28 27 22 27 19 22 21 20 30 29 26 30 28 29 28 29 25 25 
    [55] 27 26 20 26 10 21 20 16 24 24 26 27 28 27 29 29 27 23 20 18 19 26 21 25 17 22 28 
    [82] 26 27 27 25 26 25 29 29 28 25 22 30 29 28 28 25 29 30 27 28 28 30 28 29 29 30 29 
[109] 27 27 28 24 25 15 20 25 24 25 28 26 27 21 18 24 24 23 30 23 28 22 29 26 29 25 29 
[136] 20 25 28 12 16 23 13 17 12 17 26 13 26 28 26 25 27 21 30 30 30 27 20 24 21 28 26 
[163] 22 21 26 29 28 24 30 22 21 25 26 28 26 23 27 25 24 27 15 21 13 28 30 29 28 27 23 
[190] 27 23 28 29 18 27 23 24 28 30 30 30 29 18 24 21 17 16 12 28 22 23 26 21 12 20 20 
[217] 26 28 27 27 30 26 29 27 24 23 27 26 14 23 16 15 26 28 27 27 25 29 15 23 22 29 26 
[244] 20 20 21 21 24 24 20 25 23 22 24 22 26 28 28 27 24 28 28 27 27 27 21 23 21 24 28 
[271] 25 23 19 21 20 21 23 

可重复的目的,我用下面的代码,

x <- seq(0,50,length=100) 
quantile(x,c(.10,.20,.30,.40,.50,.60,.70,.80,.90,1)) 

10% 20% 30% 40% 50% 60% 70% 80% 90% 100% 
    5 10 15 20 25 30 35 40 45 50 

我试图plot(quantile(x,c(.10,.20,.30,.40,.50,.60,.70,.80,.90,1)))但情节没有显示的理想方式。我期待像弯曲这将显示类似下面的百分比正态分布钟,

enter image description here

这样做,我想我应该变量转换成正态分布之一,使用下面

y <- dnorm(x) 
plot(x,y,type="l") 

,得到了以下输出,

enter image description here

> z <- scale(x) 
> y <- dnorm(z) 
> plot(z,y, type= "l") 

enter image description here

+2

'x < - seq(-3,3,0.01); y < - dnorm(x); plot(x,y,type ='l');' – Barranka

+0

@Barranka我已经试过了。对于我的输出,我只获得上述输出。你能检查一下吗? – Observer

+0

你显然没有做Barranka建议的。他的输出是准确的。不知道你实际做了什么。但我猜'z < - scale(x); y < - dorm(z); plot(z,y,type =“l”)'是你想要做的(并且做错了)。 –

回答

2

我想你正在寻找的东西是这样的:

x <- c(26 ,30 ,27 ,28 ,27 ,27 ,29 ,28 , 3 ,12 ,27 ,24 ,29 ,25 ,26 ,30 ,25 ,27 ,28 ,27 ,25 ,14 ,30 ,28 ,24 ,28 ,27 
     ,19 ,18 ,25 ,28 ,24 ,24 , 6 ,20 ,28 ,28 ,27 ,22 ,27 ,19 ,22 ,21 ,20 ,30 ,29 ,26 ,30 ,28 ,29 ,28 ,29 ,25 ,25 
     ,27 ,26 ,20 ,26 ,10 ,21 ,20 ,16 ,24 ,24 ,26 ,27 ,28 ,27 ,29 ,29 ,27 ,23 ,20 ,18 ,19 ,26 ,21 ,25 ,17 ,22 ,28 
     ,26 ,27 ,27 ,25 ,26 ,25 ,29 ,29 ,28 ,25 ,22 ,30 ,29 ,28 ,28 ,25 ,29 ,30 ,27 ,28 ,28 ,30 ,28 ,29 ,29 ,30 ,29 
     ,27 ,27 ,28 ,24 ,25 ,15 ,20 ,25 ,24 ,25 ,28 ,26 ,27 ,21 ,18 ,24 ,24 ,23 ,30 ,23 ,28 ,22 ,29 ,26 ,29 ,25 ,29 
     ,20 ,25 ,28 ,12 ,16 ,23 ,13 ,17 ,12 ,17 ,26 ,13 ,26 ,28 ,26 ,25 ,27 ,21 ,30 ,30 ,30 ,27 ,20 ,24 ,21 ,28 ,26 
     ,22 ,21 ,26 ,29 ,28 ,24 ,30 ,22 ,21 ,25 ,26 ,28 ,26 ,23 ,27 ,25 ,24 ,27 ,15 ,21 ,13 ,28 ,30 ,29 ,28 ,27 ,23 
     ,27 ,23 ,28 ,29 ,18 ,27 ,23 ,24 ,28 ,30 ,30 ,30 ,29 ,18 ,24 ,21 ,17 ,16 ,12 ,28 ,22 ,23 ,26 ,21 ,12 ,20 ,20 
     ,26 ,28 ,27 ,27 ,30 ,26 ,29 ,27 ,24 ,23 ,27 ,26 ,14 ,23 ,16 ,15 ,26 ,28 ,27 ,27 ,25 ,29 ,15 ,23 ,22 ,29 ,26 
     ,20 ,20 ,21 ,21 ,24 ,24 ,20 ,25 ,23 ,22 ,24 ,22 ,26 ,28 ,28 ,27 ,24 ,28 ,28 ,27 ,27 ,27 ,21 ,23 ,21 ,24 ,28 
     ,25 ,23 ,19 ,21 ,20 ,21 ,23) 

dens <- density(x) 
plot(dens) 
tot <- sum(dens$y) 
qs <- sapply(c(0.25, 0.5, 0.75), function (i) max(which(cumsum(dens$y) <= tot*i))) 
lines(x = dens$x[qs], y = dens$y[qs], type = "h") 
text(x = c(20, 24, 26.6, 29.5), y = 0.02, labels = c("25%", "50%", "75%", "100%")) 

enter image description here

我觉得有可能是一个更简单的方法来获得qs值,但这似乎工作过。你可以使用“25%,50%......直到这里”或者全部25%。

1

既然你不提供任何真实的数据,我只能认为在随机数据:

values <- rnorm(100000, 0,1) # This should be your input, not some random data 
quantile(values, seq(0,1,.1)) 
##   0%   10%   20%   30%   40%   50%   60%   70%   80%   90%   100% 
##-4.576700921 -1.284870700 -0.845223706 -0.526137762 -0.250516413 0.005818037 0.259989565 0.527060926 0.845323134 1.283060660 4.422621338 

而且,如果你想绘制这个数据,您应该创建直方图:

hist(values) 
+0

但我正在寻找钟形百分点曲线吗?直方图如何提供​​帮助?我不太习惯这个。所以如果我问荒谬的问题,请耐心等待 – Observer

+0

@Observer - 钟形曲线本质上适合用于正常数据的直方图条纹。 – thelatemail

1

您可以使用ggplot2轻松制作这样的地块。

x <- rnorm(500) 
y <- dnorm(x) 
df <- data.frame(x=x, y=y) 
q <- quantile(df$x) 

df_plot <- ggplot(df) + geom_line(aes(x,y)) 
      + scale_x_continuous(breaks= seq(-4,4,0.5)) 
      + annotate(geom="text", x=q, y=0, label=names(q)) 
      + theme(text = element_text(size=22)) 
df_plot 

enter image description here

如果你想添加垂直线:

df_plot2 <- ggplot(df) + geom_line(aes(x,y)) 
      + scale_x_continuous(breaks= seq(-4,4,0.5)) 
      + annotate(geom="text", x=q, y=0, label=names(q)) 
      + geom_vline(x=q, linetype = "longdash") 
      + theme(text = element_text(size=22)) 
df_plot2 

enter image description here

要绘制正态分布的标准偏差,使用

q <- quantile(df$x, c(1-0.997, 1-0.95, 1-0.68,0.5,0.68,0.68,0.95,0.977))