MATLAB Examples

## Contents

```function call_lasso() ```
```%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% % Name - call_lasso % Creation Date - 8th Jan 2014 % Author: Soumya Banerjee % Website: https://sites.google.com/site/neelsoumya/ % % Description: % Function to generate test dataset and call generic function to % perform LASSO % % Input: % % Output: % 1) Matrix of inferred regressors % % Assumptions - % % Example usage: % call_lasso % % License - BSD % % Acknowledgements - % Dedicated to my mother Kalyani Banerjee, my father Tarakeswar Banerjee % and my wife Joyeeta Ghose. % % Change History - % 8th Jan 2014 - Creation by Soumya Banerjee %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% ```

## Construct a data set with redundant predictors, and identify those predictors using cross-validated lasso.

```%Create a matrix X of 100 five-dimensional normal variables and a response vector Y from just two components of X, with small added noise. X = randn(100,5) r = [0;2;0;-3;0] % only two nonzero coefficients Y = X*r + randn(100,1)*.1 % small added noise ```
```X = 0.5377 0.8404 0.1832 0.0799 0.2696 1.8339 -0.8880 -1.0298 -0.9485 0.4943 -2.2588 0.1001 0.9492 0.4115 -1.4831 0.8622 -0.5445 0.3071 0.6770 -1.0203 0.3188 0.3035 0.1352 0.8577 -0.4470 -1.3077 -0.6003 0.5152 -0.6912 0.1097 -0.4336 0.4900 0.2614 0.4494 1.1287 0.3426 0.7394 -0.9415 0.1006 -0.2900 3.5784 1.7119 -0.1623 0.8261 1.2616 2.7694 -0.1941 -0.1461 0.5362 0.4754 -1.3499 -2.1384 -0.5320 0.8979 1.1741 3.0349 -0.8396 1.6821 -0.1319 0.1269 0.7254 1.3546 -0.8757 -0.1472 -0.6568 -0.0631 -1.0722 -0.4838 1.0078 -1.4814 0.7147 0.9610 -0.7120 -2.1237 0.1555 -0.2050 0.1240 -1.1742 -0.5046 0.8186 -0.1241 1.4367 -0.1922 -1.2706 -0.2926 1.4897 -1.9609 -0.2741 -0.3826 -0.5408 1.4090 -0.1977 1.5301 0.6487 -0.3086 1.4172 -1.2078 -0.2490 0.8257 -1.0966 0.6715 2.9080 -1.0642 -1.0149 -0.4930 -1.2075 0.8252 1.6035 -0.4711 -0.1807 0.7172 1.3790 1.2347 0.1370 0.0458 1.6302 -1.0582 -0.2296 -0.2919 -0.0638 0.4889 -0.4686 -1.5062 0.3018 0.6113 1.0347 -0.2725 -0.4446 0.3999 0.1093 0.7269 1.0984 -0.1559 -0.9300 1.8140 -0.3034 -0.2779 0.2761 -0.1768 0.3120 0.2939 0.7015 -0.2612 -2.1321 1.8045 -0.7873 -2.0518 0.4434 1.1454 -0.7231 0.8884 -0.3538 0.3919 -0.6291 0.5265 -1.1471 -0.8236 -1.2507 -1.2038 -0.2603 -1.0689 -1.5771 -0.9480 -0.2539 0.6001 -0.8095 0.5080 -0.7411 -1.4286 0.5939 -2.9443 0.2820 -0.5078 -0.0209 -2.1860 1.4384 0.0335 -0.3206 -0.5607 -1.3270 0.3252 -1.3337 0.0125 2.1778 -1.4410 -0.7549 1.1275 -3.0292 1.1385 0.4018 1.3703 0.3502 -0.4570 -2.4969 1.4702 -1.7115 -0.2991 1.2424 0.4413 -0.3268 -0.1022 0.0229 -1.0667 -1.3981 0.8123 -0.2414 -0.2620 0.9337 -0.2551 0.5455 0.3192 -1.7502 0.3503 0.1644 -1.0516 0.3129 -0.2857 -0.0290 0.7477 0.3975 -0.8649 -0.8314 0.1825 -0.2730 -0.7519 -0.0301 -0.9792 -1.5651 1.5763 1.5163 -0.1649 -1.1564 -0.0845 -0.4809 -0.0326 0.6277 -0.5336 1.6039 0.3275 1.6360 1.0933 -2.0026 0.0983 0.6647 -0.4251 1.1093 0.9642 0.0414 0.0852 0.5894 -0.8637 0.5201 -0.7342 0.8810 -0.0628 0.0774 -0.0200 -0.0308 0.3232 -2.0220 -1.2141 -0.0348 0.2323 -0.7841 -0.9821 -1.1135 -0.7982 0.4264 -1.8054 0.6125 -0.0068 1.0187 -0.3728 1.8586 -0.0549 1.5326 -0.1332 -0.2365 -0.6045 -1.1187 -0.7697 -0.7145 2.0237 0.1034 -0.6264 0.3714 1.3514 -2.2584 0.5632 0.2495 -0.2256 -0.2248 2.2294 0.1136 -0.9930 1.1174 -0.5890 0.3376 -0.9047 0.9750 -1.0891 -0.2938 1.0001 -0.4677 -0.6407 0.0326 -0.8479 -1.6642 -0.1249 1.8089 0.5525 -1.1201 -0.5900 1.4790 -1.0799 1.1006 2.5260 -0.2781 -0.8608 0.1992 1.5442 1.6555 0.4227 0.7847 -1.5210 0.0859 0.3075 -1.6702 0.3086 -0.7236 -1.4916 -1.2571 0.4716 -0.2339 -0.5933 -0.7423 -0.8655 -1.2128 -1.0570 0.4013 -1.0616 -0.1765 0.0662 -0.2841 0.9421 2.3505 0.7914 0.6524 -0.0867 0.3005 -0.6156 -1.3320 0.3271 -1.4694 -0.3731 0.7481 -2.3299 1.0826 0.1922 0.8155 -0.1924 -1.4491 1.0061 -0.8223 0.7989 0.8886 0.3335 -0.6509 -0.0942 0.1202 -0.7648 0.3914 0.2571 0.3362 0.5712 -1.4023 0.4517 -0.9444 -0.9047 0.4128 -1.4224 -0.1303 -1.3218 -0.2883 -0.9870 0.4882 0.1837 0.9248 0.3501 0.7596 -0.1774 -0.4762 0.0000 -1.8359 -0.6572 -0.1961 0.8620 -0.0549 1.0360 -0.6039 1.4193 -1.3617 0.9111 2.4245 0.1769 0.2916 0.4550 0.5946 0.9594 -0.3075 0.1978 -0.8487 0.3502 -0.3158 -0.1318 1.5877 -0.3349 1.2503 0.4286 0.5954 -0.8045 0.5528 0.9298 -1.0360 1.0468 0.6966 1.0391 0.2398 1.8779 -0.1980 0.8351 -1.1176 -0.6904 0.9407 0.3277 -0.2437 1.2607 -0.6516 0.7873 -0.2383 0.2157 0.6601 1.1921 -0.8759 0.2296 -1.1658 -0.0679 -1.6118 0.3199 0.4400 -1.1480 -0.1952 -0.0245 -0.5583 -0.6169 0.1049 -0.2176 -1.9488 -0.3114 0.2748 0.7223 -0.3031 1.0205 -0.5700 0.6011 2.5855 0.0230 0.8617 -1.0257 0.0923 -0.6669 0.0513 0.0012 -0.9087 1.7298 0.1873 0.8261 -0.0708 -0.2099 -0.6086 -0.0825 1.5270 -2.4863 -1.6989 -0.7371 -1.9330 0.4669 0.5812 0.6076 -1.7499 -0.4390 -0.2097 -2.1924 -0.1178 0.9105 -1.7947 0.6252 -2.3193 0.6992 0.8671 r = 0 2 0 -3 0 Y = 1.4330 1.1592 -1.0159 -3.0909 -1.9549 0.9168 -0.3580 1.4556 0.8289 -2.1821 -7.0844 -1.3927 3.1074 -5.1845 8.2710 1.8160 6.7241 -2.6989 -2.1636 -4.7706 8.7325 2.8308 2.4371 -1.4243 -1.8360 -1.7412 5.2095 -0.0322 7.7486 -7.5161 1.2042 1.9714 -2.4531 5.1796 0.6582 1.6147 -9.3039 -1.0273 8.1491 -1.9361 4.3302 0.2112 -3.8907 -2.8490 -0.7423 -6.6244 -0.8913 -2.1362 -6.1038 1.6459 -1.6466 -1.0506 2.3813 3.7900 -3.4240 1.4940 -1.6419 0.9610 -0.7727 1.6332 0.7742 -1.3650 -6.4768 7.7295 0.9138 -0.2459 -1.8487 1.5106 0.6409 1.6825 1.8471 -5.0905 -0.4266 1.1244 -0.2104 3.4936 0.3849 -0.7162 4.6266 -1.3521 -9.9554 -2.0259 -0.7357 -2.1195 4.1375 -3.6373 -5.0054 0.1579 3.8324 -1.0965 1.2155 0.4324 1.1902 3.1346 2.8687 2.3702 8.1686 -0.8339 0.0023 -0.7300 ```

## Call LASSO generic function

```iFold_cross_validation = 10; % do 10 fold cross validation B = lasso_generic(X,Y,iFold_cross_validation) ```
```B = Columns 1 through 7 0 0 0 0 0 0 0 1.9524 1.9484 1.9440 1.9391 1.9338 1.9280 1.9216 0 0 0 0 0 0 0 -2.9525 -2.9482 -2.9435 -2.9384 -2.9327 -2.9265 -2.9196 0 0 0 0 0 0 0 Columns 8 through 14 0 0 0 0 0 0 0 1.9146 1.9069 1.8984 1.8891 1.8790 1.8678 1.8555 0 0 0 0 0 0 0 -2.9122 -2.9039 -2.8949 -2.8850 -2.8741 -2.8622 -2.8491 0 0 0 0 0 0 0 Columns 15 through 21 0 0 0 0 0 0 0 1.8420 1.8273 1.8111 1.7933 1.7737 1.7523 1.7287 0 0 0 0 0 0 0 -2.8348 -2.8190 -2.8017 -2.7827 -2.7619 -2.7390 -2.7139 0 0 0 0 0 0 0 Columns 22 through 28 0 0 0 0 0 0 0 1.7029 1.6746 1.6435 1.6093 1.5719 1.5308 1.4856 0 0 0 0 0 0 0 -2.6863 -2.6561 -2.6229 -2.5865 -2.5465 -2.5026 -2.4545 0 0 0 0 0 0 0 Columns 29 through 35 0 0 0 0 0 0 0 1.4361 1.3818 1.3221 1.2566 1.1848 1.1059 1.0194 0 0 0 0 0 0 0 -2.4016 -2.3436 -2.2800 -2.2101 -2.1335 -2.0493 -1.9570 0 0 0 0 0 0 0 Columns 36 through 42 0 0 0 0 0 0 0 0.9244 0.8201 0.7057 0.5801 0.4423 0.2911 0.1251 0 0 0 0 0 0 0 -1.8556 -1.7444 -1.6223 -1.4884 -1.3413 -1.1799 -1.0028 0 0 0 0 0 0 0 Columns 43 through 46 0 0 0 0 0 0 0 0 0 0 0 0 -0.8002 -0.5578 -0.2919 0 0 0 0 0 FitInfo = Intercept: [1x46 double] Lambda: [1x46 double] Alpha: 1 DF: [1x46 double] MSE: [1x46 double] PredictorNames: {} SE: [1x46 double] LambdaMinMSE: 0.0468 Lambda1SE: 0.0564 IndexMinMSE: 1 Index1SE: 3 B = Columns 1 through 7 0 0 0 0 0 0 0 1.9524 1.9484 1.9440 1.9391 1.9338 1.9280 1.9216 0 0 0 0 0 0 0 -2.9525 -2.9482 -2.9435 -2.9384 -2.9327 -2.9265 -2.9196 0 0 0 0 0 0 0 Columns 8 through 14 0 0 0 0 0 0 0 1.9146 1.9069 1.8984 1.8891 1.8790 1.8678 1.8555 0 0 0 0 0 0 0 -2.9122 -2.9039 -2.8949 -2.8850 -2.8741 -2.8622 -2.8491 0 0 0 0 0 0 0 Columns 15 through 21 0 0 0 0 0 0 0 1.8420 1.8273 1.8111 1.7933 1.7737 1.7523 1.7287 0 0 0 0 0 0 0 -2.8348 -2.8190 -2.8017 -2.7827 -2.7619 -2.7390 -2.7139 0 0 0 0 0 0 0 Columns 22 through 28 0 0 0 0 0 0 0 1.7029 1.6746 1.6435 1.6093 1.5719 1.5308 1.4856 0 0 0 0 0 0 0 -2.6863 -2.6561 -2.6229 -2.5865 -2.5465 -2.5026 -2.4545 0 0 0 0 0 0 0 Columns 29 through 35 0 0 0 0 0 0 0 1.4361 1.3818 1.3221 1.2566 1.1848 1.1059 1.0194 0 0 0 0 0 0 0 -2.4016 -2.3436 -2.2800 -2.2101 -2.1335 -2.0493 -1.9570 0 0 0 0 0 0 0 Columns 36 through 42 0 0 0 0 0 0 0 0.9244 0.8201 0.7057 0.5801 0.4423 0.2911 0.1251 0 0 0 0 0 0 0 -1.8556 -1.7444 -1.6223 -1.4884 -1.3413 -1.1799 -1.0028 0 0 0 0 0 0 0 Columns 43 through 46 0 0 0 0 0 0 0 0 0 0 0 0 -0.8002 -0.5578 -0.2919 0 0 0 0 0 ```