# generate y
x <- seq(-1, 2, by = 0.1)
n <- length(x)
y <- x^2 + rnorm(n, .3)
# regress y on x under the shape-restriction: "convex"
ans <- shapereg(y ~ conv(x))
# make a plot
plot(x, y)
lines(x, fitted(ans), col = 2)
legend("topleft", bty = "n", "shapereg: convex fit", col = 2, lty = 1)
Run the code above in your browser using DataLab