r - Bootstrap LASSO Lambda -
i trying bootstrap lambda1 parameter in lasso regression (library penalized) (not coefficients estimates know not make sense calculate e.g. 95% cis them, question lambda1 only). far:
df <- read.table(header=t, text="group class v1 v2 1 ala 1 3.98 23.2 2 ala 2 5.37 18.5 3 c 1 4.73 22.1 4 b 1 4.17 22.3 5 c 2 4.47 22.4 ")
tried this:
x<-df[,c(3,4)] # data, variables in columns, cases in rows y<-df[,2] # dichotomous response (i 1:100) { opt1<-optl1(y,x) opt1$lambda }
but got error: unexpected "}" in "}"
tried this:
f<-function(x,y,i){ opt1<-optl1(y,x,[i]) } boot(x,f,100)
but got error in boot (x,f,100): incorrect number of subscripts on matrix... can help?
here wrong loop:
1) needs syntax (i in 1:100) {} in order work;
2) needs save opt1$lambda in proper object;
3) needs values (y,x) change 1 iteration of loop another.
the r code addresses items 1) , 2) above written follows:
lambda <- null (i in 1:100) { opt1 <- optl1(y,x) # opt1 not change # since y , x same # on each iteration of loop lambda <- c(lambda, opt1$lambda) } lambda
in code, object lambda store value opt1$lambda produced @ each iteration declared @ top of loop command lambda -> null , augmented after each iteration command lambda <- c(lambda, opt1$lambda).
in general, using null trick not recommended large number of iterations. better alternative this:
lambda <- list('vector', 100) (i in 1:100) { opt1 <- optl1(y,x) # opt1 not change # since y , x same # on each iteration of loop lambda[i] <- opt1$lambda } lambda <- unlist(lambda) lambda
with second alternative, pre-allocate lambda @ top of loop list 100 components, such i-th component store value opt1$lambda produced during i-th iteration. inside loop, save value of opt1$lambda in list named lambda command:
lambda[i] <- opt1$lambda.
at end of loop, unlist lambda becomes regular vector (i.e., column of numbers).
Comments
Post a Comment