File: test.glmnet.Rout.save

package info (click to toggle)
r-cran-plotmo 3.7.0-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 3,400 kB
  • sloc: sh: 13; makefile: 2
file content (1305 lines) | stat: -rw-r--r-- 47,939 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
> # test.glmnet.R: glmnet tests for plotmo and plotres
> 
> source("test.prolog.R")
> library(earth)
Loading required package: Formula
Loading required package: plotmo
Loading required package: plotrix
> library(glmnet)
Loading required package: Matrix
Loaded glmnet 4.1-10
> data(ozone1)
> data(etitanic)
> get.tit <- function() # abbreviated titanic data
+ {
+     tit <- etitanic
+     pclass <- as.character(tit$pclass)
+     # change the order of the factors so not alphabetical
+     pclass[pclass == "1st"] <- "first"
+     pclass[pclass == "2nd"] <- "class2"
+     pclass[pclass == "3rd"] <- "classthird"
+     tit$pclass <- factor(pclass, levels=c("class2", "classthird", "first"))
+     # log age is so we have a continuous predictor even when model is age~.
+     set.seed(2015)
+     tit$logage <- log(tit$age) + rnorm(nrow(tit))
+     tit$parch <- NULL
+     # by=12 gives us a small fast model with an additive and a interaction term
+     tit <- tit[seq(1, nrow(etitanic), by=12), ]
+ }
> plotmo1 <- function(object, ..., trace=0, SHOWCALL=TRUE, caption=NULL) {
+     if(is.null(caption))
+         caption <- paste(deparse(substitute(object)), collapse=" ")
+     call <- match.call(expand.dots=TRUE)
+     call <- strip.space(paste(deparse(substitute(call)), collapse=" "))
+     printf("%s\n", call)
+     plotmo(object, trace=trace, SHOWCALL=SHOWCALL, caption=caption, ...)
+ }
> plotres1 <- function(object, ..., trace=0, SHOWCALL=TRUE, caption=NULL) {
+     if(is.null(caption))
+         caption <- paste(deparse(substitute(object)), collapse=" ")
+     call <- match.call(expand.dots=TRUE)
+     call <- strip.space(paste(deparse(substitute(call)), collapse=" "))
+     printf("%s\n", call)
+     plotres(object, trace=trace, SHOWCALL=SHOWCALL, caption=caption, ...)
+ }
> tit <- get.tit()
> set.seed(2015)
> xmat <- as.matrix(tit[,c(2,5,6)])
> set.seed(2015)
> mod.glmnet.xmat <- glmnet(xmat, tit[,4])
> # plotmo on glmnet mods is boring but we test it anyway
> plotmo1(mod.glmnet.xmat)
plotmo1(object=mod.glmnet.xmat)
 plotmo grid:    survived sibsp  logage
                        0     0 3.06991
> plotres1(mod.glmnet.xmat)
plotres1(object=mod.glmnet.xmat)
> 
> # compare to plot.glmnet
> par(mfrow=c(4,2), mar=c(3,6,3.5,6)) # extra side margins for more square plots
> plot_glmnet(mod.glmnet.xmat, main="mod.glmnet.xmat\ncompare to plot.glmnet")
> plot(0,0)
> plot_glmnet(mod.glmnet.xmat, xvar="norm", col=c(3,2,1))
> plot(mod.glmnet.xmat, xvar="norm")
> plot_glmnet(mod.glmnet.xmat, xvar="lambda")
> plot(mod.glmnet.xmat, xvar="lambda")
> plot_glmnet(mod.glmnet.xmat, xvar="dev")
> plot(mod.glmnet.xmat, xvar="dev")
> par(org.par)
> 
> set.seed(2015)
> mod.cv.glmnet.xmat <- cv.glmnet(xmat, tit[,4], nfolds=3)
> 
> # following was needed before plotmo 3.1.3 (before adding plotmo.prolog.cv.glmnet)
> # mod.cv.glmnet.xmat$x <- as.data.frame(xmat)
> # mod.cv.glmnet.xmat$y <- tit[,4]
> 
> cat("==Test plotmo trace=1 and lambda.min\n")
==Test plotmo trace=1 and lambda.min
> plotmo1(mod.cv.glmnet.xmat, predict.s="lambda.min", trace=1)
plotmo1(object=mod.cv.glmnet.xmat,predict.s="lambda.min",trace=1)
stats::predict(cv.glmnet.object, matrix[3,3], type="response", s="lambda.min")
stats::fitted(object=cv.glmnet.object)
fitted() was unsuccessful, will use predict() instead
got model response from getCall(object)$y

 plotmo grid:    survived sibsp  logage
                        0     0 3.06991

> cat("==Test plotmo trace=2 and lambda.min\n")
==Test plotmo trace=2 and lambda.min
> plotmo1(mod.cv.glmnet.xmat, predict.s="lambda.min", trace=2)
plotmo1(object=mod.cv.glmnet.xmat,predict.s="lambda.min",trace=2)
plotmo trace 2: plotmo(object, trace=trace, SHOWCALL=SHOWCALL, caption=caption,
                       ...)
--get.model.env for object with class cv.glmnet
object call is cv.glmnet(x=xmat, y=tit[, 4], nfolds=3)
assuming the environment of the cv.glmnet model is that of plotmo's caller: env(..., call, caption, object, SHOWCALL, trace)
--plotmo_prolog for cv.glmnet object 'object'
--plotmo_x for cv.glmnet object

get.object.x:
object$x is NULL (and it has no colnames)

object call is cv.glmnet(x=xmat, y=tit[, 4], nfolds=3)

get.x.from.model.frame:
terms(object) did not return the terms, will look for the formula elsewhere
no formula in getCall(object)

get.data.from.object.call.field:
eval(getCall(object)$x, env(..., call, caption, object, SHOWCALL, trace))
getCall(object)$x is usable and has column names survived sibsp logage
plotmo_x returned[88,3]:
     survived sibsp   logage
1           1     0 1.821847
13          1     0 3.814495
26          0     0 3.094392
...         0     0 5.950193
1308        0     0 5.116245

----Metadata: plotmo_predict with nresponse=NULL and newdata=NULL
plotmo_predict with NULL newdata (nrows=3), using plotmo_x to get the data
--plotmo_x for cv.glmnet object

get.object.x:
object$x is NULL (and it has no colnames)

object call is cv.glmnet(x=xmat, y=tit[, 4], nfolds=3)

get.x.from.model.frame:
terms(object) did not return the terms, will look for the formula elsewhere
no formula in getCall(object)

get.data.from.object.call.field:
eval(getCall(object)$x, env(..., call, caption, object, SHOWCALL, trace))
getCall(object)$x is usable and has column names survived sibsp logage
plotmo_x returned[88,3]:
     survived sibsp   logage
1           1     0 1.821847
13          1     0 3.814495
26          0     0 3.094392
...         0     0 5.950193
1308        0     0 5.116245
will use the above data instead of newdata=NULL for predict.cv.glmnet
stats::predict(cv.glmnet.object, matrix[3,3], type="response", s="lambda.min")
predict returned[3,1]:
   s="lambda.min"
1        25.64083
13       34.58457
26       31.45755
predict after processing with nresponse=NULL is [3,1]:
   s="lambda.min"
1        25.64083
13       34.58457
26       31.45755

----Metadata: plotmo_fitted with nresponse=NULL
stats::fitted(object=cv.glmnet.object)
fitted() was unsuccessful, will use predict() instead
plotmo_predict with NULL newdata, using plotmo_x to get the data
--plotmo_x for cv.glmnet object

get.object.x:
object$x is NULL (and it has no colnames)

object call is cv.glmnet(x=xmat, y=tit[, 4], nfolds=3)

get.x.from.model.frame:
terms(object) did not return the terms, will look for the formula elsewhere
no formula in getCall(object)

get.data.from.object.call.field:
eval(getCall(object)$x, env(..., call, caption, object, SHOWCALL, trace))
getCall(object)$x is usable and has column names survived sibsp logage
plotmo_x returned[88,3]:
     survived sibsp   logage
1           1     0 1.821847
13          1     0 3.814495
26          0     0 3.094392
...         0     0 5.950193
1308        0     0 5.116245
will use the above data instead of newdata=NULL for predict.cv.glmnet
stats::predict(cv.glmnet.object, matrix[88,3], type="response", s="lambda.min")
predict returned[88,1]:
     s="lambda.min"
1          25.64083
13         34.58457
26         31.45755
...        44.27544
1308       40.53237
predict after processing with nresponse=NULL is [88,1]:
     s="lambda.min"
1          25.64083
13         34.58457
26         31.45755
...        44.27544
1308       40.53237
got fitted values by calling predict (see above)

----Metadata: plotmo_y with nresponse=NULL
--plotmo_y with nresponse=NULL for cv.glmnet object

get.object.y:
object$y is NULL (and it has no colnames)

object call is cv.glmnet(x=xmat, y=tit[, 4], nfolds=3)

get.y.from.model.frame:
terms(object) did not return the terms, will look for the formula elsewhere
no formula in getCall(object)

get.data.from.object.call.field:
eval(getCall(object)$y, env(..., call, caption, object, SHOWCALL, trace))
getCall(object)$y is usable but without colnames so we will keep on searching

names(call) is "" "x" "y" "nfolds"
the name of argument 2 is "y" so we will not process it with argn
object$y is NULL
call$y is usable but without colnames but we will use it anyway
plotmo_y returned[88,1] with no column names:
      
1   29
2   24
3   25
... 41
88  27
plotmo_y after processing with nresponse=NULL is [88,1] with no column names:
      
1   29
2   24
3   25
... 41
88  27
converted nresponse=NA to nresponse=1
nresponse=1 (was NA) ncol(fitted) 1 ncol(predict) 1 ncol(y) 1

----Metadata: plotmo_y with nresponse=1
--plotmo_y with nresponse=1 for cv.glmnet object

get.object.y:
object$y is NULL (and it has no colnames)

object call is cv.glmnet(x=xmat, y=tit[, 4], nfolds=3)

get.y.from.model.frame:
terms(object) did not return the terms, will look for the formula elsewhere
no formula in getCall(object)

get.data.from.object.call.field:
eval(getCall(object)$y, env(..., call, caption, object, SHOWCALL, trace))
getCall(object)$y is usable but without colnames so we will keep on searching

names(call) is "" "x" "y" "nfolds"
the name of argument 2 is "y" so we will not process it with argn
object$y is NULL
call$y is usable but without colnames but we will use it anyway
got model response from getCall(object)$y
plotmo_y returned[88,1] with no column names:
      
1   29
2   24
3   25
... 41
88  27
plotmo_y after processing with nresponse=1 is [88,1]:
    plotmo_y
1         29
2         24
3         25
...       41
88        27
got response name "s="lambda.min"" from yhat
resp.levs is NULL

----Metadata: done

number of x values: survived 2 sibsp 5 logage 88

----plotmo_singles for cv.glmnet object
singles: 1 survived, 2 sibsp, 3 logage 

----plotmo_pairs for cv.glmnet object
Error in attr(x, "formula") %||% { : invalid formula
formula(object) failed for cv.glmnet object in plotmo.pairs.default
Error in x$terms %||% attr(x, "terms") %||% stop("no terms component nor attribute") : 
  no terms component nor attribute
terms(object) failed for cv.glmnet object in plotmo.pairs.default
no pairs

graphics::par(mfrow=c(2,2), mgp=c(1.5,0.4,0), tcl=-0.3, font.main=2,
              mar=c(3,2,1.2,0.8), oma=c(0,0,4,0), cex.main=1.2, cex.lab=1,
              cex.axis=1, cex=0.83)

----Figuring out ylim
--get.ylim.by.dummy.plots
--plot.degree1(draw.plot=FALSE)
degree1 plot1 (pmethod "plotmo") variable survived
newdata[2,3]:
  survived sibsp  logage
1        0     0 3.06991
2        1     0 3.06991
stats::predict(cv.glmnet.object, matrix[2,3], type="response", s="lambda.min")
predict returned[2,1]:
  s="lambda.min"
1       31.34766
2       31.24259
predict after processing with nresponse=1 is [2,1]:
  s="lambda.min"
1       31.34766
2       31.24259
Reducing trace level for subsequent degree1 plots
degree1 plot2 (pmethod "plotmo") variable sibsp
degree1 plot3 (pmethod "plotmo") variable logage
--done get.ylim.by.dummy.plots

ylim c(4.856, 44.28)    clip TRUE

--plot.degree1(draw.plot=TRUE)

 plotmo grid:    survived sibsp  logage
                        0     0 3.06991

graphics::plot.default(x=c(0,0.5,0.5,1), y=c(31.35,31.35,3...), type="n",
                       main="1 survived", xlab="", ylab="", xaxt="s", yaxt="s",
                       xlim=c(-0.1,1.1), ylim=c(4.86,44.28))
> cat("==Test plotres trace=1 and lambda.1se\n")
==Test plotres trace=1 and lambda.1se
> plotres1(mod.cv.glmnet.xmat, predict.s="lambda.1se", trace=1)
plotres1(object=mod.cv.glmnet.xmat,predict.s="lambda.1se",trace=1)
stats::residuals(object=cv.glmnet.object, type="response")
residuals() was unsuccessful, will use predict() instead
stats::predict(cv.glmnet.object, matrix[3,3], type="response", s="lambda.1se")
stats::fitted(object=cv.glmnet.object)
fitted() was unsuccessful, will use predict() instead
got model response from getCall(object)$y
graphics::plot(cv.glmnet.object)

training rsq 0.24
> cat("==Test plotres trace=2 and lambda.1se\n")
==Test plotres trace=2 and lambda.1se
> plotres1(mod.cv.glmnet.xmat, predict.s="lambda.1se", trace=2)
plotres1(object=mod.cv.glmnet.xmat,predict.s="lambda.1se",trace=2)
plotres trace 2: plotres(object, trace=trace, SHOWCALL=SHOWCALL,
                         caption=caption, ...)
--get.model.env for object with class cv.glmnet
object call is cv.glmnet(x=xmat, y=tit[, 4], nfolds=3)
assuming the environment of the cv.glmnet model is that of plotres's caller: env(..., call, caption, object, SHOWCALL, trace)
--plotmo_prolog for cv.glmnet object 'object'

----Metadata: plotmo_resids(object, type="response", nresponse=NULL)
doTryCatch invoked call.dots
TRACE plotmo_resids via try called call.dots(residuals, DROP="*",
                         KEEP="PREFIX", TRACE=if(trace==0)-1elsetr...),
                         force.object=object, force.type=residtype,
                         SHOWCALL=TRUE, predict.s="lambda.1se")
PREFIX residuals.
DROP .*
KEEP >STANDARDPREFIXES|^force\.|^def\.|^drop\.
     >PREFIX|^residuals\.
     >CALLARGS|^force\.object$|^force\.type$
     >EXPLICIT
input dotnames       force.object force.type SHOWCALL predict.s
after DROP and KEEP  force.object force.type
return dotnames      object type

stats::residuals(object=cv.glmnet.object, type="response")
residuals() was unsuccessful, will use predict() instead

----Metadata: plotmo_predict with nresponse=NULL and newdata=NULL
plotmo_predict with NULL newdata (nrows=3), using plotmo_x to get the data
--plotmo_x for cv.glmnet object

get.object.x:
object$x is NULL (and it has no colnames)

object call is cv.glmnet(x=xmat, y=tit[, 4], nfolds=3)

get.x.from.model.frame:
terms(object) did not return the terms, will look for the formula elsewhere
no formula in getCall(object)

get.data.from.object.call.field:
eval(getCall(object)$x, env(..., call, caption, object, SHOWCALL, trace))
getCall(object)$x is usable and has column names survived sibsp logage
plotmo_x returned[88,3]:
     survived sibsp   logage
1           1     0 1.821847
13          1     0 3.814495
26          0     0 3.094392
...         0     0 5.950193
1308        0     0 5.116245
will use the above data instead of newdata=NULL for predict.cv.glmnet
stats::predict(cv.glmnet.object, matrix[3,3], type="response", s="lambda.1se")
predict returned[3,1]:
   s="lambda.1se"
1        26.57900
13       33.26779
26       30.85060
predict after processing with nresponse=NULL is [3,1]:
   s="lambda.1se"
1        26.57900
13       33.26779
26       30.85060

----Metadata: plotmo_fitted with nresponse=NULL
stats::fitted(object=cv.glmnet.object)
fitted() was unsuccessful, will use predict() instead
plotmo_predict with NULL newdata, using plotmo_x to get the data
--plotmo_x for cv.glmnet object

get.object.x:
object$x is NULL (and it has no colnames)

object call is cv.glmnet(x=xmat, y=tit[, 4], nfolds=3)

get.x.from.model.frame:
terms(object) did not return the terms, will look for the formula elsewhere
no formula in getCall(object)

get.data.from.object.call.field:
eval(getCall(object)$x, env(..., call, caption, object, SHOWCALL, trace))
getCall(object)$x is usable and has column names survived sibsp logage
plotmo_x returned[88,3]:
     survived sibsp   logage
1           1     0 1.821847
13          1     0 3.814495
26          0     0 3.094392
...         0     0 5.950193
1308        0     0 5.116245
will use the above data instead of newdata=NULL for predict.cv.glmnet
stats::predict(cv.glmnet.object, matrix[88,3], type="response", s="lambda.1se")
predict returned[88,1]:
     s="lambda.1se"
1          26.57900
13         33.26779
26         30.85060
...        40.43678
1308       37.63743
predict after processing with nresponse=NULL is [88,1]:
     s="lambda.1se"
1          26.57900
13         33.26779
26         30.85060
...        40.43678
1308       37.63743
got fitted values by calling predict (see above)

----Metadata: plotmo_y with nresponse=NULL
--plotmo_y with nresponse=NULL for cv.glmnet object

get.object.y:
object$y is NULL (and it has no colnames)

object call is cv.glmnet(x=xmat, y=tit[, 4], nfolds=3)

get.y.from.model.frame:
terms(object) did not return the terms, will look for the formula elsewhere
no formula in getCall(object)

get.data.from.object.call.field:
eval(getCall(object)$y, env(..., call, caption, object, SHOWCALL, trace))
getCall(object)$y is usable but without colnames so we will keep on searching

names(call) is "" "x" "y" "nfolds"
the name of argument 2 is "y" so we will not process it with argn
object$y is NULL
call$y is usable but without colnames but we will use it anyway
plotmo_y returned[88,1] with no column names:
      
1   29
2   24
3   25
... 41
88  27
plotmo_y after processing with nresponse=NULL is [88,1] with no column names:
      
1   29
2   24
3   25
... 41
88  27
converted nresponse=NA to nresponse=1
nresponse=1 (was NA) ncol(fitted) 1 ncol(predict) 1 ncol(y) 1

----Metadata: plotmo_y with nresponse=1
--plotmo_y with nresponse=1 for cv.glmnet object

get.object.y:
object$y is NULL (and it has no colnames)

object call is cv.glmnet(x=xmat, y=tit[, 4], nfolds=3)

get.y.from.model.frame:
terms(object) did not return the terms, will look for the formula elsewhere
no formula in getCall(object)

get.data.from.object.call.field:
eval(getCall(object)$y, env(..., call, caption, object, SHOWCALL, trace))
getCall(object)$y is usable but without colnames so we will keep on searching

names(call) is "" "x" "y" "nfolds"
the name of argument 2 is "y" so we will not process it with argn
object$y is NULL
call$y is usable but without colnames but we will use it anyway
got model response from getCall(object)$y
plotmo_y returned[88,1] with no column names:
      
1   29
2   24
3   25
... 41
88  27
plotmo_y after processing with nresponse=1 is [88,1]:
    plotmo_y
1         29
2         24
3         25
...       41
88        27
got response name "s="lambda.1se"" from yhat
resp.levs is NULL

----Metadata: done

--plotmo_response for plotmo_rsq1
--plotmo_response for newdata: NULL
plotmo_response trace 2: plotmo_response(object=object, newdata=newdata,
                         trace=max(0,trace), nresponse=meta$nresponse,
                         type=meta$type, meta=meta, ...)
--get.model.env for object with class cv.glmnet
object call is cv.glmnet(x=xmat, y=tit[, 4], nfolds=3)
using attr(obj,".Environment") saved with cv.glmnet model: env(..., call, caption, object, SHOWCALL, trace)
--plotmo_y with nresponse=1 for cv.glmnet object

get.object.y:
object$y is NULL (and it has no colnames)

object call is cv.glmnet(x=xmat, y=tit[, 4], nfolds=3)

get.y.from.model.frame:
terms(object) did not return the terms, will look for the formula elsewhere
no formula in getCall(object)

get.data.from.object.call.field:
eval(getCall(object)$y, env(..., call, caption, object, SHOWCALL, trace))
getCall(object)$y is usable but without colnames so we will keep on searching

names(call) is "" "x" "y" "nfolds"
the name of argument 2 is "y" so we will not process it with argn
object$y is NULL
call$y is usable but without colnames but we will use it anyway
plotmo_y returned[88,1] with no column names:
      
1   29
2   24
3   25
... 41
88  27
plotmo_y after processing with nresponse=1 is [88,1]:
    plotmo_y
1         29
2         24
3         25
...       41
88        27
response is usable and has column name plotmo_y
plotmo_response returned[88,1]:
    plotmo_y
1         29
2         24
3         25
...       41
88        27
plotmo_response after processing with nresponse=1 is [88,1]:
    plotmo_y
1         29
2         24
3         25
...       41
88        27
--plotmo_predict for plotmo_rsq1
plotmo_predict with NULL newdata, using plotmo_x to get the data
--plotmo_x for cv.glmnet object

get.object.x:
object$x is NULL (and it has no colnames)

object call is cv.glmnet(x=xmat, y=tit[, 4], nfolds=3)

get.x.from.model.frame:
terms(object) did not return the terms, will look for the formula elsewhere
no formula in getCall(object)

get.data.from.object.call.field:
eval(getCall(object)$x, env(..., call, caption, object, SHOWCALL, trace))
getCall(object)$x is usable and has column names survived sibsp logage
plotmo_x returned[88,3]:
     survived sibsp   logage
1           1     0 1.821847
13          1     0 3.814495
26          0     0 3.094392
...         0     0 5.950193
1308        0     0 5.116245
will use the above data instead of newdata=NULL for predict.cv.glmnet
stats::predict(cv.glmnet.object, matrix[88,3], type="response", s="lambda.1se")
predict returned[88,1]:
     s="lambda.1se"
1          26.57900
13         33.26779
26         30.85060
...        40.43678
1308       37.63743
predict after processing with nresponse=1 is [88,1]:
     s="lambda.1se"
1          26.57900
13         33.26779
26         30.85060
...        40.43678
1308       37.63743
----plotmo_rinfo: plotmo_resids(object, type="response", nresponse=1)
doTryCatch invoked call.dots
TRACE plotmo_resids via try called call.dots(residuals, DROP="*",
                         KEEP="PREFIX", TRACE=if(trace==0)-1elsetr...),
                         force.object=object, force.type=residtype,
                         SHOWCALL=TRUE, predict.s="lambda.1se")
PREFIX residuals.
DROP .*
KEEP >STANDARDPREFIXES|^force\.|^def\.|^drop\.
     >PREFIX|^residuals\.
     >CALLARGS|^force\.object$|^force\.type$
     >EXPLICIT
input dotnames       force.object force.type SHOWCALL predict.s
after DROP and KEEP  force.object force.type
return dotnames      object type

stats::residuals(object=cv.glmnet.object, type="response")
calling predict() because residuals() was unsuccessful
plotmo_predict with NULL newdata, using plotmo_x to get the data
--plotmo_x for cv.glmnet object

get.object.x:
object$x is NULL (and it has no colnames)

object call is cv.glmnet(x=xmat, y=tit[, 4], nfolds=3)

get.x.from.model.frame:
terms(object) did not return the terms, will look for the formula elsewhere
no formula in getCall(object)

get.data.from.object.call.field:
eval(getCall(object)$x, env(..., call, caption, object, SHOWCALL, trace))
getCall(object)$x is usable and has column names survived sibsp logage
plotmo_x returned[88,3]:
     survived sibsp   logage
1           1     0 1.821847
13          1     0 3.814495
26          0     0 3.094392
...         0     0 5.950193
1308        0     0 5.116245
will use the above data instead of newdata=NULL for predict.cv.glmnet
stats::predict(cv.glmnet.object, matrix[88,3], type="response", s="lambda.1se")
predict returned[88,1]:
     s="lambda.1se"
1          26.57900
13         33.26779
26         30.85060
...        40.43678
1308       37.63743
predict after processing with nresponse=1 is [88,1]:
     s="lambda.1se"
1          26.57900
13         33.26779
26         30.85060
...        40.43678
1308       37.63743
--plotmo_y with nresponse=1 for cv.glmnet object

get.object.y:
object$y is NULL (and it has no colnames)

object call is cv.glmnet(x=xmat, y=tit[, 4], nfolds=3)

get.y.from.model.frame:
terms(object) did not return the terms, will look for the formula elsewhere
no formula in getCall(object)

get.data.from.object.call.field:
eval(getCall(object)$y, env(..., call, caption, object, SHOWCALL, trace))
getCall(object)$y is usable but without colnames so we will keep on searching

names(call) is "" "x" "y" "nfolds"
the name of argument 2 is "y" so we will not process it with argn
object$y is NULL
call$y is usable but without colnames but we will use it anyway
plotmo_y returned[88,1] with no column names:
      
1   29
2   24
3   25
... 41
88  27
plotmo_y after processing with nresponse=1 is [88,1]:
    plotmo_y
1         29
2         24
3         25
...       41
88        27
residuals returned[88,1]:
         resids
1     2.4210049
2    -9.2677944
3    -5.8505971
...   0.5632234
88  -10.6374291
residuals after processing with nresponse=1 is [88,1]:
         resids
1     2.4210049
2    -9.2677944
3    -5.8505971
...   0.5632234
88  -10.6374291
generated the residuals using plotmo_predict() and plotmo_y()
----plotmo_rinfo: done

graphics::par(mfrow=c(2,2), mgp=c(1.5,0.4,0), tcl=-0.3, font.main=1,
              mar=c(4,3,1.2,0.8), oma=c(0,0,4,0), cex.main=1, cex.lab=1,
              cex.axis=1, cex=0.83)
graphics::plot(cv.glmnet.object)

training rsq 0.24
> 
> set.seed(2015)
> x <- matrix(rnorm(100*20),100,20) # 20 variables
> y <- rnorm(100)
> mod <- glmnet(x,y)
> plotmo1(mod)
plotmo1(object=mod)
 plotmo grid:    x1          x2         x3         x4        x5         x6
        -0.02229245 -0.03060877 0.02595536 -0.2306748 0.2048663 -0.2711153
         x7         x8         x9        x10         x11        x12         x13
 0.04214883 -0.1573321 0.05656354 -0.2789684 -0.01729983 0.05494411 -0.04358897
       x14         x15         x16        x17       x18       x19       x20
 -0.184689 -0.01875314 -0.08998893 0.05206396 0.1317551 -0.033794 0.1125339
> 
> # test w1.label
> par(mfrow=c(2,3))
> par(cex=1)
> par(mar=c(3,3,3,1))
> plotres(mod, which=1,                w1.main="default w1.label")
> plotres(mod, which=1, w1.label=5,    w1.main="w1.label=5")
> plotres(mod, which=1, w1.label=0,    w1.main="w1.label=0")
> plotres(mod, which=1, w1.label=TRUE, w1.main="w1.label=TRUE")
> plotres(mod, which=1, w1.label=100,  w1.main="w1.label=100")
> par(org.par)
> 
> # test w1 and non w1 args passed
> par(mfrow=c(2,2), mar=c(4,4,4,4), cex=1)
> 
> plot_glmnet(mod, w1.col=3:4, w1.xvar="norm",
+             main="plot_glmnet\nw1.col=3:4 w1.xvar=\"norm\"")
> 
> plot_glmnet(mod, col=3:4, xvar="norm",
+             main="plot_glmnet\ncol=3:4 xvar=\"norm\"")
> 
> plot_glmnet(mod, col=3:4, w1.col=1:2,
+             w1.xvar="norm", xvar="lambda",
+             main="plot_glmnet\ncol=3:4 w1.col=1:2\nw1.xvar=\"norm\", xvar=\"lambda\"")
> 
> par(org.par)
> par(mfrow=c(3,2), mar=c(3,4,4,4), cex=1)
> 
> plotres(mod, which=c(1,3), do.par=FALSE, w1.col=3:4, w1.xvar="norm",
+         w1.main="plotres\nw1.col=3:4 w1.xvar=\"norm\"")
> 
> plotres(mod, which=c(1,3), do.par=FALSE, col=3:4, xvar="norm",
+         w1.main="plotres\nplotres\ncol=3:4 xvar=\"norm\"")
> 
> plotres(mod, which=c(1,3), do.par=FALSE, col=3:4, w1.col=1:2,
+         w1.main="plotres\ncol=3:4 w1.col=1:2")
> 
> par(org.par)
> 
> # glmnet with sparse matrices
> set.seed(2015)
> n <- 100
> p <- 20
> nzc <- trunc(p/10)
> x <- matrix(rnorm(n*p),n,p)
> iz <- sample(1:(n*p),size=n*p*.85,replace=FALSE)
> x[iz] <- 0
> sx <- Matrix(x,sparse=TRUE)
> # colnames(sx) <- paste("x", 1:ncol(sx), sep="") # need column names for plotmo
> inherits(sx,"sparseMatrix") # confirm that it is sparse
[1] TRUE
> beta <- rnorm(nzc)
> fx <- x[,seq(nzc)]%*%beta
> eps <- rnorm(n)
> y <- fx+eps
> px <- exp(fx)
> px <- px/(1+px)
> ly <- rbinom(n=length(px),prob=px,size=1)
> mod.glmnet.sx <- glmnet(sx,y)
> plotmo1(mod.glmnet.sx, all2=TRUE) # will give warning: too many predictors to plot all pairs
plotmo1(object=mod.glmnet.sx,all2=TRUE)
Warning: too many predictors to plot all pairs,
         so plotting degree2 plots for just the first 7 predictors.
         Call plotmo with all2=2 to plot degree2 plots for up to 20 predictors.
 plotmo grid:    x1 x2 x3 x4 x5 x6 x7 x8 x9 x10 x11 x12 x13 x14 x15 x16 x17 x18
                  0  0  0  0  0  0  0  0  0   0   0   0   0   0   0   0   0   0
 x19 x20
   0   0
> plotmo1(mod.glmnet.sx, all2=2, caption="all2=2") # test all2=2
plotmo1(object=mod.glmnet.sx,all2=2,caption="all2=2")
More than 64 degree2 plots.
Consider using plotmo's degree2 argument to limit the number of plots.
For example,  degree2=1:10  or  degree2="x1"
Call plotmo with trace=-1 to make this message go away.

 plotmo grid:    x1 x2 x3 x4 x5 x6 x7 x8 x9 x10 x11 x12 x13 x14 x15 x16 x17 x18
                  0  0  0  0  0  0  0  0  0   0   0   0   0   0   0   0   0   0
 x19 x20
   0   0
> plotmo1(mod.glmnet.sx, all2=2, degree2=1:3, caption="all2=2 degree2=1:3")
plotmo1(object=mod.glmnet.sx,all2=2,degree2=1:3,caption="all2=2degree2=1:3")
 plotmo grid:    x1 x2 x3 x4 x5 x6 x7 x8 x9 x10 x11 x12 x13 x14 x15 x16 x17 x18
                  0  0  0  0  0  0  0  0  0   0   0   0   0   0   0   0   0   0
 x19 x20
   0   0
> plotres(mod.glmnet.sx)
> par(org.par)
> 
> par(mfrow=c(2,4), mar=c(3,3,3,1), mgp=c(1.5,0.5,0), oma=c(0,0,2.5,0))
> y <- trees$Volume
> x <- as.matrix(data.frame(Girth=trees$Girth, Height=trees$Height))
> glmnet <- glmnet(x, y)
> plotres(glmnet, do.par=FALSE, caption="glmnet and lm: top and bottom should be the same")
> lm <- lm(Volume~., data=trees)
> plotres(lm, do.par=FALSE, SHOWCALL=TRUE)
> 
> par(mfrow=c(3,2), mar=c(3,3,3,1), mgp=c(1.5,0.5,0), oma=c(0,0,2.5,0))
> plotres(glmnet, do.par=FALSE, which=c(1,3), w1.xvar="norm",
+         caption="glmnet with various options", SHOWCALL=TRUE)
> plotres(glmnet, trace=1, do.par=FALSE, which=c(1,3), SHOWCALL=TRUE)
stats::residuals(object=elnet.object, type="response")
residuals() was unsuccessful, will use predict() instead
stats::predict(elnet.object, matrix[3,2], type="response", s=0)
stats::fitted(object=elnet.object)
fitted() was unsuccessful, will use predict() instead
got model response from getCall(object)$y
plot_glmnet(elnet.object, xvar="rlambda", grid.col=0, s=0, nresponse=1)

training rsq 0.95
> plotres(glmnet, trace=1, do.par=FALSE, which=c(1,3), predict.s=5, SHOWCALL=TRUE)
stats::residuals(object=elnet.object, type="response")
residuals() was unsuccessful, will use predict() instead
stats::predict(elnet.object, matrix[3,2], type="response", s=5)
stats::fitted(object=elnet.object)
fitted() was unsuccessful, will use predict() instead
got model response from getCall(object)$y
plot_glmnet(elnet.object, xvar="rlambda", grid.col=0, s=5, nresponse=1)

training rsq 0.84
> par(org.par)
> 
> printf("======== glmnet additional tests\n")
======== glmnet additional tests
> set.seed(2015)
> p <- 10
> n <- 30
> x <- cbind(matrix(rnorm(n*p),n,p))
> y <- rowSums(x[,1:3]^3)
> glmnet <- glmnet(x,y)
> plotres(glmnet, SHOWCALL=TRUE, caption="glmnet: y <- rowSums(x[,1:3]^3)")
> plotres(glmnet, SHOWCALL=TRUE, w1.xvar="norm")
> par(mfrow=c(1,1))
> omar <- par("mar")
> ocex.axis <- par("cex.axis")
> ocex.lab <- par("cex.lab")
> plotres(glmnet, SHOWCALL=TRUE, which=1)
> stopifnot(par("mar") == omar)
> stopifnot(par("cex.axis") == ocex.axis)
> stopifnot(par("cex.lab") == ocex.lab)
> par(org.par)
> 
> # test some args for plot_glmnet
> plotres(glmnet, predict.s=.05, SHOWCALL=TRUE, trace=0, col.main=2,
+         w1.xlab="my xlab", w1.ylab="my ylab",
+         w1.main="test some args for plot_glmnet1",
+         w1.col=4:1)
> 
> plot_glmnet(glmnet, trace=0, col.main=2, main="test some args for plot_glmnet2",
+         xlab="my xlab", ylab="my ylab",
+         col=4:1, ylim=c(-2,4)) # TODO xlim=c(-5,3))
> 
> plotres(glmnet, predict.s=.05, SHOWCALL=TRUE, which=c(1,3), grid.col="gray", do.par=2)
> plotres(glmnet, predict.s=.05, SHOWCALL=TRUE, which=c(1,3), w1.s.col=0, do.par=0)
> par(org.par)
> 
> # TODO the following issues a stream of warnings: restarting interrupted promise evaluation
> expect.err(try(plotres(glmnet, w1.col=nonesuch)), "cannot evaluate 'col'")
Warning in eval(dots[[i]]) : restarting interrupted promise evaluation
Warning in eval(dots[[i]]) : restarting interrupted promise evaluation
Warning in eval(dots[[i]]) : restarting interrupted promise evaluation
Warning in eval(dots[[i]]) : restarting interrupted promise evaluation
Warning in eval(dots[[i]]) : restarting interrupted promise evaluation
Warning in eval(dots[[i]]) : restarting interrupted promise evaluation
Warning in eval(dots[[i]]) : restarting interrupted promise evaluation
Warning in eval(dots[[i]]) : restarting interrupted promise evaluation
Warning in eval(dots[[i]]) : restarting interrupted promise evaluation
Warning in eval(dots[[i]]) : restarting interrupted promise evaluation
Warning in eval(dots[[i]]) : restarting interrupted promise evaluation
Warning in eval(dots[[i]]) : restarting interrupted promise evaluation
Warning in eval(dots[[i]]) : restarting interrupted promise evaluation
Warning in eval(dots[[i]]) : restarting interrupted promise evaluation
Warning in eval(dots[[i]]) : restarting interrupted promise evaluation
Warning in eval(dots[[i]]) : restarting interrupted promise evaluation
Warning in eval(dots[[i]]) : restarting interrupted promise evaluation
Warning in eval(dots[[i]], envir = env, enclos = env) :
  restarting interrupted promise evaluation
Warning in eval(dots[[i]], envir = env, enclos = env) :
  restarting interrupted promise evaluation
Warning in eval(dots[[i]], envir = env, enclos = env) :
  restarting interrupted promise evaluation
Warning in eval(dots[[i]], envir = env, enclos = env) :
  restarting interrupted promise evaluation
Warning in eval(dots[[i]], envir = env, enclos = env) :
  restarting interrupted promise evaluation
Error in eval(dots[[idot]], parent.frame(1)) : 
  ..6 used in an incorrect context, no ... to look in

plot_glmnet(elnet.object, xvar="rlambda", grid.col=0, col=..6, s=0,
    nresponse=1)

Error in eval(dots[[idot]], parent.frame(1)) : 
  ..6 used in an incorrect context, no ... to look in
Error : cannot evaluate 'col'
Got expected error from try(plotres(glmnet, w1.col = nonesuch))
> 
> printf("======== glmnet multinomial (multnet)\n")
======== glmnet multinomial (multnet)
> par(mfrow=c(4,4), mar=c(3,3,3,1))
> set.seed(2016)
> n <- 200
> p <- 4
> x <- matrix(rnorm(n*p), n, p)
> colnames(x) <- paste("x", 1:ncol(x), sep="")
> 
> # "1" is correlated with x[,1], "4" is correlated with x[,2], "2" and "3" not correlated
> y <- ifelse(x[,1] > 0.5, 1,
+      ifelse(x[,2] > 0.0, 4,
+      sample(c(2,3), size=nrow(x), replace=TRUE)))
> print(cov(x, y))
          [,1]
x1 -0.84023279
x2  0.38654310
x3 -0.11396993
x4 -0.07611821
> y <- factor(y)
> 
> # TODO Following causes the following warning:
> #      Warning: from glmnet Fortran code (error code -90); Convergence for 90th lambda value not reached after maxit=100000 iterations; solutions for larger lambdas returned
> multinomial.mod <- glmnet(x, y, family="multinomial")
Warning: from glmnet C++ code (error code -90); Convergence for 90th lambda value not reached after maxit=100000 iterations; solutions for larger lambdas returned
> 
> plotres(multinomial.mod, nresponse=1, w1.main="nresponse=1",
+         main="family=\"multinomial\"",
+         smooth.col=0, info=TRUE,
+         trace=0, which=c(1,3), do.par=FALSE, xlim=c(-.2,1.2), ylim=c(-1.2, 1.2))
> 
> plotres(multinomial.mod, nresponse=2, w1.main="nresponse=2",
+         smooth.col=0, info=TRUE,
+         trace=0, which=c(1,3), do.par=FALSE, xlim=c(-.2,1.2), ylim=c(-1.2, 1.2))
> 
> plotres(multinomial.mod, nresponse=3, w1.main="nresponse=3",
+         smooth.col=0, info=TRUE,
+         trace=0, which=c(1,3), do.par=FALSE, xlim=c(-.2,1.2), ylim=c(-1.2, 1.2))
> 
> plotres(multinomial.mod, nresponse=4, w1.main="nresponse=4",
+         smooth.col=0, info=TRUE,
+         trace=0, which=c(1,3), do.par=FALSE, xlim=c(-.2,1.2), ylim=c(-1.2, 1.2))
> 
> plotmo(multinomial.mod, nresponse=1, trace=0, do.par=FALSE, degree1=1:2)
 plotmo grid:    x1        x2         x3         x4
        -0.01157106 0.1530614 0.04853916 0.02097713
> plotmo(multinomial.mod, nresponse=2, trace=0, do.par=FALSE, degree1=1:2)
 plotmo grid:    x1        x2         x3         x4
        -0.01157106 0.1530614 0.04853916 0.02097713
> 
> par(mgp=c(1.5, .4, 0))
> plot(multinomial.mod, xvar="norm") # compare to plot.glmnet
> par(org.par)
> 
> # compare to earth
> par(mfrow=c(4,3), mar=c(3,3,1,1))
> yfac <- factor(c("a","b","c","d")[y])
> earth.mod <- earth(x, yfac, trace=0)
> 
> plotres(earth.mod, nresponse=1,
+         main=sprint("multiresponse\nnresponse=1   rsq %.2g", earth.mod$rsq.per.response[1]),
+         which=3, xlim=c(-.2, 1.2), ylim=c(-1.2, 1.2),
+         smooth.col=0, info=TRUE,
+         do.par=FALSE, trace=0, jitter=7, cex.response=.7)
> plotmo(earth.mod, nresponse=1, do.par=FALSE)
 plotmo grid:    x1        x2         x3         x4
        -0.01157106 0.1530614 0.04853916 0.02097713
> 
> plotres(earth.mod, nresponse=2,
+         main=sprint("nresponse=2   rsq %.2g", earth.mod$rsq.per.response[2]),
+         which=3, xlim=c(-.2, 1.2), ylim=c(-1.2, 1.2),
+         smooth.col=0, info=TRUE,
+         do.par=FALSE, trace=0, jitter=7, cex.response=.7)
> plotmo(earth.mod, nresponse=2, do.par=FALSE)
 plotmo grid:    x1        x2         x3         x4
        -0.01157106 0.1530614 0.04853916 0.02097713
> 
> plotres(earth.mod, nresponse=3,
+         main=sprint("nresponse=3   rsq %.2g", earth.mod$rsq.per.response[3]),
+         which=3, xlim=c(-.2, 1.2), ylim=c(-1.2, 1.2),
+         smooth.col=0, info=TRUE,
+         do.par=FALSE, trace=0, jitter=7, cex.response=.7)
> plotmo(earth.mod, nresponse=3, do.par=FALSE)
 plotmo grid:    x1        x2         x3         x4
        -0.01157106 0.1530614 0.04853916 0.02097713
> 
> plotres(earth.mod, nresponse=4,
+         main=sprint("nresponse=4   rsq %.2g", earth.mod$rsq.per.response[4]),
+         which=3, xlim=c(-.2, 1.2), ylim=c(-1.2, 1.2),
+         smooth.col=0, info=TRUE,
+         do.par=FALSE, trace=0, jitter=7, cex.response=.7)
> plotmo(earth.mod, nresponse=4, do.par=FALSE)
 plotmo grid:    x1        x2         x3         x4
        -0.01157106 0.1530614 0.04853916 0.02097713
> 
> print(summary(earth.mod))
Call: earth(x=x, y=yfac, trace=0)

                           a          b           c          d
(Intercept)      -0.00759508  0.4777590  0.32170406  0.2081320
h(x1-0.333835)    2.90462369 -0.8647319 -0.45258311 -1.5873087
h(x1-0.698191)   -2.95380448  0.8748909  0.44797354  1.6309401
h(x2- -0.724713)  0.00083945  0.8851614 -0.25504553 -0.6309554
h(x2- -0.399683)  0.00512171 -1.9558653 -0.04067399  1.9914176
h(x2-0.255847)   -0.00181500  1.1141555  0.31084638 -1.4231869

Selected 6 of 14 terms, and 2 of 4 predictors
Termination condition: Reached nk 21
Importance: x1, x2, x3-unused, x4-unused
Number of terms at each degree of interaction: 1 5 (additive model)

           GCV       RSS      GRSq       RSq
a   0.01233701  2.203452 0.9438697 0.9493692
b   0.10618691 18.965513 0.3429519 0.4073277
c   0.08491793 15.166767 0.1053911 0.1930425
d   0.07399172 13.215291 0.6874511 0.7180738
All 0.27743357 49.551022 0.6109269 0.6490472
> 
> par(org.par)
> 
> printf("======== binomial model\n")
======== binomial model
> 
> set.seed(2019)
> n <- 50
> p <- 4
> x <- matrix(rnorm(n*p), n, p)
> colnames(x) <- paste("x", 1:ncol(x), sep="")
> y <- ifelse(x[,1] + x[,2] + .1 * rnorm(n) > .5, TRUE, FALSE)
> print(cov(x, y))
         [,1]
x1 0.26996406
x2 0.19322507
x3 0.04850061
x4 0.01568008
> y <- factor(y)
> glmnet.binomial <- glmnet(x, y, family="binomial")
> par(mfrow=c(2,3), mar=c(3,3,1,1))
> plotres(glmnet.binomial, info=T, predict.s=.02, which=c(1,3), do.par=FALSE, w1.main="glmnet.binomial")
> plot(glmnet.binomial)
> earth.mod <- earth(x, y)
> set.seed(2019)
> plotres(earth.mod, info=T, which=c(1,3), do.par=FALSE)
> par(org.par)
> par(mfrow=c(2,4), mar=c(3,3,1,1))
> set.seed(2019)
> plotmo(glmnet.binomial, do.par=FALSE)
 plotmo grid:    x1         x2         x3         x4
         0.05687241 -0.2477018 -0.1266239 -0.2475514
> plotmo(earth.mod, do.par=FALSE, main="binomial earth.mod")
 plotmo grid:    x1         x2         x3         x4
         0.05687241 -0.2477018 -0.1266239 -0.2475514
> par(org.par)
> 
> printf("======== glmnet family=\"mgaussian\"\n")
======== glmnet family="mgaussian"
> set.seed(2015)
> p <- 10
> n <- 30
> x <- cbind((1:n)/n, matrix(rnorm(n*(p-1)),n,p-1))
> colnames(x) <- paste0("x", 1:p)
> # ymultresp <- cbind(rowSums(x[,1:5]^3), rowSums(x[,5:p]^3), 1:n)
> set.seed(1)
> ymultresp <- cbind(x[,1]+.001*rnorm(n), rowSums(x[,2:5]^3), rnorm(n))
> glmnet.mgaussian <- glmnet(x, ymultresp, family="mgaussian")
> plotres(glmnet.mgaussian, nresponse=1, SHOWCALL=TRUE, which=c(1:3), do.par=2, info=1)
> # manually calculate the residuals
> plot(x=predict(glmnet.mgaussian, newx=x, s=0)[,1,1],
+      y=ymultresp[,1] - predict(glmnet.mgaussian, newx=x, s=0)[,1,1],
+      pch=20, xlab="Fitted", ylab="Residuals",
+      main="Manually calculated residuals, nresponse=1, s=0")
> abline(h=0, col="gray")
> par(org.par)
> plotres(glmnet.mgaussian, nresponse=2, SHOWCALL=TRUE, which=c(1:3), do.par=2, info=1)
> # manually calculate the residuals
> plot(x=predict(glmnet.mgaussian, newx=x, s=0)[,2,1],
+      y=ymultresp[,2] - predict(glmnet.mgaussian, newx=x, s=0)[,2,1],
+      pch=20, xlab="Fitted", ylab="Residuals",
+      main="Manually calculated residuals, nresponse=2, s=0")
> abline(h=0, col="gray")
> par(org.par)
> plotmo(glmnet.mgaussian, nresponse=1, SHOWCALL=TRUE)
 plotmo grid:    x1          x2        x3         x4         x5          x6
          0.5166667 0.002216547 0.3749872 -0.1927516 -0.3806807 -0.03575992
         x7        x8         x9       x10
 0.01386232 0.0135174 0.04028881 0.0426105
> plotmo(glmnet.mgaussian, nresponse=2, SHOWCALL=TRUE)
 plotmo grid:    x1          x2        x3         x4         x5          x6
          0.5166667 0.002216547 0.3749872 -0.1927516 -0.3806807 -0.03575992
         x7        x8         x9       x10
 0.01386232 0.0135174 0.04028881 0.0426105
> 
> graphics::par(mfrow=c(2,2), mgp=c(1.5,0.4,0), tcl=-0.3, cex.main=1,
+               font.main=1, mar=c(4,3,1.2,0.8), oma=c(0,0,4,0), cex=0.83)
> 
> plotres(glmnet.mgaussian, nresponse=2, SHOWCALL=TRUE, which=3, do.par=FALSE,
+         caption="glmnet.mgaussian compare to manually calculated residuals")
> plot(x=predict(glmnet.mgaussian, newx=x, s=0)[,2,1],
+      y=ymultresp[,2] - predict(glmnet.mgaussian, newx=x, s=0)[,2,1],
+      pch=20, xlab="Fitted", ylab="Residuals",
+      main="Manual residuals, nresponse=2, s=0")
> abline(h=0, col="gray")
> 
> plotres(glmnet.mgaussian, nresponse=2, predict.s=.5, SHOWCALL=TRUE, which=3, do.par=FALSE)
> plot(x=predict(glmnet.mgaussian, newx=x, s=.5)[,2,1],
+      y=ymultresp[,2] - predict(glmnet.mgaussian, newx=x, s=.5)[,2,1],
+      pch=20, xlab="Fitted", ylab="Residuals",
+      main="Manual residuals, nresponse=2, s=.5")
> abline(h=0, col="gray")
> 
> plotres(glmnet.mgaussian, predict.s=.05, nresponse=3, info=TRUE, SHOWCALL=TRUE) # essentially random
> 
> par(org.par)
> par(mfrow=c(2,3), mar=c(3,3,3,.5), oma=c(0,0,3,0), mgp=c(1.5,0.4,0), tcl=-0.3)
> 
> data(trees)
> set.seed(2015)
> # variable with a long name
> x50 <- cbind(trees[,1:2], Girth12345678901234567890=rnorm(nrow(trees)))
> mod.with.long.name <- glmnet(data.matrix(x50),data.matrix(trees$Volume))
> plotres(mod.with.long.name, which=1, caption="test plot_glmnet with x50 and x60")
> 
> # one inactive variable (all coefs are zero for variable "rand")
> set.seed(2015)
> x60 <- cbind(trees[,1], rand=rnorm(nrow(trees)), trees[,2])
> # complicate the issue: use an unnamed column (column 3)
> colnames(x60) <- c("Girth", "rand", "")
> mod.with.inactive.var <- glmnet(data.matrix(x60),data.matrix(trees$Volume))
> mod.with.inactive.var$beta["rand",] = 0 # TODO hack force inactive variable
> plotres(mod.with.inactive.var, which=1)
> plotres(mod.with.inactive.var, which=1, w1.xvar="norm")
> # compare to plot.glmnet (but note that labels aren't always plotted unless par=c(1,1)?)
> plot(mod.with.inactive.var, xvar="norm", label=TRUE)
> # plotmo calls the unnamed column "x3", fair enough
> plotmo(mod.with.inactive.var, do.par=FALSE, pt.col=2)
 plotmo grid:    Girth        rand x3
                  12.9 0.004544606 76
> 
> # single active variable
> x70 <- cbind(trees[,1,drop=F], 0)
> a <- glmnet(data.matrix(x70), data.matrix(trees$Volume))
> par(org.par)
> par(mfrow=c(2,2), mar=c(3,3,2,4))
> plotres(a, which=1, predict.s=1, caption="single active variable")
> plotres(a, which=1, w1.xvar="norm")
> plotres(a, which=1, w1.xvar="lambda")
> plotres(a, which=1, w1.xvar="dev")
> 
> #--- test interaction of w1. and non w1 args -------------------------------------
> 
> #--- glmnet model, which=1 ---
> 
> par(org.par)
> par(mfrow=c(4,3), mar=c(3, 3, 4, 1), mgp=c(2, 0.6, 0))
> 
> plotres(mod.glmnet.xmat, which=1,
+         w1.xlim=c(6,-6),
+         w1.ylim=c(-5,5),
+         w1.col=1:2,
+         w1.main="TEST INTERACTION OF W1 ARGS PAGE 1 (which=1)\n\nwhich=1 w1.xlim=c(6,-6)\nw1.ylim=c(-5,5)) w1.col=1:2,")
> 
> plotres(mod.glmnet.xmat, which=1, cex.main=1.2,
+         xlim=c(9,-9),
+         ylim=c(-60,60),
+         col=3:4,
+         w1.main="which=1 xlim=c(9,-9)\nylim=c(-60,60)) col=3:4,")
> 
> plotres(mod.glmnet.xmat, which=1, cex.main=1,
+         xlim=c(9,-9), w1.xlim=c(6,-6),
+         ylim=c(-60,60), w1.ylim=c(-5,5),
+         w1.col=1:2, col=3:4,
+         w1.main="which=1 xlim=c(9,-9), w1.xlim=c(6,-6)\nylim=c(-60,60), w1.ylim=c(-5,5)) w1.col=1:2, col=3:4")
> 
> #--- glmnet model, which=c(1,3,4) ---
> 
> plotres(mod.glmnet.xmat, which=c(1,3,4), cex.main=1,
+         ylim=c(-70,70),    xlim=c(-20, 60),
+         col=2:3, do.par=FALSE,
+         w1.main="TEST INTERACTION OF W1 ARGS PAGE 1 (which=c(1,3,4))\nlim=c(-70,70), xlim=c(-20, 60)")
> 
> plotres(mod.glmnet.xmat, which=c(1,3,4), cex.main=1.2,
+         ylim=c(-70,70),    xlim=c(-20, 60), qq.xlim=c(-7,5),
+         col=2:3, do.par=FALSE,
+         w1.main="ylim=c(-70,70), xlim=c(-20, 60)\nqq.xlim=c(-7,5)")
> 
> plotres(mod.glmnet.xmat, which=c(1,3,4), cex.main=1.2,
+         w1.ylim=c(-7,7), w1.xlim=c(4,-4),     col=2:3, do.par=FALSE,
+         w1.main="w1.ylim=c(-7,7), w1.xlim=c(4,-4)")
> 
> # plotres(mod.glmnet.xmat, which=c(1,3,4), cex.main=.9,
> #         w1.ylim=c(-7,7), ylim=c(-20,20),
> #         qq.xlim=c(-7,5), col=2:3, do.par=FALSE,
> #         qq.ylim=c(-100,100),
> #         main="w1.ylim=c(-7,7) ylim=c(-20,20)\nqq.xlim=c(-7,5) qq.ylim=c(-100,100)")
> 
> par(org.par)
> par(mfrow=c(3,3), mar=c(3, 3, 4, 1), mgp=c(2, 0.6, 0))
> 
> plotres(mod.glmnet.xmat, which=c(1,3,4), do.par=FALSE, #  w1.main="which=c(1,3,4)",
+         w1.xlim=c(6,-6),
+         w1.ylim=c(-5,5),
+         w1.col=2:3,
+         w1.main="TEST INTERACTION OF W1 ARGS PAGE 2\n\nwhich=c(1,3,4) w1.xlim=c(6,-6)\nw1.ylim=c(-5,5)) w1.col=2:3")
> 
> plotres(mod.glmnet.xmat, which=c(1,3,4), w1.cex.main=1,  do.par=FALSE, # w1.main="which=c(1,3,4)",
+         xlim=c(-20,70),
+         ylim=c(-60,60),
+         w1.col=2:3,
+         col=3:4,
+         w1.main="which=c(1,3,4) ylim=c(-60,60))\nw1.col=2:3, col=3:4")
> 
> plotres(mod.glmnet.xmat, which=c(1,3,4), w1.cex.main=1, do.par=FALSE, # w1.main="which=c(1,3,4)",
+         xlim=c(-20,70), w1.xlim=c(6,-6),
+         ylim=c(-60,60), w1.ylim=c(-5,5),
+         col=3:4,
+         w1.main="which=c(1,3,4) xlim=c(9,-9), w1.xlim=c(6,-6)\nylim=c(-60,60), w1.ylim=c(-5,5)) w1.col=1:2, col=3:4")
> 
> par(org.par)
> 
> #-- make sure that we can work with all families
> 
> set.seed(2016)
> par(mfrow=c(3,3), mar=c(3,3,3,1))
> n <- 100
> p <- 4
> x <- matrix(rnorm(n*p), n, p)
> g2 <- sample(1:2, n, replace=TRUE)
> for(family in c("gaussian","binomial","poisson")) {
+     mod <- glmnet(x,g2,family=family)
+     plot(mod, xvar="lambda")
+     plotres(mod, w1.xvar="lambda", main=paste("family", family),
+             which=c(1,3), do.par=FALSE)
+ }
> # cox
> library(plotmo)
> n <- 100
> p <- 20
> nzc <- trunc(p/10)
> set.seed(2016)
> beta <- rnorm(nzc)
> x7 <- matrix(rnorm(n*p), n, p)
> beta <- rnorm(nzc)
> fx <- x7[,seq(nzc)] %*% beta/3
> hx <- exp(fx)
> ty <- rexp(n, hx)
> tcens <- rbinom(n=n, prob=.3, size=1)# censoring indicator
> y <- cbind(time=ty, status=1-tcens) # y=Surv(ty,1-tcens) with library(survival)
> glmnet.cox <- glmnet(x=x7, y=y, family="cox")
> plot(glmnet.cox)
> title("glmnet.cox", line=2)
> plot_glmnet(glmnet.cox, xvar="norm")
> plotres(glmnet.cox, which=3, do.par=FALSE)
> par(org.par)
> 
> # test col argument
> par(mfrow=c(2,3), mar=c(3,3,5,1), cex=1)
> mod <- glmnet(as.matrix(mtcars[-1]), mtcars[,1])
> plot_glmnet(mod, main="plot_glmnet  default")
> plot_glmnet(mod, col=c(1,2,3,0,0,NA,0,0,0,0), main="col=c(1,2,3,0,0,NA,0,0,0,0)")
> g <- "gray"
> plot_glmnet(mod, col=c("black","red","green",g,g,g,g,g,"steelblue","darkorange"), main="col=c('black','red','green',g,g,g,g,g,'steelblue','darkorange')")
> plot_glmnet(mod, col=c("black","red","green",0,0,0,0,0,"steelblue","darkorange"), main="col=c('black','red','green',0,0,0,0,0,'steelblue','darkorange')")
> plot_glmnet(mod, col=c("black","red", 0), main="col=c('black','red', 0)") # test recycling, including 0
> par(org.par)
> 
> source("test.epilog.R")