R Under development (unstable) (2024-02-12 r85894 ucrt) -- "Unsuffered Consequences" Copyright (C) 2024 The R Foundation for Statistical Computing Platform: x86_64-w64-mingw32/x64 R is free software and comes with ABSOLUTELY NO WARRANTY. You are welcome to redistribute it under certain conditions. Type 'license()' or 'licence()' for distribution details. R is a collaborative project with many contributors. Type 'contributors()' for more information and 'citation()' on how to cite R or R packages in publications. Type 'demo()' for some demos, 'help()' for on-line help, or 'help.start()' for an HTML browser interface to help. Type 'q()' to quit R. > ### Test nlsBootPredict() on a linear model > library(nlstools) 'nlstools' has been loaded. IMPORTANT NOTICE: Most nonlinear regression models and data set examples related to predictive microbiolgy have been moved to the package 'nlsMicrobio' > set.seed(123) > n <- 30 > x <- rnorm(n) > eps <- 0.3 * rnorm(n) > d <- data.frame(x = x, y = x + eps) > model <- lm(y ~ x, data = d) > new <- data.frame(x = seq(-3, 3, 0.1)) > pred.plim <- predict(model, new, interval = "prediction") > pred.clim <- predict(model, new, interval = "confidence") > plot(y ~ x, data = d) > abline(model) > lines(new$x, pred.clim[, 2], pch = 19, col = "red") > lines(new$x, pred.clim[, 3], pch = 19, col = "red") > lines(new$x, pred.plim[, 2], pch = 19, col = "blue") > lines(new$x, pred.plim[, 3], pch = 19, col = "blue") > > # using nls > formu <- as.formula(y ~ a + b *x) > nlsmodel <- nls(formu, start = list(a = 0, b = 1), data = d) > niter <- 200 > # niter <- 2000 > nlsboot <- nlsBoot(nlsmodel, niter = niter) > (pred.clim <- nlsBootPredict(nlsboot, newdata = new, interval = "confidence")) Median 2.5% 97.5% [1,] -2.81723283 -3.06886491 -2.56176657 [2,] -2.72240073 -2.96521056 -2.47398234 [3,] -2.62688270 -2.86155621 -2.38619811 [4,] -2.53181770 -2.75790186 -2.29841389 [5,] -2.43797866 -2.65424751 -2.21062966 [6,] -2.34207290 -2.55059316 -2.12284543 [7,] -2.24502643 -2.44693881 -2.03506121 [8,] -2.14756496 -2.34328446 -1.94727698 [9,] -2.04807972 -2.23963011 -1.85949275 [10,] -1.95017795 -2.13597576 -1.77170853 [11,] -1.85723345 -2.03226366 -1.68392430 [12,] -1.76459035 -1.92879542 -1.59614007 [13,] -1.66806241 -1.82883128 -1.50835585 [14,] -1.57155165 -1.72886715 -1.42057162 [15,] -1.47506156 -1.62890302 -1.33128494 [16,] -1.38158668 -1.52893888 -1.24092639 [17,] -1.28732055 -1.42498548 -1.15056784 [18,] -1.19193012 -1.32152294 -1.06160823 [19,] -1.09736481 -1.22061195 -0.97290652 [20,] -1.00410625 -1.12302497 -0.88187157 [21,] -0.90745624 -1.02483214 -0.79083661 [22,] -0.81031137 -0.92406432 -0.69980166 [23,] -0.71505169 -0.82627502 -0.60876670 [24,] -0.61889962 -0.72841827 -0.51773174 [25,] -0.52157624 -0.62623433 -0.42983868 [26,] -0.42683848 -0.52394020 -0.33735571 [27,] -0.33118873 -0.42326139 -0.24609849 [28,] -0.23786960 -0.32492441 -0.15449917 [29,] -0.14332530 -0.22215654 -0.06509836 [30,] -0.04585133 -0.12859456 0.03399433 [31,] 0.05058588 -0.03043206 0.13318525 [32,] 0.14705772 0.06766891 0.22733001 [33,] 0.24119613 0.16446070 0.32344443 [34,] 0.33755519 0.25427284 0.42300254 [35,] 0.43214403 0.34667850 0.52256065 [36,] 0.52647995 0.43317279 0.62208172 [37,] 0.62390411 0.52448037 0.72157104 [38,] 0.72079144 0.61985771 0.82117664 [39,] 0.81411689 0.71096015 0.92079309 [40,] 0.91012841 0.80206260 1.02111009 [41,] 1.00785607 0.89268696 1.12215759 [42,] 1.10218544 0.98412098 1.22320508 [43,] 1.19951544 1.07508127 1.32428941 [44,] 1.29468570 1.16604155 1.42539870 [45,] 1.39116797 1.25513984 1.52761939 [46,] 1.48527700 1.34198474 1.62953524 [47,] 1.58047608 1.42886857 1.73441227 [48,] 1.67468220 1.51575240 1.83759862 [49,] 1.76869460 1.60257838 1.94078498 [50,] 1.86463717 1.68928230 2.04397134 [51,] 1.96049585 1.77598622 2.14715769 [52,] 2.05618626 1.86273760 2.25024088 [53,] 2.15312465 1.94958776 2.35329727 [54,] 2.24875449 2.03643792 2.45635366 [55,] 2.34387805 2.12328807 2.55772073 [56,] 2.43967975 2.21084405 2.65792040 [57,] 2.53711007 2.30020056 2.76324790 [58,] 2.63340424 2.38955707 2.86887040 [59,] 2.72959131 2.47554498 2.97208648 [60,] 2.82530730 2.56103654 3.07530257 [61,] 2.92142186 2.64652811 3.17851865 > (pred.plim <- nlsBootPredict(nlsboot, newdata = new, interval = "prediction")) Median 2.5% 97.5% [1,] -2.81156029 -3.31437355 -2.27764257 [2,] -2.73771552 -3.27981895 -2.18539695 [3,] -2.63525405 -3.08939408 -2.09519587 [4,] -2.57005016 -2.95515841 -1.97747747 [5,] -2.46821618 -2.93303625 -1.96016775 [6,] -2.39178847 -2.86568046 -1.68223035 [7,] -2.25212430 -2.74238984 -1.78353859 [8,] -2.21657017 -2.65896935 -1.59621918 [9,] -2.02554499 -2.56086884 -1.58921698 [10,] -1.95530358 -2.46666009 -1.49606581 [11,] -1.90786850 -2.29507605 -1.28546905 [12,] -1.79722318 -2.22052568 -1.14699796 [13,] -1.70311397 -2.18837023 -1.05472323 [14,] -1.60066569 -2.05463643 -0.98507056 [15,] -1.48792163 -1.91809896 -0.91807750 [16,] -1.42888777 -1.86901978 -0.87050450 [17,] -1.28693880 -1.74373558 -0.86112798 [18,] -1.22557919 -1.60290108 -0.70156863 [19,] -1.13583961 -1.59088200 -0.61161767 [20,] -0.99427412 -1.46211770 -0.38928708 [21,] -0.91306506 -1.39666339 -0.43015214 [22,] -0.85403742 -1.24949895 -0.40943922 [23,] -0.74452951 -1.22090440 -0.30023989 [24,] -0.67605355 -1.08107391 -0.22231932 [25,] -0.56940429 -0.95688978 0.01514277 [26,] -0.44164002 -0.87614760 -0.05747353 [27,] -0.33143112 -0.77537022 0.26876255 [28,] -0.30696583 -0.68657377 0.17819028 [29,] -0.16520093 -0.60708025 0.26980506 [30,] -0.12390914 -0.51478356 0.31114975 [31,] -0.01433719 -0.40848685 0.43599896 [32,] 0.07598464 -0.32474756 0.68950435 [33,] 0.21914024 -0.25065051 0.86040954 [34,] 0.27235729 -0.10333262 0.90631235 [35,] 0.43987253 0.01959801 1.01438291 [36,] 0.45197515 0.10601563 1.14321731 [37,] 0.55916984 0.14634499 1.02231898 [38,] 0.65269608 0.26571445 1.20396295 [39,] 0.77528177 0.37415362 1.34427761 [40,] 0.88063381 0.45589500 1.32525205 [41,] 1.02191172 0.55514687 1.56833146 [42,] 1.06086132 0.64000747 1.65307906 [43,] 1.15390947 0.71974206 1.77641620 [44,] 1.27921488 0.89752021 1.78938867 [45,] 1.39386996 0.95918537 1.94016941 [46,] 1.48498235 1.04131152 1.97068130 [47,] 1.55316657 1.04122912 2.08435371 [48,] 1.64948805 1.15138646 2.15021618 [49,] 1.76940691 1.37131105 2.31602033 [50,] 1.84076275 1.40577238 2.45304957 [51,] 1.90693000 1.46021705 2.43589689 [52,] 2.06220192 1.60483896 2.72860268 [53,] 2.16668163 1.66909986 2.74461269 [54,] 2.22338747 1.75824779 2.86996867 [55,] 2.29596473 1.94881227 2.90885518 [56,] 2.44366609 1.94934694 2.93433809 [57,] 2.48361793 2.00899269 3.01496469 [58,] 2.66475825 2.13613039 3.22360952 [59,] 2.78338227 2.18512024 3.30968447 [60,] 2.82765555 2.30179900 3.42300120 [61,] 2.93908539 2.34957865 3.59145763 > > lines(new$x, pred.clim[, "2.5%"], pch = 19, col = "red", lwd = 2, lty = 2) > lines(new$x, pred.clim[, "97.5%"], pch = 19, col = "red", lwd = 2, lty = 2) > lines(new$x, pred.plim[, "2.5%"], pch = 19, col = "blue", lwd = 2, lty = 2) > lines(new$x, pred.plim[, "97.5%"], pch = 19, col = "blue", lwd = 2, lty = 2) > > ### Use of nlsBootPredict() a non linear model > data(vmkm) > nls1 <- nls(michaelis,vmkm,list(Km=1,Vmax=1)) > plotfit(nls1, smooth = TRUE) > > nlsb1 <- nlsBoot(nls1, niter = niter) > new1 <- data.frame(S = seq(0.3, 2, 0.1)) > (pred.clim <- nlsBootPredict(nlsb1, newdata = new1, interval = "confidence")) Median 2.5% 97.5% [1,] 0.1608109 0.1509141 0.1727522 [2,] 0.2073174 0.1955793 0.2206870 [3,] 0.2507233 0.2377474 0.2650256 [4,] 0.2913338 0.2775170 0.3066177 [5,] 0.3297796 0.3151818 0.3452034 [6,] 0.3659105 0.3509235 0.3805432 [7,] 0.3993828 0.3855575 0.4127916 [8,] 0.4313941 0.4185898 0.4446739 [9,] 0.4618000 0.4491425 0.4743594 [10,] 0.4904356 0.4770375 0.5026707 [11,] 0.5171692 0.5032976 0.5310446 [12,] 0.5430686 0.5285592 0.5563806 [13,] 0.5674258 0.5524375 0.5806221 [14,] 0.5909129 0.5755214 0.6060323 [15,] 0.6129958 0.5971787 0.6297974 [16,] 0.6340656 0.6175658 0.6519336 [17,] 0.6542444 0.6358989 0.6748199 [18,] 0.6733062 0.6532512 0.6962930 > (pred.plim <- nlsBootPredict(nlsb1, newdata = new1, interval = "prediction")) Median 2.5% 97.5% [1,] 0.1608731 0.1132890 0.2116213 [2,] 0.2070677 0.1592908 0.2611559 [3,] 0.2514852 0.2016383 0.3045645 [4,] 0.2933006 0.2386858 0.3376569 [5,] 0.3295287 0.2789410 0.3686969 [6,] 0.3655752 0.3154331 0.4258422 [7,] 0.4005530 0.3535601 0.4564297 [8,] 0.4310858 0.3829141 0.4871092 [9,] 0.4611489 0.4105682 0.5213850 [10,] 0.4911382 0.4438490 0.5470805 [11,] 0.5151841 0.4659091 0.5670586 [12,] 0.5440623 0.4968048 0.6022715 [13,] 0.5665871 0.5174057 0.6238999 [14,] 0.5918079 0.5402556 0.6436058 [15,] 0.6122645 0.5609896 0.6670848 [16,] 0.6344426 0.5836094 0.6918973 [17,] 0.6558639 0.6010505 0.7115876 [18,] 0.6721842 0.6202672 0.7369739 > > lines(new1$S, pred.clim[, "2.5%"], pch = 19, col = "red", lwd = 2, lty = 2) > lines(new1$S, pred.clim[, "97.5%"], pch = 19, col = "red", lwd = 2, lty = 2) > lines(new1$S, pred.plim[, "2.5%"], pch = 19, col = "blue", lwd = 2, lty = 2) > lines(new1$S, pred.plim[, "97.5%"], pch = 19, col = "blue", lwd = 2, lty = 2) > > ### Use of nlsBootPredict() on a model with two independent variables > data(vmkmki) > nls2 <- nls(compet_mich, vmkmki, list(Km=1,Vmax=20,Ki=0.5)) > plotfit(nls2, variable=1) > nlsb2 <- nlsBoot(nls2, niter = niter) > # Define a new dataframe with a fixed value of one of the independent variable > new2 <- data.frame(S = seq(0, 200, length.out = 50), I = rep(20, 50)) > (pred.clim <- nlsBootPredict(nlsb2, newdata = new2, interval = "confidence")) Median 2.5% 97.5% [1,] 0.000000 0.000000 0.000000 [2,] 2.229075 1.964014 2.505346 [3,] 3.965738 3.555823 4.375876 [4,] 5.354522 4.879420 5.825738 [5,] 6.505401 5.979054 6.983076 [6,] 7.465570 6.920498 7.955946 [7,] 8.275283 7.733432 8.751216 [8,] 8.970185 8.441740 9.414154 [9,] 9.569839 9.064398 9.987621 [10,] 10.098104 9.599816 10.491223 [11,] 10.555800 10.061758 10.937895 [12,] 10.968567 10.473366 11.347157 [13,] 11.346909 10.875290 11.727552 [14,] 11.685760 11.198077 12.050186 [15,] 11.991060 11.480176 12.351816 [16,] 12.263835 11.756363 12.658216 [17,] 12.507935 12.008256 12.916669 [18,] 12.744018 12.222000 13.141986 [19,] 12.954766 12.426739 13.361690 [20,] 13.148448 12.615910 13.572899 [21,] 13.333774 12.804298 13.759353 [22,] 13.504555 12.992251 13.930505 [23,] 13.658966 13.155973 14.095090 [24,] 13.807970 13.299565 14.245012 [25,] 13.945484 13.424567 14.385361 [26,] 14.073855 13.533892 14.517291 [27,] 14.193626 13.636401 14.641238 [28,] 14.308591 13.732711 14.757907 [29,] 14.411881 13.835735 14.867920 [30,] 14.508574 13.933486 14.977025 [31,] 14.603692 14.024380 15.085697 [32,] 14.697277 14.101632 15.188795 [33,] 14.777074 14.174832 15.286738 [34,] 14.858842 14.244292 15.379904 [35,] 14.939895 14.310291 15.468632 [36,] 15.008791 14.373081 15.553234 [37,] 15.074873 14.432890 15.627097 [38,] 15.141404 14.490021 15.700052 [39,] 15.205895 14.541330 15.773152 [40,] 15.268470 14.587407 15.843136 [41,] 15.330931 14.631452 15.910199 [42,] 15.387727 14.673597 15.974519 [43,] 15.445724 14.722060 16.036262 [44,] 15.501432 14.769568 16.095579 [45,] 15.554985 14.815204 16.152611 [46,] 15.607016 14.859076 16.207486 [47,] 15.659084 14.901284 16.260326 [48,] 15.707870 14.941941 16.311242 [49,] 15.754209 14.978260 16.360336 [50,] 15.796139 15.012992 16.407704 > (pred.plim <- nlsBootPredict(nlsb2, newdata = new2, interval = "prediction")) Median 2.5% 97.5% [1,] -0.02823239 -4.6796892 2.300498 [2,] 2.23988020 -2.4610981 4.180531 [3,] 4.03047429 -0.2730675 6.414034 [4,] 5.52789874 0.5575357 7.928287 [5,] 6.68569359 2.0075501 8.968554 [6,] 7.60314026 3.2221837 9.698371 [7,] 8.44784141 3.2101837 10.590407 [8,] 9.01537699 4.5710485 11.533643 [9,] 9.74755605 5.2898349 11.762063 [10,] 10.14661468 5.7804012 12.703409 [11,] 10.59862844 6.3850096 12.352637 [12,] 11.08462408 6.7354124 13.201548 [13,] 11.42194521 6.4875500 13.493741 [14,] 11.81036466 7.9775168 14.065000 [15,] 12.11941906 7.8355846 14.483614 [16,] 12.39492332 7.9310672 14.453802 [17,] 12.70962864 8.0828245 15.178719 [18,] 12.75944521 7.8138912 14.875768 [19,] 13.08234420 8.3194057 15.277659 [20,] 13.37378021 9.1323991 15.609978 [21,] 13.49241554 8.5067203 15.774638 [22,] 13.49576424 8.4338337 16.035849 [23,] 13.80516762 8.6570890 16.059327 [24,] 14.04334657 11.2241878 16.318820 [25,] 13.98801553 9.9748788 16.452846 [26,] 14.42058255 10.5225994 16.614654 [27,] 14.31375903 9.9244092 16.754540 [28,] 14.47231626 9.8332919 16.740688 [29,] 14.61017495 9.9023478 16.397990 [30,] 14.75729082 10.0015352 16.649989 [31,] 14.64862466 10.5641991 17.411711 [32,] 14.70121861 10.0548378 17.284624 [33,] 14.79272477 10.5636470 16.964207 [34,] 15.00099123 11.1700228 16.895946 [35,] 15.16623125 10.4803098 17.492793 [36,] 15.10861103 10.7964766 17.593089 [37,] 15.39438961 9.8479320 17.150725 [38,] 15.33274682 11.6535831 17.551700 [39,] 15.29669078 10.9813625 17.796919 [40,] 15.46281695 10.1845532 17.698473 [41,] 15.49241761 10.9351275 18.234832 [42,] 15.37161823 10.4486275 17.965742 [43,] 15.62841446 10.3465026 17.877197 [44,] 15.57961100 10.9049824 17.996772 [45,] 15.62867122 10.9045776 17.953193 [46,] 15.71784187 10.7934857 17.884115 [47,] 15.76231069 10.7309725 17.770117 [48,] 15.69185738 10.7586563 17.699649 [49,] 15.74437127 10.6136744 18.027233 [50,] 15.96582240 11.7123818 18.094859 > > plot(new2$S, pred.clim[, "Median"], type = "l", col = "black", lwd = 2, lty = 2) > lines(new2$S, pred.clim[, "2.5%"], pch = 19, col = "red", lwd = 2, lty = 2) > lines(new2$S, pred.clim[, "97.5%"], pch = 19, col = "red", lwd = 2, lty = 2) > lines(new2$S, pred.plim[, "2.5%"], pch = 19, col = "blue", lwd = 2, lty = 2) > lines(new2$S, pred.plim[, "97.5%"], pch = 19, col = "blue", lwd = 2, lty = 2) > > > proc.time() user system elapsed 1.20 0.10 1.26