Skip to content

Instantly share code, notes, and snippets.

@ayota
Created February 11, 2015 18:06
Show Gist options
  • Save ayota/44f26693cd1fd7755646 to your computer and use it in GitHub Desktop.
Save ayota/44f26693cd1fd7755646 to your computer and use it in GitHub Desktop.
lasso fit!
x.dat <- model.matrix(Y ~ ., data=data)[,-1]
set.seed(1)
train <- sample(1:nrow(data), nrow(data)/2)
y.train <- data$Y[train]
x.train <- x.dat[train,]
test <- (-train)
y.test <- data$Y[test]
x.test <- x.dat[test,]
grid <- 10^seq(10,-2,length=100)
fit.lasso <- glmnet(x.train,y.train, alpha=1, lambda=grid, thresh=1e-12)
#choosing lambda
set.seed(1)
cv.out <- cv.glmnet(x.train, y.train, alpha=1)
plot(cv.out)
bestlam <- cv.out$lambda.min
#mse using best lambda
pred.lasso <- predict(fit.lasso, s=bestlam, newx=x.test)
#test mse
mse.lasso <- mean((pred.lasso-y.test)^2)
#fit on all data
fit.all <- glmnet(x.dat, data$Y, alpha=1)
predict(fit.all, type="coefficients", s=bestlam)[1:11,]
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment