# Manual for the package: ProxReg

    library(ProxReg)

## What you’ll find

- Introduction
- Functions
  - ols
  - k_fold_cross
  - ols_KCV
- L1 and L2 regularization
  - ridge
  - lasso_ista
  - lasso_ista_back
  - l_CV

## Introduction

This is the introduction to the package “linearreg”, which is used for linear regression model’s construction such as OLS (Ordinary Least Squares regression), Ridge regression and Lasso regression implemented through ISTA algorithm. Moreover, the package also contains functions that realize the K-fold cross validation, a helpful procedure in choosing lambda values.

## Functions

### ols

The Ordinary Least Square (OLS) regression is one of the most common and simple techniques to estimate parametersof a linear regression model. It basically find the line that fits better to observations by minimizing the sum of square errors between the predicted and observed values.

However, it presents some disadvantages, since it is sensitive to Outliers, it may not be appropriate its applications to datasets with bounds of extreme values. Furthermore, overfitting and multicollinearity are as well two factors that undermine the model’s credibility. For this reason, OLS regression is no recommended for high-dimensional data.

The function generate by default a table that provides information about a ordinary least square regression. Here is a example below :

Given a dataset named df with its dependent variable: hours, and independent variables: scores and entertain_hours, we get the following table :

    df = data.frame("hours"=c(1, 2, 4, 5, 5, 6, 6, 7, 8, 10, 11, 11, 12, 12, 14),
    "score"=c(64, 66, 76, 73, 74, 81, 83, 82, 80, 88, 84, 82, 91, 93, 89),
    "entertain_hours"=c(6,5,3,2,2,2,1,1,0.5,1,0.3,0.3,0.2,0.2,0.1))
    ols(df,"score",c("hours","entertain_hours"),alpha = 0.025,verbose = FALSE)
    #>                   score
    #> intercept       74.1312
    #> hours            1.2277
    #> entertain_hours -1.8668

The superior part is a general information about the regression model, where :

- model : current linear regression model

- Df : degree of freedom

- F-statistic : Is a statistical measure and indicates whether at least one independent variable explains the variation in the dependent variable. When it is true, the regression model has significance. The more large is F-statistic, the less is the probability of Type-I error.

- R-squared : coefficient of determination,is the ratio of Sum of squares due to regression (SSreg)to total sum of squares(SYY).
  \(R^2 = \frac {SSreg}{SYY}\)
  This number is a summary of the strength of the relation between the x and the y in the data. For instance, \(R^2=0.84\) explains that about 84% of the variability in the observed values is explained by the model.

- R.S-ajusted:It is a modified version of \(R^2\), it takes into account the number of independent variables in the model, since the value of \(R^2\) generally increases when independent variables are added event if they contribute little to the explanation of dependent variable. This can lead to a misunderstanding and people think the model fits better than it really does. The objective of \(R^2\) adjusted is to penalize the addition of the less relevant independent variables in the model.

- prob(F-statsictic) : p-value of F test, when it is lower than a established value (generally 0.05),we shall reject the null hypothesis (all the regression coefficients are equal to 0), in other words, we may consider there’s a relations between X and Y .

- Residual standard error: Is a measure that indicates the dispersion between the observed values and predicted values.

Under the lines, we have information about the coefficients, where:

- coefficients: Is the estimated value of the coefficients

- standard error: Standard error of each coefficient

- t value:Is a statistical measure calculated from t-test with null hypothesis: \(\beta_i = 0\), which means that the i coefficient does not contribute to the explanation of Y values.

- p(>|t|): P-value obtained from t-test of each coefficient, when it is lower than a pre-fixed value (0.025 in general case) we should reject the null hypothesis and accept the alternative one.

- confidence interval: It presents an confidence interval of each coefficient given a value of alpha, which is by default 0,025.

## k_fold_cross

In some cases, we might front situations when we have to compare linear models and choose the best-fitting one. The most popular method for estimating prediction error is cross validation.

The K-fold cross validations is a method consisted in divided the dataset into k independent training sets and one test set.

The following function \(\color{blue}{ k_-fold_-cross}\) create k different versions of the dataset , each has k-1 training subsets and one test set.

    df = data.frame("hours"=c(1, 2, 4, 5, 5, 6, 6, 7, 8, 10, 11, 11, 12, 12, 14),
    "score"=c(64, 66, 76, 73, 74, 81, 83, 82, 80, 88, 84, 82, 91, 93, 89),
    "entertain_hours"=c(6,5,3,2,2,2,1,1,0.5,1,0.3,0.3,0.2,0.2,0.1))

    k_fold_cross(df,k=3)
    #> # A tibble: 3 × 2
    #>   train            test
    #>   <list>           <list>
    #> 1 <named list [2]> <named list [2]>
    #> 2 <named list [2]> <named list [2]>
    #> 3 <named list [2]> <named list [2]>

## ols_KCV

This function realize k-fold cross validation for ordinary least square regression. In each k circle, a linear regression is obtained from a learning set , then , it is evaluated with the test set to calculate the prediction error, finally, we estimate the mean squared error (MSE) through the k prediction errors.

For instance, suppose k equals to 5, and the dataset is the same df that we have been using throughout the document. Applying the function we get the mean square error (MSE) and its squared value (RMSE).

    ols_KCV(df,k=5,"hours",c("score","entertain_hours"))
    #>
    #>  Model:           OLS              R-squared:   0.84
    #>  Df:              9               R.S-ajusted:   0.81
    #>  F-statistic:    24.33             prob(F-statistic): 0.0002345068
    #>  Resudual standard error: 1.42
    #>  =====================================================================
    #>                 coefficients standard error t value  p(>|t|)  [ 0.025 0.975 ]
    #> intercept            -8.9452         9.6519 -0.9268 0.378214 -30.7793 12.8889
    #> score                 0.2357         0.1094  2.1545 0.059599  -0.0118  0.4832
    #> entertain_hours      -1.9895         0.6869 -2.8963 0.017702  -3.5434 -0.4356
    #>
    #>  Model:           OLS              R-squared:   0.86
    #>  Df:              9               R.S-ajusted:   0.82
    #>  F-statistic:    26.78             prob(F-statistic): 0.0001624636
    #>  Resudual standard error: 1.43
    #>  =====================================================================
    #>                 coefficients standard error t value  p(>|t|)  [ 0.025 0.975 ]
    #> intercept           -10.4506         9.0925 -1.1494 0.280018 -31.0193 10.1181
    #> score                 0.2412         0.1039  2.3215 0.045374   0.0062  0.4762
    #> entertain_hours      -0.9716         0.5639 -1.7230 0.118985  -2.2472  0.3040
    #>
    #>  Model:           OLS              R-squared:   0.83
    #>  Df:              9               R.S-ajusted:   0.79
    #>  F-statistic:    21.42             prob(F-statistic): 0.0003785287
    #>  Resudual standard error: 1.67
    #>  =====================================================================
    #>                 coefficients standard error t value  p(>|t|)  [ 0.025 0.975 ]
    #> intercept           -14.1091        10.7617 -1.3110 0.222320 -38.4538 10.2356
    #> score                 0.2876         0.1226  2.3458 0.043604   0.0103  0.5649
    #> entertain_hours      -0.8087         0.7042 -1.1484 0.280410  -2.4017  0.7843
    #>
    #>  Model:           OLS              R-squared:   0.82
    #>  Df:              9               R.S-ajusted:   0.78
    #>  F-statistic:    20             prob(F-statistic): 0.0004877631
    #>  Resudual standard error: 1.74
    #>  =====================================================================
    #>                 coefficients standard error t value  p(>|t|)  [ 0.025 0.975 ]
    #> intercept           -12.6164        11.7462 -1.0741 0.310730 -39.1882 13.9554
    #> score                 0.2702         0.1344  2.0104 0.075279  -0.0338  0.5742
    #> entertain_hours      -0.9518         0.7394 -1.2873 0.230108  -2.6244  0.7208
    #>
    #>  Model:           OLS              R-squared:   0.86
    #>  Df:              9               R.S-ajusted:   0.83
    #>  F-statistic:    26.94             prob(F-statistic): 0.000158776
    #>  Resudual standard error: 1.71
    #>  =====================================================================
    #>                 coefficients standard error t value  p(>|t|)  [ 0.025 0.975 ]
    #> intercept           -14.9623        11.7250 -1.2761 0.233867 -41.4861 11.5615
    #> score                 0.2975         0.1354  2.1972 0.055594  -0.0088  0.6038
    #> entertain_hours      -0.6628         0.6108 -1.0851 0.306086  -2.0445  0.7189
    #> [1] 2.074146

# L1 and L2 regularization

When a dataset is immense, it can lead to multicollinearity or ,the relation between the dependent variables we include in our linear regression model, can augment the model’s complexity and generate overfitting. L1 and L2 regularization are methods to create less complex model and prevent overfitting by adding penalty term to the loss function of the model. Both techniques add bias to the estimators but tend to have smaller mean square error and more stable than OLS estimators.

L1 regularization is known as Lasso regression, and L2 as Ridge regression. The main difference between them is the penalty term.

In Ridge regression, it adds squared magnitude of coefficient as the penalty term to the loss function, where \(\lambda\) is a real number choosed by ourselves.

meanwhiles in Lasso regression, the penalty term is instead absolute magnitude of coefficients.

AS a consequence, L2 regularizationt tend to shrinkage some coefficients toward zero ans remains all features in model althought with smaller weight one respect to another. However, L2 regularization drive some coefficients to exactly zero, these zero coefficients are considered irrelevant and do not contribute to the model’s prediction.

## ridge

Suppose we have a dataframe named test , we generate the coefficients of ridge regression with the function \(\color{blue}{ridge}\) given some values of lambda.

    library("glmnet")
    #> Loading required package: Matrix
    #> Loaded glmnet 4.1-8
    data("QuickStartExample")
    test<-as.data.frame(cbind(QuickStartExample$y,QuickStartExample$x))
    ridge(data=test,y="V1",x=colnames(test)[2:21],lambda=c(0.01,0.1))
    #>                   0.01          0.1
    #> intercept  0.109116513  0.109551940
    #> V2         1.380959646  1.379949878
    #> V3         0.025036976  0.025228001
    #> V4         0.767404571  0.766632886
    #> V5         0.066720714  0.066302816
    #> V6        -0.905882112 -0.905021421
    #> V7         0.618372569  0.618237227
    #> V8         0.124494060  0.124512059
    #> V9         0.401019100  0.400720930
    #> V10       -0.036571552 -0.036710140
    #> V11        0.136478104  0.136012870
    #> V12        0.251566242  0.251290431
    #> V13       -0.069907751 -0.069857280
    #> V14       -0.049381966 -0.049257632
    #> V15       -1.163915499 -1.162992110
    #> V16       -0.147286747 -0.146860043
    #> V17       -0.051541308 -0.051267927
    #> V18       -0.055874048 -0.055604583
    #> V19        0.057081673  0.057089484
    #> V20       -0.006411382 -0.006304197
    #> V21       -1.148370996 -1.146910033