Professional Documents
Culture Documents
# Regularization
# Ridge Regression in R
set.seed(123)
library(glmnet)
library(dplyr)
library(psych)
data("mtcars")
as.matrix()
lambda = lambdas_to_try,
plot(ridge_cv)
# Run model
lambda = lambdas_to_try[lambda],
standardize = TRUE)
%*% t(X_scaled)
df <- tr(H)
+ 2 * df
+ 2 * df * log(nrow(X_scaled))
}
plot(log(lambdas_to_try), aic, col = "orange", type = "l",
standardize = TRUE)
standardize = TRUE)
standardize = FALSE)
output
10 10 10 10 10 10 10 10 10 10 10 10 10 10 10 10 10 10 10 10
-5 10
Log( A.)
1
0
k:Mj{tamb da s_ to _ try)
10 10 10 10
/
I
I
/
/
-5 10
L og La m bd a
L 2 regularization
# Regularization
# Lasso Regression
set.seed(123)
library(glmnet)
library(dplyr)
library(psych)
data("mtcars")
as.matrix()
lambda = lambdas_to_try,
plot(lasso_cv)
standardize = TRUE)
y_hat_cv)^2
standardize = FALSE)
output:
10 2 0
"'
--
--Orat
.. . .. .
c y 1
--qsec
-am
......
W1
-c, ,t
-5 0 5 10
Log Lambda