predict.sgdnet: Predictions for sgdnet Models

Description Usage Arguments Value Author(s) See Also Examples

Description

Predictions for sgdnet Models

Usage

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
## S3 method for class 'sgdnet'
predict(object, newx = NULL, s = NULL, type,
  exact = FALSE, newoffset = NULL, ...)

## S3 method for class 'sgdnet_gaussian'
predict(object, newx = NULL, s = NULL,
  type = c("link", "response", "coefficients", "nonzero"),
  exact = FALSE, newoffset = NULL, ...)

## S3 method for class 'sgdnet_binomial'
predict(object, newx = NULL, s = NULL,
  type = c("link", "response", "coefficients", "nonzero", "class"),
  exact = FALSE, newoffset = NULL, ...)

## S3 method for class 'sgdnet_multinomial'
predict(object, newx = NULL, s = NULL,
  type = c("link", "response", "coefficients", "nonzero", "class"),
  exact = FALSE, newoffset = NULL, ...)

## S3 method for class 'sgdnet_mgaussian'
predict(object, newx = NULL, s = NULL,
  type = c("link", "response", "coefficients", "nonzero"),
  exact = FALSE, newoffset = NULL, ...)

Arguments

object

an object of class 'sgdnet'.

newx

new data to predict on. Must be provided if type is "link".

s

the lambda penalty value on which to base the predictions.

type

type of prediction to return, one of

link

linear predictors,

response

responses,

coefficients

coefficients (weights); equivalent to calling coef()

nonzero

nonzero coefficients at each step of the regularization path, and

class

class predictions for each new data point in newx at each step of the regularization path – only useful for 'binomial' and 'multinomial' families.

exact

if the given value of s is not in the model and exact = TRUE, the model will be refit using s. If FALSE, predictions will be made using a linearly interpolated coefficient matrix.

newoffset

if an offset was used in the call to sgdnet(), a new offset can be provided here for making predictions (but not for type = 'coefficients'/'nonzero')

...

arguments to be passed on to stats::update() to refit the model via sgdnet() if s is missing from the model and an exact fit is required by exact.

Value

Predictions for object given data in newx.

Author(s)

Jerome Friedman, Trevor Hastie, Rob Tibshirani, Noah Simon (original), Johan Larsson (modifications)

See Also

sgdnet(), coef.sgdnet(), predict.cv_sgdnet()

Examples

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
# Gaussian

# Split into training and test sets
n <- length(abalone$y)
train_ind <- sample(n, size = floor(0.8 * n))

# Fit the model using the training set
fit_gaussian <- sgdnet(abalone$x[train_ind, ], abalone$y[train_ind])

# Predict using the test set
pred_gaussian <- predict(fit_gaussian, newx = abalone$x[-train_ind, ])

# Mean absolute prediction error along regularization path
mae <- 1/(n - length(train_ind)) *
         colSums(abs(abalone$y[-train_ind] - pred_gaussian))

# Binomial
n <- length(heart$y)
train_ind <- sample(n, size = floor(0.8 * n))

fit_binomial <- sgdnet(heart$x[train_ind, ],
                       heart$y[train_ind],
                       family = "binomial")

# Predict classes at custom lambda value (s) using linear interpolation
predict(fit_binomial, heart$x[-train_ind, ], type = "class", s = 1/n)

# Multinomial
n <- length(wine$y)
train_ind <- sample(n, size = floor(0.8 * n))
fit_multinomial <- sgdnet(wine$x[train_ind, ],
                          wine$y[train_ind],
                          family = "multinomial",
                          alpha = 0.25)
predict(fit_multinomial,
        wine$x[-train_ind, ],
        s = 0.0001,
        exact = TRUE,
        type = "class")

# Multivariate gaussian regression, predict nonzero coefficients
fit_mgaussian <- sgdnet(student$x, student$y, family = "mgaussian")
predict(fit_mgaussian, type = "nonzero")

jolars/sgdnet documentation built on May 22, 2019, 11:52 p.m.