Error in R Code for the Lasso Ridge regression

lasso
r
ridge_regression

#1

Hello,

I am trying to write the corresponding R code for the article http://www.analyticsvidhya.com/blog/2016/01/complete-tutorial-ridge-lasso-regression-python/

as I am not very much conversant with Python.

RCode:

rm(list = ls())
#Part 1:
x = array()
i = seq(60,300,4)
x = i*(pi/180)
set.seed(10)
y = sin(x) + rnorm(61,mean = 0,sd = 0.15)
plot(x,y,col = "blue")
data = data.frame('x' = x,'y' = y)

#Part 2:
for (i in seq(2,16)){
  #power of 1 is already there
  colname = paste('x_',i)
  #new var will be x_power
  data[colname] = data['x']**i}

#Part 3:
linear_regression <- function(data,power,models_to_plot){
#initialize predictors:
predictors= 'x'
if (power>=2){
  for (i in seq(2,power)){
    predictors[i] = paste('x_',i)
  }
}

#Create a dataframe of predictors and y:
lm.data = data.frame(data[predictors],data['y'])

#Fit the model:
lm.fit = lm(y ~ .,data = lm.data)
reg_coeff = lm.fit$coefficients
#Return the result in pre-defined format
rss = sum((lm.fit$residuals)^2)
ret = c()
ret[1] = rss
ret[2] = reg_coeff[1]
for (i in 3:(length(lm.fit$coefficients)+1)){
  ret[i] = reg_coeff[i-1]
}
return (ret)
}

#Part 4:
#Initialize a dataframe to store the results:
col = c()
for (i in seq(1,16)){
   col[i] = paste('coeff_x_',i)
}
col = c("rss","intercept",col)

ind = c()
for (i in seq(1,16)){
  ind[i] = paste('model_pow_',i)
}


coef_matrix_simple = matrix(nrow = 15,ncol = 17)
rownames(coef_matrix_simple) = ind[1:15]
colnames(coef_matrix_simple) = col[1:17]
coef_matrix_simple = as.data.frame(coef_matrix_simple)

#Define the powers for which a plot is required:
models_to_plot = c(1:231,3:232,6:233,9:234,12:235,15:236)

#Iterate through all powers and assimilate results:
for (i in seq(1,16)){
  coef_matrix_simple[i,1:(i+2)] = linear_regression(data, power=i, models_to_plot=models_to_plot)
}

However the last part where I want to update the coef_matrix_simple throws an error:

Can someone please help me with this??


#2

You are trying to access an extra column that doesn’t exist. This should fix it:

> for (i in seq(1,15))