R中的钟形百分位曲线
我有一个变量,它有学生的分数。我正在寻找绘制分数百分位数的最佳方法。来看看我的数据R中的钟形百分位曲线,r,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 2
[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
并得到了如下输出:
>zy图(z,y,type=“l”)
由于您没有提供任何真实数据,我只能考虑随机数据:
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
由于您没有提供任何真实数据,我只能考虑随机数据:
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
我想你在找这样的东西:
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%"))
x我想你在找这样的东西:
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%"))
x您可以使用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
x您可以使用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
xx@Barranka我已经试过了。对于我的输出,我只得到上面的输出。你能检查一下吗?你显然没有按照巴兰卡的建议做。他的输出是准确的。不知道你到底做了什么。但我猜我试过Barranka的密码,也试过urs。但是我得到的输出不是钟形的。请查看更新的问题。您需要绘制直方图。试试“hist”。另外,阅读x@Barranka我已经试过了。对于我的输出,我只得到上面的输出。你能检查一下吗?你显然没有按照巴兰卡的建议做。他的输出是准确的。不知道你到底做了什么。但我猜我试过Barranka的密码,也试过urs。但是我得到的输出不是钟形的。请查看更新的问题。您需要绘制直方图。试试“hist”。还有,请阅读,但我正在寻找钟形百分位曲线,对吗?直方图有什么帮助?我不太习惯这样。所以,如果我问了一个荒谬的问题,请容忍me@Observer-钟形曲线基本上是针对正常数据生成的直方图条的拟合。但我正在寻找钟形百分位曲线,对吗?直方图有什么帮助?我不太习惯这样。所以,如果我问了一个荒谬的问题,请容忍me@Observer-钟形曲线基本上是针对正常数据生成的直方图条的拟合。
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
q <- quantile(df$x, c(1-0.997, 1-0.95, 1-0.68,0.5,0.68,0.68,0.95,0.977))