
Extract coefficients from a glmnet object
predict.glmnet.RdSimilar to other predict methods, this functions predicts fitted values,
logits, coefficients and more from a fitted "glmnet" object.
Usage
# S3 method for class 'glmnet'
coef(object, s = NULL, exact = FALSE, ...)
# S3 method for class 'glmnet'
predict(
object,
newx,
s = NULL,
type = c("link", "response", "coefficients", "nonzero", "class"),
exact = FALSE,
newoffset,
...
)
# S3 method for class 'relaxed'
predict(
object,
newx,
s = NULL,
gamma = 1,
type = c("link", "response", "coefficients", "nonzero", "class"),
exact = FALSE,
newoffset,
...
)Arguments
- object
Fitted
"glmnet"model object or a"relaxed"model (which inherits from class "glmnet").- s
Value(s) of the penalty parameter
lambdaat which predictions are required. Default is the entire sequence used to create the model.- exact
This argument is relevant only when predictions are made at values of
s(lambda) different from those used in the fitting of the original model. Not available for"relaxed"objects. Ifexact=FALSE(default), then the predict function uses linear interpolation to make predictions for values ofs(lambda) that do not coincide with those used in the fitting algorithm. While this is often a good approximation, it can sometimes be a bit coarse. Withexact=TRUE, these different values ofsare merged (and sorted) withobject$lambda, and the model is refit before predictions are made. In this case, it is required to supply the original datax=andy=as additional named arguments topredict()orcoef(). The workhorsepredict.glmnet()needs toupdatethe model, and so needs the data used to create it. The same is true ofweights,offset,penalty.factor,lower.limits,upper.limitsif these were used in the original call. Failure to do so will result in an error.- ...
This is the mechanism for passing arguments like
x=whenexact=TRUE; seeexactargument.- newx
Matrix of new values for
xat which predictions are to be made. Must be a matrix; can be sparse as inMatrixpackage. This argument is not used fortype=c("coefficients","nonzero")- type
Type of prediction required. Type
"link"gives the linear predictors for"binomial","multinomial","poisson"or"cox"models; for"gaussian"models it gives the fitted values. Type"response"gives the fitted probabilities for"binomial"or"multinomial", fitted mean for"poisson"and the fitted relative-risk for"cox"; for"gaussian"type"response"is equivalent to type"link". Type"coefficients"computes the coefficients at the requested values fors. Note that for"binomial"models, results are returned only for the class corresponding to the second level of the factor response. Type"class"applies only to"binomial"or"multinomial"models, and produces the class label corresponding to the maximum probability. Type"nonzero"returns a list of the indices of the nonzero coefficients for each value ofs.- newoffset
If an offset is used in the fit, then one must be supplied for making predictions (except for
type="coefficients"ortype="nonzero")- gamma
Single value of
gammaat which predictions are required, for "relaxed" objects.
Details
The shape of the objects returned are different for "multinomial"
objects. This function actually calls NextMethod(), and the
appropriate predict method is invoked for each of the three model types.
coef(...) is equivalent to predict(type="coefficients",...)
References
Friedman, J., Hastie, T. and Tibshirani, R. (2008)
Regularization Paths for Generalized Linear Models via Coordinate
Descent (2010), Journal of Statistical Software, Vol. 33(1), 1-22,
doi:10.18637/jss.v033.i01
.
Simon, N., Friedman, J., Hastie, T. and Tibshirani, R. (2011)
Regularization Paths for Cox's Proportional
Hazards Model via Coordinate Descent, Journal of Statistical Software, Vol.
39(5), 1-13,
doi:10.18637/jss.v039.i05
.
Glmnet webpage with four vignettes, https://glmnet.stanford.edu.
Author
Jerome Friedman, Trevor Hastie and Rob Tibshirani
Maintainer:
Trevor Hastie [email protected]
Examples
x=matrix(rnorm(100*20),100,20)
y=rnorm(100)
g2=sample(1:2,100,replace=TRUE)
g4=sample(1:4,100,replace=TRUE)
fit1=glmnet(x,y)
predict(fit1,newx=x[1:5,],s=c(0.01,0.005))
#> s=0.010 s=0.005
#> [1,] -0.5132584 -0.5331257
#> [2,] -0.5372573 -0.5773358
#> [3,] -1.0683086 -1.1034454
#> [4,] -0.9886583 -1.0401801
#> [5,] -0.4313292 -0.4114062
predict(fit1,type="coef")
#> 21 x 64 sparse Matrix of class "dgCMatrix"
#> [[ suppressing 64 column names ‘s0’, ‘s1’, ‘s2’ ... ]]
#>
#> (Intercept) -0.0442567 -0.04496474 -0.04560989 -0.046160751 -0.045657264
#> V1 . . . . .
#> V2 . . . . .
#> V3 . . . 0.000577883 0.015780762
#> V4 . . . . .
#> V5 . . . . 0.001011909
#> V6 . . . . .
#> V7 . . . . .
#> V8 . 0.02069055 0.03954300 0.056716609 0.072217639
#> V9 . . . . .
#> V10 . . . . .
#> V11 . . . . .
#> V12 . . . . .
#> V13 . . . . .
#> V14 . . . . .
#> V15 . . . . .
#> V16 . . . . .
#> V17 . . . . .
#> V18 . . . . .
#> V19 . . . . .
#> V20 . . . . .
#>
#> (Intercept) -0.04446197 -0.04337287 -0.042330277 -0.040840842 -0.03897606
#> V1 . . . . .
#> V2 . . . . .
#> V3 0.02952409 0.04204650 0.053539325 0.065145078 0.07816295
#> V4 . . . . .
#> V5 0.01326212 0.02442405 0.034738790 0.045099593 0.05379812
#> V6 . . . . .
#> V7 . . . . .
#> V8 0.08584841 0.09826827 0.109383087 0.117500897 0.12438900
#> V9 . . . -0.001443378 -0.01237245
#> V10 . . . . .
#> V11 . . . . .
#> V12 . . . . .
#> V13 . . . . .
#> V14 . . . . .
#> V15 . . . . .
#> V16 . . . . .
#> V17 . . . . .
#> V18 . . . . .
#> V19 . . 0.001498243 0.012970610 0.02389819
#> V20 . . . 0.003304468 0.01290332
#>
#> (Intercept) -0.03727699 -0.036034726 -0.0355684260 -0.035302225 -0.035059504
#> V1 . . . . .
#> V2 . . 0.0000958465 0.004642898 0.008780777
#> V3 0.09002419 0.102318544 0.1121409787 0.121173858 0.129404507
#> V4 . . . . .
#> V5 0.06172363 0.068500812 0.0740398904 0.078927388 0.083380704
#> V6 . . . . .
#> V7 . . . . .
#> V8 0.13066559 0.137980752 0.1468138072 0.153885933 0.160331189
#> V9 -0.02233063 -0.032490581 -0.0412656318 -0.049716426 -0.057416483
#> V10 . . . . .
#> V11 . -0.008258860 -0.0173368031 -0.025253544 -0.032467526
#> V12 . . . . .
#> V13 . . . . .
#> V14 . . . . .
#> V15 . . . . .
#> V16 . . . . .
#> V17 . -0.001372574 -0.0082861585 -0.014079022 -0.019358208
#> V18 . . . . .
#> V19 0.03385487 0.042071901 0.0493990671 0.056037610 0.062086622
#> V20 0.02164932 0.029460277 0.0363972666 0.042472914 0.048009114
#>
#> (Intercept) -0.03512505 -0.035040067 -0.034348986 -0.03372101 -0.0331931975
#> V1 . . . . .
#> V2 0.01371561 0.018728397 0.023155379 0.02720542 0.0308841461
#> V3 0.13685689 0.143659320 0.149887664 0.15557352 0.1606621279
#> V4 . . . . 0.0002672673
#> V5 0.08752281 0.091463360 0.095327501 0.09883857 0.1021168438
#> V6 . -0.002303424 -0.009462335 -0.01598217 -0.0218839512
#> V7 . . . . .
#> V8 0.16502731 0.168539186 0.171314400 0.17385725 0.1759730180
#> V9 -0.06450211 -0.071015621 -0.076981958 -0.08242510 -0.0873918552
#> V10 . . . . .
#> V11 -0.03946368 -0.046237752 -0.052850531 -0.05887878 -0.0642140782
#> V12 . . . . 0.0007931385
#> V13 . . . . .
#> V14 -0.00498723 -0.012190900 -0.019379504 -0.02593057 -0.0319171261
#> V15 . . . . .
#> V16 . . . . .
#> V17 -0.02433265 -0.029251038 -0.034443096 -0.03917023 -0.0433320466
#> V18 . . . . .
#> V19 0.06786371 0.073041321 0.077293511 0.08116315 0.0847677167
#> V20 0.05292853 0.057497031 0.061977213 0.06605481 0.0698250042
#>
#> (Intercept) -0.032873977 -0.032583704 -0.032319222 -0.031766419 -0.0309881035
#> V1 . . . . .
#> V2 0.034162887 0.037150251 0.039872127 0.042555269 0.0455823693
#> V3 0.165046620 0.169049359 0.172696455 0.176043561 0.1789563605
#> V4 0.001697147 0.003001439 0.004189812 0.004730234 0.0052162204
#> V5 0.105361019 0.108307210 0.110991636 0.113403943 0.1154085953
#> V6 -0.027097803 -0.031852555 -0.036184893 -0.040422035 -0.0441619592
#> V7 . . . . .
#> V8 0.177249608 0.178432618 0.179510659 0.180377566 0.1810933713
#> V9 -0.091959170 -0.096125431 -0.099921541 -0.103202772 -0.1066144379
#> V10 . . . . .
#> V11 -0.068596571 -0.072597101 -0.076242250 -0.079653064 -0.0831052335
#> V12 0.003222192 0.005425522 0.007433081 0.008758919 0.0096931683
#> V13 . . . . 0.0006600282
#> V14 -0.037355984 -0.042309138 -0.046822247 -0.050998621 -0.0549790321
#> V15 . . . . 0.0024692757
#> V16 . . . . .
#> V17 -0.046763278 -0.049892903 -0.052744561 -0.055511295 -0.0581281467
#> V18 . . . -0.002127938 -0.0045400621
#> V19 0.088223804 0.091367117 0.094231172 0.097022435 0.0994484838
#> V20 0.073372730 0.076601026 0.079542513 0.082429256 0.0849205090
#>
#> (Intercept) -0.030198976 -0.029717011 -0.0292390338 -0.028770257 -0.0284083450
#> V1 . . . . .
#> V2 0.048626568 0.051477493 0.0540308251 0.056273187 0.0584013074
#> V3 0.181507300 0.184054401 0.1863368371 0.188353543 0.1901918343
#> V4 0.005830306 0.006636147 0.0073104516 0.007807759 0.0083768323
#> V5 0.117141485 0.118892355 0.1203916887 0.121698814 0.1228151098
#> V6 -0.047404136 -0.050532798 -0.0533410658 -0.055847315 -0.0581727449
#> V7 . . . . 0.0004146151
#> V8 0.181582300 0.182170416 0.1828379344 0.183531453 0.1842485528
#> V9 -0.110046716 -0.113419799 -0.1164695602 -0.119181849 -0.1216673824
#> V10 . 0.002748005 0.0053294087 0.007683267 0.0098734805
#> V11 -0.086387515 -0.089216752 -0.0917049897 -0.093895609 -0.0959457161
#> V12 0.010485139 0.010810319 0.0110832874 0.011323159 0.0114233618
#> V13 0.002155447 0.003378787 0.0045044125 0.005527686 0.0064939195
#> V14 -0.058742102 -0.062011050 -0.0648764351 -0.067403768 -0.0696874440
#> V15 0.005855558 0.008790680 0.0114439158 0.013823440 0.0160449374
#> V16 . . 0.0005100248 0.001280700 0.0019392373
#> V17 -0.060473467 -0.063009472 -0.0654417081 -0.067751447 -0.0698660295
#> V18 -0.006584098 -0.008030172 -0.0093578615 -0.010610759 -0.0117604421
#> V19 0.101582508 0.103565492 0.1053876585 0.107074806 0.1085887564
#> V20 0.087016290 0.088961991 0.0907392257 0.092373718 0.0938248198
#>
#> (Intercept) -0.028296110 -0.028203460 -0.028119183 -0.028042392 -0.027972423
#> V1 . . . . .
#> V2 0.060625766 0.062664053 0.064520032 0.066211065 0.067751867
#> V3 0.191763487 0.193208688 0.194524864 0.195724066 0.196816733
#> V4 0.009163119 0.009913063 0.010596148 0.011218502 0.011785565
#> V5 0.123713406 0.124504163 0.125223804 0.125879487 0.126476919
#> V6 -0.060402060 -0.062444738 -0.064305310 -0.066000574 -0.067545234
#> V7 0.002534927 0.004473473 0.006239716 0.007849056 0.009315426
#> V8 0.184815864 0.185396610 0.185927920 0.186412106 0.186853282
#> V9 -0.123822755 -0.125810313 -0.127620912 -0.129270623 -0.130773776
#> V10 0.011978728 0.013915661 0.015680880 0.017289294 0.018754821
#> V11 -0.097883507 -0.099674296 -0.101305634 -0.102792025 -0.104146369
#> V12 0.011167307 0.010905991 0.010667464 0.010450111 0.010252065
#> V13 0.007491187 0.008402543 0.009232876 0.009989438 0.010678788
#> V14 -0.071711302 -0.073553358 -0.075230965 -0.076759506 -0.078152254
#> V15 0.018173091 0.020129138 0.021911268 0.023535061 0.025014600
#> V16 0.002350748 0.002725919 0.003068686 0.003381053 0.003665673
#> V17 -0.071764854 -0.073509128 -0.075099415 -0.076548478 -0.077868814
#> V18 -0.012991946 -0.014094679 -0.015099247 -0.016014583 -0.016848604
#> V19 0.110027388 0.111315642 0.112489212 0.113558527 0.114532848
#> V20 0.095121305 0.096283286 0.097341718 0.098306119 0.099184845
#>
#> (Intercept) -0.027908669 -0.027850580 -0.027797651 -0.027749424 -2.770936e-02
#> V1 . . . . 1.984754e-05
#> V2 0.069155789 0.070434990 0.071600551 0.072662566 7.363067e-02
#> V3 0.197812330 0.198719481 0.199546043 0.200299176 2.009843e-01
#> V4 0.012302252 0.012773038 0.013202001 0.013592856 1.394657e-02
#> V5 0.127021278 0.127517277 0.127969213 0.128381000 1.287573e-01
#> V6 -0.068952671 -0.070235076 -0.071403555 -0.072468229 -7.343818e-02
#> V7 0.010651529 0.011868935 0.012978191 0.013988904 1.490953e-02
#> V8 0.187255265 0.187621536 0.187955270 0.188259355 1.885361e-01
#> V9 -0.132143393 -0.133391337 -0.134528417 -0.135564482 -1.365077e-01
#> V10 0.020090154 0.021306860 0.022415478 0.023425609 2.434916e-02
#> V11 -0.105380396 -0.106504795 -0.107529306 -0.108462803 -1.093120e-01
#> V12 0.010071614 0.009907193 0.009757379 0.009620874 9.499352e-03
#> V13 0.011306899 0.011879210 0.012400678 0.012875821 1.330626e-02
#> V14 -0.079421274 -0.080577558 -0.081631121 -0.082591088 -8.346475e-02
#> V15 0.026362700 0.027591040 0.028710256 0.029730045 3.066368e-02
#> V16 0.003925008 0.004161305 0.004376609 0.004572787 4.753326e-03
#> V17 -0.079071854 -0.080168020 -0.081166806 -0.082076862 -8.290367e-02
#> V18 -0.017608533 -0.018300952 -0.018931859 -0.019506717 -2.003294e-02
#> V19 0.115420612 0.116229510 0.116966548 0.117638110 1.182470e-01
#> V20 0.099985507 0.100715041 0.101379764 0.101985436 1.025347e-01
#>
#> (Intercept) -0.0277549580 -0.027795397 -0.027832235 -0.027865800 -0.027891685
#> V1 0.0004548094 0.000846255 0.001202879 0.001527820 0.001828706
#> V2 0.0745653787 0.075414846 0.076188780 0.076893957 0.077509303
#> V3 0.2015855603 0.202135298 0.202636144 0.203092495 0.203491795
#> V4 0.0142228692 0.014474631 0.014704008 0.014913006 0.015081734
#> V5 0.1291204630 0.129451267 0.129752669 0.130027295 0.130286779
#> V6 -0.0742995543 -0.075085488 -0.075801618 -0.076454129 -0.077032547
#> V7 0.0157379399 0.016493355 0.017181667 0.017808831 0.018366206
#> V8 0.1887770477 0.188996550 0.189196606 0.189378893 0.189525017
#> V9 -0.1373663880 -0.138148173 -0.138860474 -0.139509495 -0.140084547
#> V10 0.0252208553 0.026016788 0.026742028 0.027402841 0.027989038
#> V11 -0.1100576978 -0.110737376 -0.111356674 -0.111920954 -0.112415387
#> V12 0.0094448915 0.009394824 0.009349183 0.009307596 0.009287879
#> V13 0.0136427887 0.013949654 0.014229274 0.014484053 0.014716175
#> V14 -0.0842322857 -0.084931965 -0.085569489 -0.086150375 -0.086671988
#> V15 0.0316249336 0.032498540 0.033294512 0.034019771 0.034669922
#> V16 0.0049413371 0.005112982 0.005269409 0.005411942 0.005549187
#> V17 -0.0835811857 -0.084200221 -0.084764324 -0.085278316 -0.085741537
#> V18 -0.0205566667 -0.021034047 -0.021469020 -0.021865350 -0.022232277
#> V19 0.1187320119 0.119175352 0.119579312 0.119947386 0.120292294
#> V20 0.1029741608 0.103375918 0.103741987 0.104075536 0.104387017
#>
#> (Intercept) -0.027919642 -0.027945428 -0.027968937 -0.027990358 -0.028009876
#> V1 0.002098329 0.002343729 0.002567321 0.002771049 0.002956679
#> V2 0.078096964 0.078632694 0.079120770 0.079565479 0.079970679
#> V3 0.203871775 0.204218387 0.204534181 0.204821915 0.205084088
#> V4 0.015255755 0.015415714 0.015561491 0.015694313 0.015815336
#> V5 0.130514814 0.130721792 0.130910328 0.131082111 0.131238633
#> V6 -0.077575584 -0.078070542 -0.078521491 -0.078932376 -0.079306759
#> V7 0.018888049 0.019363640 0.019796965 0.020191793 0.020551547
#> V8 0.189676197 0.189815731 0.189943005 0.190058984 0.190164660
#> V9 -0.140624095 -0.141116447 -0.141565059 -0.141973812 -0.142346253
#> V10 0.028538362 0.029039563 0.029496267 0.029912401 0.030291566
#> V11 -0.112885045 -0.113313566 -0.113704004 -0.114059754 -0.114383900
#> V12 0.009252477 0.009219504 0.009189439 0.009162043 0.009137081
#> V13 0.014927513 0.015120256 0.015295883 0.015455907 0.015601716
#> V14 -0.087155055 -0.087595130 -0.087996069 -0.088361385 -0.088694247
#> V15 0.035272581 0.035822117 0.036322833 0.036779064 0.037194765
#> V16 0.005666892 0.005774066 0.005871759 0.005960780 0.006041894
#> V17 -0.086168275 -0.086557488 -0.086912174 -0.087235356 -0.087529829
#> V18 -0.022561608 -0.022860911 -0.023133590 -0.023382045 -0.023608428
#> V19 0.120597674 0.120875303 0.121128245 0.121358716 0.121568712
#> V20 0.104663854 0.104915535 0.105144830 0.105353753 0.105544117
#>
#> (Intercept) -0.028027660 -0.028033796 -0.028047558 -0.028061656 -0.028074777
#> V1 0.003125817 0.003272687 0.003412824 0.003541148 0.003658261
#> V2 0.080339883 0.080603748 0.080907921 0.081192492 0.081452879
#> V3 0.205322969 0.205522394 0.205718543 0.205900335 0.206066561
#> V4 0.015925607 0.015989621 0.016075476 0.016160919 0.016240168
#> V5 0.131381249 0.131517647 0.131636818 0.131744221 0.131841914
#> V6 -0.079647883 -0.079928182 -0.080210376 -0.080470845 -0.080708531
#> V7 0.020879341 0.021147981 0.021420317 0.021670951 0.021899369
#> V8 0.190260948 0.190351330 0.190429498 0.190502185 0.190568579
#> V9 -0.142685607 -0.142970574 -0.143249112 -0.143507198 -0.143743213
#> V10 0.030637048 0.030926648 0.031213137 0.031476533 0.031716754
#> V11 -0.114679250 -0.114922989 -0.115166050 -0.115391300 -0.115597083
#> V12 0.009114336 0.009124010 0.009106381 0.009086646 0.009068352
#> V13 0.015734570 0.015864247 0.015973896 0.016073610 0.016164522
#> V14 -0.088997538 -0.089256951 -0.089508102 -0.089738759 -0.089949195
#> V15 0.037573536 0.037893933 0.038206464 0.038494620 0.038757772
#> V16 0.006115801 0.006199721 0.006263510 0.006318505 0.006368008
#> V17 -0.087798141 -0.088040136 -0.088264511 -0.088468129 -0.088653269
#> V18 -0.023814700 -0.023999093 -0.024173083 -0.024329841 -0.024472155
#> V19 0.121760053 0.121945782 0.122106690 0.122250800 0.122381642
#> V20 0.105717569 0.105884402 0.106029988 0.106160715 0.106279465
#>
#> (Intercept) -0.028086782 -0.028097731 -0.028107710 -0.028116802 -0.028125087
#> V1 0.003765040 0.003862356 0.003951034 0.004031836 0.004105460
#> V2 0.081690342 0.081906758 0.082103961 0.082283648 0.082447374
#> V3 0.206218131 0.206356260 0.206482123 0.206596806 0.206701302
#> V4 0.016312635 0.016378714 0.016438933 0.016493804 0.016543802
#> V5 0.131930925 0.132012035 0.132085943 0.132153287 0.132214648
#> V6 -0.080925127 -0.081122481 -0.081302301 -0.081466146 -0.081615436
#> V7 0.022107448 0.022297026 0.022469758 0.022627143 0.022770547
#> V8 0.190629032 0.190684083 0.190734232 0.190779923 0.190821553
#> V9 -0.143958422 -0.144154545 -0.144333253 -0.144496087 -0.144644455
#> V10 0.031935639 0.032135071 0.032316782 0.032482350 0.032633208
#> V11 -0.115784649 -0.115955559 -0.116111286 -0.116253179 -0.116382467
#> V12 0.009051692 0.009036526 0.009022712 0.009010127 0.008998660
#> V13 0.016247375 0.016322870 0.016391659 0.016454338 0.016511448
#> V14 -0.090140985 -0.090315747 -0.090474988 -0.090620083 -0.090752288
#> V15 0.038997658 0.039216258 0.039415443 0.039596934 0.039762303
#> V16 0.006412986 0.006453938 0.006491244 0.006525233 0.006556202
#> V17 -0.088821833 -0.088975384 -0.089115282 -0.089242748 -0.089358890
#> V18 -0.024601721 -0.024719756 -0.024827302 -0.024925292 -0.025014578
#> V19 0.122500781 0.122609322 0.122708219 0.122798330 0.122880435
#> V20 0.106387604 0.106486125 0.106575892 0.106657684 0.106732210
#>
#> (Intercept) -0.028132636 -0.028139514 -0.028145782 -0.028151492
#> V1 0.004172544 0.004233669 0.004289363 0.004340109
#> V2 0.082596555 0.082732483 0.082856336 0.082969186
#> V3 0.206796514 0.206883268 0.206962316 0.207034341
#> V4 0.016589358 0.016630867 0.016668689 0.016703150
#> V5 0.132270558 0.132321502 0.132367919 0.132410213
#> V6 -0.081751463 -0.081875406 -0.081988338 -0.082091238
#> V7 0.022901211 0.023020267 0.023128747 0.023227589
#> V8 0.190859485 0.190894047 0.190925538 0.190954232
#> V9 -0.144779643 -0.144902821 -0.145015057 -0.145117322
#> V10 0.032770665 0.032895910 0.033010029 0.033114009
#> V11 -0.116500270 -0.116607607 -0.116705408 -0.116794521
#> V12 0.008988212 0.008978692 0.008970018 0.008962115
#> V13 0.016563485 0.016610899 0.016654101 0.016693465
#> V14 -0.090872749 -0.090982508 -0.091082517 -0.091173641
#> V15 0.039912981 0.040050274 0.040175369 0.040289352
#> V16 0.006584420 0.006610131 0.006633558 0.006654903
#> V17 -0.089464714 -0.089561137 -0.089648994 -0.089729045
#> V18 -0.025095931 -0.025170057 -0.025237598 -0.025299139
#> V19 0.122955246 0.123023411 0.123085521 0.123142112
#> V20 0.106800116 0.106861988 0.106918364 0.106969732
fit2=glmnet(x,g2,family="binomial")
predict(fit2,type="response",newx=x[2:5,])
#> s0 s1 s2 s3 s4 s5 s6 s7
#> [1,] 0.5 0.4997320 0.4944013 0.4901133 0.4850156 0.4862938 0.4912072 0.4951752
#> [2,] 0.5 0.5022189 0.5093657 0.5167082 0.5191784 0.5240946 0.5308489 0.5352418
#> [3,] 0.5 0.4855880 0.4622509 0.4404211 0.4190582 0.3966423 0.3753429 0.3557118
#> [4,] 0.5 0.5150400 0.5131165 0.5107642 0.5108066 0.5075230 0.5001201 0.4923919
#> s8 s9 s10 s11 s12 s13 s14
#> [1,] 0.4963003 0.4943477 0.4950097 0.4958120 0.4965805 0.4990908 0.5021824
#> [2,] 0.5424932 0.5534539 0.5596466 0.5656082 0.5710834 0.5774794 0.5866271
#> [3,] 0.3410245 0.3318351 0.3270027 0.3228576 0.3190153 0.3152403 0.3116100
#> [4,] 0.4829993 0.4729389 0.4616839 0.4514817 0.4420999 0.4347643 0.4278313
#> s15 s16 s17 s18 s19 s20 s21
#> [1,] 0.5037256 0.5054235 0.5067073 0.5085367 0.5094248 0.5102760 0.5125359
#> [2,] 0.5939000 0.6004953 0.6061470 0.6107258 0.6153713 0.6197131 0.6222252
#> [3,] 0.3055225 0.2998230 0.2937421 0.2881091 0.2834319 0.2791381 0.2757489
#> [4,] 0.4203634 0.4134605 0.4069652 0.4024117 0.3968665 0.3917373 0.3858941
#> s22 s23 s24 s25 s26 s27 s28
#> [1,] 0.5148578 0.5170217 0.5190311 0.5208943 0.5226199 0.5242162 0.5256910
#> [2,] 0.6243662 0.6263988 0.6283187 0.6301266 0.6318247 0.6334155 0.6349024
#> [3,] 0.2726832 0.2698551 0.2672395 0.2648228 0.2625919 0.2605346 0.2586391
#> [4,] 0.3803525 0.3752679 0.3706018 0.3663224 0.3623998 0.3588063 0.3555158
#> s29 s30 s31 s32 s33 s34 s35
#> [1,] 0.5270522 0.5283073 0.5294634 0.5305225 0.5315012 0.5324006 0.5332264
#> [2,] 0.6362891 0.6375798 0.6387790 0.6398826 0.6409131 0.6418663 0.6427466
#> [3,] 0.2568940 0.2552888 0.2538134 0.2524472 0.2512039 0.2500634 0.2490178
#> [4,] 0.3525041 0.3497487 0.3472288 0.3449248 0.3428194 0.3408958 0.3391385
#> s36 s37 s38 s39 s40 s41 s42
#> [1,] 0.5339842 0.5346792 0.5353162 0.5358960 0.5364306 0.5369201 0.5373679
#> [2,] 0.6435588 0.6443072 0.6449962 0.6456163 0.6461993 0.6467352 0.6472270
#> [3,] 0.2480596 0.2471819 0.2463784 0.2456298 0.2449578 0.2443436 0.2437821
#> [4,] 0.3375337 0.3360684 0.3347307 0.3335132 0.3323987 0.3313819 0.3304541
#> s43 s44 s45 s46 s47 s48 s49
#> [1,] 0.5377774 0.5381519 0.5384942 0.5388053 0.5390911 0.5393522 0.5395907
#> [2,] 0.6476780 0.6480914 0.6484702 0.6487991 0.6491166 0.6494085 0.6496755
#> [3,] 0.2432689 0.2428000 0.2423717 0.2419644 0.2416076 0.2412828 0.2409864
#> [4,] 0.3296078 0.3288358 0.3281317 0.3275003 0.3269140 0.3263793 0.3258917
#> s50 s51
#> [1,] 0.5398084 0.5400072
#> [2,] 0.6499196 0.6501428
#> [3,] 0.2407159 0.2404690
#> [4,] 0.3254471 0.3250418
predict(fit2,type="nonzero")
#> $s0
#> NULL
#>
#> $s1
#> [1] 2 15
#>
#> $s2
#> [1] 2 10 15
#>
#> $s3
#> [1] 2 10 15
#>
#> $s4
#> [1] 2 10 12 14 15
#>
#> $s5
#> [1] 2 10 12 14 15 18
#>
#> $s6
#> [1] 2 10 12 14 15 18
#>
#> $s7
#> [1] 2 8 10 12 14 15 18
#>
#> $s8
#> [1] 2 5 8 10 12 14 15 18
#>
#> $s9
#> [1] 2 5 7 8 10 12 14 15 18 19
#>
#> $s10
#> [1] 2 5 7 8 9 10 12 13 14 15 18 19
#>
#> $s11
#> [1] 2 5 7 8 9 10 12 13 14 15 18 19
#>
#> $s12
#> [1] 2 5 7 8 9 10 12 13 14 15 18 19
#>
#> $s13
#> [1] 2 5 7 8 9 10 11 12 13 14 15 16 18 19
#>
#> $s14
#> [1] 2 5 7 8 9 10 11 12 13 14 15 16 17 18 19
#>
#> $s15
#> [1] 2 5 7 8 9 10 11 12 13 14 15 16 17 18 19
#>
#> $s16
#> [1] 2 4 5 7 8 9 10 11 12 13 14 15 16 17 18 19
#>
#> $s17
#> [1] 2 4 5 7 8 9 10 11 12 13 14 15 16 17 18 19 20
#>
#> $s18
#> [1] 1 2 3 4 5 7 8 9 10 11 12 13 14 15 16 17 18 19 20
#>
#> $s19
#> [1] 1 2 3 4 5 7 8 9 10 11 12 13 14 15 16 17 18 19 20
#>
#> $s20
#> [1] 1 2 3 4 5 7 8 9 10 11 12 13 14 15 16 17 18 19 20
#>
#> $s21
#> [1] 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20
#>
#> $s22
#> [1] 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20
#>
#> $s23
#> [1] 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20
#>
#> $s24
#> [1] 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20
#>
#> $s25
#> [1] 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20
#>
#> $s26
#> [1] 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20
#>
#> $s27
#> [1] 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20
#>
#> $s28
#> [1] 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20
#>
#> $s29
#> [1] 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20
#>
#> $s30
#> [1] 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20
#>
#> $s31
#> [1] 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20
#>
#> $s32
#> [1] 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20
#>
#> $s33
#> [1] 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20
#>
#> $s34
#> [1] 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20
#>
#> $s35
#> [1] 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20
#>
#> $s36
#> [1] 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20
#>
#> $s37
#> [1] 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20
#>
#> $s38
#> [1] 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20
#>
#> $s39
#> [1] 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20
#>
#> $s40
#> [1] 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20
#>
#> $s41
#> [1] 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20
#>
#> $s42
#> [1] 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20
#>
#> $s43
#> [1] 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20
#>
#> $s44
#> [1] 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20
#>
#> $s45
#> [1] 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20
#>
#> $s46
#> [1] 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20
#>
#> $s47
#> [1] 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20
#>
#> $s48
#> [1] 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20
#>
#> $s49
#> [1] 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20
#>
#> $s50
#> [1] 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20
#>
#> $s51
#> [1] 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20
#>
fit3=glmnet(x,g4,family="multinomial")
predict(fit3,newx=x[1:3,],type="response",s=0.01)
#> , , s=0.01
#>
#> 1 2 3 4
#> [1,] 0.3874077 0.12590332 0.4176651 0.06902391
#> [2,] 0.4197499 0.06082136 0.1656653 0.35376341
#> [3,] 0.3174607 0.08102948 0.5622602 0.03924958
#>