Regression coefficients and fitted values in LASSO-type problems | R Documentation |
Retrieving regression coefficients and predicted values from the 'solveEN' and 'LARS' functions' outputs
## S3 method for class 'LASSO'
coef(object, ...)
## S3 method for class 'LASSO'
fitted(object, ...)
object |
An object of the class 'LASSO' returned either by the function 'LARS' or 'solveEN' |
... |
Other arguments:
|
Method coef
returns a matrix that contains the regression coefficients (in rows) associated to each value of lambda (in columns). When the regression was applied to an object Gamma
with more than one column, method coef
returns a list
Method fitted
returns a matrix with fitted values
Xβ (in rows)
for each value of lambda (in columns).
require(SFSI)
data(wheatHTP)
y = as.vector(Y[,"E1"]) # Response variable
X = scale(X_E1) # Predictors
# Training and testing sets
tst = which(Y$trial %in% 1:10)
trn = seq_along(y)[-tst]
# Calculate covariances in training set
XtX = var(X[trn,])
Xty = cov(X[trn,],y[trn])
# Run the penalized regression
fm = solveEN(XtX,Xty,alpha=0.5)
# Regression coefficients
dim(coef(fm))
dim(coef(fm, ilambda=50)) # Coefficients associated to the 50th lambda
dim(coef(fm, nsup=25)) # Coefficients where around nsup=25 are non-zero
# Predicted values
yHat1 = fitted(fm, X=X[trn,]) # training data
yHat2 = fitted(fm, X=X[tst,]) # testing data
# Penalization vs correlation
plot(-log(fm$lambda[-1]),cor(y[trn],yHat1[,-1]),main="training",type="l")
plot(-log(fm$lambda[-1]),cor(y[tst],yHat2[,-1]),main="testing",type="l")
Add the following code to your website.
For more information on customizing the embed code, read Embedding Snippets.