set.seed(12345)
n <- 200
tau <- 1
x <- seq(-2, 2, length.out=n)
f <- x^4 + x
y <- f + (1/tau) * rnorm(n)
## Clean Data
plot(x, y, pch=16, cex.lab=1.5, cex.axis=1.5, cex.sub=1.5, col='gray')
lines(x, f, lwd=3)
tau <- 1
b <- y
## Least Squares method
cvx <- fitted(cobs::conreg(y, convex=TRUE))
## MM method
sol_mm <- L2E_convex(y, b, tau)
## PG method
sol_pg <- L2E_convex(y, b, tau, method='PG')
plot(x, y, pch=16, cex.lab=1.5, cex.axis=1.5, cex.sub=1.5, col='gray')
lines(x, f, lwd=3)
lines(x, cvx, col='blue', lwd=3) ## LS
lines(x, sol_mm$beta, col='red', lwd=3) ## MM
lines(x, sol_pg$beta, col='dark green', lwd=3) ## PG
## Contaminated Data
ix <- 0:9
y[45 + ix] <- 14 + rnorm(10)
plot(x, y, pch=16, cex.lab=1.5, cex.axis=1.5, cex.sub=1.5, col='gray')
lines(x, f, lwd=3)
tau <- 1
b <- y
cvx <- fitted(cobs::conreg(y, convex=TRUE))
sol_mm <- L2E_convex(y, b, tau)
sol_pg <- L2E_convex(y, b, tau, method='PG')
plot(x, y, pch=16, cex.lab=1.5, cex.axis=1.5, cex.sub=1.5, col='gray')
lines(x, f, lwd=3)
lines(x, cvx, col='blue', lwd=3) ## LS
lines(x, sol_mm$beta, col='red', lwd=3) ## MM
lines(x, sol_pg$beta, col='dark green', lwd=3) ## PG
Run the code above in your browser using DataLab