**R snippets**, and kindly contributed to R-bloggers)

Recently I needed a simple example showing when application of regularization in regression is worthwhile. Here is the code I came up with (along with basic application of parallelization of code execution).

Assume you have 60 observations and 50 explanatory variables *x1* to *x50*. All these variables are IID from uniform distribution on interval [0, 1). Predicted variable *y* is generated as a sum of variables *x1* to *x50* and independent random noise *N*(0, 1).

Our objective is to compare for such data: (a) linear regression on all 50 variables, regressions obtained by variable selection using (b) AIC and (c) BIC criteria and (d) Lasso regularization.

What we do is generate 100 times the training data set and compare the four predictions against known expected value of *y* for 10 000 randomly selected values of explanatory variables. We use mean squared deviation of the prediction from the mean (thus for ideal model it is equal to 0).

Here is the code that runs the simulation. Because each step of the procedure is lengthily I parallelize the computations.

**(**parallel

**)**

**<-**

**function**

**(**job

**)**

**{**

**(**lasso2

**)**

**<-**

**function**

**(**v, n

**)**

**{**

**<-**data.frame

**(**replicate

**(**v, runif

**(**n

**)))**

**$**y

**<-**rowSums

**(**data.set

**)**

**(**data.set

**)**

**<-**c

**(**paste

**(**“x”, 1

**:**v, sep

**=**“”

**)**, “y”

**)**

**(**data.set

**)**

**}**

**<-**50

**<-**60

**<-**gen.data

**(**v, n

**)**

**$**y

**<-**data.set

**$**y

**+**rnorm

**(**n

**)**

**<-**gen.data

**(**v, 10000

**)**

**<-**lm

**(**y

**~**., data.set

**)**

**<-**step

**(**model.lm, trace

**=**0

**)**

**=**step

**(**model.lm, trace

**=**0, k

**=**log

**(**n

**))**

**<-**l1ce

**(**y

**~**., data.set,

**=**

**NULL**, standardize

**=**

**FALSE**

**)**

**=**list

**(**model.lm, model.aic, model.bic, model.lasso

**)**

**<-**numeric

**(**length

**(**models

**))**

**for**

**(**j

**in**seq_along

**(**models

**))**

**{**

**<-**predict

**(**models

**[[**j

**]]**, newdata

**=**new.set

**)**

**[**j

**]**

**<-**mean

**((**pred

**–**new.set

**$**y

**)**

**^**2

**)**

**}**

**(**results

**)**

**}**

**<-**makeCluster

**(**4

**)**

**(**msd

**<-**t

**(**parSapply

**(**cl, 1

**:**100, run

**)))**# 58.07 seconds

**(**cl

**)**

**(**msd

**)**

**=**c

**(**“lm”, “aic”, “bic”, “lasso”

**)**

**(**mar

**=**c

**(**2, 2, 1, 1

**))**

**(**msd

**)**

**for**

**(**i

**in**1

**:**ncol

**(**msd

**))**

**{**

**(**c

**(**i

**–**0.4, i

**+**0.4

**)**, rep

**(**mean

**(**msd

**[**, i

**])**, 2

**)**,

**=**“red”, lwd

**=**2

**)**

**}**

The code produces boxplots for distribution of mean squared deviation from theoretical mean and additionally puts red line at the mean level of mean squared deviation. Here is the result:

**leave a comment**for the author, please follow the link and comment on their blog:

**R snippets**.

R-bloggers.com offers

**daily e-mail updates**about R news and tutorials on topics such as: visualization (ggplot2, Boxplots, maps, animation), programming (RStudio, Sweave, LaTeX, SQL, Eclipse, git, hadoop, Web Scraping) statistics (regression, PCA, time series, trading) and more...