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
|
> # test.offset.R
>
> source("test.prolog.R")
> library(earth)
Loading required package: plotmo
Loading required package: plotrix
Loading required package: TeachingDemos
>
> almost.equal <- function(x, y, max=1e-8)
+ {
+ stopifnot(max >= 0 && max <= .01)
+ length(x) == length(y) && max(abs(x - y)) < max
+ }
> # check that earth model matches lm model in all essential details
> check.earth.matches.lm <- function(earth, lm, newdata=data[c(3,1,9),],
+ check.coef.names=TRUE,
+ check.casenames=TRUE,
+ max=1e-8,
+ max.residuals=1e-8)
+ {
+ check.names <- function(earth.names, lm.names)
+ {
+ if(check.casenames &&
+ # lm always adds rownames even if "1", "2", "3": this seems
+ # wasteful and not particulary helpful, so earth doesn't do
+ # this, hence the first !isTRUE(all.equal) below
+ !isTRUE(all.equal(lm.names, paste(1:length(lm.names)))) &&
+ !isTRUE(all.equal(earth.names, lm.names))) {
+ print(earth.names)
+ print(lm.names)
+ stop(deparse(substitute(earth.names)), " != ",
+ deparse(substitute(lm.names)))
+ }
+ }
+ cat0("check ", deparse(substitute(earth)), " vs ",
+ deparse(substitute(lm)), "\n")
+
+ # sort is needed because earth may reorder predictors based in importance
+ stopifnot(almost.equal(sort(coef(earth)), sort(coef(lm)), max=max))
+ if(check.coef.names)
+ stopifnot(identical(sort(names(coef(earth))), sort(names(coef(lm)))))
+
+ stopifnot(length(earth$coefficients) == length(lm$coefficients))
+ stopifnot(almost.equal(sort(earth$coefficients), sort(lm$coefficients), max=max))
+
+ stopifnot(length(earth$residuals) == length(lm$residuals))
+ stopifnot(almost.equal(earth$residuals, lm$residuals, max=max.residuals))
+
+ stopifnot(length(earth$fitted.values) == length(lm$fitted.values))
+ stopifnot(almost.equal(earth$fitted.values, lm$fitted.values, max=max))
+
+ stopifnot(almost.equal(fitted(earth), fitted(lm), max=max))
+ if(!is.null(names(fitted(earth))) && !is.null(names(fitted(lm))))
+ check.names(names(fitted(earth)), names(fitted(lm)))
+ stopifnot(almost.equal(residuals(earth), residuals(lm), max=max.residuals))
+ if(!is.null(names(residuals(earth))) && !is.null(names(residuals(lm))))
+ check.names(names(residuals(earth)), names(residuals(lm)))
+
+ predict.earth <- predict(earth)
+ predict.lm <- predict(lm)
+ stopifnot(almost.equal(predict.earth, predict.lm, max=max))
+ if(!is.null(names(predict.earth)) && !is.null(names(predict.lm)))
+ check.names(names(predict.earth), names(predict.lm))
+
+ predict.earth <- predict(earth, newdata=newdata)
+ predict.lm <- predict(lm, newdata=newdata)
+ stopifnot(almost.equal(predict.earth, predict.lm, max=max))
+ if(!is.null(names(predict.earth)) && !is.null(names(predict.lm)))
+ check.names(names(predict.earth), names(predict.lm))
+ # we calculate earth rsq manually to match the rsq technique of lm
+ # this is necessary to get the same rsq when an offset is used
+ if(is.null(earth$offset)) {
+ earth.rsq <- earth$rsq
+ rss <- if (is.null(earth$weights))
+ sum(earth$residuals^2)
+ else
+ sum(earth$weights * earth$residuals^2)
+ } else {
+ if (is.null(earth$weights)) {
+ mss <- sum((earth$fitted.values - mean(earth$fitted.values))^2)
+ rss <- sum(earth$residuals^2)
+ } else {
+ stopifnot(almost.equal(lm$weights, earth$weights, max=max))
+ m <- sum(earth$weights * earth$fitted.values /sum(earth$weights))
+ mss <- sum(earth$weights * (earth$fitted.values - m)^2)
+ rss <- sum(earth$weights * earth$residuals^2)
+ }
+ earth.rsq <- mss / (mss + rss)
+ }
+ stopifnot(almost.equal(earth.rsq, summary(lm)$r.squared, max=max))
+
+ # check internal consistency of earth model
+ stopifnot(earth$gcv == earth$gcv[1])
+ stopifnot(almost.equal(earth$rsq.per.response[1], earth$rsq, max=1e-15))
+ stopifnot(almost.equal(earth$grsq.per.response[1], earth$grsq, max=1e-15))
+ if(is.null(earth$weights))
+ stopifnot(almost.equal(earth$rss.per.response, earth$rss, max=1e-10))
+ }
> # check that earth model matches lm model in all essential details
> check.earth.matches.glm <- function(earth, glm, newdata=data[c(3,1,9),],
+ check.coef.names=TRUE,
+ check.casenames=FALSE,
+ max=1e-8,
+ max.residuals=1e-8)
+ {
+ check.names <- function(earth.names, glm.names)
+ {
+ if(check.casenames &&
+ # glm always adds rownames even if "1", "2", "3": this seems
+ # wasteful and not particulary helpful, so earth doesn't do
+ # this, hence the first !isTRUE(all.equal) below
+ !isTRUE(all.equal(glm.names, paste(1:length(glm.names)))) &&
+ !isTRUE(all.equal(earth.names, glm.names))) {
+ print(earth.names)
+ print(glm.names)
+ stop(deparse(substitute(earth.names)), " != ",
+ deparse(substitute(glm.names)))
+ }
+ }
+ cat0("check ", deparse(substitute(earth)), " vs ",
+ deparse(substitute(glm)), "\n")
+
+ # sort is needed because earth may reorder predictors based in importance
+ earth.glm <- earth$glm.list[[1]]
+ stopifnot(!is.null(earth.glm))
+ stopifnot(almost.equal(sort(coef(earth.glm)), sort(coef(glm)), max=max))
+ if(check.coef.names)
+ stopifnot(identical(sort(names(coef(earth.glm))), sort(names(coef(glm)))))
+
+ stopifnot(length(earth.glm$coefficients) == length(glm$coefficients))
+ stopifnot(almost.equal(sort(earth.glm$coefficients), sort(glm$coefficients), max=max))
+
+ stopifnot(length(earth.glm$residuals) == length(glm$residuals))
+ stopifnot(almost.equal(earth.glm$residuals, glm$residuals, max=max))
+
+ stopifnot(length(earth.glm$fitted.values) == length(glm$fitted.values))
+ stopifnot(almost.equal(earth.glm$fitted.values, glm$fitted.values, max=max))
+
+ stopifnot(almost.equal(fitted(earth.glm), fitted(glm), max=max))
+ if(!is.null(names(fitted(earth.glm))) && !is.null(names(fitted(glm))))
+ check.names(names(fitted(earth.glm)), names(fitted(glm)))
+
+ stopifnot(almost.equal(residuals(earth.glm), residuals(glm), max=max.residuals))
+ if(!is.null(names(residuals(earth.glm))) && !is.null(names(residuals(glm))))
+ check.names(names(residuals(earth.glm)), names(residuals(glm)))
+
+ stopifnot(almost.equal(residuals(earth, type="response"), residuals(glm, type="response"), max=max.residuals))
+ stopifnot(almost.equal(residuals(earth, type="glm.response"), residuals(glm, type="response"), max=max.residuals))
+ stopifnot(almost.equal(residuals(earth, type="deviance"), residuals(glm, type="deviance"), max=max.residuals))
+ stopifnot(almost.equal(residuals(earth, type="glm.pearson"), residuals(glm, type="pearson"), max=max.residuals))
+ stopifnot(almost.equal(residuals(earth, type="glm.working"), residuals(glm, type="working"), max=max.residuals))
+ # commented out because partial residuals don't match (because factors are expanded differently?)
+ # stopifnot(almost.equal(residuals(earth, type="glm.partial"), residuals(glm, type="partial"), max=max.residuals))
+
+ # predict without newdata
+ predict.glm <- predict(glm)
+ predict.earth <- predict(earth)
+ stopifnot(almost.equal(predict.earth, predict.glm, max=max))
+ if(!is.null(names(predict.earth)) && !is.null(names(predict.glm)))
+ check.names(names(predict.earth), names(predict.glm))
+
+ # predict type=default
+ predict.glm <- predict(glm, newdata=newdata)
+ predict.earth <- predict(earth, newdata=newdata)
+ stopifnot(almost.equal(predict.earth, predict.glm, max=max))
+ if(!is.null(names(predict.earth)) && !is.null(names(predict.glm)))
+ check.names(names(predict.earth), names(predict.glm))
+
+ # predict type="response"
+ predict.glm.response <- predict(glm, newdata=newdata, type="response")
+ predict.earth.response <- predict(earth, newdata=newdata, type="response")
+ if(!is.null(names(predict.earth)) && !is.null(names(predict.glm)))
+ check.names(names(predict.earth), names(predict.glm))
+ stopifnot(almost.equal(predict.earth.response, predict.glm.response, max=max))
+ if(!is.null(names(predict.earth.response)) && !is.null(names(predict.glm.response)))
+ check.names(names(predict.earth.response), names(predict.glm.response))
+
+ # predict type="link"
+ predict.earth.link <- predict(earth, newdata=newdata, type="link")
+ predict.glm.link <- predict(glm, newdata=newdata, type="link")
+ stopifnot(almost.equal(predict.earth.link, predict.glm.link, max=max))
+ if(!is.null(names(predict.earth)) && !is.null(names(predict.lm)))
+ check.names(names(predict.earth), names(predict.glm))
+
+ # check internal consistency of earth model
+ stopifnot(earth$gcv == earth$gcv[1])
+ stopifnot(almost.equal(earth$rsq.per.response[1], earth$rsq, max=1e-15))
+ stopifnot(almost.equal(earth$grsq.per.response[1], earth$grsq, max=1e-15))
+ if(is.null(earth$weights))
+ stopifnot(almost.equal(earth$rss.per.response, earth$rss, max=1e-10))
+ }
> devratio <- function(mod)
+ {
+ if(is.null(mod$deviance))
+ mod <- mod$glm.list[[1]]
+ stopifnot(!is.null(mod))
+ stopifnot(!is.null(mod$deviance))
+ stopifnot(!is.null(mod$null.deviance))
+ sprint("devratio %.2f", 1 - mod$deviance / mod$null.deviance)
+ }
> print.devratio <- function(s, mod)
+ {
+ printf("%-22s %s\n", s, devratio(mod))
+ }
> #------------------------------------------------------------------------------
> # linear model
>
> n <- 100
> set.seed(2018)
> x1 <- ((1:n) + runif(n, min=0, max=10)) / n
> set.seed(2019)
> x2 <- ((1:n) + runif(n, min=0, max=10)) / n
> y <- 3 * x1 + rnorm(n)
>
> myoffset <- (1:n) / n
> data <- data.frame(y=y, x1=x1, myoffset=myoffset)
>
> lm.weights <- lm(y ~ x1, data=data, weights=sin(myoffset))
> earth.weights <- earth(y ~ x1, data=data, weights=sin(myoffset),
+ linpreds=TRUE, thresh=0, penalty=-1)
> check.earth.matches.lm(earth.weights, lm.weights)
check earth.weights vs lm.weights
>
> myoffset <- (1:n) / n
> data <- data.frame(y=y, x1=x1, myoffset=myoffset)
> lm4 <- lm(y ~ x1 + offset(myoffset), data=data)
> earth4 <- earth(y ~ x1 + offset(myoffset), data=data,
+ linpreds=TRUE, thresh=0, penalty=-1)
> check.earth.matches.lm(earth4, lm4)
check earth4 vs lm4
> cat("==print(earth4)==\n")
==print(earth4)==
> print(earth4)
Selected 2 of 2 terms, and 1 of 1 predictors
Termination condition: No new term increases RSq at 2 terms
Importance: x1
Offset: myoffset with values 0.01, 0.02, 0.03, 0.04, 0.05, 0.06, 0.07, 0...
Number of terms at each degree of interaction: 1 1 (additive model)
GCV 0.8740205 RSS 87.40205 GRSq 0.297288 RSq 0.297288
> cat("==summary(earth4)==\n")
==summary(earth4)==
> print(summary(earth4))
Call: earth(formula=y~x1+offset(myoffset), data=data, linpreds=TRUE, thresh=0,
penalty=-1)
coefficients
(Intercept) -0.0949532
x1 2.0994281
Selected 2 of 2 terms, and 1 of 1 predictors
Termination condition: No new term increases RSq at 2 terms
Importance: x1
Offset: myoffset with values 0.01, 0.02, 0.03, 0.04, 0.05, 0.06, 0.07, 0...
Number of terms at each degree of interaction: 1 1 (additive model)
GCV 0.8740205 RSS 87.40205 GRSq 0.297288 RSq 0.297288
> cat("==summary(earth4, details=TRUE)==\n")
==summary(earth4, details=TRUE)==
> print(summary(earth4, details=TRUE))
Call: earth(formula=y~x1+offset(myoffset), data=data, linpreds=TRUE, thresh=0,
penalty=-1)
coefficients
(Intercept) -0.0949532
x1 2.0994281
Number of cases: 100
Selected 2 of 2 terms, and 1 of 1 predictors
Termination condition: No new term increases RSq at 2 terms
Importance: x1
Offset: myoffset with values 0.01, 0.02, 0.03, 0.04, 0.05, 0.06, 0.07, 0...
Number of terms at each degree of interaction: 1 1 (additive model)
GCV 0.8740205 RSS 87.40205 GRSq 0.297288 RSq 0.297288
>
> par(mfrow=c(4, 2), mar=c(3, 3, 3, 1), mgp=c(1.5, 0.5, 0), oma=c(0, 0, 5, 0))
> set.seed(2018)
> plotmo(lm4, trace=0, pt.col=2, do.par=FALSE)
plotmo grid: x1 myoffset
0.5474997 0.505
> mtext(
+ "row1: lm4\nrow2: earth4\nrow3: lm4 grid.levels=list(myoffset=-3)\nrow4: earth4 grid.levels=list(myoffset=-3)",
+ outer=TRUE, cex=.8)
> set.seed(2018)
> plotmo(earth4, trace=0, pt.col=2, do.par=FALSE)
note: the offset in the formula is not plotted
(use all1=TRUE to plot the offset, or use trace=-1 to silence this message)
plotmo grid: x1 myoffset
0.5474997 0.505
> empty.plot()
> set.seed(2018)
> plotmo(lm4, trace=0, pt.col=2, do.par=FALSE, grid.levels=list(myoffset=-3))
plotmo grid: x1 myoffset
0.5474997 -3
> set.seed(2018)
> plotmo(earth4, trace=0, pt.col=2, do.par=FALSE, grid.levels=list(myoffset=-3))
note: the offset in the formula is not plotted
(use all1=TRUE to plot the offset, or use trace=-1 to silence this message)
plotmo grid: x1 myoffset
0.5474997 -3
> par(old.par)
>
> plotres(lm4)
> plotres(earth4)
>
> # linear model with weights and offset
>
> lm4.weights <- lm(y ~ x1 + offset(exp(myoffset)), data=data, weights=sin(myoffset))
> earth4.weights <- earth(y ~ x1 + offset(exp(myoffset)), data=data, weights=sin(myoffset),
+ linpreds=TRUE, thresh=0, penalty=-1)
> check.earth.matches.lm(earth4.weights, lm4.weights)
check earth4.weights vs lm4.weights
> print(earth4.weights)
Selected 2 of 2 terms, and 1 of 1 predictors
Termination condition: No new term increases RSq at 2 terms
Importance: x1
Offset: exp(myoffset) with values 1.01005, 1.020201, 1.030455, 1.040811,...
Weights: 0.009999833, 0.01999867, 0.0299955, 0.03998933, 0.04997917, 0.0...
Number of terms at each degree of interaction: 1 1 (additive model)
GCV 0.4535297 RSS 45.35297 GRSq 0.1077639 RSq 0.1077639
> print(summary(earth4.weights))
Call: earth(formula=y~x1+offset(exp(myoffset)), data=data,
weights=sin(myoffset), linpreds=TRUE, thresh=0, penalty=-1)
coefficients
(Intercept) -0.9497941
x1 1.4249365
Selected 2 of 2 terms, and 1 of 1 predictors
Termination condition: No new term increases RSq at 2 terms
Importance: x1
Offset: exp(myoffset) with values 1.01005, 1.020201, 1.030455, 1.040811,...
Weights: 0.009999833, 0.01999867, 0.0299955, 0.03998933, 0.04997917, 0.0...
Number of terms at each degree of interaction: 1 1 (additive model)
GCV 0.4535297 RSS 45.35297 GRSq 0.1077639 RSq 0.1077639
>
> #------------------------------------------------------------------------------
> # error handling
>
> data <- data.frame(y=y, x1=x1)
> expect.err(try(earth(y ~ x1 + offset(myoffset), data=data)), "the offset variable 'myoffset' in 'offset(myoffset)' must be in the data")
Error : the offset variable 'myoffset' in 'offset(myoffset)' must be in the data
Got error as expected from try(earth(y ~ x1 + offset(myoffset), data = data))
> expect.err(try(earth(y ~ x1 + offset(myoffset))), "if an offset is specified in the formula, the 'data' argument must be used")
Error : if an offset is specified in the formula, the 'data' argument must be used
Got error as expected from try(earth(y ~ x1 + offset(myoffset)))
>
> data <- data.frame(y=y, x1=x1, offset0=rep(0, length.out=n), offset1=rep(1, length.out=n))
> expect.err(try(earth(y ~ x1 + offset(offset0) + offset(offset1), data=data)), "only one offset is allowed")
Error : only one offset is allowed
Got error as expected from try(earth(y ~ x1 + offset(offset0) + offset(offset1), data = data))
>
> #------------------------------------------------------------------------------
> # poisson model with and without linear predictors
>
> library(MASS)
> data(Insurance)
> Ins <- Insurance
> Ins$Claims[Ins$Claims > 100] <- 100
> Ins$day <- (1:nrow(Insurance)) / nrow(Insurance) # non linear term (like a seasonal effect)
> Ins$Claims <- round(Ins$Claims * (1 + sin(2 * pi * Ins$day)))
> pois <- glm(Claims ~ Group + Age + day + offset(log(Holders)),
+ data = Ins, family = poisson)
> earth.pois.linpreds <- earth(Claims ~ offset(log(Holders)) + Group + Age + day,
+ data = Ins, glm=list(family = poisson),
+ linpreds=TRUE, thresh=0, penalty=-1)
> check.earth.matches.glm(earth.pois.linpreds, pois, newdata=Ins[4:6,])
check earth.pois.linpreds vs pois
> earth.pois <- earth(Claims ~ Group + Age + day + offset(log(Holders)),
+ data = Ins, glm=list(family = poisson))
> cat("==print(earth.pois)==\n")
==print(earth.pois)==
> print(earth.pois)
Earth selected 6 of 14 terms, and 3 of 7 predictors
Termination condition: Reached nk 21
Importance: day, Age.L, Group.L, Group.Q-unused, Group.C-unused, ...
Offset: log(Holders) with values log(197), log(264), log(246), log(1680)...
Number of terms at each degree of interaction: 1 5 (additive model)
Earth GCV 1037.405 RSS 45532.35 GRSq 0.6401826 RSq 0.7453446
GLM (family poisson, link log):
nulldev df dev df devratio AIC iters converged
1935.717 63 462.4144 58 0.761 753.7 5 1
> cat("==summary(earth.pois)==\n")
==summary(earth.pois)==
> print(summary(earth.pois))
Call: earth(formula=Claims~Group+Age+day+offset(log(Holders)), data=Ins,
glm=list(family=poisson))
GLM coefficients
Claims
(Intercept) -1.6274033
h(-0.223607-Group.L) 0.6591962
h(Group.L- -0.223607) 1.2356444
h(Age.L-0.223607) -2.1045753
h(day-0.421875) -10.3890623
h(day-0.578125) 12.8123676
Earth selected 6 of 14 terms, and 3 of 7 predictors
Termination condition: Reached nk 21
Importance: day, Age.L, Group.L, Group.Q-unused, Group.C-unused, ...
Offset: log(Holders) with values log(197), log(264), log(246), log(1680)...
Number of terms at each degree of interaction: 1 5 (additive model)
Earth GCV 1037.405 RSS 45532.35 GRSq 0.6401826 RSq 0.7453446
GLM (family poisson, link log):
nulldev df dev df devratio AIC iters converged
1935.717 63 462.4144 58 0.761 753.7 5 1
> cat("==summary(earth.pois, details=TRUE)==\n")
==summary(earth.pois, details=TRUE)==
> print(summary(earth.pois, details=TRUE))
Call: earth(formula=Claims~Group+Age+day+offset(log(Holders)), data=Ins,
glm=list(family=poisson))
Earth coefficients
Claims
(Intercept) 86.23803
h(-0.223607-Group.L) -71.10802
h(Group.L- -0.223607) -38.47796
h(Age.L-0.223607) 122.63443
h(day-0.421875) -545.81580
h(day-0.578125) 583.55576
GLM coefficients
Claims
(Intercept) -1.6274033
h(-0.223607-Group.L) 0.6591962
h(Group.L- -0.223607) 1.2356444
h(Age.L-0.223607) -2.1045753
h(day-0.421875) -10.3890623
h(day-0.578125) 12.8123676
GLM deviance residuals:
Min 1Q Median 3Q Max
-6.7202654 -1.8420108 -0.2845822 1.2156207 6.5715831
GLM coefficients (family poisson, link log)
Estimate Std. Error z value Pr(>|z|)
(Intercept) -1.62740331 0.03503726 -46.44779 < 2.22e-16
h(Age.L-0.223607) -2.10457528 0.08442153 -24.92937 < 2.22e-16
h(Group.L- -0.223607) 1.23564436 0.06612011 18.68788 < 2.22e-16
h(-0.223607-Group.L) 0.65919622 0.11236250 5.86669 4.4457e-09
h(day-0.421875) -10.38906233 0.65640911 -15.82711 < 2.22e-16
h(day-0.578125) 12.81236762 1.02262981 12.52884 < 2.22e-16
Number of cases: 64
Earth selected 6 of 14 terms, and 3 of 7 predictors
Termination condition: Reached nk 21
Importance: day, Age.L, Group.L, Group.Q-unused, Group.C-unused, ...
Offset: log(Holders) with values log(197), log(264), log(246), log(1680)...
Number of terms at each degree of interaction: 1 5 (additive model)
Earth GCV 1037.405 RSS 45532.35 GRSq 0.6401826 RSq 0.7453446
GLM (family poisson, link log):
nulldev df dev df devratio AIC iters converged
1935.717 63 462.4144 58 0.761 753.7 5 1
> earth.pois.no.penalty <- earth(Claims ~ Group + Age + day + offset(log(Holders)),
+ data = Ins, glm=list(family = poisson),
+ thresh=0, penalty=-1)
> print.devratio("pois", pois)
pois devratio 0.62
> print.devratio("earth.pois.linpreds", earth.pois.linpreds$glm.list[[1]])
earth.pois.linpreds devratio 0.62
> print.devratio("earth.pois", earth.pois$glm.list[[1]])
earth.pois devratio 0.76
> print.devratio("earth.pois.no.penalty", earth.pois.no.penalty$glm.list[[1]])
earth.pois.no.penalty devratio 0.90
>
> par(mfrow=c(3, 4), mar=c(3, 3, 3, 1), mgp=c(1.5, 0.5, 0), oma=c(0, 0, 5, 0))
> set.seed(2018)
> plotmo(pois, trace=0, pt.col=2, do.par=FALSE, ylim=c(0,50))
plotmo grid: Group Age day Holders
<1l <25 0.5078125 136
> mtext(sprint(
+ "row1: pois (%s)\nrow2: earth.pois.linpreds (%s)\nrow3: earth.pois.linpreds(all1=TRUE)",
+ devratio(pois), devratio(earth.pois.linpreds)),
+ outer=TRUE, cex=.8)
> set.seed(2018)
> plotmo(earth.pois.linpreds, trace=0, pt.col=2, do.par=FALSE, ylim=c(0,50))
note: the offset in the formula is not plotted
(use all1=TRUE to plot the offset, or use trace=-1 to silence this message)
plotmo grid: Holders Group Age day
136 <1l <25 0.5078125
> empty.plot()
> set.seed(2018)
> plotmo(earth.pois.linpreds, all1=T, trace=-1, pt.col=2, do.par=FALSE, ylim=c(0,50))
> par(old.par)
>
> plotres(pois, type="response", caption='pois, type="response"')
> plotres(earth.pois.linpreds, type="response", caption='earth.pois.linpreds, type="response"')
>
> par(mfrow=c(3, 4), mar=c(3, 3, 3, 1), mgp=c(1.5, 0.5, 0), oma=c(0, 0, 5, 0))
> set.seed(2018)
> plotmo(pois, trace=0, pt.col=2, do.par=FALSE, ylim=c(0,50), grid.levels=list(Holders=20))
plotmo grid: Group Age day Holders
<1l <25 0.5078125 20
> mtext(
+ "----- grid.levels=list(Holders=20)) -----\nrow1: pois\nrow2: earth.pois.linpreds\nrow3: earth.pois.linpreds(all1=TRUE)",
+ outer=TRUE, cex=.8)
> set.seed(2018)
> plotmo(earth.pois.linpreds, trace=0, pt.col=2, do.par=FALSE, ylim=c(0,50), grid.levels=list(Holders=20))
note: the offset in the formula is not plotted
(use all1=TRUE to plot the offset, or use trace=-1 to silence this message)
plotmo grid: Holders Group Age day
20 <1l <25 0.5078125
> empty.plot()
> set.seed(2018)
> plotmo(earth.pois.linpreds, all1=T, trace=-1, pt.col=2, do.par=FALSE, ylim=c(0,50), grid.levels=list(Holders=20))
> par(old.par)
>
> plotmo(earth.pois.linpreds, pmethod="partdep", do.par=2,
+ caption=sprint("earth.pois.linpreds, pmethod=\"partdep\", %s", devratio(earth.pois.linpreds)))
note: the offset in the formula is not plotted
(use all1=TRUE to plot the offset, or use trace=-1 to silence this message)
calculating partdep for Group
calculating partdep for Age
calculating partdep for day
> plotmo(earth.pois.linpreds, pmethod="partdep", do.par=0,
+ grid.levels=list(Age=">35"), degree1="day", main="day with Age=\">35\"")
note: the offset in the formula is not plotted
(use all1=TRUE to plot the offset, or use trace=-1 to silence this message)
calculating partdep for day
> plotmo(earth.pois, pmethod="partdep",
+ caption=sprint("earth.pois, pmethod=\"partdep\", %s", devratio(earth.pois)))
note: the offset in the formula is not plotted
(use all1=TRUE to plot the offset, or use trace=-1 to silence this message)
calculating partdep for Group
calculating partdep for Age
calculating partdep for day
> plotmo(earth.pois.no.penalty, pmethod="partdep",
+ caption=sprint("earth.pois.no.penalty, pmethod=\"partdep\", %s", devratio(earth.pois.no.penalty)))
note: the offset in the formula is not plotted
(use all1=TRUE to plot the offset, or use trace=-1 to silence this message)
calculating partdep for Group
calculating partdep for Age
calculating partdep for day
>
> #------------------------------------------------------------------------------
> # poisson model with weights
>
> Ins <- Insurance
> Ins$Claims[Ins$Claims > 100] <- 100
> Ins$day <- (1:nrow(Insurance)) / nrow(Insurance) # non linear term (like a seasonal effect)
> Ins$Claims <- round(Ins$Claims * (1 + sin(2 * pi * Ins$day)))
> weights <- 1:nrow(Ins)
>
> pois.weights <- glm(Claims ~ Group + Age + day,
+ data = Ins, family = poisson, weights=weights)
>
> earth.pois.linpreds.weights <- earth(Claims ~ Group + Age + day,
+ data = Ins, glm=list(family = poisson),
+ weights=weights,
+ linpreds=TRUE, thresh=0, penalty=-1)
> check.earth.matches.glm(earth.pois.linpreds.weights, pois.weights, newdata=Ins[1:3,])
check earth.pois.linpreds.weights vs pois.weights
>
> #------------------------------------------------------------------------------
> # poisson model with weights, some of which are zero
>
> Ins <- Insurance
> Ins$Claims[Ins$Claims > 100] <- 100
> Ins$day <- (1:nrow(Insurance)) / nrow(Insurance) # non linear term (like a seasonal effect)
> Ins$Claims <- round(Ins$Claims * (1 + sin(2 * pi * Ins$day)))
> weights <- 1:nrow(Ins)
> weights[4] <- 0
> weights[8] <- 0
>
> pois.weights.some.zero <- glm(Claims ~ Group + Age + day,
+ data = Ins, family = poisson, weights=weights)
>
> earth.pois.linpreds.weights.some.zero <- earth(Claims ~ Group + Age + day,
+ data = Ins, glm=list(family = poisson),
+ weights=weights,
+ linpreds=TRUE, thresh=0, penalty=-1)
> check.earth.matches.glm(earth.pois.linpreds.weights.some.zero, pois.weights.some.zero, newdata=Ins[1:3,],
+ max=1e-5, max.residuals=1e-2) # TODO why does max.residuals have to be so big here?
check earth.pois.linpreds.weights.some.zero vs pois.weights.some.zero
>
> plotres(pois.weights.some.zero, caption="pois.weights.some.zero")
> plotres(earth.pois.linpreds.weights.some.zero, caption="earth.pois.linpreds.weights.some.zero")
> plotmo(pois.weights.some.zero, caption="pois.weights.some.zero")
plotmo grid: Group Age day
<1l <25 0.5078125
> plotmo(earth.pois.linpreds.weights.some.zero, caption="earth.pois.linpreds.weights.some.zero")
plotmo grid: Group Age day
<1l <25 0.5078125
>
> #------------------------------------------------------------------------------
> # multiple response models
>
> data(trees)
> tr <- trees
> set.seed(2018)
> tr$Vol2 <- tr$Volume + 10 * rnorm(nrow(tr))
>
> earth10 <- earth(Volume ~ Girth + offset(log(Height)), data=tr,
+ linpreds=TRUE, thresh=0, penalty=-1)
> lm10 <- lm(Volume ~ Girth + offset(log(Height)), data=tr)
> check.earth.matches.lm(earth10, lm10, newdata=tr[c(3:5),])
check earth10 vs lm10
> cat("earth10:\n")
earth10:
> print(summary(earth10))
Call: earth(formula=Volume~Girth+offset(log(Height)), data=tr, linpreds=TRUE,
thresh=0, penalty=-1)
coefficients
(Intercept) -41.083623
Girth 5.051736
Selected 2 of 2 terms, and 1 of 1 predictors
Termination condition: No new term increases RSq at 2 terms
Importance: Girth
Offset: log(Height) with values log(70), log(65), log(63), log(72), log(...
Number of terms at each degree of interaction: 1 1 (additive model)
GCV 16.66619 RSS 516.6518 GRSq 0.9358693 RSq 0.9358693
>
> earth20 <- earth(Vol2 ~ Girth + offset(log(Height)), data=tr,
+ linpreds=TRUE, thresh=0, penalty=-1)
> cat("earth20:\n")
earth20:
> print(summary(earth20))
Call: earth(formula=Vol2~Girth+offset(log(Height)), data=tr, linpreds=TRUE,
thresh=0, penalty=-1)
coefficients
(Intercept) -39.29178
Girth 4.85556
Selected 2 of 2 terms, and 1 of 1 predictors
Termination condition: No new term increases RSq at 2 terms
Importance: Girth
Offset: log(Height) with values log(70), log(65), log(63), log(72), log(...
Number of terms at each degree of interaction: 1 1 (additive model)
GCV 97.46317 RSS 3021.358 GRSq 0.6974626 RSq 0.6974626
>
> earth30 <- earth(cbind(Volume, Vol2) ~ Girth + offset(log(Height)), data=tr,
+ linpreds=TRUE, thresh=0, penalty=-1)
> cat("earth30:\n")
earth30:
> print(summary(earth30))
Call: earth(formula=cbind(Volume,Vol2)~Girth+offset(log(Height)), data=tr,
linpreds=TRUE, thresh=0, penalty=-1)
Volume Vol2
(Intercept) -41.083623 -39.29178
Girth 5.051736 4.85556
Selected 2 of 2 terms, and 1 of 1 predictors
Termination condition: No new term increases RSq at 2 terms
Importance: Girth
Offset: log(Height) with values log(70), log(65), log(63), log(72), log(...
Number of terms at each degree of interaction: 1 1 (additive model)
GCV RSS GRSq RSq
Volume 16.66619 516.6518 0.9358692 0.9358692
Vol2 97.46317 3021.3583 0.6974626 0.6974626
All 114.12936 3538.0101 0.8039118 0.8039118
>
> plotmo(lm10, all1=TRUE, pt.col=2)
plotmo grid: Girth Height
12.9 76
> plotmo(earth10, all1=TRUE, pt.col=2)
plotmo grid: Girth Height
12.9 76
> plotmo(earth20, all1=TRUE, pt.col=2)
plotmo grid: Girth Height
12.9 76
> plotmo(earth30, nresponse=1, all1=TRUE, pt.col=2)
plotmo grid: Girth Height
12.9 76
> plotmo(earth30, nresponse=2, all1=TRUE, pt.col=2)
plotmo grid: Girth Height
12.9 76
>
> plotres(lm10)
> plotres(earth10)
> plotres(earth20)
> plotres(earth30, nresponse=2)
> plotres(earth30, nresponse=1)
>
> # multiple response pois model with weights (basic test)
>
> Ins <- Insurance
> Ins$Claims[Ins$Claims > 100] <- 100
> Ins$day <- (1:nrow(Insurance)) / nrow(Insurance) # non linear term (like a seasonal effect)
> Ins$Claims <- round(Ins$Claims * (1 + sin(2 * pi * Ins$day)))
> Ins$Claims2 <- Insurance$Claims2 <- round(Insurance$Claims^1.5)
> weights <- 1:nrow(Ins)
> weights[4] <- 0
> weights[8] <- 0
>
> earth.pois.multiple.response <-
+ earth(x=Insurance$Age, y=cbind(Insurance$Claims, Insurance$Claims2),
+ trace=1, # Insurance$Age expands to x.L x.Q x.C
+ glm=list(family = poisson), weights=weights)
x[64,3] with colnames x.L x.Q x.C
y[64,2] with colnames y1 y2
weights[64,1] with no column names
Forward pass term 1, 2, 4
RSq changed by less than 0.001 at 3 terms (DeltaRSq 5.7e-05)
After forward pass GRSq 0.175 RSq 0.325
Prune method "backward" penalty 2 nprune null: selected 2 of 3 terms, and 1 of 3 preds
After pruning pass GRSq 0.278 RSq 0.323
GLM y1 devratio 0.58 dof 62/63 iters 5
GLM y2 devratio 0.57 dof 62/63 iters 6
> cat("earth.pois.multiple.response:\n")
earth.pois.multiple.response:
> print(earth.pois.multiple.response)
Earth selected 2 of 3 terms, and 1 of 3 predictors
Termination condition: RSq changed by less than 0.001 at 3 terms
Importance: x.L, x.Q-unused, x.C-unused
Weights: 1, 2, 3, 3.23125e-07, 5, 6, 7, 3.23125e-07, 9, 10, 11, 12, 13, ...
Number of terms at each degree of interaction: 1 1 (additive model)
Earth
GCV RSS GRSq RSq
y1 45124.4 2623560 0.4258811 0.4617544
y2 12454506.9 724112813 0.2772184 0.3223809
All 12499631.2 726736372 0.2778934 0.3230137
GLM (family poisson, link log):
nulldev df dev df devratio AIC iters converged
y1 94934.7 63 39830.03 62 0.580 49300 5 1
y2 1613414.9 63 698038.19 62 0.567 710400 6 1
> cat("summary(earth.pois.multiple.response:\n")
summary(earth.pois.multiple.response:
> print(summary(earth.pois.multiple.response))
Call: earth(x=Insurance$Age, y=cbind(Insurance$Claims,Insurance$Claims2),
weights=weights, trace=1, glm=list(family=poisson))
GLM coefficients
y1 y2
(Intercept) 2.693536 4.327129
h(x.L-0.223607) 4.045294 5.791490
Earth selected 2 of 3 terms, and 1 of 3 predictors
Termination condition: RSq changed by less than 0.001 at 3 terms
Importance: x.L, x.Q-unused, x.C-unused
Weights: 1, 2, 3, 3.23125e-07, 5, 6, 7, 3.23125e-07, 9, 10, 11, 12, 13, ...
Number of terms at each degree of interaction: 1 1 (additive model)
Earth
GCV RSS GRSq RSq
y1 45124.4 2623560 0.4258811 0.4617544
y2 12454506.9 724112813 0.2772184 0.3223809
All 12499631.2 726736372 0.2778934 0.3230137
GLM (family poisson, link log):
nulldev df dev df devratio AIC iters converged
y1 94934.7 63 39830.03 62 0.580 49300 5 1
y2 1613414.9 63 698038.19 62 0.567 710400 6 1
> plotmo(earth.pois.multiple.response, nresponse=1, pt.col=2)
>
> # test update.earth with weights and offset
>
> data(trees)
> tr <- trees
> set.seed(2018)
> tr$Vol2 <- tr$Volume + 10 * rnorm(nrow(tr))
> my.weights <- 1:nrow(tr)
> my.weights[3] <- 0
>
> earth30 <- earth(Volume ~ Girth + offset(log(Height)), data=tr,
+ linpreds=TRUE, thresh=0, penalty=-1)
> lm30 <- lm(Volume ~ Girth + offset(log(Height)), data=tr)
> check.earth.matches.lm(earth30, lm30, newdata=tr[c(3:5),])
check earth30 vs lm30
>
> lm31 <- lm(Volume ~ Girth, data=tr)
> earth31 <- earth(Volume ~ Girth, data=tr,
+ linpreds=TRUE, thresh=0, penalty=-1)
> earth31.offset <- update(earth31, formula.=Volume ~ Girth + offset(log(Height)))
> check.earth.matches.lm(earth31.offset, lm30, newdata=tr[c(3:5),])
check earth31.offset vs lm30
> earth.nooffset <- update(earth31.offset, formula.=Volume ~ Girth)
> check.earth.matches.lm(earth.nooffset, lm31, newdata=tr[c(3:5),])
check earth.nooffset vs lm31
>
> lm31.weights <- lm(Volume ~ Girth, data=tr, weights=my.weights)
> earth31.weights <- update(earth31, weights=my.weights)
> # lower max is needed below because of zeros in my.weights
> check.earth.matches.lm(earth31.weights, lm31.weights, newdata=tr[c(3:5),], max=1e-6, max.residuals=1e-6)
check earth31.weights vs lm31.weights
>
> lm31.weights.offset <- lm(Volume ~ Girth + offset(log(Height)), data=tr, weights=my.weights)
> earth31.weights.offset <- update(earth31.weights, formula=Volume ~ Girth + offset(log(Height)))
> check.earth.matches.lm(earth31.weights.offset, lm31.weights.offset, newdata=tr[c(3:5),], max=1e-6, max.residuals=1e-6)
check earth31.weights.offset vs lm31.weights.offset
> cat("earth31.weights.offset:\n")
earth31.weights.offset:
> print(summary(earth31.weights.offset))
Call: earth(formula=Volume~Girth+offset(log(Height)), data=tr,
weights=c(1,2,0,4,5,6,7...), linpreds=TRUE, thresh=0, penalty=-1)
coefficients
(Intercept) -49.507279
Girth 5.594527
Selected 2 of 2 terms, and 1 of 1 predictors
Termination condition: No new term increases RSq at 2 terms
Importance: Girth
Offset: log(Height) with values log(70), log(65), log(63), log(72), log(...
Weights: 1, 2, 1.590323e-07, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 1...
Number of terms at each degree of interaction: 1 1 (additive model)
GCV 292.7582 RSS 9075.505 GRSq 0.9339319 RSq 0.9339319
>
> source("test.epilog.R")
|