0

I'm trying to apply a lasso regression for my data. I'm using lars package for R. Using coef function, I get coefficients of lasso model and using them, I plot this model. But this model is always wrong by a constant (blue color).

FX <- cbind(1, X, X^2, X^3, X^4, X^5,X^6, X^7)
lasso <- lars(FX, Y, type='lasso')
alpha <- coef(lasso, s=1, mode='lambda')
tr_x <- (1:100)/100
y0 <- getFuncByParam(tr_x, alpha)
lines(x,y0, col='blue', lwd=2)

But when I use predict function, I'm getting correct model (pink color)

Ftest <- cbind(1, tr_x , tr_x^2, tr_x^3, tr_x^4, tr_x^5, tr_x^6, tr_x^7)
y0 <- predict(lasso, Ftest, 1, type='fit', mode='lambda')

enter image description here enter image description here

UPD

getFuncByParam <- function(x, a){
    n <- length(a)
    res <- 0
    for(i in 1 : n ){
        res <- res + a[i]*x^(i - 1)
    }
    return(res)
}
Rostislav Shtanko
  • 704
  • 2
  • 9
  • 30

0 Answers0