# The supervised `s2net`

`s2net` can be used as a supervised method (without unlabeled data) and it is equivalent to elastic net.

## Data

The `auto_mpg` dataset is available when `s2net` is installed.

    library(s2net)
    data("auto_mpg")

    # Preprocess the data using the s2Data function
    train = s2Data(auto_mpg$P1$xL, auto_mpg$P1$yL, preprocess = TRUE)

## Ordinary least squares

To fit an OLS model, we will use the `lm` function (without intercept).

    lm.fit = lm( y~ 0 + ., data = data.frame(train$xL, y = train$yL))

To obtain the estimations from `s2net` we use

    obj = s2netR(train, s2Params(0))
    # We set all the hyper-parameters to 0

We can compare the training objectives of both algorithms, as well as the estimations.

    library(Metrics)
    # Training error
    ypred = predict.lm(lm.fit, data.frame(train$xL))
    print("OLS error:")
    mse(ypred, train$yL)

    ypred = predict(obj, train$xL)
    print("s2net error:")
    mse(ypred, train$yL)

    #Estimations
    data.frame(mle = lm.fit$coefficients, s2net = obj$beta)

      [1] "OLS error:"
      [1] 11.14708
      [1] "s2net error:"
      [1] 11.14754
                          mle        s2net
      cylinders.L   0.3548161  0.285474997
      cylinders.Q  -0.9132173 -0.935717172
      cylinders.C   0.3835068  0.330481968
      cylinders.4          NA -0.008096223
      displacement -1.0056651 -0.945328949
      horsepower    0.1307953  0.124370552
      weight       -2.8561218 -2.901267109
      acceleration  1.1041176  1.115766617
      year          3.7855521  3.782110411

## Lasso

    library(glmnet)

    lasso.fit = glmnet(train$xL, train$yL, family = "gaussian",
                               alpha = 1, lambda = 0.01, intercept = F)
    ypred = predict(lasso.fit, train$xL)
    print("Lasso error:")
    mse(ypred, train$yL)

    obj = s2netR(train, s2Params(lambda1 = 0.01))
    ypred = predict(obj, train$xL)
    print("s2net error")
    mse(ypred, train$yL)

    print("Coefficients")
    data.frame(lasso = as.numeric(lasso.fit$beta), s2net = obj$beta)

      [1] "Lasso error:"
      [1] 11.1477
      [1] "s2net error"
      [1] 11.1495
      [1] "Coefficients"
              lasso       s2net
      1  0.00000000  0.04400422
      2 -1.12939790 -1.06249932
      3  0.03454994  0.11384660
      4 -0.06378002 -0.04329510
      5 -0.97177003 -0.88722825
      6  0.11854687  0.10780812
      7 -2.87208217 -2.93069375
      8  1.10283623  1.11822773
      9  3.77265557  3.76858994

## Elastic net

    enet.fit = glmnet(train$xL, train$yL, family = "gaussian",
                              alpha = 0.3333, lambda = 0.03, intercept = F)
    ypred = predict(enet.fit, train$xL)
    print("glmnet error")
    mse(ypred, train$yL)

    obj = s2netR(train, s2Params(lambda1 = 0.01, lambda2 = 0.01))
    ypred = predict(obj, train$xL)
    print("s2net error")
    mse(ypred, train$yL)

    print("Coefficients")
    data.frame(enet = as.matrix(enet.fit$beta), s2net = obj$beta)

      [1] "glmnet error"
      [1] 11.14839
      [1] "s2net error"
      [1] 11.16189
      [1] "Coefficients"
                            s0       s2net
      cylinders.L   0.00000000  0.02176857
      cylinders.Q  -1.12966519 -1.11081728
      cylinders.C   0.03962925  0.08926029
      cylinders^4  -0.06613103 -0.07240406
      displacement -0.98765401 -1.05183911
      horsepower    0.11712469  0.10903802
      weight       -2.84667192 -2.72983278
      acceleration  1.09711179  1.07104828
      year          3.76243231  3.70533071