Skip to content

Instantly share code, notes, and snippets.

@adini121
Created February 19, 2016 19:00
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save adini121/636828fe5dd4282e25df to your computer and use it in GitHub Desktop.
Save adini121/636828fe5dd4282e25df to your computer and use it in GitHub Desktop.
library(lars) #load lars library
# load data from csv file
fullData <- read.csv('/Users/adityanisal/Dropbox/ExtractedResultFiles/CSV/fireplace-mv1.csv')
# display data
fullData
# remove "null" columns (columns with all 0s)
M <- fullData[,colSums(fullData^2) !=0]
M
# Take all columns (features) except the last solutionVector
x <- M[,c(1:8)]
x
# solution vector
y <- M[,9]
#normalize with mean =0 and sd=1
fin_x = apply(x, 2, function(aa){
return( ((aa - mean(aa))/sd(aa)))
})
# create lasso model
fit <- lars(fin_x,y,type = "lasso")
# show steps
fit
# show standardized coefficients
coef(fit)
# plot lasso path
dev.new()
plot(fit,breaks = FALSE)
legend('topleft',inset=c(0.05,0),names(M[,c(-9)]),lty = 1:length(names(M[,c(-9)])),col=1:8,lwd=1,box.lty=0,cex=.7)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment