2016-06-18 9 views
3

私は本当にこれに固執してきました、誰もが私を助けることができる願っています!私は54列のデータセットを持っており、リッジ回帰のあるテストセットについて予測したいと考えています。 (また、rsquaredやMAEとしての評価指標で)働くリッジ回帰と予測を行うための別の方法かもしれない何リッジ回帰の予測R

Error in UseMethod("predict") : 
    no applicable method for 'predict' applied to an object of class "ridgelm" 

:私はこれを行うと

nn <-nrow(longley) 
index <- 1:nrow(longley) 
testindex <- sample(index, trunc(length(index)/3)) 
testset <- longley[testindex,] 
trainset <-longley[-testindex,] 
trainset1 <- trainset[,-7] 

# Fit the ridge regression model: 

mod <- lm.ridge(y ~., data = trainset, lambda = 0.661) 

# Predict and evaluate it by using MAE function: 

mae <- function(model) { 
    y = trainset$Employed 
    y.pred <- predict(model, trainset) 
    return(mean(abs(y-y.pred))) 
} 

私は、次のエラーメッセージが表示されますか?

答えて

1

ridgelmオブジェクトには実際にはpredictメソッドがありません。手で行う必要があります。

> methods(class = 'ridgelm') 
[1] coef plot print select 

これは線形モデルであり、そのフィッティングの行列計算の問題だけであるので:

y.pred <- as.matrix(cbind(const=1,trainset)) %*% coef(model) 

私たちは、定数を追加する必要があるあなたが見ることができるようにところで、どちらsummaryがあります1を線形モードの一定係数に関連付けることができる。

重要:リッジ回帰を使用するには、通常は説明変数をスケールし、平均を引く。ベストプラクティスは、トレーニングからスケーリングの定義を学習し、新しいデータから変数を中心にするトレーニングセットの手段を使用することです。クロスバリデーションに関するこの関連記事に興味があります:

https://stats.stackexchange.com/questions/152203/how-to-calculate-predicted-values-using-an-lm-ridge-object

関連する問題