0

In Lasso regression the L1 constraint is used:

I'm trying to plot the constraint using R. An example looks like: enter image description here

Here is the simple R code I wrote:

beta= seq(-1, 1, length=100)
lambda=2
penalty=lambda*abs(beta)
plot(penalty, type="l") 

It draws only the lower part of the plot. How could I plot the upper part too in one plot? Here is the plot:

enter image description here

Hack-R
  • 22,422
  • 14
  • 75
  • 131
Ville
  • 547
  • 1
  • 3
  • 21

1 Answers1

2

To plot abs(b1) + abs(b2) = 1 you can plot b2 = 1 - abs(b1) and the negative of that (since the abs means either is a solution)

b1 <- seq(-1, 1, .01)
b2 <- 1 - abs(b1)
plot(b1, b2, ylim = c(-1, 1), type = 'l')
b2 <- -b2
lines(b1, b2)
IceCreamToucan
  • 28,083
  • 2
  • 22
  • 38