Skip to content

Instantly share code, notes, and snippets.

@Harshit1694 Harshit1694/ridge.R
Created Jun 29, 2019

Embed
What would you like to do?
install.packages("glmnet")
library(glmnet)
train$Item_Weight[is.na(train$Item_Weight)] <- mean(train$Item_Weight, na.rm = TRUE)
train$Outlet_Size[is.na(train$Outlet_Size)] <- "Small"
train$Item_Visibility[train$Item_Visibility == 0] <- mean(train$Item_Visibility)
train$Outlet_Establishment_Year=2013 - train$Outlet_Establishment_Year
train<-train[c(-1)]
Y<-train[c(11)]
X <- model.matrix(Item_Outlet_Sales~., train)
lambda <- 10^seq(10, -2, length = 100)
set.seed(567)
part <- sample(2, nrow(X), replace = TRUE, prob = c(0.7, 0.3))
X_train<- X[part == 1,]
X_cv<- X[part == 2,]
Y_train<- Y[part == 1,]
Y_cv<- Y[part == 2,]
#ridge regression
ridge_reg <- glmnet(X[X_train,], Y[X_train], alpha = 0, lambda = lambda)
summary(ridge_reg)
#find the best lambda via cross validation
ridge_reg1 <- cv.glmnet(X[X_train,], Y[X_train], alpha = 0)
bestlam <- ridge_reg1$lambda.min
ridge.pred <- predict(ridge_reg, s = bestlam, newx = X[X_cv,])
m<-mean((Y_cv - ridge.pred)^2)
m
out = glmnet(X[X_train,],Y[X_train],alpha = 0)
ridge.coef<-predict(ridge_reg, type = "coefficients", s = bestlam)[1:40,]
ridge.coef
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.