Search code examples
rsortingdoubleglmnet

How do I sort an R double by the second column, while preserving parings?


I have a double where the first column is a course name and the second column is a lambda value (these are coefficients from a penalized regression model). My double is a few thousand rows long, with most of the lambda values being zero. However, there are a few non-zero values. How can I filter this double so that only the non-zero coefficients remain? Here is what I have:

To see the coefficients with the minimum cross-validation error:

course_coef1 <- as.matrix(coef(lasso_reg, lasso_reg$lambda.min))

head(course_coef1)
                    1
(Intercept) 0.4170463
PHYS 1116   0.0000000
VISST 2511  0.0000000
MATH 1920   0.0000000
PHIL 1110   0.0000000
FREN 1220   0.0000000

when I do this, I remove the left column of the double, which I don't want to do. I want to be able to see the course to which coefficient refers as well

non_zero <-  course_coef1[course_coef1[,1] != 0]

non_zero
 [1]  4.170463e-01  1.186766e-02  1.022153e-02 -1.728692e-02 -1.267802e-02  2.953045e-02 -7.366728e-04 -6.825617e-02  2.581637e-02  1.030888e-01
[11] -6.815507e-02 -6.177919e-04  3.138149e-02  1.297283e-05  7.753567e-02 -1.562090e-01 -2.301548e-01 -2.635691e-02 -1.382577e-02  1.487066e-02
[21] -3.922772e-04 -2.267470e-02 -2.668698e-02  3.372374e-02  2.309662e-02  4.383800e-02  8.291964e-03  2.643610e-04 -2.237277e-03 -3.068006e-04

Solution

  • Two problems.

    1. You are grabbing columns, since you have omitted the comma, try [... != 0,] (add the comma);
    2. It might be dropping from a matrix to a vector, fix with ,drop=FALSE.
    z <- as.matrix(coef(lm(mpg~disp+factor(cyl), data=mtcars)))
    z
    #                     [,1]
    # (Intercept)  29.53476781
    # disp         -0.02730864
    # factor(cyl)6 -4.78584624
    # factor(cyl)8 -4.79208587
    
    z[z[,1] < 0]
    # [1] -0.02730864 -4.78584624 -4.79208587
    z[z[,1] < 0,]
    #         disp factor(cyl)6 factor(cyl)8 
    #  -0.02730864  -4.78584624  -4.79208587 
    z[z[,1] < 0,, drop = FALSE]
    #                     [,1]
    # disp         -0.02730864
    # factor(cyl)6 -4.78584624
    # factor(cyl)8 -4.79208587