我不确定这个问题是否与 CrossValidated 或 StackOverflow 更相关。如有必要,我很乐意迁移它。
我试图从Roger Koenker的分位数回归小插图中重现这些图,但使用Frank Harrell的序数回归模型。
这是 Koenker 在第 8 页上的图(我最感兴趣的是分位数与预测器的图):
代码
我可以制作类似的图,但不完全相同。例如:
## Load libraries
library(dplyr)
library(rms)
library(ggplot2)
## Simulate data
set.seed(123)
n <- 100
df <- data.frame(x1 = rnorm(n),
x2 = sample(c(-1,0,1), n, TRUE)) %>%
mutate(y = x1 + rnorm(n))
d <- datadist(df)
options(datadist="d")
## Fit ORM model
f1 <- orm(y ~ x1 + x2, data = df)
## Estimate quantiles
qu <- Quantile(f1)
q25 <- function(x) qu(0.25, x)
q50 <- function(x) qu(0.50, x)
q75 <- function(x) qu(0.75, x)
## Point predictions
qplot <- Predict(f1, fun =q25) %>% mutate(q = 25) %>%
bind_rows(Predict(f1, fun = q50) %>% mutate(q = 50)) %>%
bind_rows(Predict(f1, fun = p75) %>% mutate(q = 75))
qplot %>%
mutate(q = as.factor(q)) %>%
ggplot(aes(x = x1, y = yhat, group = q, color = q)) +
geom_line()
我相信 SAS 在proc quantreg
.