R 散点图的非参数分位数回归曲线
我用R 散点图的非参数分位数回归曲线,r,regression,scatter-plot,quantile,R,Regression,Scatter Plot,Quantile,我用IV=time,DV=concentration创建了一个散点图(多组GRP)。我想将分位数回归曲线(0.025,0.05,0.5,0.95,0.975)添加到绘图中 顺便说一下,这就是我创建散点图所做的: attach(E) ## E is the name I gave to my data ## Change Group to factor so that may work with levels in the legend Group<-as.character(Group)
IV=time
,DV=concentration
创建了一个散点图(多组GRP)。我想将分位数回归曲线(0.025,0.05,0.5,0.95,0.975)
添加到绘图中
顺便说一下,这就是我创建散点图所做的:
attach(E) ## E is the name I gave to my data
## Change Group to factor so that may work with levels in the legend
Group<-as.character(Group)
Group<-as.factor(Group)
## Make the colored scatter-plot
mycolors = c('red','orange','green','cornflowerblue')
plot(Time,Concentration,main="Template",xlab="Time",ylab="Concentration",pch=18,col=mycolors[Group])
## This also works identically
## with(E,plot(Time,Concentration,col=mycolors[Group],main="Template",xlab="Time",ylab="Concentration",pch=18))
## Use identify to identify each point by group number (to check)
## identify(Time,Concentration,col=mycolors[Group],labels=Group)
## Press Esc or press Stop to stop identify function
## Create legend
## Use locator(n=1,type="o") to find the point to align top left of legend box
legend('topright',legend=levels(Group),col=mycolors,pch=18,title='Group')
我也试过qcbvnonpar::evd,但曲线看起来不是很平滑。也许可以看看quantreg::RQS,用于平滑样条曲线和分位数回归。 抱歉,示例数据不太好:
set.seed(1234)
period <- 100
x <- 1:100
y <- sin(2*pi*x/period) + runif(length(x),-1,1)
require(quantreg)
mod <- rqss(y ~ qss(x))
mod2 <- rqss(y ~ qss(x), tau=0.75)
mod3 <- rqss(y ~ qss(x), tau=0.25)
plot(x, y)
lines(x[-1], mod$coef[1] + mod$coef[-1], col = 'red')
lines(x[-1], mod2$coef[1] + mod2$coef[-1], col = 'green')
lines(x[-1], mod3$coef[1] + mod3$coef[-1], col = 'green')
set.seed(1234)
期间我过去经常与rqs
斗争,我的问题几乎总是与积分的顺序有关
在不同的时间点有多个测量值,这就是为什么得到不同的长度。这对我很有用:
dat <- read.csv("~/Downloads/Test.csv")
library(quantreg)
dat <- plyr::arrange(dat,Time)
fit<-rqss(Concentration~qss(Time,constraint="N"),tau=0.5,data = dat)
with(dat,plot(Time,Concentration))
lines(unique(dat$Time)[-1],fit$coef[1] + fit$coef[-1])
dat以防需要ggplot2
graphic
我以@EDi的例子为基础。我增加了x
和y
,这样分位数线就不会那么摇摆了。由于这种增加,我需要在一些调用中使用unique(x)
代替x
以下是修改后的设置:
set.seed(1234)
period <- 100
x <- rep(1:100,each=100)
y <- 1*sin(2*pi*x/period) + runif(length(x),-1,1)
require(quantreg)
mod <- rqss(y ~ qss(x))
mod2 <- rqss(y ~ qss(x), tau=0.75)
mod3 <- rqss(y ~ qss(x), tau=0.25)
#@swihart的ggplot2示例:
##进入数据集,以便ggplot2可以获得一些乐趣:
qrdf如果您无法提供自己的数据,请尝试创建一个随机数数据集,并演示您的问题。让我们看看你试过什么。它给了我们一些可以合作的东西,同时也是一种真诚的象征。对不起,我会编一些数字。它可能相当大。这可能有助于生成数据。几点意见:您显示的数字似乎来自?那篇论文似乎有一个相关的R包(还没有看它…),要得到完全非参数的光滑分位数是相当困难的(我认为)。两种可能的解决方案是:(1)拟合广义加性模型(例如,库(样条);rq(y~s(x,5),tau=0.9)
);(2) 使用分位数的运行估计值。也许quantreg的rqss()也适合您?rqss()
-如果需要非参数,并且示例图像表明它是基于样条线的拟合,那么rqss()
肯定是我首先要看的地方。它与您的示例配合得非常好,但我不确定为什么,我在xy中不断得到错误。坐标(x,y):“x”和“y”长度不同
我的数据集警告,即使我检查我的x和y具有相同的n。仍在进行错误调试:您能多使用一些数据吗?上面的示例数据显然不合适。这很有效!非常感谢你。我不知道订购会成为一个问题。
set.seed(1234)
period <- 100
x <- rep(1:100,each=100)
y <- 1*sin(2*pi*x/period) + runif(length(x),-1,1)
require(quantreg)
mod <- rqss(y ~ qss(x))
mod2 <- rqss(y ~ qss(x), tau=0.75)
mod3 <- rqss(y ~ qss(x), tau=0.25)
# @EDi's base graphics example
plot(x, y)
lines(unique(x)[-1], mod$coef[1] + mod$coef[-1], col = 'red')
lines(unique(x)[-1], mod2$coef[1] + mod2$coef[-1], col = 'green')
lines(unique(x)[-1], mod3$coef[1] + mod3$coef[-1], col = 'green')
# @swihart's ggplot2 example:
## get into dataset so that ggplot2 can have some fun:
qrdf <- data.table(x = unique(x)[-1],
median = mod$coef[1] + mod$coef[-1],
qupp = mod2$coef[1] + mod2$coef[-1],
qlow = mod3$coef[1] + mod3$coef[-1]
)
line_size = 2
ggplot() +
geom_point(aes(x=x, y=y),
color="black", alpha=0.5) +
## quantiles:
geom_line(data=qrdf,aes(x=x, y=median),
color="red", alpha=0.7, size=line_size) +
geom_line(data=qrdf,aes(x=x, y=qupp),
color="blue", alpha=0.7, size=line_size, lty=1) +
geom_line(data=qrdf,aes(x=x, y=qlow),
color="blue", alpha=0.7, size=line_size, lty=1)