-
Notifications
You must be signed in to change notification settings - Fork 22
/
Copy pathdemoOLSqr.r
30 lines (29 loc) · 1.07 KB
/
demoOLSqr.r
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
# title: Non-Interactive Demo of Polynomial and Spline Fitting with OLS and quantile regression and how to use a list of models
# major: regression modeling
# minor: curve fitting
# Quick way to compare multiple types of OLS and quantile regression fits
set.seed(1)
n <- 20
x <- runif(n)
y <- abs(x - 0.5) + rnorm(n, mean=0, sd=.1)
require(rms)
mods <- list(y ~ x, y ~ pol(x, 2), y ~ pol(x, 3), y ~ pol(x, 4),
y ~ pol(x, 5), y ~ rcs(x, 3), y ~ rcs(x, 4), y ~ rcs(x, 5))
z <- NULL
xeval <- seq(0, 1, length=100)
xd <- data.frame(x=xeval)
for(method in c('OLS', 'Quantile Regression')) {
for(mod in mods) {
f <- switch(method,
OLS=ols(mod),
'Quantile Regression'=Rq(mod))
p <- predict(f, newdata=xd)
z <- rbind(z, data.frame(method=method, model=deparse(mod), x=xeval, y=p))
}
}
d <- data.frame(x, y)
dtrue <- data.frame(x=c(0, .5, 1), y=c(.5, 0, .5))
ggplot(z, aes(x=x, y=y, color=method)) + geom_line() +
geom_line(data=dtrue, color='blue', alpha=.5, linetype='dotted') +
geom_point(data=d, alpha=.3) +
facet_wrap(~ model)