У меня была та же проблема, я безрезультатно расспрашивал меня, а затем я отправил электронное письмо сопровождающему пакета (Тревору Хасти), который дал ответ. Проблема возникает, когда ряды сильно коррелированы. Решение состоит в том, чтобы уменьшить порог в вызове функции glmnet()
(а не через glmnet.control()
). В приведенном ниже коде используется встроенный набор данных EuStockMarkets
и применяется VAR с lambda=0
. Для XSMI коэффициент OLS ниже 1, коэффициент glmnet
по умолчанию больше 1 с разницей около 0,03, а коэффициент glmnet
с thresh=1e-14
очень близок к коэффициенту OLS (разница 1.8e-7
).
# Use built-in panel data with integrated series
data("EuStockMarkets")
selected_market <- 2
# Take logs for good measure
EuStockMarkets <- log(EuStockMarkets)
# Get dimensions
num_entities <- dim(EuStockMarkets)[2]
num_observations <- dim(EuStockMarkets)[1]
# Build the response with the most recent observations at the top
Y <- as.matrix(EuStockMarkets[num_observations:2, selected_market])
X <- as.matrix(EuStockMarkets[(num_observations - 1):1, ])
# Run OLS, which adds an intercept by default
ols <- lm(Y ~ X)
ols_coef <- coef(ols)
# run glmnet with lambda = 0
fit <- glmnet(y = Y, x = X, lambda = 0)
lasso_coef <- coef(fit)
# run again, but with a stricter threshold
fit_threshold <- glmnet(y = Y, x = X, lambda = 0, thresh = 1e-14)
lasso_threshold_coef <- coef(fit_threshold)
# build a dataframe to compare the two approaches
comparison <- data.frame(ols = ols_coef,
lasso = lasso_coef[1:length(lasso_coef)],
lasso_threshold = lasso_threshold_coef[1:length(lasso_threshold_coef)]
)
comparison$difference <- comparison$ols - comparison$lasso
comparison$difference_threshold <- comparison$ols - comparison$lasso_threshold
# Show the two values for the autoregressive parameter and their difference
comparison[1 + selected_market, ]
R
возвращает:
ols lasso lasso_threshold difference difference_threshold
XSMI 0.9951249 1.022945 0.9951248 -0.02782045 1.796699e-07
person
miguelmorin
schedule
23.02.2018