2017-01-26 5 views
0

Glmnetを使ってlasso推定を計算しようとします。 cv.glmnetを使用する代わりに、ラムダを0.34に固定してから係数を計算します。しかし、後でラムダRの値をチェックすると、ラムダ= NULLになります。任意の手掛かりどのようにラムダを修正するには?R - Glnmentパッケージで修正ラムダを選択する方法

# sample the data 
q <- rnorm(n*p, mean=0, sd=1) #create the matrix values from N(0,1) 
x <- matrix(q, nrow=n, ncol=p) 
e <- rnorm(n, mean=0, sd=1) # create error array 
y <- x%*%beta1 + e # get the y values 

#compute the coefficients LASSO 
lasso <- glmnet(x,y) 
lambda <- lasso$lambda.1se # selected lambda by cross-validation 
beta.hat <- as.matrix(coef(lasso, s=0.34)) #get the estimate beta's 
beta.hat <- beta.hat[2:(p+1),1] # delete intercept 

答えて

0

使用する必要があります:lasso <- glmnet::cv.glmnet(x,y)。 sinceyouがglmnet::glmnet代わりのglmnet::cv.glmnet使用

# sample the data 
require(glmnet) 
set.seed(1) 
n = 100; p =10 
q <- rnorm(n*p, mean=0, sd=1) #create the matrix values from N(0,1) 
x <- matrix(q, nrow=n, ncol=p) 
e <- rnorm(n, mean=0, sd=1) # create error array 
beta1 <- runif(10) 
y <- x%*%beta1 + e # get the y values 

#compute the coefficients LASSO 
lasso <- cv.glmnet(x,y) 
lambda <- lasso$lambda.1se # selected lambda by cross-validation 
print(lambda) 
beta.hat <- as.matrix(coef(lasso, s=0.34)) #get the estimate beta's 
beta.hat <- beta.hat[2:(p+1),1] # delete intercept 

lambdaNULLです:

はここにあなたのコード内で変更された(n,p,beta1ため、私は与えられた値)です。あなたがstr(lasso, 1)を入力すると、あなたが見ることができるようglmnetの出力からオブジェクトlambda.1seはありません。

> str(lasso, 1) 
List of 12 
$ a0  : Named num [1:64] -0.0804 -0.0762 -0.0725 -0.0691 -0.066 ... 
    ..- attr(*, "names")= chr [1:64] "s0" "s1" "s2" "s3" ... 
$ beta  :Formal class 'dgCMatrix' [package "Matrix"] with 6 slots 
$ df  : int [1:64] 0 1 1 1 1 1 2 3 3 3 ... 
$ dim  : int [1:2] 10 64 
$ lambda : num [1:64] 1.373 1.251 1.14 1.039 0.946 ... 
$ dev.ratio: num [1:64] 0 0.0678 0.1241 0.1709 0.2097 ... 
$ nulldev : num 472 
$ npasses : int 271 
$ jerr  : int 0 
$ offset : logi FALSE 
$ call  : language glmnet(x = x, y = y) 
$ nobs  : int 100 
- attr(*, "class")= chr [1:2] "elnet" "glmnet" 
関連する問題