Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>When I ran the code, I got something a bit different, since set.seed() had not been specified. Instead of using the variable name "letter", I used "letter_" as a convenient splitting marker:</p> <pre><code>&gt; fnew &lt;- rownames(myfit$beta)[which(myfit$beta != 0)] &gt; fnew [1] "letter_c" "letter_d" "letter_e" "letter_f" "letter_h" "letter_k" "letter_l" [8] "letter_o" "letter_q" "letter_r" "letter_s" "letter_t" "letter_u" "letter_v" [15] "letter_w" </code></pre> <p>Then made the split and packaged into a character matrix:</p> <pre><code>&gt; fnewmtx &lt;- cbind( lapply(sapply(fnew, strsplit, split="_"), "[[", 2), + lapply(sapply(fnew, strsplit, split="_"), "[[", 1)) </code></pre> <blockquote> <p>fnewmtx [,1] [,2]<br> letter_c "c" "letter" letter_d "d" "letter" letter_e "e" "letter" letter_f "f" "letter" snipped the rest</p> </blockquote> <p>And wrapped the paste function(s) output in as.formula() which is half of the answer to how to "convert between formula and its character representation and back." The other half is as.character()</p> <pre><code>form &lt;- as.formula( paste("~", paste( paste(" I(", fnewmtx[,2], "_ ==", "'",fnewmtx[,1],"') ", sep="") , sep="", collapse="+") ) ) # edit: needed to add back the underscore </code></pre> <p>And the output is now an appropriate class object:</p> <pre><code>&gt; class(form) [1] "formula" &gt; form ~I(letter_ == "c") + I(letter_ == "d") + I(letter_ == "e") + I(letter_ == "f") + I(letter_ == "h") + I(letter_ == "k") + I(letter_ == "l") + I(letter_ == "o") + I(letter_ == "q") + I(letter_ == "r") + I(letter_ == "s") + I(letter_ == "t") + I(letter_ == "u") + I(letter_ == "v") + I(letter_ == "w") </code></pre> <p>I find it interesting that the as.formula conversion made the single-quotes around the letters into double-quotes.</p> <p>Edit: Now that the problem has an additional dimension or two, my suggestion is to skip the recreation of the formula. Note that the rownames of myfit$beta are exactly the same as the column names of X, so instead use the non-zero rownames as indices to select columns in the X matrix:</p> <pre><code>&gt; str(X[ , which( colnames(X) %in% rownames(myfit$beta)[which(myfit$beta != 0)] )] ) Formal class 'dgCMatrix' [package "Matrix"] with 6 slots ..@ i : int [1:429] 9 54 91 157 166 37 55 68 117 131 ... ..@ p : int [1:61] 0 5 13 20 28 36 42 50 60 68 ... ..@ Dim : int [1:2] 200 60 ..@ Dimnames:List of 2 .. ..$ : chr [1:200] "1" "2" "3" "4" ... .. ..$ : chr [1:60] "letter_b" "letter_c" "letter_e" "letter_f" ... ..@ x : num [1:429] 1 1 1 1 1 1 1 1 1 1 ... ..@ factors : list() &gt; myfit2 &lt;- glmnet(X[ , which( colnames(X) %in% rownames(myfit$beta)[which(myfit$beta != 0)] )] ,as.vector(y),lambda=.05) &gt; myfit2 Call: glmnet(x = X[, which(colnames(X) %in% rownames(myfit$beta)[ which(myfit$beta != 0)])], y = as.vector(y), lambda = 0.05) Df %Dev Lambda [1,] 60 0.9996 0.05 </code></pre>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload