Non-linear growth curves with Stan
[This article was first published on mages' blog, and kindly contributed to R-bloggers]. (You can report issue about the content on this page here)
Want to share your content on R-bloggers? click here if you have a blog, or here if you don't.
I suppose the go to tool for fitting non-linear models in R is Want to share your content on R-bloggers? click here if you have a blog, or here if you don't.
nls
of the stats
package. In this post I will show an alternative approach with Stan/RStan, as illustrated in the example, Dugongs: “nonlinear growth curve”, that is part of Stan’s documentation. The original example itself is taken from OpenBUGS. The data describes the length and age measurements for 27 captured dugongs (sea cows). Carlin and Gelfand (1991) model the data using a nonlinear growth curve with no inflection point and an asymptote as (x_i) tends to infinity:
[
Y_i sim mathcal{N}(mu_i, sigma^2),; i = 1,dots,27\
mu_i = alpha – beta lambda^{x_i},; alpha,, beta > 0;, 0 < lambda < 1 ] Fitting the curve with
nls
gives the following results:Writing the model in Stan requires a few more lines, but gives me also the opportunity to generate output from the posterior distributions.
The predicted parameters and errors are very much the same as in the least square output of
nls
, but with the Stan output I can also review the 90% credible intervals.Session Info
R version 3.2.2 (2015-08-14) Platform: x86_64-apple-darwin13.4.0 (64-bit) Running under: OS X 10.11.1 (El Capitan) locale: [1] en_GB.UTF-8/en_GB.UTF-8/en_GB.UTF-8/C/en_GB.UTF-8/en_GB.UTF-8 attached base packages: [1] stats graphics grDevices utils datasets [6] methods base other attached packages: [1] rstan_2.8.0 ggplot2_1.0.1.9003 Rcpp_0.12.1 loaded via a namespace (and not attached): [1] colorspace_1.2-6 scales_0.3.0 plyr_1.8.3 [4] parallel_3.2.2 tools_3.2.2 inline_0.3.14 [7] gtable_0.1.2 gridExtra_2.0.0 codetools_0.2-14 [10] grid_3.2.2 stats4_3.2.2 munsell_0.4.2
To leave a comment for the author, please follow the link and comment on their blog: mages' blog.
R-bloggers.com offers daily e-mail updates about R news and tutorials about learning R and many other topics. Click here if you're looking to post or find an R/data-science job.
Want to share your content on R-bloggers? click here if you have a blog, or here if you don't.