-
Notifications
You must be signed in to change notification settings - Fork 23
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
ols_step_both_p(): Error in if (pvals[minp] <= pent) {: argument is of length zero #175
Comments
Hi @DrMondesire, Thanks for sharing a reproducible example. We have recently fixed this issue in the development version. The fix will be available on CRAN next month. In the meanwhile, please install the development version of olsrr from GitHub as a temporary solution.
I have reproduced the example below. Please do let us know if you run into any other issues.
|
Regarding library(olsrr)
#>
#> Attaching package: 'olsrr'
#> The following object is masked from 'package:datasets':
#>
#> rivers
library(readr)
library(dplyr)
#>
#> Attaching package: 'dplyr'
#> The following objects are masked from 'package:stats':
#>
#> filter, lag
#> The following objects are masked from 'package:base':
#>
#> intersect, setdiff, setequal, union
library(rsample)
# read data
finance <- read_csv("https://mondesirecis544quiz1.s3.amazonaws.com/finances/2018_Financial_Data.csv")
#>
#> -- Column specification ------------------------------------
#> cols(
#> .default = col_double(),
#> Symbol = col_character(),
#> Sector = col_character()
#> )
#> i Use `spec()` for the full column specifications.
# Select only the columns for prediction
financeSelect <- select(finance, Revenue, `Operating Income`, `Earnings before Tax`, `Net Income`)
# Remove NAs
cleanedFinances <- na.omit(financeSelect)
# split into train and test
set.seed(1353)
data_split <- rsample::initial_split(cleanedFinances)
train_data <- rsample::training(data_split)
test_data <- rsample::testing(data_split)
# LM: Predict Revenue when given Income and Earning Data
lmModel <- lm(Revenue ~ `Operating Income` + `Earnings before Tax` + `Net Income`, data = train_data)
lmModel
#>
#> Call:
#> lm(formula = Revenue ~ `Operating Income` + `Earnings before Tax` +
#> `Net Income`, data = train_data)
#>
#> Coefficients:
#> (Intercept) `Operating Income` `Earnings before Tax`
#> 2.030e+09 1.645e+00 5.807e+00
#> `Net Income`
#> -2.437e+00
# Stepwise Both Directions
stepwiseBothModel <- ols_step_both_aic(lmModel)
# stepwise summary
stepwiseBothModel
#>
#>
#> Stepwise Summary
#> -------------------------------------------------------------------------------------------------------
#> Variable Method AIC RSS Sum Sq R-Sq Adj. R-Sq
#> -------------------------------------------------------------------------------------------------------
#> `Earnings before Tax` addition 157018.025 8.851326e+23 6.604662e+23 0.42732 0.42714
#> `Operating Income` addition 156987.778 8.761034e+23 6.694954e+23 0.43316 0.43280
#> `Net Income` addition 156974.448 8.718431e+23 6.737557e+23 0.43592 0.43538
#> -------------------------------------------------------------------------------------------------------
# stepwise model
stepwiseBothModel$model
#>
#> Call:
#> lm(formula = paste(response, "~", paste(preds, collapse = " + ")),
#> data = l)
#>
#> Coefficients:
#> (Intercept) `Earnings before Tax` `Operating Income`
#> 2.030e+09 5.807e+00 1.645e+00
#> `Net Income`
#> -2.437e+00
# predict
stepwisePrediction <- predict(stepwiseBothModel$model, test_data)
stepwisePrediction
#> 1 2 3 4 5 6
#> 17104768187 -93875794170 396973832540 3564820894 87264581558 29750234601
#> 7 8 9 10 11 12
#> 550218192 1832517325 17427446774 4121332625 45694129495 34360854778
#> 13 14 15 16 17 18
#> 13941713000 47490730029 5157363319 7248929645 16496489439 16983022847
#> 19 20 21 22 23 24
#> 2811847787 14425406968 1696093593 30379799005 2477872130 -3968881015
#> 25 26 27 28 29 30
#> 31015328037 3722391379 17292584387 5666477564 3317867838 15316382118
#> 31 32 33 34 35 36
#> 12183776046 4336333676 54281714845 38400254575 3514434409 1847175271
#> 37 38 39 40 41 42
#> 38304635355 2073309406 4131238108 3301694683 63367398095 2281561773
#> 43 44 45 46 47 48
#> 2613745190 24283379780 2857010074 34673083116 16146831132 34531433040
#> 49 50 51 52 53 54
#> 14395133299 4489950476 -662114642 5686236342 13733347698 2351740367
#> 55 56 57 58 59 60
#> 66506937044 3070293046 13185271103 13597695213 5219057110 3011548185
#> 61 62 63 64 65 66
#> 4590067602 22994300572 7995004290 21489616132 13676427480 10391149571
#> 67 68 69 70 71 72
#> 5160031026 5157045072 1730084261 3154142061 36520372842 24963577428
#> 73 74 75 76 77 78
#> 1709014261 11219132911 18611040192 1968704859 3213355891 14900426133
#> 79 80 81 82 83 84
#> 1736372402 5485849721 21458522451 4788321593 12588970731 1883422360
#> 85 86 87 88 89 90
#> 3847115444 -1999054830 3439582030 8288151742 5576160752 173182001547
#> 91 92 93 94 95 96
#> 6173283024 3141440182 -532427371 10547728602 2698040922 3138485160
#> 97 98 99 100 101 102
#> 7338238961 5398356337 4426541340 2307550862 36865879274 8253593631
#> 103 104 105 106 107 108
#> 4828279344 1720796866 16643978510 1019695091 22441917646 2623933902
#> 109 110 111 112 113 114
#> 3051302542 10031880544 752292257 12729885277 2992764150 4319420021
#> 115 116 117 118 119 120
#> 3253364361 9449121572 7104944568 2128118010 5364768531 3246664550
#> 121 122 123 124 125 126
#> -35708500134 2483508722 2026476459 4109728872 9501905016 28116054257
#> 127 128 129 130 131 132
#> -6610597156 10616172765 1944615064 2459859662 28488098525 14992189350
#> 133 134 135 136 137 138
#> 5002942401 21391289752 14181305716 2173020280 47810649310 1901078536
#> 139 140 141 142 143 144
#> 4401762391 1885900127 2609897678 4683802984 351098265 5591486060
#> 145 146 147 148 149 150
#> 5093419750 20936595066 15685340603 3874259450 11251618407 -435383500
#> 151 152 153 154 155 156
#> 11326879547 1572714571 6043451109 3226172419 8909324721 2851592018
#> 157 158 159 160 161 162
#> 2380230904 2799760075 3260828651 2987480608 4257125569 2389395873
#> 163 164 165 166 167 168
#> 2184796619 1506064305 1646086189 7644438324 19078413957 62282579594
#> 169 170 171 172 173 174
#> 8593219518 1931164651 2365288127 151411628 6385515432 432961475
#> 175 176 177 178 179 180
#> 1608478768 10997433374 1088722805 2353785373 2070669025 20038180173
#> 181 182 183 184 185 186
#> 2120694893 17527822754 1810634339 484003023 2097895335 4401132350
#> 187 188 189 190 191 192
#> 3421056755 2908281447 2905499015 15467655572 11141142574 1092097616
#> 193 194 195 196 197 198
#> 5129394694 2227344108 3315946735 1939329176 9080122619 1953088733
#> 199 200 201 202 203 204
#> 2477865823 21976690498 3488050722 2517573372 1219016172 3608169713
#> 205 206 207 208 209 210
#> 5083867505 7379378888 1648676961 7053227393 4068263197 5093535905
#> 211 212 213 214 215 216
#> 3815563800 9596597909 4377107711 3415949028 3368053454 1460403611
#> 217 218 219 220 221 222
#> 1161136302 1978255554 15737811026 2335887577 4180496992 8119159258
#> 223 224 225 226 227 228
#> 18528253403 4772003392 3399670169 3152350721 1393846773 2087713676
#> 229 230 231 232 233 234
#> 3507557456 8702332032 4982489669 4173860245 2597617747 5788349357
#> 235 236 237 238 239 240
#> 2937530661 2126493425 6173995387 2303154605 1912957849 8983605351
#> 241 242 243 244 245 246
#> 2356317772 12568147153 10460470916 1762776977 2166748985 27042567344
#> 247 248 249 250 251 252
#> 2458496412 2898584066 3544068284 1905114070 1817751440 2036067359
#> 253 254 255 256 257 258
#> 5180218434 4336355088 6184073542 6666083833 7099165654 1324066092
#> 259 260 261 262 263 264
#> 1443170936 2807557477 4636710989 5610476348 1013598586 4029593588
#> 265 266 267 268 269 270
#> 9422299463 7969823899 -12363224457 1808962211 1948339270 2534905567
#> 271 272 273 274 275 276
#> 6030049523 1743661714 1661556779 2217153987 2166714087 3072714240
#> 277 278 279 280 281 282
#> 1921147352 8127518724 2166455703 2377559139 31766807814 14949257520
#> 283 284 285 286 287 288
#> 3412885014 2601905419 2449982283 2357999546 2424492888 3280970396
#> 289 290 291 292 293 294
#> 5216282634 6367316717 3605342767 2163265191 3013467888 4862955295
#> 295 296 297 298 299 300
#> 2559945235 3387967665 3276736563 25908154322 1483762459 3804289476
#> 301 302 303 304 305 306
#> 4775177353 3591026048 2148211470 1744593098 1831923027 2009740622
#> 307 308 309 310 311 312
#> 3567212514 3625253229 2220797839 2375499785 2338311819 2615102940
#> 313 314 315 316 317 318
#> 2192381566 4508087460 1621312327 2133860826 5052679427 2536532199
#> 319 320 321 322 323 324
#> 146007505860 1962449250 3363469671 4545943702 1961704370 2000935825
#> 325 326 327 328 329 330
#> 2510704142 2342543118 2713363246 18757013689 3651687889 2749665556
#> 331 332 333 334 335 336
#> 2222287871 1724854506 6921218761 2366054284 4712728760 3495505598
#> 337 338 339 340 341 342
#> 4444097621 1479278287 3802656694 930517313 2493689718 1963609312
#> 343 344 345 346 347 348
#> 2348434367 2954200154 3228382707 1569560036 4465158201 4623279065
#> 349 350 351 352 353 354
#> 1758600754 2337653195 3210110998 2048231068 2476690901 1968637526
#> 355 356 357 358 359 360
#> 5014856049 1750142834 1864457876 1518543183 5683242499 1706532691
#> 361 362 363 364 365 366
#> 2571818210 1500873848 3654833591 6070891399 2325464476 2066917700
#> 367 368 369 370 371 372
#> 2878318193 1872006295 2700549644 1988748713 5398139158 2330773538
#> 373 374 375 376 377 378
#> 3071137878 3028056599 1961087036 3096475690 1997261459 2117700800
#> 379 380 381 382 383 384
#> 2444620161 2115146100 2412001706 1537954303 1598229329 1668620907
#> 385 386 387 388 389 390
#> 2827456282 6370062427 2001920741 2519354723 1529810255 2038746113
#> 391 392 393 394 395 396
#> 2525349139 4678300196 2011002209 2042890563 1994250883 4085893415
#> 397 398 399 400 401 402
#> 3794163586 5633470522 1424202457 -295560426 1833782532 2516409468
#> 403 404 405 406 407 408
#> 1906157543 2211615651 2340689973 1863061933 6954713274 1872311918
#> 409 410 411 412 413 414
#> 1862080237 1483237227 4055411312 4488132050 1651156236 1848387983
#> 415 416 417 418 419 420
#> 28322538300 2373609821 2050888075 3399926842 2766436141 1922285781
#> 421 422 423 424 425 426
#> 2280685707 1537892472 1865874697 2018800430 1681019274 2712702133
#> 427 428 429 430 431 432
#> 2432891479 2259990301 2387843405 6493582101 3232331128 2053730785
#> 433 434 435 436 437 438
#> 2855093526 2450356625 2101015982 6400353941 3050310999 4920835123
#> 439 440 441 442 443 444
#> 2081207683 1976291446 3086086355 3884436538 3060169256 2374815001
#> 445 446 447 448 449 450
#> 5844875558 1653741382 3244115531 2228836982 1970548791 2591805125
#> 451 452 453 454 455 456
#> 2503978687 2009457146 2974888414 1990086827 1757673969 2301908812
#> 457 458 459 460 461 462
#> 2167159433 2472007266 1886486960 2259476243 1407149304 2347276728
#> 463 464 465 466 467 468
#> 141172724 3303653827 3487598838 9680097038 1926929499 2255267306
#> 469 470 471 472 473 474
#> 2049789274 11917002690 3112463988 2292158779 1454157191 2263523104
#> 475 476 477 478 479 480
#> 5678500082 2050154680 2286290136 4847052394 2330403016 2490825230
#> 481 482 483 484 485 486
#> 2498656461 1807733159 15780262670 4297974472 2123052157 3509086912
#> 487 488 489 490 491 492
#> 3194660522 2006010146 2722974467 2001655582 3936295811 1512773344
#> 493 494 495 496 497 498
#> 1946858831 834674097 1718014879 2515423355 3076323544 1665462997
#> 499 500 501 502 503 504
#> 4621109438 3526850428 1690680507 3243166503 -458795512 2006356344
#> 505 506 507 508 509 510
#> 2012101091 1942935125 2954955838 2273472924 4454118751 2334741079
#> 511 512 513 514 515 516
#> 2521161438 1812173933 2113569713 2946579284 2492276514 2184165859
#> 517 518 519 520 521 522
#> 1796134285 2349525697 1633939511 1658904884 2033429295 3245651295
#> 523 524 525 526 527 528
#> 4860603003 9967393393 2264411367 2312922852 3680168227 3207432383
#> 529 530 531 532 533 534
#> 1332611642 1672351185 2818144387 1446168121 1839253367 1872310582
#> 535 536 537 538 539 540
#> 1779155038 1977804437 2773432291 2218685772 2175647421 2197195660
#> 541 542 543 544 545 546
#> 1944516381 2199842567 1985344398 2323239549 1576401331 3285798401
#> 547 548 549 550 551 552
#> 2508882980 1424874625 1856669474 2008221726 2474477210 2817754936
#> 553 554 555 556 557 558
#> 2125887405 2612557953 2123369184 2244771335 2073244643 2761754312
#> 559 560 561 562 563 564
#> 2740039070 2215619075 2306041894 1921146528 2095529655 2705120639
#> 565 566 567 568 569 570
#> 2389552571 1498878488 4090017282 1880757270 3220760957 1038821563
#> 571 572 573 574 575 576
#> 2040733718 5606460876 1499682804 2662793368 2737627364 2065617824
#> 577 578 579 580 581 582
#> 3827917027 2132152644 1940002604 1501627866 1922888410 2109013039
#> 583 584 585 586 587 588
#> 1738248743 2278152715 2683198224 4328299270 1966183466 1901842946
#> 589 590 591 592 593 594
#> 1695519142 2013193468 1995080156 1970247496 1942782840 -490073297
#> 595 596 597 598 599 600
#> 1977866302 2342280012 2213470246 1933141807 6516705709 2103221353
#> 601 602 603 604 605 606
#> 2242747414 2251743928 4791103784 2303723357 2206013560 2313155506
#> 607 608 609 610 611 612
#> 3719602501 2299999541 2016875281 3044661082 2576791357 2073955163
#> 613 614 615 616 617 618
#> 2120516561 2412012035 628192984 2658474155 2468518097 2134619380
#> 619 620 621 622 623 624
#> 4186153713 2085389995 2327091483 1581205054 2248141225 2030991651
#> 625 626 627 628 629 630
#> 2258447642 2287212876 2054282267 2001018856 2013987269 2421744781
#> 631 632 633 634 635 636
#> 2158532384 1993815342 2471458983 2147613539 2964744046 3053956029
#> 637 638 639 640 641 642
#> 1851773000 1426479014 3431144621 1978267114 1977858056 3180799579
#> 643 644 645 646 647 648
#> 3188788211 1880193281 1505968454 1542880980 2991127487 1965613177
#> 649 650 651 652 653 654
#> 2196341545 3279777217 1769799959 1943204726 1025685376 1835218890
#> 655 656 657 658 659 660
#> 2270923467 4101294033 2866819297 1983002999 2982260113 1979004532
#> 661 662 663 664 665 666
#> 2387370384 2088915410 4069775963 1909426190 1716987361 2685707628
#> 667 668 669 670 671 672
#> 1899731530 8182675387 2430938075 1945694209 2138368657 3144821006
#> 673 674 675 676 677 678
#> 2605496827 2546459448 1936216568 2198568987 2255605329 2496867926
#> 679 680 681 682 683 684
#> 2241652576 2005286983 2606974809 1989426801 2418193798 1875697587
#> 685 686 687 688 689 690
#> 2896028936 1725683893 2071912439 1934715974 2120950856 2184169920
#> 691 692 693 694 695 696
#> 2223675377 1979948382 2714908067 2020281575 2129165135 2046877312
#> 697 698 699 700 701 702
#> 1722968311 2528832014 2301752788 1956688724 2374649816 2008083110
#> 703 704 705 706 707 708
#> 1907312604 2024868964 2398475712 2340304510 2615837258 1838413986
#> 709 710 711 712 713 714
#> 2034479292 2682989259 1957085587 2417903958 1944443741 2012914434
#> 715 716 717 718 719 720
#> 2481361124 1963558190 2128150041 1924210811 2303165659 2236139055
#> 721 722 723 724 725 726
#> 1963298577 2013278722 2303179418 2093970527 12605340862 2492069055
#> 727 728 729 730 731 732
#> 2523779869 2347063229 2064590971 2070840688 1993802365 1894044090
#> 733 734 735 736 737 738
#> 1662140721 1975961334 2864631622 1895141666 2271346322 2518993712
#> 739 740 741 742 743 744
#> 2102763389 2035388795 1526763342 4918638805 2253043092 2075265439
#> 745 746 747 748 749 750
#> 2122005360 2101967317 1832607396 2007112749 2053533155 1922162377
#> 751 752 753 754 755 756
#> 1999180005 2484630165 2309469353 2166371816 2004475011 2195451678
#> 757 758 759 760 761 762
#> 2074980830 2303451185 2254835758 1170829350 2149871693 1845027032
#> 763 764 765 766 767 768
#> 2145521303 2179316605 5628074956 1942874758 2054971358 2149556761
#> 769 770 771 772 773 774
#> 1788468648 2070995867 8405546058 1940218659 1806094046 1651722846
#> 775 776 777 778 779 780
#> 2473769446 2054800014 1733123496 2230508801 1932570913 2485826509
#> 781 782 783 784 785 786
#> 2248611559 2213192895 2006597687 2131805712 2231451931 1979908504
#> 787 788 789 790 791 792
#> 2071024830 1658360125 1768015925 2854156150 2128907612 2040322087
#> 793 794 795 796 797 798
#> 2067727584 2316085756 2110943562 2173159071 1965209555 2053825357
#> 799 800 801 802 803 804
#> 1984047878 2053082848 1993436595 1834814072 2061275598 2035901595
#> 805 806 807 808 809 810
#> 2231370879 2296867282 2566570491 1923848759 2352236412 1832897943
#> 811 812 813 814 815 816
#> 2108771849 1841361973 2114887814 2126979974 1901175810 2187384893
#> 817 818 819 820 821 822
#> 2123456918 2274432147 2323990155 1992095938 4808023783 1967213667
#> 823 824 825 826 827 828
#> 2279589992 2035185836 2109396599 1944962732 2158674362 2144296685
#> 829 830 831 832 833 834
#> 1982412761 2087482882 3362515014 2006333036 1635091147 1943873480
#> 835 836 837 838 839 840
#> 2241132208 1843266090 2008525604 2058120426 2018731097 2324775761
#> 841 842 843 844 845 846
#> 1946822961 1986353211 2054736273 2300375959 2087347587 2197997911
#> 847 848 849 850 851 852
#> 2424793249 2059150049 2306642958 1952197168 2028894309 1966219592
#> 853 854 855 856 857 858
#> 1521202236 2594146630 2071844863 2117775757 2090967026 2003931148
#> 859 860 861 862 863 864
#> 2020693219 1860183558 1976464110 2293416322 1982076212 2122961751
#> 865 866 867 868 869 870
#> 2030612939 2129388321 2236298232 2030641479 2108039179 2060865854
#> 871 872 873 874 875 876
#> 1927876729 3627378863 1885104169 2088753999 2022215057 2190590290
#> 877 878 879 880 881 882
#> 1979734818 2139526610 2001827425 2014970527 37198980421 1797695552
#> 883 884 885 886 887 888
#> 1968205288 1849461923 1695261901 1853782514 2033209932 7199993241
#> 889 890 891 892 893 894
#> 2126794799 1999897305 2114511443 1695761615 3589720342 2058289363
#> 895 896 897 898 899 900
#> 1985011127 1974409651 2007323053 1980906668 2003105838 1941969433
#> 901 902 903 904 905 906
#> 2094163543 2042998649 1871549114 36556049486 1999533214 2646654919
#> 907 908 909 910 911 912
#> 2010888302 2090644312 1997919421 1963756882 2158556140 2120719225
#> 913 914 915 916 917 918
#> 1962218630 2023034818 2024871824 3710489527 2070028328 1994513439
#> 919 920 921 922 923 924
#> 1999723181 2066857410 1994375812 1953943912 2112493283 -89658187
#> 925 926 927 928 929 930
#> 1930760906 1990324402 2040483562 2001626253 1871618789 2025869706
#> 931 932 933 934 935 936
#> 2141222487 2038406560 1963807493 1994529761 1682646716 2023196187
#> 937 938 939 940 941 942
#> 2079496450 2198242147 1965420353 2088455510 1932284757 1973946279
#> 943 944 945 946 947 948
#> 1946168841 1960603409 1921386039 2007381672 1998347623 2027219553
#> 949 950 951 952 953 954
#> 1986274999 2046226654 2018530477 2236807379 1590176234 2262364553
#> 955 956 957 958 959 960
#> 2006913232 2097940707 1943819996 2087179812 2003330215 2105450313
#> 961 962 963 964 965 966
#> 1996370298 1147891102 2049563754 2684754054 2008914904 2028511789
#> 967 968 969 970 971 972
#> 2015952735 1986845122 2057690021 2906789608 2010417689 2000471323
#> 973 974 975 976 977 978
#> 2065039396 2097600396 1941482925 2041507657 2281837138 2036040406
#> 979 980 981 982 983 984
#> 2096108679 3253254257 2018596757 2237413405 2062304336 1949974552
#> 985 986 987 988 989 990
#> 1996624607 2040899980 2012308244 2002187793 1398147460 1854030427
#> 991 992 993 994 995 996
#> 2013231291 2035929728 2039149744 1927270264 2218176272 1960143153
#> 997 998 999 1000 1001 1002
#> 2195917613 1948703122 1992291028 2046925277 2071976411 2031322900
#> 1003 1004 1005 1006 1007 1008
#> 1952637442 1926432739 2061850993 2163843046 2068958154 2079920323
#> 1009 1010 1011 1012 1013 1014
#> 2014717771 1986155779 1964670087 2034352308 2034008892 2037152589
#> 1015 1016 1017 1018 1019 1020
#> 1854420126 1965554148 2077493690 1935192373 2022405369 2222344517
#> 1021 1022 1023 1024 1025 1026
#> 2035353928 2022958258 2018847273 1982199007 2090841598 2026637744
#> 1027 1028 1029 1030 1031 1032
#> 2058649545 2002411036 2106016937 2019203714 2021158007 2095525672
#> 1033 1034 1035 1036 1037 1038
#> 2010288182 2035943774 2044411983 1920609851 2080675252 1983751668
#> 1039 1040 1041 1042 1043 1044
#> 2134534838 2140548620 1958661504 1900412216 122355117197 2037437822
#> 1045 1046 1047 1048
#> 2016453503 2032989540 1924577430 2077957284 Created on 2021-01-21 by the reprex package (v0.3.0) |
Thank you @aravindhebbali ! I pulled the latest dev and it worked for both_p() and both_aic(). I am now able to build the models and predict! |
When loading the included data set and performing ols_step_both_p(), the "Error in if (pvals[minp] <= pent) {: argument is of length zero" error is generated. Different pent and prem values were also attempted. ols_step_both_aic() executes without error.
Edit: ols_step_both_aic() generates an error when using the predict() function. The following code would be at the bottom of the reprex:
stepwisePrediction <- predict(stepwiseBothModel$model, test)
Error in UseMethod("predict") : no applicable method for 'predict' applied to an object of class "NULL"
The text was updated successfully, but these errors were encountered: