2016-09-03 4 views
1

Я пытаюсь использовать пакет penalized для калибровки штрафа за линейную регрессию так, чтобы коэффициенты подмножества переменных являются положительными. Мне удалось откалибровать модель, но я не использую ее для создания новых прогнозов.R пакет «наказан»: ошибка при использовании функции прогнозирования() с матричным вводом для «оштрафованных» и «непрозрачных»

Вот игрушка пример:

require(dplyr) 
require(penalized) 
require(ggplot2) 

head(diamonds) 
# A tibble: 6 x 10 
# carat  cut color clarity depth table price  x  y  z 
# <dbl>  <ord> <ord> <ord> <dbl> <dbl> <int> <dbl> <dbl> <dbl> 
#1 0.23  Ideal  E  SI2 61.5 55 326 3.95 3.98 2.43 
#2 0.21 Premium  E  SI1 59.8 61 326 3.89 3.84 2.31 
#3 0.23  Good  E  VS1 56.9 65 327 4.05 4.07 2.31 
#4 0.29 Premium  I  VS2 62.4 58 334 4.20 4.23 2.63 
#5 0.31  Good  J  SI2 63.3 58 335 4.34 4.35 2.75 
#6 0.24 Very Good  J VVS2 62.8 57 336 3.94 3.96 2.48 

response = diamonds$price 
penalized_vars = "x" 
unpenalized_vars = "depth" 
fit_penalized = penalized(response=as.matrix(response), 
          penalized = model.matrix(~., select_(diamonds, .dots = penalized_vars)), 
          unpenalized = model.matrix(~., select_(diamonds, .dots = unpenalized_vars)), 
          model="linear", 
          positive=TRUE, 
          maxiter=25) 
# nonzero coefficients: 3 

show(fit_penalized) 
#Penalized linear regression object 
#4 regression coefficients of which 3 are non-zero 

#Loglikelihood =  -482648.2 

head(fitted(fit_penalized)) 
#   1   2   3   4   5   6 
#-1679.6983 -1924.0012 -1515.2681 -863.6912 -393.7956 -1668.7105 

До сих пор так хорошо. Как я могу использовать это для прогнозирования значений на основе новой информации? Я попытался

predict(fit_penalized, 
     penalized = model.matrix(~., select_(vars, .dots = penalized_vars)), 
     unpenalized = model.matrix(~., select_(vars, .dots = unpenalized_vars))) 
# Error in terms.default([email protected]$unpenalized) : 
# no terms component nor attribute 

ответ

2

Что касается последнего penalized_0.9-47 обновляемой 2016-05-27


Как будет видно из моего теста ниже, unpenalized может быть указан только через «формула» не " матрица ", чтобы получить predict работы. Кажется, это ошибка в пакете. Ошибка не с penalized, а с predict. С одной стороны, ?penalized::predict говорит:

In particular, if penalized and/or unpenalized 
was specified in matrix form, a matrix must be given with the new 
subjects' data. The columns of these matrices must be exactly the 
same as in the matrices supplied in the original call that 
produced the ‘penfit’ object. If either penalized or unpenalized 
was given as a ‘formula’ in the original call, the user of 
‘predict’ must supply a new ‘data’ argument. 

кажется правомерным передать матрицу unpenalized, но мой практический тест отрицает это. Поэтому попробуйте связаться с автором пакета.

В качестве дополнительной заметки ваша текущая спецификация будет иметь штрафный перехват, а также свободный перехват. Вы должны удалить один из них для идентификации. В дальнейшем я отказываюсь от свободного перехвата.

library(penalized) 
library(ggplot2) 
X1 <- model.matrix (~x, diamonds) ## model matrix for penalized terms 
X2 <- model.matrix(~ depth - 1, diamonds) ## model matrix for free terms 
vars <- diamonds[1:5, ] ## prediction dataset 
Xp1 <- model.matrix(~x, vars) ## prediction matrix for penalized terms 
Xp2 <- model.matrix(~ depth - 1, vars) ## prediction matrix for free terms 

## use "formula" for both 
fit <- penalized (price, ~ x, ~ depth - 1, data = diamonds, model = "linear", positive = TRUE) 
predict(fit, ~ x, ~ depth - 1, vars) 
#   mu sigma2 
#1 -1523.5643 3600667 
#2 -1328.9947 3600667 
#3 -183.9204 3600667 
#4 -950.1526 3600667 
#5 -717.6854 3600667 

## "matrix" for `penalized` and "formula" for `unpenalized` 
fit <- penalized (price, X1, ~ depth - 1, data = diamonds, model = "linear", positive = TRUE) 
predict(fit, Xp1, ~ depth - 1, vars) 
#   mu sigma2 
#1 -1523.5643 3600667 
#2 -1328.9947 3600667 
#3 -183.9204 3600667 
#4 -950.1526 3600667 
#5 -717.6854 3600667 

## "formula" for `penalized` and "matrix" for `unpenalized` 
fit <- penalized (price, ~ x, X2, data = diamonds, model = "linear", positive = TRUE) 
predict(fit, ~ x, Xp2, vars) 
# Error in terms.default([email protected]$unpenalized) : 
# no terms component nor attribute 

## "matrix" for both 
fit <- penalized (price, X1, X2, data = diamonds, model = "linear", positive = TRUE) 
predict(fit, Xp1, Xp2, vars) 
# Error in terms.default([email protected]$unpenalized) : 
# no terms component nor attribute 
+0

Интересно, переход на формулы работал! Благодаря! – phildeutsch

Смежные вопросы