非线性分位数回归的以下工作流程似乎有效。但是我不知道如何绘制生成的曲线。
顺便说一句:我更喜欢使用函数 graphics::curve(( 而不是 graphics::lines((
require(quantreg)
# load sample data
dat <- DNase
# introduce variable
x <- DNase$conc
y <- DNase$density
# introduce function
f <- function(a, b, x) {(a*x/(b+x))}
# fit the model
fm0 <- nls(log(y) ~ log(f(a,b,x)), dat, start = c(a = 1, b = 1))
# fit a nonlinear least-square regression
fit <- nls(y ~ f(a,b,x), dat, start = coef(fm0))
# receive coeffientes
co <- coef(fit)
a=co[1]
b=co[2]
# plot
plot(y~x)
# add curve
curve((a*x/(b+x)), add=T)
# then fit the median using nlrq
dat.nlrq <- nlrq(y ~ SSlogis(x, Asym, mid, scal), data=dat, tau=0.5)
# add curve
???
编辑:我正在寻找一种绘制公式的各种分位数回归的方法,例如a * x/(b + x(。 插入公式让我想到一个问题,该将什么作为"开始"参数
dat.nlrq.075 <- nlrq(formula=fit, data = dat, start=???, tau = 0.75)
curve
使用lines
所以当lines
更容易使用时,真的没有理由使用curve
。
首先确保对数据进行排序,以便绘图正确。 然后与nls
或nlrq
配合,并将fitted
用于适合的线。
library(quantreg)
dat <- DNase[order(DNase$conc), ]
fit.nlrq <- nlrq(density ~ SSlogis(conc, Asym, mid, scal), data = dat, tau = 0.5)
plot(density ~ conc, dat)
lines(fitted(fit.nlrq) ~ conc, dat)
如果要在不同数量的等间距点(例如 250(处绘制拟合,则执行相同的操作,但使用predict
而不是fitted
:
x <- seq(min(dat$conc), max(dat$conc), length = 250)
lines(predict(fit.nlrq, list(conc = x)) ~ x, lty = 2, col = "red")
相同的样式适用于nls
。
请注意,如果使用require
则应检查其值。 如果您不想这样做,请改用library
。