├── .gitignore ├── Instats ├── Day-1 │ ├── Session-1 │ │ ├── intro-why-use-a-SIMM.html │ │ └── why-use-a-simm-instats.pptx │ ├── Session-2 │ │ ├── likelihood-regression-instats.pptx │ │ └── revision_of_likelihood_and_regression.pdf │ ├── Session-3 │ │ ├── R-revision-instats.pptx │ │ └── Revision_of_R.R │ ├── Session-4 │ │ ├── Exploring-Basic-SIA-Data.Rmd │ │ ├── Exploring-Basic-SIA-Data.nb.html │ │ ├── Practical01.csv │ │ ├── explore-SIA-data-instats.pptx │ │ ├── first-biplot.Rmd │ │ └── first-biplot.nb.html │ └── Session-5 │ │ ├── Practical02.csv │ │ ├── basic-SIA-linear-models-instats.pptx │ │ ├── basic-SIA-linear-models.Rmd │ │ └── basic-SIA-linear-models.nb.html ├── Day-2 │ ├── Session-10 │ │ └── open-practical-instats.pptx │ ├── Session-6 │ │ ├── intro-bayes-instats.pptx │ │ └── intro_bayes.pdf │ ├── Session-7 │ │ ├── jags-practical-instats.pptx │ │ ├── practical_R_jags_and_lr.R │ │ ├── practical_jags_logistic.R │ │ └── practical_jags_poisson.R │ ├── Session-8 │ │ ├── reg_and_simms.pdf │ │ └── simms-regression-instats.pptx │ └── Session-9 │ │ ├── simmr-practical-instats.pptx │ │ ├── simmr_vignette.Rmd │ │ └── simmr_vignette.nb.html ├── Day-3 │ ├── Session-11 │ │ ├── siar-statistics-instats.pptx │ │ └── siar_stats.pdf │ ├── Session-12 │ │ ├── mixsiar-practical-instats.pptx │ │ ├── mixsiar_script.R │ │ ├── output_jags_new.R │ │ └── simmr_with_priors.R │ ├── Session-13 │ │ ├── siar-dos-and-donts.html │ │ └── siar-dos-donts-instats.pptx │ ├── Session-14 │ │ ├── siar-dos-and-donts.html │ │ └── siar-dos-donts-instats.pptx │ └── Session-15 │ │ ├── source-aggregation-instats.pptx │ │ ├── source-aggregation.Rmd │ │ └── source-aggregation.nb.html └── Day-4 │ ├── Session-16 │ ├── siber-intro-ellipses-instats.pptx │ └── siber-intro-ellipses.html │ ├── Session-17 │ ├── siber-comparing-populations.nb.html │ └── siber-practical-ellipses-instats.pptx │ ├── Session-18 │ ├── siber-comparing-communities.nb.html │ └── siber-practical-communities-instats.pptx │ ├── Session-19 │ ├── cosimmr-practical-instats.pptx │ └── cosimmr_presentation.pdf │ └── Session-20 │ └── open-practical.pptx ├── LICENSE ├── Prerequisites.Rmd ├── Prerequisites.html ├── README.md ├── Revision_of_R.R ├── Workflow.Rmd ├── Workflow.html ├── _config.yml ├── aj-content ├── data │ ├── demo.siber.data.csv │ ├── example_ellipse_data.csv │ ├── example_layman_data.csv │ ├── example_layman_data_2.csv │ ├── example_layman_data_all.csv │ └── example_layman_data_text_labels.csv ├── images │ ├── SEA-community1.png │ ├── biplot.jpeg │ ├── chem_frac.png │ ├── community-hulls.png │ ├── crayfish-carp.png │ ├── different-ellipses.png │ ├── dos-and-donts.png │ ├── dspace-pspace.jpg │ ├── dspace-pspace.png │ ├── ellipse-vs-hulls.png │ ├── fry-feasible.png │ ├── geese-over-time.png │ ├── grass-in-geese.png │ ├── heavy_frac.png │ ├── ideal_iso_community.png │ ├── infer_process.png │ ├── isoconc.png │ ├── isosource-screenshot.png │ ├── layman-community.png │ ├── layman-frag.png │ ├── layman-pops.png │ ├── lipid_corrected_liver.jpg │ ├── matrix-plot.png │ ├── omit-source-table.png │ ├── omit-source.png │ ├── posteriori-agg.png │ ├── print-web-trinity.jpg │ ├── sample-size-community-hulls.png │ ├── sample-size-hulls-ellipses.png │ ├── semmens-2009.TIF │ ├── siar-biplot.png │ ├── siar-by-group.png │ ├── siar-histograms.png │ ├── siber-ellipse-example.png │ ├── siber-jae.png │ ├── sider-mammals.png │ ├── simm-1.png │ ├── specialist-generalist.png │ ├── stacked-trinity.jpg │ ├── standard-ellipse.png │ ├── swallows-over-time.png │ ├── tcd-stacked-square-2.png │ └── tcd-stacked-square.png ├── intro-why-use-a-SIMM.Rmd ├── intro-why-use-a-SIMM.html ├── mystyle.css ├── practicals │ ├── Introduction-to-SIDER.Rmd │ ├── SIDER-example.Rmd │ ├── SIDER-example.nb.html │ ├── day-1-pm1 │ │ ├── Exploring-Basic-SIA-Data.Rmd │ │ ├── Exploring-Basic-SIA-Data.nb.html │ │ ├── Practical01.csv │ │ ├── first-biplot.Rmd │ │ ├── first-biplot.nb.html │ │ ├── ggplot-playing.Rmd │ │ └── ggplot-playing.nb.html │ ├── day-1-pm2 │ │ ├── Practical02.csv │ │ ├── basic-SIA-linear-models.Rmd │ │ └── basic-SIA-linear-models.nb.html │ ├── first-Rnotebook.Rmd │ ├── first-Rnotebook.nb.html │ ├── siber-comparing-communities.Rmd │ ├── siber-comparing-communities.nb.html │ ├── siber-comparing-populations.Rmd │ ├── siber-comparing-populations.nb.html │ ├── source-aggregation.Rmd │ ├── source-aggregation.nb.html │ ├── test_n_run-tree1_chain1.rda │ ├── test_n_run-tree1_chain2.rda │ ├── test_n_run-tree1_conv.rda │ ├── test_n_run-tree2_chain1.rda │ ├── test_n_run-tree2_chain2.rda │ └── test_n_run-tree2_conv.rda ├── random-scripts │ └── siber-test-text-group-labels.Rmd ├── siar-dos-and-donts.Rmd ├── siar-dos-and-donts.html ├── siber-intro-ellipses.Rmd ├── siber-intro-ellipses.html ├── siber-intro-hulls.Rmd ├── siber-intro-hulls.html ├── sider-intro.Rmd ├── sider-intro.html ├── test-limits-of-siber-data-import.Rmd ├── test-limits-of-siber-data-import.nb.html ├── utility-scripts │ └── plot.siber.data.r └── vignettes │ ├── Customising-Plots-Manually.R │ ├── Customising-Plots-Manually.Rmd │ ├── Customising-Plots-Manually.html │ ├── Ellipse-Overlap.R │ ├── Ellipse-Overlap.Rmd │ ├── Ellipse-Overlap.html │ ├── Introduction-to-SIBER.R │ ├── Introduction-to-SIBER.Rmd │ ├── Introduction-to-SIBER.html │ ├── Points-Inside-Outside-Ellipse.R │ ├── Points-Inside-Outside-Ellipse.Rmd │ ├── Points-Inside-Outside-Ellipse.html │ ├── SIDER-test-larus.Rmd │ ├── SIDER-test-larus.nb.html │ ├── test_n_run-tree1_chain1.rda │ ├── test_n_run-tree1_chain2.rda │ ├── test_n_run-tree1_conv.rda │ ├── test_n_run-tree2_chain1.rda │ ├── test_n_run-tree2_chain2.rda │ └── test_n_run-tree2_conv.rda ├── ap_notes ├── CHANGES.md ├── Thomas_Bayes.pdf ├── complex_simms │ ├── Geese2Harmonic.png │ ├── Geese2IsoSpace.png │ ├── GeeseConsumers2.csv │ ├── complex_SIMMs.Rmd │ ├── complex_SIMMs.pdf │ ├── geese2_jags_output.rda │ ├── plot_geese_output.R │ └── run_geese_harmonic.R ├── cosimmr │ └── cosimmr_presentation.pdf ├── gg_plot_class.R ├── header.tex ├── intro_bayes │ ├── Thomas_Bayes.gif │ ├── intro_bayes.Rmd │ └── intro_bayes.pdf ├── maynooth_uni_logo.jpg ├── old_slides │ ├── building_simms │ │ ├── Create_spline_data.R │ │ ├── Geese2Spline.png │ │ ├── SIMM_spline_output.rda │ │ ├── bases.r │ │ ├── building_SIMMs.Rmd │ │ ├── building_SIMMs.html │ │ ├── building_SIMMs.pdf │ │ ├── plot_spline_geese_output.R │ │ └── run_spline_geese.R │ ├── prac_using_siar │ │ ├── prac_using_siar.Rmd │ │ └── prac_using_siar.pdf │ └── using_mixsiar │ │ ├── MixSIAR_model.txt │ │ ├── matrix_plot.tiff │ │ ├── mixsiar_screen.tiff │ │ ├── mixsiar_screen_isospace.tiff │ │ ├── mixsiar_screen_mix.tiff │ │ ├── mixsiar_screen_source.tiff │ │ ├── overall_p.tiff │ │ ├── re_sds.tiff │ │ ├── trace.tiff │ │ ├── using_mixsiar.Rmd │ │ ├── using_mixsiar.html │ │ └── using_mixsiar.pdf ├── prac_jags │ ├── practical_R_jags_and_lr.R │ ├── practical_jags_logistic.R │ └── practical_jags_poisson.R ├── prac_mixsiar_and_jags │ ├── mixsiar_script.R │ ├── mixsiar_script_as_notebook.Rmd │ ├── mixsiar_script_as_notebook.nb.html │ ├── prac_mixsiar_and_jags.Rmd │ ├── prac_mixsiar_and_jags.pdf │ └── wolves_data │ │ ├── wolves_consumer.csv │ │ ├── wolves_discrimination.csv │ │ └── wolves_sources.csv ├── prac_using_simmr │ ├── geese_data.xls │ ├── prac_using_simmr.Rmd │ ├── prac_using_simmr.pdf │ ├── quick_start.Rmd │ ├── simmr_from_excel.R │ ├── simmr_vignette.Rmd │ ├── simmr_vignette.nb.html │ └── simmr_with_priors.R ├── reg_and_simms │ ├── geese1demo.rda │ ├── reg_and_simms.Rmd │ ├── reg_and_simms.pdf │ ├── reg_and_simms_aj_playing.Rmd │ └── sourcesdemo.rda ├── revision_of_R │ └── Revision_of_R.R ├── revision_of_likelihood_regression │ ├── revision_of_likelihood_and_regression.Rmd │ └── revision_of_likelihood_and_regression.pdf ├── siar_mixsiar │ ├── siar_mixsiar.Rmd │ └── siar_mixsiar.pdf └── siar_stats │ ├── siar_stats.Rmd │ └── siar_stats.pdf ├── extra_code ├── DIY_isospace_plot.R ├── mixsiar_examples │ ├── cladocera.R │ ├── geese.R │ ├── geese2.R │ ├── geese_consumer.csv │ ├── isopod.R │ ├── killerwhale.R │ ├── lake.R │ └── wolves.R ├── nick_code.R ├── output_jags_new.R ├── run_simmr_indeff.R ├── simmr_mcmc_indeff.R ├── simmr_with_factors_and_priors.R ├── test-sample-size-SIBER.Rmd └── test-sample-size-SIBER.nb.html ├── index.md ├── my_plot.pdf ├── my_prostate_file.rda ├── my_prostate_file.rds ├── papers ├── 2007_Ecol_Schmidt et al.pdf ├── Bearhop2004Determining Trophic Niche.pdf ├── Cheung & Szpak 2020 J Arch Method & Theory Online.pdf ├── Cheung & Szpak 2021 J Arch Meth & Theory online.pdf ├── Fry-MEPS.pdf ├── Jackson_SiBER_2011.pdf ├── Layman et al 2007 Ecology Letters.pdf ├── Layman2007SIRCommunityTrophicStructure.pdf ├── Newsome_et_al__Proof2.pdf ├── Pacella et al, 2013.pdf ├── Parnell_BSimms_2013.pdf ├── Parnell_SIAR_2010.pdf ├── Phillips&Koch2002.pdf ├── Phillips_SIMMadvice_2014.pdf └── m490p285.pdf └── simms_course.Rproj /.gitignore: -------------------------------------------------------------------------------- 1 | # History files 2 | .Rhistory 3 | .Rapp.history 4 | 5 | # Session Data files 6 | .RData 7 | 8 | # Example code in package build process 9 | *-Ex.R 10 | 11 | # Output files from R CMD build 12 | /*.tar.gz 13 | 14 | # Output files from R CMD check 15 | /*.Rcheck/ 16 | 17 | # RStudio files 18 | .Rproj.user/ 19 | 20 | # produced vignettes 21 | vignettes/*.html 22 | vignettes/*.pdf 23 | 24 | # OAuth2 token, see https://github.com/hadley/httr/releases/tag/v0.3 25 | .httr-oauth 26 | 27 | # knitr and R markdown default cache directories 28 | /*_cache/ 29 | /cache/ 30 | 31 | # Temporary files created by R markdown 32 | *.utf8.md 33 | *.knit.md 34 | .Rproj.user 35 | 36 | # Mac files 37 | .DS_Store 38 | 39 | # Compiled files 40 | src/*.so 41 | Src/*.o 42 | 43 | # Latex files 44 | *.aux 45 | *.log 46 | *.synctex.gz 47 | MixSIAR_model.txt 48 | isospace_plot_1_2.pdf 49 | test.pdf 50 | test.txt 51 | 52 | ## Ignore InStats folder 53 | #Instats/ 54 | 55 | # MS temp files 56 | ~$* 57 | -------------------------------------------------------------------------------- /Instats/Day-1/Session-1/why-use-a-simm-instats.pptx: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/andrewcparnell/simms_course/3052327e05aa93897fcfa31e2e8dd49fa3e45708/Instats/Day-1/Session-1/why-use-a-simm-instats.pptx -------------------------------------------------------------------------------- /Instats/Day-1/Session-2/likelihood-regression-instats.pptx: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/andrewcparnell/simms_course/3052327e05aa93897fcfa31e2e8dd49fa3e45708/Instats/Day-1/Session-2/likelihood-regression-instats.pptx -------------------------------------------------------------------------------- /Instats/Day-1/Session-2/revision_of_likelihood_and_regression.pdf: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/andrewcparnell/simms_course/3052327e05aa93897fcfa31e2e8dd49fa3e45708/Instats/Day-1/Session-2/revision_of_likelihood_and_regression.pdf -------------------------------------------------------------------------------- /Instats/Day-1/Session-3/R-revision-instats.pptx: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/andrewcparnell/simms_course/3052327e05aa93897fcfa31e2e8dd49fa3e45708/Instats/Day-1/Session-3/R-revision-instats.pptx -------------------------------------------------------------------------------- /Instats/Day-1/Session-4/Practical01.csv: -------------------------------------------------------------------------------- 1 | Taxon,ID,d13C,d15N,C:N 2 | POM,AF_POM01,-20.477,13.554,9.5 3 | POM,AF_POM02,-19.879,12.461,8 4 | POM,AF_POM03,-19.437,14.053,9.1 5 | POM,AF_POM04,-20.276,13.129,8.1 6 | POM,AF_POM05,-19.126,12.484,7.3 7 | Benthic.algae,AF_BA01 ,-13.408,12.144,7.7 8 | Benthic.algae,AF_BA02,-13.621,12.156,9.3 9 | Benthic.algae,AF_BA03,-14.159,12,9 10 | Benthic.algae,AF_BA04,-13.9,11.84,9.2 11 | Benthic.algae,AF_BA05,-6.2,12.482,10 12 | Fissurella,AF_LA01 ,-15.658,15.622,3.4 13 | Fissurella,AF_LA02,-17.436,16.25,3.3 14 | Fissurella,AF_LA03,-14.836,16.122,3.4 15 | Fissurella,AF_LA04,-14.482,15.145,3.4 16 | Fissurella,AF_LA05,-15.063,14.597,3.5 17 | Fissurella,AF_LA06,-12,16.009,11.4 18 | Fissurella,AF_LA07,-17.387,16.003,9.8 19 | Fissurella,AF_LA08,-18.314,17.858,3.8 20 | Fissurella,AF_LA09,-20.166,16.046,3.8 21 | Fissurella,AF_LA10,-15.415,15.193,3.8 22 | Perumytilus,AF_BV01 ,-18.862,16.509,3.6 23 | Perumytilus,AF_BV02,-13.2,15.985,12.5 24 | Perumytilus,AF_BV03,-16.338,16.551,3.4 25 | Perumytilus,AF_BV04,-18.347,18.865,3.4 26 | Perumytilus,AF_BV05,-19.283,17.003,3.4 27 | Perumytilus,AF_BV06,-14.2,15.604,10.1 28 | Perumytilus,AF_BV07,-19.227,16.948,3.6 29 | Perumytilus,AF_BV08,-19.949,15.578,3.4 30 | Perumytilus,AF_BV09,-17.064,15.687,3.5 31 | Perumytilus,AF_BV10,-17.505,17.25,3.7 32 | Cabrilla,AF_Ph01 ,-15.625,20.71,3.1 33 | Cabrilla,AF_Ph02,-17.578,20.572,4 34 | Cabrilla,AF_Ph03,-15.1,20.777,3.7 35 | Cabrilla,AF_Ph04,-14.5,20.616,3.3 36 | Cabrilla,AF_Ph05,-17.597,21.959,4.1 37 | Cabrilla,AF_Ph06,-22.4,20.125,5.8 38 | Cabrilla,AF_Ph07,-16.788,22.881,3.6 39 | Cabrilla,AF_Ph08,-18.266,19.052,4.4 40 | Cabrilla,AF_Ph09,-17.865,20.724,4.3 41 | Cabrilla,AF_Ph10,-19.1,22.636,4.6 42 | Cabrilla,AF_Ph11,-16.775,20.591,3.5 43 | Cabrilla,AF_Ph12,-19.982,21.99,5.3 44 | Cabrilla,AF_Ph13,-17.279,20.478,4 45 | Cabrilla,AF_Ph14,-17.166,20.062,3.9 46 | Cabrilla,AF_Ph15,-19.443,20.252,5.9 47 | Cabrilla,AF_Ph16,-22.3,20.257,5.9 48 | Cabrilla,AF_Ph17,-16.897,17.956,3.7 49 | Cabrilla,AF_Ph18,-18.619,21.067,4.5 50 | Cabrilla,AF_Ph19,-14.963,19.884,3.1 51 | Cabrilla,AF_Ph20,-23.1,23.378,6.8 52 | Cabrilla,AF_Ph21,-16.821,18.69,3.6 53 | Cabrilla,AF_Ph22,-18.897,17.89,5 54 | Cabrilla,AF_Ph23,-16.159,21.033,3.1 55 | Cabrilla,AF_Ph24,-18.716,21.469,4.5 56 | Cabrilla,AF_Ph25,-18.801,21.066,4.6 57 | Cabrilla,AF_Ph26,-17.083,19.383,3.7 58 | Cabrilla,AF_Ph27,-18.146,21.192,4.4 59 | Cabrilla,AF_Ph28,-17.238,20.004,3.9 60 | Cabrilla,AF_Ph29,-18.62,19.689,4.5 61 | Cabrilla,AF_Ph30,-17.998,20.227,4.3 62 | -------------------------------------------------------------------------------- /Instats/Day-1/Session-4/explore-SIA-data-instats.pptx: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/andrewcparnell/simms_course/3052327e05aa93897fcfa31e2e8dd49fa3e45708/Instats/Day-1/Session-4/explore-SIA-data-instats.pptx -------------------------------------------------------------------------------- /Instats/Day-1/Session-5/basic-SIA-linear-models-instats.pptx: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/andrewcparnell/simms_course/3052327e05aa93897fcfa31e2e8dd49fa3e45708/Instats/Day-1/Session-5/basic-SIA-linear-models-instats.pptx -------------------------------------------------------------------------------- /Instats/Day-2/Session-10/open-practical-instats.pptx: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/andrewcparnell/simms_course/3052327e05aa93897fcfa31e2e8dd49fa3e45708/Instats/Day-2/Session-10/open-practical-instats.pptx -------------------------------------------------------------------------------- /Instats/Day-2/Session-6/intro-bayes-instats.pptx: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/andrewcparnell/simms_course/3052327e05aa93897fcfa31e2e8dd49fa3e45708/Instats/Day-2/Session-6/intro-bayes-instats.pptx -------------------------------------------------------------------------------- /Instats/Day-2/Session-6/intro_bayes.pdf: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/andrewcparnell/simms_course/3052327e05aa93897fcfa31e2e8dd49fa3e45708/Instats/Day-2/Session-6/intro_bayes.pdf -------------------------------------------------------------------------------- /Instats/Day-2/Session-7/jags-practical-instats.pptx: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/andrewcparnell/simms_course/3052327e05aa93897fcfa31e2e8dd49fa3e45708/Instats/Day-2/Session-7/jags-practical-instats.pptx -------------------------------------------------------------------------------- /Instats/Day-2/Session-7/practical_R_jags_and_lr.R: -------------------------------------------------------------------------------- 1 | # Practical on using linear regression in JAGS 2 | 3 | # Call in the package we need 4 | library(R2jags) 5 | 6 | # Here is some data on sea level rise 7 | sea_level = read.csv('https://raw.githubusercontent.com/andrewcparnell/tsme_course/master/data/church_and_white_global_tide_gauge.csv') 8 | head(sea_level) 9 | 10 | # Create a plot of the data 11 | with(sea_level,plot(year_AD,sea_level_m)) 12 | # nicer plotting syntax ;) that matches the call to lm 13 | #plot(year_AD ~ sea_level_m, data = sea_level) 14 | 15 | # Let's fit a linear regression with sea_level_m as the response and year as the covariate 16 | 17 | # Fit a standard linear regression 18 | lr_1 = lm(sea_level_m ~ year_AD, data = sea_level) 19 | summary(lr_1) 20 | 21 | plot(lr_1$residuals ~ lr_1$fitted.values) 22 | 23 | # Now look at the JAGS code for this model 24 | model_code = ' 25 | model 26 | { 27 | # Likelihood 28 | for (i in 1:n) { 29 | y[i] ~ dnorm(alpha + beta * x[i], sigma^-2) 30 | } 31 | 32 | # Priors 33 | alpha ~ dnorm(0, 100^-2) 34 | beta ~ dnorm(0, 100^-2) 35 | sigma ~ dunif(0, 10) 36 | } 37 | ' 38 | 39 | # Set up the data - these match the data objects in the jags code 40 | model_data = list(n = nrow(sea_level), 41 | y = sea_level$sea_level_m, 42 | x = sea_level$year_AD) 43 | 44 | # Choose which parameters to save 45 | model_parameters = c('alpha', 'beta', 46 | 'sigma') 47 | 48 | # Run the model 49 | model_run = jags(data = model_data, 50 | parameters.to.save = model_parameters, 51 | model.file = textConnection(model_code)) 52 | 53 | # Look at the output 54 | print(model_run) 55 | 56 | # Things to do: 57 | # 1 Compare the lm output to the jags output to see they match 58 | # 2 Change the prior distributions - make them narrower? 59 | # 3 Change the code/results to get mm per year 60 | 61 | # Further output ---------------------------------------------------------- 62 | 63 | plot(model_run) 64 | traceplot(model_run) 65 | 66 | # We can access the full posterior data frame 67 | head(model_run$BUGSoutput$sims.matrix) 68 | 69 | # Or you can access just the posterior means 70 | model_run$BUGSoutput$mean$alpha 71 | model_run$BUGSoutput$mean$beta 72 | 73 | # We can plot the posterior distributions of the parameters 74 | plot(density(model_run$BUGSoutput$sims.list$alpha)) 75 | plot(density(model_run$BUGSoutput$sims.list$beta)) 76 | 77 | # Create a 90% interval for the sea level rate in mm per year 78 | quantile(model_run$BUGSoutput$sims.list$beta, 79 | probs = c(0.05, 0.95)) * 1000 80 | 81 | # We can create a plot of the posterior mean line 82 | alpha_mean = model_run$BUGSoutput$mean$alpha[1] 83 | beta_mean = model_run$BUGSoutput$mean$beta[1] 84 | x = sea_level$year_AD 85 | with(sea_level,plot(year_AD,sea_level_m)) 86 | lines(x, alpha_mean + beta_mean * x, col = 'red') 87 | legend('topleft', 88 | legend = c('Data', 'Posterior mean'), 89 | lty=c(-1,1), 90 | pch=c(1,-1), 91 | col=c('black','red')) 92 | 93 | -------------------------------------------------------------------------------- /Instats/Day-2/Session-7/practical_jags_logistic.R: -------------------------------------------------------------------------------- 1 | # Practical session on using JAGS for logistic regression 2 | 3 | # Start up code - clear workspace and call packages 4 | rm(list=ls()) # Clear the workspace 5 | library(R2jags) 6 | library(boot) # Package contains the logit transform 7 | 8 | # Data -------------------------------------------------------------------- 9 | 10 | # Adapted data from Royle and Dorazio (Chapter 2) 11 | # Moth mortality data 12 | N = 12 # Number of experiments 13 | K = 20 # Number of moths in each experiment 14 | y = c(1,4,9,13,18,20, 0,2,6,10,12,16) # Number died 15 | sex = c(rep('male',6), rep('female',6)) # Sex 16 | dose = rep(0:5, 2) # Dose of ??? 17 | sex_male = as.integer(sex == 'male') # Turn sex into an integer 18 | # The key questions is: what are the effects of dose and sex? 19 | 20 | # Quick exploratory plot 21 | plot(dose, y, col = sex_male + 1) 22 | legend('topleft', legend = c('Female', 'Male'), 23 | col = 1:2, pch = 1) 24 | 25 | # Or better plot of proportion died 26 | plot(dose, y/K, col = sex_male + 1, las = 1) 27 | legend('topleft', legend = c('Female', 'Male'), 28 | col = 1:2, pch = 1) 29 | 30 | # JAGS code --------------------------------------------------------------- 31 | 32 | # Jags code to fit the model to the simulated data 33 | model_code = ' 34 | model 35 | { 36 | # Likelihood 37 | for (i in 1:N) { 38 | y[i] ~ dbin(p_died[i], K) 39 | logit(p_died[i]) <- alpha + beta_male * x_male[i] + beta_dose * x_dose[i] 40 | } 41 | 42 | # Priors 43 | alpha ~ dnorm(0, 10^-2) 44 | beta_male ~ dnorm(0, 10^-2) 45 | beta_dose ~ dnorm(0, 10^-2) 46 | } 47 | ' 48 | 49 | # Run the model ----------------------------------------------------------- 50 | 51 | # Set up the data 52 | model_data = list(N = N, y = y, x_male = sex_male, x_dose = dose, K = K) 53 | 54 | # Choose the parameters to watch 55 | model_parameters = c("alpha", "beta_male", "beta_dose") 56 | 57 | # Run the model 58 | model_run = jags(data = model_data, 59 | parameters.to.save = model_parameters, 60 | model.file = textConnection(model_code)) 61 | 62 | # Look at output ---------------------------------------------------------- 63 | 64 | print(model_run) 65 | plot(model_run) 66 | 67 | # Look at effect of sex - quantified by beta_male 68 | hist(model_run$BUGSoutput$sims.list$beta_male, breaks = 30) 69 | # Seems positive - males more likely to die 70 | 71 | # Can vconvert this into odds: 72 | hist(exp(model_run$BUGSoutput$sims.list$beta_male), breaks = 30) 73 | # 3 times higher chance of dying if you're male! 74 | 75 | # Look at the posterior 76 | head(model_run$BUGSoutput$sims.matrix) 77 | 78 | # Extract the mean values of the parameters 79 | alpha_mean = model_run$BUGSoutput$mean$alpha[1] 80 | beta_male_mean = model_run$BUGSoutput$mean$beta_male[1] 81 | beta_dose_mean = model_run$BUGSoutput$mean$beta_dose[1] 82 | 83 | # What about effect of dose? 84 | o = order(dose) 85 | plot(dose, y/K, col = sex_male + 1, las = 1) 86 | # Males 87 | with(model_run, 88 | lines(dose[o], 89 | inv.logit(alpha_mean + beta_male_mean * 1 + 90 | beta_dose_mean * dose[o]), 91 | col = 'red')) 92 | # Females 93 | with(model_run, 94 | lines(dose[o], 95 | inv.logit(alpha_mean + beta_male_mean * 0 + 96 | beta_dose_mean * dose[o]), 97 | col = 'blue')) 98 | 99 | # Legend 100 | legend('topleft', 101 | legend = c('Males', 'Females'), 102 | lty = 1, 103 | col = c('red','blue')) 104 | 105 | 106 | # Other tasks ------------------------------------------------------------- 107 | 108 | # 1) See if there is an interaction between sex and dose in the above example. To do this add an extra term in the model beta_int * x_male[i] * x_dose[i]. Don't forget to include beta_int in your parameters to watch vector. Is beta_int precisely estimated in the posterior? 109 | # 2) It almost always the case that the death rate increases with dose. Try changing the prior distribution on beta_dose to reflect the fact that the parameter should be positive. How much effect does this have on the posterior ditribution of beta_dose? 110 | # 3) (Harder) A common task is to estimate the LD-50, the dose at which 50% of the animals have died. See if you can estimate the LD-50 for males and females with uncertainty. (hint: either estimate a 95% CI for the LD50 for each group or, better yet, produce a full posterior distribution) 111 | 112 | -------------------------------------------------------------------------------- /Instats/Day-2/Session-7/practical_jags_poisson.R: -------------------------------------------------------------------------------- 1 | # Practical session on using JAGS for Poisson regression 2 | 3 | # Start up code - clear workspace and call packages 4 | rm(list=ls()) # Clear the workspace 5 | library(R2jags) 6 | 7 | # Data -------------------------------------------------------------------- 8 | 9 | butterflies = data.frame(garden = factor(c(1, 1, 1, 1, 1, 1, 1, 1, 2, 2, 2, 10 | 2, 2, 2, 2, 2, 3, 3, 3, 3, 3, 3, 11 | 3, 3)), 12 | monarchs = c(0, 4, 2, 2, 0, 6, 0, 0, 5, 9, 7, 13 | 5, 7, 5, 9, 5, 10, 14, 12, 12, 14 | 10, 16, 10, 10)) 15 | 16 | # A simple plot 17 | boxplot(monarchs ~ garden, data = butterflies) 18 | 19 | # JAGS code --------------------------------------------------------------- 20 | 21 | # Jags code to fit the model to the simulated data 22 | model_code = ' 23 | model 24 | { 25 | # Likelihood 26 | for (i in 1:N) { 27 | y[i] ~ dpois(rate[i]) 28 | log(rate[i]) <- alpha + beta_garden2 * x_garden2[i] + 29 | beta_garden3 * x_garden3[i] 30 | } 31 | # Priors 32 | alpha ~ dnorm(0, 10^-2) 33 | beta_garden2 ~ dnorm(0, 10^-2) 34 | beta_garden3 ~ dnorm(0, 10^-2) 35 | } 36 | ' 37 | 38 | # Run the model ----------------------------------------------------------- 39 | 40 | # Set up the data 41 | model_data = with(butterflies, 42 | list(N = length(garden), y = monarchs, 43 | x_garden2 = as.integer(garden==2), 44 | x_garden3 = as.integer(garden==3))) 45 | 46 | # Choose the parameters to watch 47 | model_parameters = c("alpha", "beta_garden2", "beta_garden3") 48 | 49 | # Run the model 50 | model_run = jags(data = model_data, 51 | parameters.to.save = model_parameters, 52 | model.file = textConnection(model_code)) 53 | 54 | # Look at output ---------------------------------------------------------- 55 | 56 | print(model_run) 57 | plot(model_run) 58 | 59 | # Extract tbe output of each garden and plot them against the data 60 | garden_1 = exp(model_run$BUGSoutput$sims.list$alpha) 61 | garden_2 = exp(model_run$BUGSoutput$sims.list$beta_garden2) 62 | garden_3 = exp(model_run$BUGSoutput$sims.list$beta_garden3) 63 | n_samples = length(garden_3) 64 | 65 | posterior = data.frame(garden = rep(1:3, each = n_samples), 66 | monarchs = c(garden_1, garden_2, garden_3)) 67 | 68 | #boxplot(monarchs ~ garden, data = butterflies) 69 | boxplot(monarchs ~ garden, data = posterior) 70 | 71 | # Other tasks ------------------------------------------------------------- 72 | 73 | # 1) Think carefully about how the model is set up and what the parameters actually represent. Perhpas we could change the model to get a parameter that represents the rate for each garden? 74 | # 2) The Poisson distribution unrealistically assumes that the mean and the variance are equal. Do you know any other count distributions that might fit these data better. If so fit them! 75 | 76 | -------------------------------------------------------------------------------- /Instats/Day-2/Session-8/reg_and_simms.pdf: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/andrewcparnell/simms_course/3052327e05aa93897fcfa31e2e8dd49fa3e45708/Instats/Day-2/Session-8/reg_and_simms.pdf -------------------------------------------------------------------------------- /Instats/Day-2/Session-8/simms-regression-instats.pptx: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/andrewcparnell/simms_course/3052327e05aa93897fcfa31e2e8dd49fa3e45708/Instats/Day-2/Session-8/simms-regression-instats.pptx -------------------------------------------------------------------------------- /Instats/Day-2/Session-9/simmr-practical-instats.pptx: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/andrewcparnell/simms_course/3052327e05aa93897fcfa31e2e8dd49fa3e45708/Instats/Day-2/Session-9/simmr-practical-instats.pptx -------------------------------------------------------------------------------- /Instats/Day-3/Session-11/siar-statistics-instats.pptx: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/andrewcparnell/simms_course/3052327e05aa93897fcfa31e2e8dd49fa3e45708/Instats/Day-3/Session-11/siar-statistics-instats.pptx -------------------------------------------------------------------------------- /Instats/Day-3/Session-11/siar_stats.pdf: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/andrewcparnell/simms_course/3052327e05aa93897fcfa31e2e8dd49fa3e45708/Instats/Day-3/Session-11/siar_stats.pdf -------------------------------------------------------------------------------- /Instats/Day-3/Session-12/mixsiar-practical-instats.pptx: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/andrewcparnell/simms_course/3052327e05aa93897fcfa31e2e8dd49fa3e45708/Instats/Day-3/Session-12/mixsiar-practical-instats.pptx -------------------------------------------------------------------------------- /Instats/Day-3/Session-12/mixsiar_script.R: -------------------------------------------------------------------------------- 1 | # A script to run MixSIAR on the Wolves example 2 | # The full versions are in the MixSIAR manual: https://github.com/brianstock/MixSIAR/blob/master/Manual/mixsiar_manual.pdf 3 | # and the MixSIAR vignettes: https://cran.r-project.org/web/packages/MixSIAR/vignettes/ 4 | 5 | library(MixSIAR) 6 | library(tidyr) # For pivoting later in new output_jags 7 | library(ggplot2) 8 | 9 | # My new output function 10 | source("output_jags_new.R") 11 | 12 | # Find the data 13 | mix.filename = system.file("extdata", "wolves_consumer.csv", package = "MixSIAR") 14 | #system(paste('open',mix.filename)) 15 | 16 | # Load into MixSIAR 17 | mix = load_mix_data(filename=mix.filename, 18 | iso_names=c("d13C","d15N"), 19 | factors=c("Region","Pack"), 20 | fac_random=c(TRUE,TRUE), 21 | fac_nested=c(FALSE,TRUE), 22 | cont_effects=NULL) 23 | 24 | # Have a look at the mixture data 25 | # mix.data = read.csv(mix.filename) 26 | # str(mix.data) 27 | 28 | # Replace the system.file call with the path to your file 29 | source.filename = system.file("extdata", "wolves_sources.csv", package = "MixSIAR") 30 | 31 | # Load the source data 32 | source = load_source_data(filename=source.filename, 33 | source_factors="Region", 34 | conc_dep=FALSE, 35 | data_type="means", 36 | mix) 37 | 38 | # Replace the system.file call with the path to your file 39 | discr.filename = system.file("extdata", "wolves_discrimination.csv", package = "MixSIAR") 40 | 41 | # Load the discrimination/TDF data 42 | discr = load_discr_data(filename=discr.filename, mix) 43 | 44 | # Isospace plot 45 | plot_data(filename="isospace_plot", 46 | plot_save_pdf = FALSE, 47 | plot_save_png = FALSE, 48 | mix = mix, 49 | source = source, 50 | discr = discr) 51 | 52 | # Alternative plot saving it and editing it 53 | p <- plot_data(filename="isospace_plot", 54 | plot_save_pdf = FALSE, 55 | plot_save_png = FALSE, 56 | mix = mix, 57 | source = source, 58 | discr = discr, return_obj = TRUE) 59 | 60 | # Can now do e.g. 61 | p + theme(legend.position = "None") 62 | 63 | # Plot the prior 64 | #plot_prior(alpha.prior=1,source) 65 | 66 | # Write the model out 67 | model_filename = "MixSIAR_model.txt" # Name of the JAGS model file 68 | write_JAGS_model(model_filename, 69 | resid_err = TRUE, 70 | process_err = TRUE, 71 | mix, source) 72 | 73 | # Test run 74 | jags.1 = run_model(run="test", 75 | mix, source, discr, 76 | model_filename, 77 | alpha.prior = c(1,1,1), 78 | resid_err = TRUE, 79 | process_err = TRUE) 80 | 81 | # Long run - change run to 'normal' if you have 5 hours to kill 82 | jags.2 = run_model(run="very short", 83 | mix, source, discr, 84 | model_filename, 85 | alpha.prior = 1, 86 | resid_err = TRUE, 87 | process_err = TRUE) 88 | 89 | # Get the output 90 | # output_JAGS(jags.1, mix = mix, source = source, 91 | # output_options = 92 | # list(summary_save = TRUE, 93 | # summary_name = "summary_statistics", 94 | # sup_post = FALSE, 95 | # plot_post_save_pdf = FALSE, 96 | # plot_post_name = "posterior_density", 97 | # sup_pairs = FALSE, 98 | # plot_pairs_save_pdf = FALSE, 99 | # plot_pairs_name = "pairs_plot", 100 | # sup_xy = FALSE, 101 | # plot_xy_save_pdf = FALSE, 102 | # plot_xy_name = "xy_plot", 103 | # gelman = FALSE, 104 | # heidel =FALSE, 105 | # geweke = FALSE, 106 | # diag_save = FALSE, 107 | # diag_name = "diagnostics", 108 | # indiv_effect = FALSE, 109 | # plot_post_save_png = FALSE, 110 | # plot_pairs_save_png = FALSE, 111 | # plot_xy_save_png = FALSE, 112 | # diag_save_ggmcmc = FALSE)) 113 | 114 | out <- output_JAGS(jags.1, mix = mix, source = source, 115 | c('summary_diagnostics', 116 | 'summary_statistics', 117 | 'summary_quantiles', 118 | 'plot_global', 119 | 'plot_factors')) 120 | 121 | 122 | -------------------------------------------------------------------------------- /Instats/Day-3/Session-12/simmr_with_priors.R: -------------------------------------------------------------------------------- 1 | # Taken from the help(simmr_elicit) file 2 | 3 | # Clear workspace and load in packages 4 | rm(list = ls()) 5 | library(simmr) 6 | 7 | # (Fake) data set: 10 observations, 2 tracers, 4 sources 8 | mix = matrix(c(-10.13, -10.72, -11.39, -11.18, -10.81, -10.7, -10.54, 9 | -10.48, -9.93, -9.37, 11.59, 11.01, 10.59, 10.97, 11.52, 11.89, 10 | 11.73, 10.89, 11.05, 12.3), ncol=2, nrow=10) 11 | colnames(mix) = c('d13C','d15N') 12 | s_names=c('Source A','Source B','Source C','Source D') 13 | s_means = matrix(c(-14, -15.1, -11.03, -14.44, 3.06, 7.05, 13.72, 5.96), ncol=2, nrow=4) 14 | s_sds = matrix(c(0.48, 0.38, 0.48, 0.43, 0.46, 0.39, 0.42, 0.48), ncol=2, nrow=4) 15 | c_means = matrix(c(2.63, 1.59, 3.41, 3.04, 3.28, 2.34, 2.14, 2.36), ncol=2, nrow=4) 16 | c_sds = matrix(c(0.41, 0.44, 0.34, 0.46, 0.46, 0.48, 0.46, 0.66), ncol=2, nrow=4) 17 | conc = matrix(c(0.02, 0.1, 0.12, 0.04, 0.02, 0.1, 0.09, 0.05), ncol=2, nrow=4) 18 | 19 | # Load into simmr 20 | simmr_1 = simmr_load(mixtures=mix, 21 | source_names=s_names, 22 | source_means=s_means, 23 | source_sds=s_sds, 24 | correction_means=c_means, 25 | correction_sds=c_sds, 26 | concentration_means = conc) 27 | 28 | # Iso-space plot 29 | plot(simmr_1) 30 | 31 | # MCMC run 32 | simmr_1_out = simmr_mcmc(simmr_1) 33 | 34 | # Summary 35 | summary(simmr_1_out,'quantiles') 36 | # A bit vague: 37 | # 2.5 38 | # Source A 0.029 0.115 0.203 0.312 0.498 39 | # Source B 0.146 0.232 0.284 0.338 0.453 40 | # Source C 0.216 0.255 0.275 0.296 0.342 41 | # Source D 0.032 0.123 0.205 0.299 0.465 42 | 43 | # Look at prior/posterior 44 | prior_viz(simmr_1_out) 45 | 46 | # Now suppose I had prior information that: 47 | # proportion means = 0.5,0.2,0.2,0.1 48 | # proportion sds = 0.08,0.02,0.01,0.02 49 | prior=simmr_elicit(n_sources = 4, 50 | c(0.5,0.2,0.2,0.1), 51 | c(0.08,0.02,0.01,0.02)) 52 | 53 | simmr_1a_out = simmr_mcmc(simmr_1, 54 | prior_control=list(means=prior$mean, 55 | sd=prior$sd, 56 | sigma_shape = c(3, 3), 57 | sigma_rate = c(3/50, 3/50))) 58 | 59 | summary(simmr_1a_out,'quantiles') 60 | # Much more precise: 61 | # 2.5 62 | # Source A 0.441 0.494 0.523 0.553 0.610 63 | # Source B 0.144 0.173 0.188 0.204 0.236 64 | # Source C 0.160 0.183 0.196 0.207 0.228 65 | # Source D 0.060 0.079 0.091 0.105 0.135 66 | 67 | # Look at prior/posterior 68 | prior_viz(simmr_1a_out) 69 | -------------------------------------------------------------------------------- /Instats/Day-3/Session-13/siar-dos-donts-instats.pptx: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/andrewcparnell/simms_course/3052327e05aa93897fcfa31e2e8dd49fa3e45708/Instats/Day-3/Session-13/siar-dos-donts-instats.pptx -------------------------------------------------------------------------------- /Instats/Day-3/Session-14/siar-dos-donts-instats.pptx: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/andrewcparnell/simms_course/3052327e05aa93897fcfa31e2e8dd49fa3e45708/Instats/Day-3/Session-14/siar-dos-donts-instats.pptx -------------------------------------------------------------------------------- /Instats/Day-3/Session-15/source-aggregation-instats.pptx: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/andrewcparnell/simms_course/3052327e05aa93897fcfa31e2e8dd49fa3e45708/Instats/Day-3/Session-15/source-aggregation-instats.pptx -------------------------------------------------------------------------------- /Instats/Day-4/Session-16/siber-intro-ellipses-instats.pptx: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/andrewcparnell/simms_course/3052327e05aa93897fcfa31e2e8dd49fa3e45708/Instats/Day-4/Session-16/siber-intro-ellipses-instats.pptx -------------------------------------------------------------------------------- /Instats/Day-4/Session-17/siber-practical-ellipses-instats.pptx: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/andrewcparnell/simms_course/3052327e05aa93897fcfa31e2e8dd49fa3e45708/Instats/Day-4/Session-17/siber-practical-ellipses-instats.pptx -------------------------------------------------------------------------------- /Instats/Day-4/Session-18/siber-practical-communities-instats.pptx: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/andrewcparnell/simms_course/3052327e05aa93897fcfa31e2e8dd49fa3e45708/Instats/Day-4/Session-18/siber-practical-communities-instats.pptx -------------------------------------------------------------------------------- /Instats/Day-4/Session-19/cosimmr-practical-instats.pptx: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/andrewcparnell/simms_course/3052327e05aa93897fcfa31e2e8dd49fa3e45708/Instats/Day-4/Session-19/cosimmr-practical-instats.pptx -------------------------------------------------------------------------------- /Instats/Day-4/Session-19/cosimmr_presentation.pdf: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/andrewcparnell/simms_course/3052327e05aa93897fcfa31e2e8dd49fa3e45708/Instats/Day-4/Session-19/cosimmr_presentation.pdf -------------------------------------------------------------------------------- /Instats/Day-4/Session-20/open-practical.pptx: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/andrewcparnell/simms_course/3052327e05aa93897fcfa31e2e8dd49fa3e45708/Instats/Day-4/Session-20/open-practical.pptx -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # Stable Isotope Mixing Models course 2 | 3 | Slides and code for the Stable Isotope Mixing Models course given by Andrew Parnell and Andrew Jackson 4 | 5 | For the course page see [here](https://andrewcparnell.github.io/simms_course/index.html). 6 | -------------------------------------------------------------------------------- /Workflow.Rmd: -------------------------------------------------------------------------------- 1 | --- 2 | title: 'What package should I use?' 3 | author: "Andrew Parnell and Andrew Jackson" 4 | output: 5 | html_document: default 6 | pdf_document: default 7 | --- 8 | 9 | ## I am interested in quantifying the __diet/consumption__ of my organisms 10 | 11 | You will need source and consumer isotope data (and optionally discrimination factors and concentration dependence). 12 | 13 | If you have no extra covariates then you should start with __simmr__. If you have extra covariates or raw source data then you should start with __MixSIAR__. If you have no trophic discrimination factors you should start with __SIDER__ and then move to __simmr__ or __MixSIAR__. 14 | 15 | ## I am interested in quantifying the __niche behaviour__ of my organisms. 16 | 17 | You will need consumer isotope data only and you should start with __SIBER__. -------------------------------------------------------------------------------- /_config.yml: -------------------------------------------------------------------------------- 1 | theme: jekyll-theme-cayman -------------------------------------------------------------------------------- /aj-content/data/example_ellipse_data.csv: -------------------------------------------------------------------------------- 1 | group,x,y 2 | 1,5.394867262,4.764343342 3 | 1,5.876490188,7.302932725 4 | 1,6.375809019,4.053575019 5 | 1,6.603434778,3.106753733 6 | 1,8.502267081,10.73373426 7 | 1,7.20950612,7.082829243 8 | 1,5.978550597,3.511956803 9 | 1,5.703899924,3.155316691 10 | 1,6.379951747,4.165533161 11 | 1,6.372268298,7.548374224 12 | 1,6.283806253,8.396794224 13 | 1,6.806669406,9.196459002 14 | 1,5.905358359,3.624892238 15 | 1,6.951191693,10.13476848 16 | 1,5.633094965,3.373993092 17 | 1,6.431308711,10.23102858 18 | 1,6.905275025,8.355759388 19 | 1,6.713014943,5.412331535 20 | 1,7.241486053,5.682442885 21 | 1,6.562991506,3.305996273 22 | 2,1.952547384,1.661447394 23 | 2,11.89564378,-1.051769336 24 | 2,14.01968466,0.869805129 25 | 2,4.909964898,1.76268613 26 | 2,2.283268293,3.037531122 27 | 2,8.545675059,1.75422039 28 | 2,5.810866197,1.335037334 29 | 2,15.6541802,-0.828216644 30 | 2,6.182665574,1.95647659 31 | 2,9.011400138,3.36148399 32 | 2,6.443592338,-0.254152302 33 | 2,3.45073219,3.696210219 34 | 2,7.06752307,2.484850216 35 | 2,-0.669036335,5.34246954 36 | 2,12.02187783,0.392012255 37 | 2,6.929617536,2.57307794 38 | 2,14.76554402,0.665692894 39 | 2,8.1801018,0.879437944 40 | 2,3.580053558,4.619131877 41 | 2,8.704797729,3.402456371 42 | 3,0.242970766,8.949482674 43 | 3,1.831353815,6.461537536 44 | 3,1.520604819,6.173023379 45 | 3,2.921375951,8.093151809 46 | 3,0.906247482,7.188650105 47 | 3,3.16646534,5.317819678 48 | 3,1.44825342,6.918647867 49 | 3,6.569125297,3.916190473 50 | 3,4.347750793,4.487826703 51 | 3,4.756822162,4.199100925 52 | 3,3.644656978,4.063314785 53 | 3,6.389163883,2.322274238 54 | 3,1.488106545,7.154863477 55 | 3,1.856210823,5.746695171 56 | 3,5.860781543,4.215669568 57 | 3,1.456474927,4.521496387 58 | 3,2.393833516,6.796780613 59 | 3,2.001761228,4.025481559 60 | 3,2.155723374,5.976196207 61 | 3,2.24216024,5.519312864 62 | -------------------------------------------------------------------------------- /aj-content/data/example_layman_data.csv: -------------------------------------------------------------------------------- 1 | group,x,y 1,5.132626337,3.688303346 1,5.027934415,4.86449461 1,6.043919263,7.318477302 1,5.110484276,5.406555477 1,4.755094023,3.77654147 1,5.35203914,5.617408095 1,4.96480273,3.855380519 1,5.485278005,5.425605628 1,5.310339664,4.98686112 1,4.922920527,3.40061299 1,4.995183205,3.994522482 1,6.079741693,6.703973534 1,5.667668213,6.246105804 1,5.205787713,5.192862848 1,4.964732589,4.183737055 1,5.224107993,4.974401533 1,4.992166699,4.083743111 1,5.925788905,7.077552652 1,5.29923779,4.813968073 1,5.954730084,6.566349426 2,0.186432047,3.60685959 2,3.53581808,4.225075007 2,0.504743861,4.391172372 2,2.624164956,4.545632584 2,-0.054902607,4.614149715 2,-1.113834017,3.916090414 2,0.780661622,5.916003992 2,2.23226819,5.004188948 2,2.466778349,5.259539064 2,3.080695315,2.416390976 2,-0.908995482,5.959884781 2,3.495390467,2.008932394 2,0.342270113,3.99767439 2,1.335476776,2.852039795 2,1.736848919,1.843641011 2,0.207525262,6.615010247 2,-1.151851479,4.203319505 2,0.600401651,4.212496411 2,1.250162161,3.953428173 2,0.120473278,5.058224924 3,-3.688510878,11.69510182 3,-2.906022384,10.93901342 3,6.77827967,7.865499873 3,0.894642457,10.88325565 3,-1.668558317,9.213310114 3,-0.94066673,6.163726342 3,4.110663246,9.377163909 3,1.657192561,7.462571797 3,-0.075812522,13.09718642 3,-1.388245018,7.460632999 3,-1.727593713,5.825064099 3,7.045624514,5.655886041 3,3.705080392,3.922238305 3,6.926990223,6.022669553 3,-0.398681454,3.066479872 3,-0.186033824,2.893589666 3,-2.225552909,11.25626059 3,0.116377112,8.889179479 3,2.294062806,9.285560188 3,4.968259159,-0.782315842 4,6.00688289,2.337958172 4,8.294047021,2.692525403 4,10.90199989,1.43206863 4,7.705966491,0.92379691 4,10.0504632,3.348141318 4,11.78596602,0.266933853 4,8.108676329,-0.311553644 4,11.55182111,7.901243686 4,11.27023748,-1.74532422 4,7.706629171,-1.290571585 4,7.205830206,3.582226344 4,12.3892023,6.000890177 4,9.380700364,4.159523124 4,11.722788,3.364908249 4,7.607627329,2.231744866 4,13.18092562,1.788754671 4,6.297666993,-0.549545151 4,11.83613119,2.684453952 4,11.04510384,0.816048666 4,10.52970271,4.603357269 -------------------------------------------------------------------------------- /aj-content/data/example_layman_data_2.csv: -------------------------------------------------------------------------------- 1 | group,x,y 2 | 1,1.47672050748147,1.17108075220332 3 | 1,1.78474114579384,-0.115619850113597 4 | 1,2.08297957968058,-0.226144899986645 5 | 1,1.69239938904151,-0.797729970774438 6 | 1,2.5937068348509,-1.46970522952285 7 | 1,0.929267911564972,-0.196223500356883 8 | 1,0.316196549834006,0.055579962490119 9 | 1,0.34828093429191,-2.05319335099901 10 | 2,-8.84901120321665,1.70961876535899 11 | 2,-8.95323608345684,-0.462825098668409 12 | 2,-7.94081971439991,-0.409293519802588 13 | 2,-8.21818793125998,0.881776553508364 14 | 2,-8.21801657550971,1.49923222563852 15 | 2,-7.79031833673483,1.28024865761635 16 | 2,-7.67738978696099,-1.07200982937118 17 | 2,-7.49540215349238,0.276722442832635 18 | 3,11.4585043207037,7.61766767768741 19 | 3,9.61891573633192,7.14580158872087 20 | 3,11.7729793582136,7.45554630150557 21 | 3,8.97119317938735,6.23782773891042 22 | 3,10.1851368813756,7.10026079251286 23 | 3,11.8594554661989,5.99124923667695 24 | 3,11.0004299225393,5.21345444750101 25 | 3,10.1402652670935,6.4228613572901 26 | 4,10.3086128988337,0.272440220521721 27 | 4,10.4797696121373,0.618156170175794 28 | 4,9.8960927665982,2.79915198759397 29 | 4,10.9044343602082,1.24147963524833 30 | 4,9.47090978813302,0.258169823442064 31 | 4,9.08935434189085,1.7818003762512 32 | 4,10.1533162271111,2.05617815831442 33 | 4,9.04291356989047,1.81824535576175 34 | -------------------------------------------------------------------------------- /aj-content/data/example_layman_data_all.csv: -------------------------------------------------------------------------------- 1 | iso1,iso2,group,community 5.132626337,3.688303346,1,1 5.027934415,4.86449461,1,1 6.043919263,7.318477302,1,1 5.110484276,5.406555477,1,1 4.755094023,3.77654147,1,1 5.35203914,5.617408095,1,1 4.96480273,3.855380519,1,1 5.485278005,5.425605628,1,1 5.310339664,4.98686112,1,1 4.922920527,3.40061299,1,1 4.995183205,3.994522482,1,1 6.079741693,6.703973534,1,1 5.667668213,6.246105804,1,1 5.205787713,5.192862848,1,1 4.964732589,4.183737055,1,1 5.224107993,4.974401533,1,1 4.992166699,4.083743111,1,1 5.925788905,7.077552652,1,1 5.29923779,4.813968073,1,1 5.954730084,6.566349426,1,1 0.186432047,3.60685959,2,1 3.53581808,4.225075007,2,1 0.504743861,4.391172372,2,1 2.624164956,4.545632584,2,1 -0.054902607,4.614149715,2,1 -1.113834017,3.916090414,2,1 0.780661622,5.916003992,2,1 2.23226819,5.004188948,2,1 2.466778349,5.259539064,2,1 3.080695315,2.416390976,2,1 -0.908995482,5.959884781,2,1 3.495390467,2.008932394,2,1 0.342270113,3.99767439,2,1 1.335476776,2.852039795,2,1 1.736848919,1.843641011,2,1 0.207525262,6.615010247,2,1 -1.151851479,4.203319505,2,1 0.600401651,4.212496411,2,1 1.250162161,3.953428173,2,1 0.120473278,5.058224924,2,1 -3.688510878,11.69510182,3,1 -2.906022384,10.93901342,3,1 6.77827967,7.865499873,3,1 0.894642457,10.88325565,3,1 -1.668558317,9.213310114,3,1 -0.94066673,6.163726342,3,1 4.110663246,9.377163909,3,1 1.657192561,7.462571797,3,1 -0.075812522,13.09718642,3,1 -1.388245018,7.460632999,3,1 -1.727593713,5.825064099,3,1 7.045624514,5.655886041,3,1 3.705080392,3.922238305,3,1 6.926990223,6.022669553,3,1 -0.398681454,3.066479872,3,1 -0.186033824,2.893589666,3,1 -2.225552909,11.25626059,3,1 0.116377112,8.889179479,3,1 2.294062806,9.285560188,3,1 4.968259159,-0.782315842,3,1 6.00688289,2.337958172,4,1 8.294047021,2.692525403,4,1 10.90199989,1.43206863,4,1 7.705966491,0.92379691,4,1 10.0504632,3.348141318,4,1 11.78596602,0.266933853,4,1 8.108676329,-0.311553644,4,1 11.55182111,7.901243686,4,1 11.27023748,-1.74532422,4,1 7.706629171,-1.290571585,4,1 7.205830206,3.582226344,4,1 12.3892023,6.000890177,4,1 9.380700364,4.159523124,4,1 11.722788,3.364908249,4,1 7.607627329,2.231744866,4,1 13.18092562,1.788754671,4,1 6.297666993,-0.549545151,4,1 11.83613119,2.684453952,4,1 11.04510384,0.816048666,4,1 10.52970271,4.603357269,4,1 1.476720507,1.171080752,1,2 1.784741146,-0.11561985,1,2 2.08297958,-0.2261449,1,2 1.692399389,-0.797729971,1,2 2.593706835,-1.46970523,1,2 0.929267912,-0.1962235,1,2 0.31619655,0.055579962,1,2 0.348280934,-2.053193351,1,2 -8.849011203,1.709618765,2,2 -8.953236083,-0.462825099,2,2 -7.940819714,-0.40929352,2,2 -8.218187931,0.881776554,2,2 -8.218016576,1.499232226,2,2 -7.790318337,1.280248658,2,2 -7.677389787,-1.072009829,2,2 -7.495402153,0.276722443,2,2 11.45850432,7.617667678,3,2 9.618915736,7.145801589,3,2 11.77297936,7.455546302,3,2 8.971193179,6.237827739,3,2 10.18513688,7.100260793,3,2 11.85945547,5.991249237,3,2 11.00042992,5.213454448,3,2 10.14026527,6.422861357,3,2 10.3086129,0.272440221,4,2 10.47976961,0.61815617,4,2 9.896092767,2.799151988,4,2 10.90443436,1.241479635,4,2 9.470909788,0.258169823,4,2 9.089354342,1.781800376,4,2 10.15331623,2.056178158,4,2 9.04291357,1.818245356,4,2 -------------------------------------------------------------------------------- /aj-content/data/example_layman_data_text_labels.csv: -------------------------------------------------------------------------------- 1 | iso1,iso2,group,community 2 | 5.132626337,3.688303346,a,A 3 | 5.027934415,4.86449461,a,A 4 | 6.043919263,7.318477302,a,A 5 | 5.110484276,5.406555477,a,A 6 | 4.755094023,3.77654147,a,A 7 | 5.35203914,5.617408095,a,A 8 | 4.96480273,3.855380519,a,A 9 | 5.485278005,5.425605628,a,A 10 | 5.310339664,4.98686112,a,A 11 | 4.922920527,3.40061299,a,A 12 | 4.995183205,3.994522482,a,A 13 | 6.079741693,6.703973534,a,A 14 | 5.667668213,6.246105804,a,A 15 | 5.205787713,5.192862848,a,A 16 | 4.964732589,4.183737055,a,A 17 | 5.224107993,4.974401533,a,A 18 | 4.992166699,4.083743111,a,A 19 | 5.925788905,7.077552652,a,A 20 | 5.29923779,4.813968073,a,A 21 | 5.954730084,6.566349426,a,A 22 | 0.186432047,3.60685959,b,A 23 | 3.53581808,4.225075007,b,A 24 | 0.504743861,4.391172372,b,A 25 | 2.624164956,4.545632584,b,A 26 | -0.054902607,4.614149715,b,A 27 | -1.113834017,3.916090414,b,A 28 | 0.780661622,5.916003992,b,A 29 | 2.23226819,5.004188948,b,A 30 | 2.466778349,5.259539064,b,A 31 | 3.080695315,2.416390976,b,A 32 | -0.908995482,5.959884781,b,A 33 | 3.495390467,2.008932394,b,A 34 | 0.342270113,3.99767439,b,A 35 | 1.335476776,2.852039795,b,A 36 | 1.736848919,1.843641011,b,A 37 | 0.207525262,6.615010247,b,A 38 | -1.151851479,4.203319505,b,A 39 | 0.600401651,4.212496411,b,A 40 | 1.250162161,3.953428173,b,A 41 | 0.120473278,5.058224924,b,A 42 | -3.688510878,11.69510182,c,A 43 | -2.906022384,10.93901342,c,A 44 | 6.77827967,7.865499873,c,A 45 | 0.894642457,10.88325565,c,A 46 | -1.668558317,9.213310114,c,A 47 | -0.94066673,6.163726342,c,A 48 | 4.110663246,9.377163909,c,A 49 | 1.657192561,7.462571797,c,A 50 | -0.075812522,13.09718642,c,A 51 | -1.388245018,7.460632999,c,A 52 | -1.727593713,5.825064099,c,A 53 | 7.045624514,5.655886041,c,A 54 | 3.705080392,3.922238305,c,A 55 | 6.926990223,6.022669553,c,A 56 | -0.398681454,3.066479872,c,A 57 | -0.186033824,2.893589666,c,A 58 | -2.225552909,11.25626059,c,A 59 | 0.116377112,8.889179479,c,A 60 | 2.294062806,9.285560188,c,A 61 | 4.968259159,-0.782315842,c,A 62 | 6.00688289,2.337958172,d,A 63 | 8.294047021,2.692525403,d,A 64 | 10.90199989,1.43206863,d,A 65 | 7.705966491,0.92379691,d,A 66 | 10.0504632,3.348141318,d,A 67 | 11.78596602,0.266933853,d,A 68 | 8.108676329,-0.311553644,d,A 69 | 11.55182111,7.901243686,d,A 70 | 11.27023748,-1.74532422,d,A 71 | 7.706629171,-1.290571585,d,A 72 | 7.205830206,3.582226344,d,A 73 | 12.3892023,6.000890177,d,A 74 | 9.380700364,4.159523124,d,A 75 | 11.722788,3.364908249,d,A 76 | 7.607627329,2.231744866,d,A 77 | 13.18092562,1.788754671,d,A 78 | 6.297666993,-0.549545151,d,A 79 | 11.83613119,2.684453952,d,A 80 | 11.04510384,0.816048666,d,A 81 | 10.52970271,4.603357269,d,A 82 | 1.476720507,1.171080752,a,B 83 | 1.784741146,-0.11561985,a,B 84 | 2.08297958,-0.2261449,a,B 85 | 1.692399389,-0.797729971,a,B 86 | 2.593706835,-1.46970523,a,B 87 | 0.929267912,-0.1962235,a,B 88 | 0.31619655,0.055579962,a,B 89 | 0.348280934,-2.053193351,a,B 90 | -8.849011203,1.709618765,b,B 91 | -8.953236083,-0.462825099,b,B 92 | -7.940819714,-0.40929352,b,B 93 | -8.218187931,0.881776554,b,B 94 | -8.218016576,1.499232226,b,B 95 | -7.790318337,1.280248658,b,B 96 | -7.677389787,-1.072009829,b,B 97 | -7.495402153,0.276722443,b,B 98 | 11.45850432,7.617667678,c,B 99 | 9.618915736,7.145801589,c,B 100 | 11.77297936,7.455546302,c,B 101 | 8.971193179,6.237827739,c,B 102 | 10.18513688,7.100260793,c,B 103 | 11.85945547,5.991249237,c,B 104 | 11.00042992,5.213454448,c,B 105 | 10.14026527,6.422861357,c,B 106 | 10.3086129,0.272440221,d,B 107 | 10.47976961,0.61815617,d,B 108 | 9.896092767,2.799151988,d,B 109 | 10.90443436,1.241479635,d,B 110 | 9.470909788,0.258169823,d,B 111 | 9.089354342,1.781800376,d,B 112 | 10.15331623,2.056178158,d,B 113 | 9.04291357,1.818245356,d,B -------------------------------------------------------------------------------- /aj-content/images/SEA-community1.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/andrewcparnell/simms_course/3052327e05aa93897fcfa31e2e8dd49fa3e45708/aj-content/images/SEA-community1.png -------------------------------------------------------------------------------- /aj-content/images/biplot.jpeg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/andrewcparnell/simms_course/3052327e05aa93897fcfa31e2e8dd49fa3e45708/aj-content/images/biplot.jpeg -------------------------------------------------------------------------------- /aj-content/images/chem_frac.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/andrewcparnell/simms_course/3052327e05aa93897fcfa31e2e8dd49fa3e45708/aj-content/images/chem_frac.png -------------------------------------------------------------------------------- /aj-content/images/community-hulls.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/andrewcparnell/simms_course/3052327e05aa93897fcfa31e2e8dd49fa3e45708/aj-content/images/community-hulls.png -------------------------------------------------------------------------------- /aj-content/images/crayfish-carp.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/andrewcparnell/simms_course/3052327e05aa93897fcfa31e2e8dd49fa3e45708/aj-content/images/crayfish-carp.png -------------------------------------------------------------------------------- /aj-content/images/different-ellipses.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/andrewcparnell/simms_course/3052327e05aa93897fcfa31e2e8dd49fa3e45708/aj-content/images/different-ellipses.png -------------------------------------------------------------------------------- /aj-content/images/dos-and-donts.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/andrewcparnell/simms_course/3052327e05aa93897fcfa31e2e8dd49fa3e45708/aj-content/images/dos-and-donts.png -------------------------------------------------------------------------------- /aj-content/images/dspace-pspace.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/andrewcparnell/simms_course/3052327e05aa93897fcfa31e2e8dd49fa3e45708/aj-content/images/dspace-pspace.jpg -------------------------------------------------------------------------------- /aj-content/images/dspace-pspace.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/andrewcparnell/simms_course/3052327e05aa93897fcfa31e2e8dd49fa3e45708/aj-content/images/dspace-pspace.png -------------------------------------------------------------------------------- /aj-content/images/ellipse-vs-hulls.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/andrewcparnell/simms_course/3052327e05aa93897fcfa31e2e8dd49fa3e45708/aj-content/images/ellipse-vs-hulls.png -------------------------------------------------------------------------------- /aj-content/images/fry-feasible.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/andrewcparnell/simms_course/3052327e05aa93897fcfa31e2e8dd49fa3e45708/aj-content/images/fry-feasible.png -------------------------------------------------------------------------------- /aj-content/images/geese-over-time.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/andrewcparnell/simms_course/3052327e05aa93897fcfa31e2e8dd49fa3e45708/aj-content/images/geese-over-time.png -------------------------------------------------------------------------------- /aj-content/images/grass-in-geese.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/andrewcparnell/simms_course/3052327e05aa93897fcfa31e2e8dd49fa3e45708/aj-content/images/grass-in-geese.png -------------------------------------------------------------------------------- /aj-content/images/heavy_frac.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/andrewcparnell/simms_course/3052327e05aa93897fcfa31e2e8dd49fa3e45708/aj-content/images/heavy_frac.png -------------------------------------------------------------------------------- /aj-content/images/ideal_iso_community.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/andrewcparnell/simms_course/3052327e05aa93897fcfa31e2e8dd49fa3e45708/aj-content/images/ideal_iso_community.png -------------------------------------------------------------------------------- /aj-content/images/infer_process.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/andrewcparnell/simms_course/3052327e05aa93897fcfa31e2e8dd49fa3e45708/aj-content/images/infer_process.png -------------------------------------------------------------------------------- /aj-content/images/isoconc.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/andrewcparnell/simms_course/3052327e05aa93897fcfa31e2e8dd49fa3e45708/aj-content/images/isoconc.png -------------------------------------------------------------------------------- /aj-content/images/isosource-screenshot.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/andrewcparnell/simms_course/3052327e05aa93897fcfa31e2e8dd49fa3e45708/aj-content/images/isosource-screenshot.png -------------------------------------------------------------------------------- /aj-content/images/layman-community.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/andrewcparnell/simms_course/3052327e05aa93897fcfa31e2e8dd49fa3e45708/aj-content/images/layman-community.png -------------------------------------------------------------------------------- /aj-content/images/layman-frag.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/andrewcparnell/simms_course/3052327e05aa93897fcfa31e2e8dd49fa3e45708/aj-content/images/layman-frag.png -------------------------------------------------------------------------------- /aj-content/images/layman-pops.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/andrewcparnell/simms_course/3052327e05aa93897fcfa31e2e8dd49fa3e45708/aj-content/images/layman-pops.png -------------------------------------------------------------------------------- /aj-content/images/lipid_corrected_liver.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/andrewcparnell/simms_course/3052327e05aa93897fcfa31e2e8dd49fa3e45708/aj-content/images/lipid_corrected_liver.jpg -------------------------------------------------------------------------------- /aj-content/images/matrix-plot.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/andrewcparnell/simms_course/3052327e05aa93897fcfa31e2e8dd49fa3e45708/aj-content/images/matrix-plot.png -------------------------------------------------------------------------------- /aj-content/images/omit-source-table.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/andrewcparnell/simms_course/3052327e05aa93897fcfa31e2e8dd49fa3e45708/aj-content/images/omit-source-table.png -------------------------------------------------------------------------------- /aj-content/images/omit-source.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/andrewcparnell/simms_course/3052327e05aa93897fcfa31e2e8dd49fa3e45708/aj-content/images/omit-source.png -------------------------------------------------------------------------------- /aj-content/images/posteriori-agg.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/andrewcparnell/simms_course/3052327e05aa93897fcfa31e2e8dd49fa3e45708/aj-content/images/posteriori-agg.png -------------------------------------------------------------------------------- /aj-content/images/print-web-trinity.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/andrewcparnell/simms_course/3052327e05aa93897fcfa31e2e8dd49fa3e45708/aj-content/images/print-web-trinity.jpg -------------------------------------------------------------------------------- /aj-content/images/sample-size-community-hulls.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/andrewcparnell/simms_course/3052327e05aa93897fcfa31e2e8dd49fa3e45708/aj-content/images/sample-size-community-hulls.png -------------------------------------------------------------------------------- /aj-content/images/sample-size-hulls-ellipses.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/andrewcparnell/simms_course/3052327e05aa93897fcfa31e2e8dd49fa3e45708/aj-content/images/sample-size-hulls-ellipses.png -------------------------------------------------------------------------------- /aj-content/images/semmens-2009.TIF: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/andrewcparnell/simms_course/3052327e05aa93897fcfa31e2e8dd49fa3e45708/aj-content/images/semmens-2009.TIF -------------------------------------------------------------------------------- /aj-content/images/siar-biplot.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/andrewcparnell/simms_course/3052327e05aa93897fcfa31e2e8dd49fa3e45708/aj-content/images/siar-biplot.png -------------------------------------------------------------------------------- /aj-content/images/siar-by-group.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/andrewcparnell/simms_course/3052327e05aa93897fcfa31e2e8dd49fa3e45708/aj-content/images/siar-by-group.png -------------------------------------------------------------------------------- /aj-content/images/siar-histograms.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/andrewcparnell/simms_course/3052327e05aa93897fcfa31e2e8dd49fa3e45708/aj-content/images/siar-histograms.png -------------------------------------------------------------------------------- /aj-content/images/siber-ellipse-example.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/andrewcparnell/simms_course/3052327e05aa93897fcfa31e2e8dd49fa3e45708/aj-content/images/siber-ellipse-example.png -------------------------------------------------------------------------------- /aj-content/images/siber-jae.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/andrewcparnell/simms_course/3052327e05aa93897fcfa31e2e8dd49fa3e45708/aj-content/images/siber-jae.png -------------------------------------------------------------------------------- /aj-content/images/sider-mammals.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/andrewcparnell/simms_course/3052327e05aa93897fcfa31e2e8dd49fa3e45708/aj-content/images/sider-mammals.png -------------------------------------------------------------------------------- /aj-content/images/simm-1.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/andrewcparnell/simms_course/3052327e05aa93897fcfa31e2e8dd49fa3e45708/aj-content/images/simm-1.png -------------------------------------------------------------------------------- /aj-content/images/specialist-generalist.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/andrewcparnell/simms_course/3052327e05aa93897fcfa31e2e8dd49fa3e45708/aj-content/images/specialist-generalist.png -------------------------------------------------------------------------------- /aj-content/images/stacked-trinity.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/andrewcparnell/simms_course/3052327e05aa93897fcfa31e2e8dd49fa3e45708/aj-content/images/stacked-trinity.jpg -------------------------------------------------------------------------------- /aj-content/images/standard-ellipse.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/andrewcparnell/simms_course/3052327e05aa93897fcfa31e2e8dd49fa3e45708/aj-content/images/standard-ellipse.png -------------------------------------------------------------------------------- /aj-content/images/swallows-over-time.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/andrewcparnell/simms_course/3052327e05aa93897fcfa31e2e8dd49fa3e45708/aj-content/images/swallows-over-time.png -------------------------------------------------------------------------------- /aj-content/images/tcd-stacked-square-2.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/andrewcparnell/simms_course/3052327e05aa93897fcfa31e2e8dd49fa3e45708/aj-content/images/tcd-stacked-square-2.png -------------------------------------------------------------------------------- /aj-content/images/tcd-stacked-square.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/andrewcparnell/simms_course/3052327e05aa93897fcfa31e2e8dd49fa3e45708/aj-content/images/tcd-stacked-square.png -------------------------------------------------------------------------------- /aj-content/mystyle.css: -------------------------------------------------------------------------------- 1 | 2 | 3 | .outer { 4 | position: absolute; 5 | bottom: 0; 6 | top: 0; 7 | border: solid; 8 | } 9 | .one { 10 | float: right; 11 | height: 75%; 12 | } 13 | .two { 14 | position: relative; 15 | bottom: 0; 16 | right: 0; 17 | font-size: 0.75em; 18 | width: 60%; 19 | height: 25%; 20 | float: right; 21 | clear: right; 22 | } 23 | 24 | 25 | .ref { 26 | font-size: 0.6em; 27 | 28 | } 29 | 30 | .centerImage 31 | { 32 | text-align:center; 33 | display:block; 34 | } -------------------------------------------------------------------------------- /aj-content/practicals/day-1-pm1/Practical01.csv: -------------------------------------------------------------------------------- 1 | Taxon,ID,d13C,d15N,C:N 2 | POM,AF_POM01,-20.477,13.554,9.5 3 | POM,AF_POM02,-19.879,12.461,8 4 | POM,AF_POM03,-19.437,14.053,9.1 5 | POM,AF_POM04,-20.276,13.129,8.1 6 | POM,AF_POM05,-19.126,12.484,7.3 7 | Benthic.algae,AF_BA01 ,-13.408,12.144,7.7 8 | Benthic.algae,AF_BA02,-13.621,12.156,9.3 9 | Benthic.algae,AF_BA03,-14.159,12,9 10 | Benthic.algae,AF_BA04,-13.9,11.84,9.2 11 | Benthic.algae,AF_BA05,-6.2,12.482,10 12 | Fissurella,AF_LA01 ,-15.658,15.622,3.4 13 | Fissurella,AF_LA02,-17.436,16.25,3.3 14 | Fissurella,AF_LA03,-14.836,16.122,3.4 15 | Fissurella,AF_LA04,-14.482,15.145,3.4 16 | Fissurella,AF_LA05,-15.063,14.597,3.5 17 | Fissurella,AF_LA06,-12,16.009,11.4 18 | Fissurella,AF_LA07,-17.387,16.003,9.8 19 | Fissurella,AF_LA08,-18.314,17.858,3.8 20 | Fissurella,AF_LA09,-20.166,16.046,3.8 21 | Fissurella,AF_LA10,-15.415,15.193,3.8 22 | Perumytilus,AF_BV01 ,-18.862,16.509,3.6 23 | Perumytilus,AF_BV02,-13.2,15.985,12.5 24 | Perumytilus,AF_BV03,-16.338,16.551,3.4 25 | Perumytilus,AF_BV04,-18.347,18.865,3.4 26 | Perumytilus,AF_BV05,-19.283,17.003,3.4 27 | Perumytilus,AF_BV06,-14.2,15.604,10.1 28 | Perumytilus,AF_BV07,-19.227,16.948,3.6 29 | Perumytilus,AF_BV08,-19.949,15.578,3.4 30 | Perumytilus,AF_BV09,-17.064,15.687,3.5 31 | Perumytilus,AF_BV10,-17.505,17.25,3.7 32 | Cabrilla,AF_Ph01 ,-15.625,20.71,3.1 33 | Cabrilla,AF_Ph02,-17.578,20.572,4 34 | Cabrilla,AF_Ph03,-15.1,20.777,3.7 35 | Cabrilla,AF_Ph04,-14.5,20.616,3.3 36 | Cabrilla,AF_Ph05,-17.597,21.959,4.1 37 | Cabrilla,AF_Ph06,-22.4,20.125,5.8 38 | Cabrilla,AF_Ph07,-16.788,22.881,3.6 39 | Cabrilla,AF_Ph08,-18.266,19.052,4.4 40 | Cabrilla,AF_Ph09,-17.865,20.724,4.3 41 | Cabrilla,AF_Ph10,-19.1,22.636,4.6 42 | Cabrilla,AF_Ph11,-16.775,20.591,3.5 43 | Cabrilla,AF_Ph12,-19.982,21.99,5.3 44 | Cabrilla,AF_Ph13,-17.279,20.478,4 45 | Cabrilla,AF_Ph14,-17.166,20.062,3.9 46 | Cabrilla,AF_Ph15,-19.443,20.252,5.9 47 | Cabrilla,AF_Ph16,-22.3,20.257,5.9 48 | Cabrilla,AF_Ph17,-16.897,17.956,3.7 49 | Cabrilla,AF_Ph18,-18.619,21.067,4.5 50 | Cabrilla,AF_Ph19,-14.963,19.884,3.1 51 | Cabrilla,AF_Ph20,-23.1,23.378,6.8 52 | Cabrilla,AF_Ph21,-16.821,18.69,3.6 53 | Cabrilla,AF_Ph22,-18.897,17.89,5 54 | Cabrilla,AF_Ph23,-16.159,21.033,3.1 55 | Cabrilla,AF_Ph24,-18.716,21.469,4.5 56 | Cabrilla,AF_Ph25,-18.801,21.066,4.6 57 | Cabrilla,AF_Ph26,-17.083,19.383,3.7 58 | Cabrilla,AF_Ph27,-18.146,21.192,4.4 59 | Cabrilla,AF_Ph28,-17.238,20.004,3.9 60 | Cabrilla,AF_Ph29,-18.62,19.689,4.5 61 | Cabrilla,AF_Ph30,-17.998,20.227,4.3 62 | -------------------------------------------------------------------------------- /aj-content/practicals/day-1-pm1/ggplot-playing.Rmd: -------------------------------------------------------------------------------- 1 | --- 2 | title: "GGplot playing" 3 | output: html_notebook 4 | --- 5 | 6 | A file for the SIMMS07 course April 2021 in which we played around with various plotting options. 7 | 8 | ```{r import-data} 9 | library(tidyverse) 10 | 11 | # import the data. 12 | # If you get an error about "No such file or directory" then you 13 | # need to take care about where R is currently working, and where your 14 | # data file is in relation to this. 15 | mydata <- read.csv("Practical01.csv", 16 | header = TRUE, 17 | stringsAsFactors = FALSE) 18 | 19 | # As per the summary statistics file, I am going to remove our outlier 20 | # note the use of dplyr::filter() to avoid loading the entire 21 | # dplyr pacakge for just one function 22 | mydata <- dplyr::filter(mydata, d13C < max(d13C)) 23 | 24 | 25 | ``` 26 | 27 | 28 | 29 | A basic plot 30 | 31 | ```{r} 32 | 33 | # set up a basic plot with equal coordinate axes, and scatter plot with colour 34 | # and shape by Taxon 35 | 36 | first.plot <- ggplot(data = mydata, 37 | aes(x = d13C, 38 | y = d15N)) + 39 | geom_point(aes(color = Taxon, shape = Taxon), size = 4) + 40 | ylab(expression(paste(delta^{15}, "N (\u2030)"))) + 41 | xlab(expression(paste(delta^{13}, "C (\u2030)"))) + 42 | theme(text = element_text(size=20)) + 43 | coord_equal() 44 | 45 | # And print our plot to screen 46 | print(first.plot) 47 | ``` 48 | 49 | 50 | Add viridis colour palette 51 | 52 | ```{r} 53 | # augment the basic plot by using the viridis colour palette and theme_classic 54 | viridis_plot <- first.plot + 55 | scale_fill_viridis_d(labels = c("Benthic algae", 56 | expression(italic("Cabrilla")), 57 | expression(italic("Fissurella")), 58 | expression(italic("Perumytilus")), 59 | "POM")) + 60 | theme_classic() + 61 | theme(axis.ticks.length = unit(-0.25, "cm"), 62 | axis.text.x = element_text(margin=unit(c(0.5,0.5,0.5,0.5), "cm")), 63 | axis.text.y = element_text(margin=unit(c(0.5,0.5,0.5,0.5), "cm")) ) 64 | 65 | 66 | print(viridis_plot) 67 | ``` 68 | 69 | and add custom shapes: see the vignette `ggplot2-specs` for help picking which number corresponds to which shape 70 | 71 | ```{r} 72 | 73 | my_shapes <- c(19, 17, 15, 18, 8) 74 | 75 | shape_plot <- viridis_plot + scale_shape_manual(values = my_shapes) 76 | 77 | print(shape_plot) 78 | 79 | 80 | 81 | ``` 82 | 83 | 84 | 85 | -------------------------------------------------------------------------------- /aj-content/practicals/first-Rnotebook.Rmd: -------------------------------------------------------------------------------- 1 | --- 2 | title: "R Notebook" 3 | output: html_notebook 4 | --- 5 | 6 | This is an [R Markdown](http://rmarkdown.rstudio.com) Notebook. When you execute code within the notebook, the results appear beneath the code. 7 | 8 | Try executing this chunk by clicking the *Run* button within the chunk or by placing your cursor inside it and pressing *Cmd+Shift+Enter*. 9 | 10 | ```{r} 11 | plot(cars) 12 | ``` 13 | 14 | Add a new chunk by clicking the *Insert Chunk* button on the toolbar or by pressing *Cmd+Option+I*. 15 | 16 | ```{r} 17 | summary(cars) 18 | ``` 19 | 20 | 21 | When you save the notebook, an HTML file containing the code and output will be saved alongside it (click the *Preview* button or press *Cmd+Shift+K* to preview the HTML file). 22 | 23 | The preview shows you a rendered HTML copy of the contents of the editor. Consequently, unlike *Knit*, *Preview* does not run any R code chunks. Instead, the output of the chunk when it was last run in the editor is displayed. 24 | 25 | 26 | # Header 1 27 | 28 | asdf 29 | 30 | ## Header 2 31 | 32 | asdf 33 | 34 | ### Header 3 35 | 36 | adsf 37 | 38 | you can have *italics* or **bold** or even mix the two *__both__*. 39 | 40 | 41 | 42 | 43 | a 44 | sdfsfdfes 45 | 46 | can also display maths quite nicely 47 | 48 | $$y = ax+b$$ 49 | -------------------------------------------------------------------------------- /aj-content/practicals/test_n_run-tree1_chain1.rda: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/andrewcparnell/simms_course/3052327e05aa93897fcfa31e2e8dd49fa3e45708/aj-content/practicals/test_n_run-tree1_chain1.rda -------------------------------------------------------------------------------- /aj-content/practicals/test_n_run-tree1_chain2.rda: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/andrewcparnell/simms_course/3052327e05aa93897fcfa31e2e8dd49fa3e45708/aj-content/practicals/test_n_run-tree1_chain2.rda -------------------------------------------------------------------------------- /aj-content/practicals/test_n_run-tree1_conv.rda: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/andrewcparnell/simms_course/3052327e05aa93897fcfa31e2e8dd49fa3e45708/aj-content/practicals/test_n_run-tree1_conv.rda -------------------------------------------------------------------------------- /aj-content/practicals/test_n_run-tree2_chain1.rda: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/andrewcparnell/simms_course/3052327e05aa93897fcfa31e2e8dd49fa3e45708/aj-content/practicals/test_n_run-tree2_chain1.rda -------------------------------------------------------------------------------- /aj-content/practicals/test_n_run-tree2_chain2.rda: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/andrewcparnell/simms_course/3052327e05aa93897fcfa31e2e8dd49fa3e45708/aj-content/practicals/test_n_run-tree2_chain2.rda -------------------------------------------------------------------------------- /aj-content/practicals/test_n_run-tree2_conv.rda: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/andrewcparnell/simms_course/3052327e05aa93897fcfa31e2e8dd49fa3e45708/aj-content/practicals/test_n_run-tree2_conv.rda -------------------------------------------------------------------------------- /aj-content/siber-intro-ellipses.Rmd: -------------------------------------------------------------------------------- 1 | --- 2 | title: "SIBER introduction - Ellipses" 3 | author: "Andrew L Jackson" 4 | date: "`r format(Sys.time(), '%d %B, %Y')`" 5 | output: 6 | ioslides_presentation: 7 | css: mystyle.css 8 | logo: images/tcd-stacked-square-2.png 9 | transition: slower 10 | --- 11 | 12 | ```{r setup, include=F} 13 | library(tidyverse) 14 | library(magrittr) 15 | 16 | ``` 17 | 18 | 19 | ## Inference from d-space 20 | 21 | > - Isotopes are like naturally occurring PCA analyses 22 | > - They are two axes, behind which lie multiple biotic and physical factors 23 | > - d15N tells us a lot about trophic level for example 24 | > - d13C can tell us about marine Vs terrestrial among other scenopoetic factors 25 | > - Idea is that isotopic niche correlates in some way with ecological niche 26 | 27 | ## Specialists Vs generalists 28 | 29 | specialists vs generalists in isotope d-space 30 | 31 |
Bearhop, Adam, Waldron, Fuller & MacLeod. 2004. Determining trophic niche width: a novel approach using stable isotope analysis. J Anim Ecol.
32 | 33 | ## Drivers of niche width 34 | 35 | niche width shrinks with fragmentation 36 | 37 |
Layman et al. 2007. Niche width collapse in a resilient top predator following ecosystem fragmentation.
38 | 39 | ## Invasive species 40 | 41 | niche width of native species affected by invasive species 42 | 43 |
Jackson, M.C. et al. 2012. Population-Level Metrics of Trophic Structure Based on Stable Isotopes and Their Application to Invasion Ecology. PLoSONE.
44 | 45 | 46 | ## The problem with convex hulls 47 | They can only get bigger. 48 | 49 | ```{r, echo=FALSE, message=FALSE, fig.width = 4, fig.height = 4} 50 | 51 | # library(siar) 52 | 53 | set.seed(2) 54 | 55 | X <- matrix(stats::rnorm(20), ncol = 2) 56 | 57 | plot(X, cex = 1, asp = 1, xlim=c(-2.5,2.5), ylim=c(-2.5, 2.5), bty="L", pch = 15) 58 | hpts <- chull(X) 59 | hpts <- c(hpts, hpts[1]) 60 | lines(X[hpts, ]) 61 | 62 | Z <- matrix(stats::rnorm(10), ncol = 2) 63 | points(Z, col="red", pch=16) 64 | 65 | ZX <- rbind(Z,X) 66 | hpts <- chull(ZX) 67 | hpts <- c(hpts, hpts[1]) 68 | lines(ZX[hpts, ], col = "red", lty = 2) 69 | 70 | ``` 71 | 72 | ## SIBER 73 | 74 | headline from SIBER paper 75 | 76 | 77 | ## The standard ellipse 78 | 79 | illustration of a standard bivariate ellipse 80 | 81 | - a = semi-major axis length 82 | - b = semi-minor axis length 83 | - `Area = pi*a*b` 84 | - contains ~= 40% of the data 85 | - located by the means of X and Y 86 | - shape and size determined by covariance matrix Sigma 87 | 88 | ## Uncertainty of the ellipse 89 | 90 | comparison of ellipses and hulls 91 | 92 | ## Sample size, ellipses and hulls 93 | 94 | effect of sample size on fitted hulls and ellipses 95 | 96 | a = Hulls & b = Ellipses 97 | 98 | ## Sample size and bias in ellipse estimation 99 | 100 | effect of sample size on fitted hulls and ellipses 101 | 102 | a = SEA, b = SEAc and c = SEA_B 103 | 104 | 105 | ## SIBER has two quite different routines 106 | 107 | - Question is: do you want to compare populations within a community, or make comparisons across entire communities? 108 | 109 | ```{r, echo=FALSE, message = FALSE, fig.width = 7, fig.height = 3.5} 110 | 111 | library(SIBER) 112 | 113 | data("demo.siber.data") 114 | mydata <- demo.siber.data 115 | 116 | 117 | # create the siber objects 118 | siber.example_1 <- createSiberObject(mydata %>% filter(community == 1)) 119 | siber.example_2 <- createSiberObject(mydata %>% filter(community == 2)) 120 | 121 | 122 | par(mfrow = c(1,2)) 123 | 124 | plotSiberObject(siber.example_1, 125 | ax.pad = 2, 126 | hulls = F, community.hulls.args, 127 | ellipses = F, group.ellipses.args, 128 | group.hulls = F, group.hull.args, 129 | bty = "L", 130 | iso.order = c(1,2), 131 | xlab = expression({delta}^13*C~'\u2030'), 132 | ylab = expression({delta}^15*N~'\u2030'), 133 | x.limits = c(-20, 0), 134 | y.limits = c(-5, 10) 135 | ) 136 | 137 | plotSiberObject(siber.example_2, 138 | ax.pad = 2, 139 | hulls = F, community.hulls.args, 140 | ellipses = F, group.ellipses.args, 141 | group.hulls = F, group.hull.args, 142 | bty = "L", 143 | iso.order = c(1,2), 144 | xlab = expression({delta}^13*C~'\u2030'), 145 | ylab = expression({delta}^15*N~'\u2030'), 146 | x.limits = c(-20, 0), 147 | y.limits = c(-5, 10) 148 | ) 149 | 150 | ``` 151 | 152 | ## Comparisons of populations 153 | 154 | four ellipses fit to data 155 | 156 | ## SIBER output 157 | 158 | estimates of ellipse size for community 1 159 | 160 | ## Other ways one might compare populations 161 | 162 | four ellipses fit to data 163 | 164 | 165 | 166 | 167 | 168 | 169 | 170 | 171 | 172 | 173 | 174 | 175 | -------------------------------------------------------------------------------- /aj-content/sider-intro.Rmd: -------------------------------------------------------------------------------- 1 | --- 2 | title: "TDF estimation - SIDER" 3 | author: "Andrew L Jackson" 4 | date: "`r format(Sys.time(), '%d %B, %Y')`" 5 | output: 6 | ioslides_presentation: 7 | css: mystyle.css 8 | logo: images/tcd-stacked-square-2.png 9 | transition: slower 10 | --- 11 | 12 | ## Trophic Discrimination Factors 13 | > - You are what you eat plus a few permil 14 | > - The "few permil" are important in SIMMs 15 | > - lots of names: 16 | > - Fractionation factors (denoted $\Delta$) 17 | > - (Trophic) Discrimination factors 18 | > - Trophic Enrichment factors 19 | > - Offsets 20 | > - not always positive, e.g. aphids feeding on phloem 21 | 22 | ## Knowing the TDF matters 23 | 24 | ```{r tef-effects, echo=FALSE, message = FALSE, fig.width = 7, fig.height = 5} 25 | library(viridisLite) 26 | palette(viridis(3)) 27 | 28 | x <- c(-10, -5) 29 | 30 | tdf <- c(0, 1, 2) 31 | 32 | plot(rep(x, 3), sort(rep(tdf,2)), pch = 15, col = rep(c(1,3), 3), cex = 4, 33 | ylim = c(-1, 3), axes = FALSE, xlab = "", ylab = "", 34 | main = "Three diets of 50:50") 35 | axis(1, at = -10:-5) 36 | mtext("d13C", 1, line = 3, cex = 2) 37 | axis(2, at = tdf, las = 1) 38 | mtext("TDF", 2, line = 2, cex = 2) 39 | points(mean(x) + 0, 0, pch = 16, col = 2, cex = 4) 40 | lines(x, c(0,0), lty = 2, col = "grey") 41 | points(mean(x) + 1, 1, pch = 16, col = 2, cex = 4) 42 | lines(x, c(1,1), lty = 2, col = "grey") 43 | points(mean(x) + 2, 2, pch = 16, col = 2, cex = 4) 44 | lines(x, c(2,2), lty = 2, col = "grey") 45 | ``` 46 | 47 | ## They affect the estimates of diet 48 | > - They alter the geometry of sources and mixtures 49 | > - They move mixtures closer or further away from their sources 50 | > - They add uncertainty to the system 51 | 52 | ## How do we get them for our species? 53 | > - Controlled feeding experiments 54 | > - let your species's tissue reach equilibrium with a food 55 | > - change the food to a different isotope $\delta$ 56 | > - wait until tissue at equilibrium again 57 | > - return to original diet 58 | > - gives you tissue turn-over rates and TDF 59 | - Often impractical 60 | - In many cases impossible 61 | 62 | ## Current Alternatives 63 | > - find a value for a related species or functionally similar species 64 | > - use a generic value for your taxonomic group e.g. 65 | > - +3.5 for $\delta^{15}\text{N}$ 66 | > - +1.3 for $\delta^{13}\text{C}$ 67 | 68 | ## SIDER 69 | > - Stable Isotope Discrimination Estimation in R 70 | > - A Bayesian regression model for estimation (prediction) of TDFs for $\delta^{15}\text{N}$ and $\delta^{13}\text{C}$ 71 | > - Uses phylogenetic regression to draw inference from related species whose TDFs are known. 72 | > - Accounts for multiple observations on the same species via a random term 73 | > - Also uses ecological information on diet type to improve prediction 74 | > - Fitted model based on existing data which can be expanded in the future to include more: species, tissues and isotopes. 75 | > - Returns a posterior distribution for the estimate which can be passed to SIMMs as mean +/- standard deviation. 76 | 77 | ## Mammals in SIDER 78 | You can add in your own species as long as it is in the larger mammal tree (Kuhn et al 2011) or the bird tree (Jetz et al 2012). You can add species for which there already exists TDF data or for which the TDF data is missing. 79 | phylogram of the mammals in the SIDER dataset 80 | 81 | ## Explantory variables 82 | The following explanatory variables are required to estimate the TDF for your species 83 | > - Habitat: 84 | > - terrestrial 85 | > - marine 86 | > - Tissue 87 | > - liver / blood / kidney / muscle / *hair* / **milk** / **feather** / **claws** / collagen 88 | > - Diet type 89 | > - herbivore, carnivore, omnivore, *pellet* 90 | 91 | ## Imputation of the missing TDFs 92 | > - The value for either $\delta^{15}\text{N}$ and $\delta^{13}\text{C}$ are then entered as `NA` in the corresponding column 93 | > - during the MCMC process, these `NA`s are then imputed naturally during the model fitting 94 | > - information is drawn from the other data via the linear regression model fitted using the `MCMCglmm` package. 95 | > - Fixed part: `delta13C ~ diet.type + habitat` 96 | > - Random part: `~ animal + sp.col + tissue` 97 | 98 | ## Model Output 99 | > - A full posterior distribution of the TDF(s) for which you provided expantory variable information but no TDF. 100 | > - You can summarise this as a mean and standard deviation for further inclusion in a SIMM 101 | > - How do we know this model works? 102 | 103 | 104 | 105 | 106 | 107 | 108 | 109 | 110 | 111 | 112 | 113 | -------------------------------------------------------------------------------- /aj-content/test-limits-of-siber-data-import.Rmd: -------------------------------------------------------------------------------- 1 | --- 2 | title: "Test limits of SIBER data formats" 3 | output: html_notebook 4 | --- 5 | 6 | ```{r try-import} 7 | 8 | library(SIBER) 9 | 10 | sbr_data <- read.csv("~/Downloads/test-break-siber.csv", header = T, stringsAsFactors = FALSE) 11 | 12 | str(sbr_data) 13 | 14 | head(sbr_data) 15 | 16 | sbr_obj <- createSiberObject(sbr_data) 17 | 18 | ``` 19 | 20 | Plot the raw isotope data 21 | 22 | ```{r} 23 | 24 | # of the three plotting functions. 25 | community.hulls.args <- list(col = 1, lty = 1, lwd = 1) 26 | group.ellipses.args <- list(n = 100, p.interval = 0.5, lty = 1, lwd = 2) 27 | group.hull.args <- list(lty = 2, col = "grey20") 28 | 29 | # plot the raw data 30 | par(mfrow=c(1,1)) 31 | plotSiberObject(sbr_obj, 32 | ax.pad = 2, 33 | hulls = F, community.hulls.args, 34 | ellipses = T, group.ellipses.args, 35 | group.hulls = F, group.hull.args, 36 | bty = "L", 37 | iso.order = c(1,2), 38 | xlab = expression({delta}^13*C~'\u2030'), 39 | ylab = expression({delta}^15*N~'\u2030') 40 | ) 41 | 42 | ``` 43 | -------------------------------------------------------------------------------- /aj-content/utility-scripts/plot.siber.data.r: -------------------------------------------------------------------------------- 1 | plot.siber.data <- function (fname, tt, add.hull = T, ...) { 2 | 3 | require(siar) 4 | 5 | #full.path <- paste(getwd(), fname , sep = "") 6 | 7 | mydata <- read.csv(fname, header=T) 8 | 9 | M <- max(mydata$group) 10 | 11 | with(mydata, { 12 | 13 | # ------------------------------------------------------------------------------ 14 | # Plot out some of the data and results 15 | # ------------------------------------------------------------------------------ 16 | 17 | # Plot the raw [simulated in this case] data 18 | 19 | # Now lets calculate the convex hull as per the current method based 20 | # simply on the means for each group 21 | means.x <- aggregate(x,list(group),mean)$x 22 | means.y <- aggregate(y,list(group),mean)$x 23 | sample.hull <- convexhull(means.x,means.y) 24 | 25 | 26 | 27 | #dev.new() 28 | #par(mfrow=c(1,1)) 29 | plot(x,y,col=group, 30 | xlab=expression({delta}^13*C~'\u2030'), 31 | ylab=expression({delta}^15*N~'\u2030'), 32 | pch=1, 33 | main = tt, 34 | asp = 1, 35 | bty = "L", 36 | ...) 37 | 38 | if (add.hull) 39 | {lines(sample.hull$xcoords,sample.hull$ycoords,lty=1,col=1,lwd=2)} 40 | 41 | #legend("topleft", 42 | # legend=as.character(c(paste("group ",1:M),"sample hull")), 43 | # pch=c(rep(1,M),NA),col=c(1:M,1,1), lty=c(rep(NA,M),1), 44 | # bty="n") 45 | 46 | 47 | }) # end of with(mydata, {...}) 48 | 49 | return(M) # needed by calling code chunk 50 | 51 | } # end of function 52 | -------------------------------------------------------------------------------- /aj-content/vignettes/Customising-Plots-Manually.R: -------------------------------------------------------------------------------- 1 | ## ---- echo = FALSE------------------------------------------------------- 2 | knitr::opts_chunk$set(collapse = TRUE, comment = "#>", 3 | fig.width = 6, fig.height = 5) 4 | 5 | 6 | ## ---- echo = TRUE-------------------------------------------------------- 7 | # remove previously loaded items from the current environment and remove previous graphics. 8 | rm(list=ls()) 9 | graphics.off() 10 | 11 | # Here, I set the seed each time so that the results are comparable. 12 | # This is useful as it means that anyone that runs your code, *should* 13 | # get the same results as you, although random number generators change 14 | # from time to time. 15 | set.seed(1) 16 | 17 | library(SIBER) 18 | 19 | # Load the viridis package and create a new palette with 3 colours, one for 20 | # each of the 3 groups we have in this dataset. 21 | library(viridis) 22 | palette(viridis(3)) 23 | 24 | # load in the included demonstration dataset 25 | data("demo.siber.data") 26 | 27 | 28 | # 29 | # create the siber object 30 | siber.example <- createSiberObject(demo.siber.data) 31 | 32 | 33 | 34 | ## ---- echo=TRUE---------------------------------------------------------- 35 | plotSiberObject(siber.example, 36 | ax.pad = 2, 37 | hulls = FALSE, 38 | ellipses = FALSE, 39 | group.hulls = FALSE, 40 | bty = "L", 41 | iso.order = c(1,2), 42 | xlab = expression({delta}^13*C~'\u2030'), 43 | ylab = expression({delta}^15*N~'\u2030') 44 | ) 45 | # Call addEllipse directly on each group to customise the plot fully 46 | 47 | # change c.id and g.id to select the group of data you want 48 | # you could embed this in a loop easily enough if you wanted to 49 | # set up the order of lines and simply loop through them. 50 | c.id <- 1 # specify the community ID 51 | g.id <- 1 # specify the group ID within the community 52 | 53 | # see help file for addEllipse for more information 54 | # NB i am using the group identifier g.id to select the colour 55 | # of the ellipse line so that it matches the one created by 56 | # plotSiberObject(), but you could override this if you wish. 57 | # The function addEllipse returns the coordinates it used for plotting, 58 | # but more than likely you dont need this information. Here I store these in 59 | # a new variable coords for clarity, but you could just as easily call this tmp. 60 | # See help file for addEllipse for more details on the options, but in short: 61 | # the first two entries look up the means and covariance matrix of the data you 62 | # specified using the group and commmunity indices above. 63 | # m = NULL is used as we are not plotting an ellipse around the mean. 64 | # n = 100 just determines how many points are used to draw a smooth ellipse. 65 | # p.interval = 0.95 for a 95% ellipse of the data 66 | # ci.mean = FALSE as we are not plotting an ellipse around the mean. 67 | # col = your choice of colour. 68 | # lty = your choice of line type. 69 | # lwd = your choice of line width. 70 | coords <- addEllipse(siber.example$ML.mu[[c.id]][ , , g.id], 71 | siber.example$ML.cov[[c.id]][ , , g.id], 72 | m = NULL, 73 | n = 100, 74 | p.interval = 0.95, 75 | ci.mean = FALSE, 76 | col = g.id, 77 | lty = 3, 78 | lwd = 2) 79 | 80 | -------------------------------------------------------------------------------- /aj-content/vignettes/Customising-Plots-Manually.Rmd: -------------------------------------------------------------------------------- 1 | --- 2 | title: "Customising Plots Manually" 3 | author: "Andrew L Jackson" 4 | date: "`r Sys.Date()`" 5 | output: rmarkdown::html_vignette 6 | vignette: > 7 | %\VignetteIndexEntry{Customising Plots Manually} 8 | %\VignetteEngine{knitr::rmarkdown} 9 | \usepackage[utf8]{inputenc} 10 | %\VignetteDepends{viridis} 11 | --- 12 | 13 | ```{r, echo = FALSE} 14 | knitr::opts_chunk$set(collapse = TRUE, comment = "#>", 15 | fig.width = 6, fig.height = 5) 16 | 17 | ``` 18 | 19 | ## Creating a Blank Plot 20 | In the vignette [Introduction to Siber](Introduction-to-SIBER.html) we used the bundled functions `plotSiberObject()` to create plots with ellipses and hulls and saw how we can use `plotGroupEllipses()` to add some customised ellipses. Here we will look in more detail at how we can add customised elements to a blank plot by calling the underlying functions directly. Again we will use the bundled example dataset. 21 | 22 | ```{r, echo = TRUE} 23 | # remove previously loaded items from the current environment and remove previous graphics. 24 | rm(list=ls()) 25 | graphics.off() 26 | 27 | # Here, I set the seed each time so that the results are comparable. 28 | # This is useful as it means that anyone that runs your code, *should* 29 | # get the same results as you, although random number generators change 30 | # from time to time. 31 | set.seed(1) 32 | 33 | library(SIBER) 34 | 35 | # Load the viridis package and create a new palette with 3 colours, one for 36 | # each of the 3 groups we have in this dataset. 37 | library(viridis) 38 | palette(viridis(3)) 39 | 40 | # load in the included demonstration dataset 41 | data("demo.siber.data") 42 | 43 | 44 | # 45 | # create the siber object 46 | siber.example <- createSiberObject(demo.siber.data) 47 | 48 | 49 | ``` 50 | 51 | You dont even have to use `plotSiberObject()` to plot your raw data. You could plot it all yourself directly from your raw data using any points, colours and axes styles that you want. 52 | 53 | Next we want to add a single ellipse, fully customised to one of our clusters of data; i.e. one of the groups within a community. Here I define the group and community using two created variables so as you could adapt this run for any group/community combination you wanted, or perhaps more suitably, write a loop to traverse all your data. 54 | 55 | ```{r, echo=TRUE} 56 | plotSiberObject(siber.example, 57 | ax.pad = 2, 58 | hulls = FALSE, 59 | ellipses = FALSE, 60 | group.hulls = FALSE, 61 | bty = "L", 62 | iso.order = c(1,2), 63 | xlab = expression({delta}^13*C~'\u2030'), 64 | ylab = expression({delta}^15*N~'\u2030') 65 | ) 66 | # Call addEllipse directly on each group to customise the plot fully 67 | 68 | # change c.id and g.id to select the group of data you want 69 | # you could embed this in a loop easily enough if you wanted to 70 | # set up the order of lines and simply loop through them. 71 | c.id <- 1 # specify the community ID 72 | g.id <- 1 # specify the group ID within the community 73 | 74 | # see help file for addEllipse for more information 75 | # NB i am using the group identifier g.id to select the colour 76 | # of the ellipse line so that it matches the one created by 77 | # plotSiberObject(), but you could override this if you wish. 78 | # The function addEllipse returns the coordinates it used for plotting, 79 | # but more than likely you dont need this information. Here I store these in 80 | # a new variable coords for clarity, but you could just as easily call this tmp. 81 | # See help file for addEllipse for more details on the options, but in short: 82 | # the first two entries look up the means and covariance matrix of the data you 83 | # specified using the group and commmunity indices above. 84 | # m = NULL is used as we are not plotting an ellipse around the mean. 85 | # n = 100 just determines how many points are used to draw a smooth ellipse. 86 | # p.interval = 0.95 for a 95% ellipse of the data 87 | # ci.mean = FALSE as we are not plotting an ellipse around the mean. 88 | # col = your choice of colour. 89 | # lty = your choice of line type. 90 | # lwd = your choice of line width. 91 | coords <- addEllipse(siber.example$ML.mu[[c.id]][ , , g.id], 92 | siber.example$ML.cov[[c.id]][ , , g.id], 93 | m = NULL, 94 | n = 100, 95 | p.interval = 0.95, 96 | ci.mean = FALSE, 97 | col = g.id, 98 | lty = 3, 99 | lwd = 2) 100 | ``` 101 | 102 | As this is just a simple base R graphics window, you can add lines, points and text as you like. You can also call the functions `plotGroupHulls(siber.example)` and `plotCommunityHulls(siber.example)` directly and customise their inputs as per the vignette [Introduction to Siber](Introduction-to-SIBER.html). 103 | 104 | -------------------------------------------------------------------------------- /aj-content/vignettes/Ellipse-Overlap.R: -------------------------------------------------------------------------------- 1 | ## ---- echo = FALSE------------------------------------------------------- 2 | knitr::opts_chunk$set(collapse = TRUE, comment = "#>", 3 | fig.width = 6, fig.height = 5) 4 | library(viridis) 5 | palette(viridis(3)) 6 | 7 | library(SIBER) 8 | 9 | if (!require(spatstat, quietly = TRUE)) "This vignette requires the package spatstat. 10 | Please install manually using install.packages('spatstat') as this package is 11 | not installed along with SIBER." 12 | 13 | 14 | ## ------------------------------------------------------------------------ 15 | # remove previously loaded items from the current environment and remove previous graphics. 16 | rm(list=ls()) 17 | graphics.off() 18 | 19 | # Here, I set the seed each time so that the results are comparable. 20 | # This is useful as it means that anyone that runs your code, *should* 21 | # get the same results as you, although random number generators change 22 | # from time to time. 23 | set.seed(1) 24 | 25 | library(SIBER) 26 | 27 | # load in the included demonstration dataset 28 | data("demo.siber.data") 29 | # 30 | # create the siber object 31 | siber.example <- createSiberObject(demo.siber.data) 32 | 33 | 34 | # Or if working with your own data read in from a *.csv file, you would use 35 | # This *.csv file is included with this package. To find its location 36 | # type 37 | # fname <- system.file("extdata", "demo.siber.data.csv", package = "SIBER") 38 | # in your command window. You could load it directly by using the 39 | # returned path, or perhaps better, you could navigate to this folder 40 | # and copy this file to a folder of your own choice, and create a 41 | # script from this vingette to analyse it. This *.csv file provides 42 | # a template for how your own files should be formatted. 43 | 44 | # mydata <- read.csv(fname, header=T) 45 | # siber.example <- createSiberObject(mydata) 46 | 47 | 48 | # Create lists of plotting arguments to be passed onwards to the 49 | # plotting functions. With p.interval = NULL, these are SEA. NB not SEAc though 50 | # which is what we will base our overlap calculations on. This implementation 51 | # needs to be added in a future update. For now, the best way to plot SEAc is to 52 | # add the ellipses manually following the vignette on this topic. 53 | group.ellipses.args <- list(n = 100, p.interval = NULL, lty = 1, lwd = 2) 54 | 55 | 56 | 57 | par(mfrow=c(1,1)) 58 | plotSiberObject(siber.example, 59 | ax.pad = 2, 60 | hulls = F, community.hulls.args, 61 | ellipses = T, group.ellipses.args, 62 | group.hulls = F, group.hull.args, 63 | bty = "L", 64 | iso.order = c(1,2), 65 | xlab = expression({delta}^13*C~'\u2030'), 66 | ylab = expression({delta}^15*N~'\u2030') 67 | ) 68 | 69 | 70 | ## ---- MLoverlap---------------------------------------------------------- 71 | # In this example, I will calculate the overlap between ellipses for groups 2 72 | # and 3 in community 1 (i.e. the green and yellow open circles of data). 73 | 74 | # The first ellipse is referenced using a character string representation where 75 | # in "x.y", "x" is the community, and "y" is the group within that community. 76 | # So in this example: community 1, group 2 77 | ellipse1 <- "1.2" 78 | 79 | # Ellipse two is similarly defined: community 1, group3 80 | ellipse2 <- "1.3" 81 | 82 | # The overlap of the maximum likelihood fitted standard ellipses are 83 | # estimated using 84 | sea.overlap <- maxLikOverlap(ellipse1, ellipse2, siber.example, 85 | p.interval = NULL, n = 100) 86 | 87 | # the overlap betweeen the corresponding 95% prediction ellipses is given by: 88 | ellipse95.overlap <- maxLikOverlap(ellipse1, ellipse2, siber.example, 89 | p.interval = 0.95, n = 100) 90 | 91 | # so in this case, the overlap as a proportion of the two ellipses, would be 92 | prop.95.over <- ellipse95.overlap[3] / (ellipse95.overlap[2] + ellipse95.overlap[1]) 93 | 94 | 95 | ## ---- bayesOverlap------------------------------------------------------- 96 | # options for running jags 97 | parms <- list() 98 | parms$n.iter <- 2 * 10^4 # number of iterations to run the model for 99 | parms$n.burnin <- 1 * 10^3 # discard the first set of values 100 | parms$n.thin <- 10 # thin the posterior by this many 101 | parms$n.chains <- 2 # run this many chains 102 | 103 | # define the priors 104 | priors <- list() 105 | priors$R <- 1 * diag(2) 106 | priors$k <- 2 107 | priors$tau.mu <- 1.0E-3 108 | 109 | # fit the ellipses which uses an Inverse Wishart prior 110 | # on the covariance matrix Sigma, and a vague normal prior on the 111 | # means. Fitting is via the JAGS method. 112 | ellipses.posterior <- siberMVN(siber.example, parms, priors) 113 | 114 | # and teh corresponding Bayesian estimates for the overlap between the 115 | # 95% ellipses is given by: 116 | bayes95.overlap <- bayesianOverlap(ellipse1, ellipse2, ellipses.posterior, 117 | draws = 100, p.interval = 0.95, n = 100) 118 | 119 | # a histogram of the overlap 120 | hist(bayes95.overlap[,3], 10) 121 | 122 | # and as above, you can express this a proportion of one or other or both of 123 | # the two ellipses. 124 | bayes.prop.95.over <- (bayes95.overlap[,3] / 125 | (bayes95.overlap[,2] + bayes95.overlap[,1]) 126 | ) 127 | 128 | hist(bayes.prop.95.over, 10) 129 | 130 | 131 | -------------------------------------------------------------------------------- /aj-content/vignettes/Points-Inside-Outside-Ellipse.R: -------------------------------------------------------------------------------- 1 | ## ---- echo = FALSE------------------------------------------------------- 2 | knitr::opts_chunk$set(collapse = TRUE, comment = "#>", 3 | fig.width = 6, fig.height = 5) 4 | 5 | 6 | ## ----gendata, echo = TRUE------------------------------------------------ 7 | 8 | # set the random seed generator so we get consistent results each time 9 | # we run this code. 10 | set.seed(2) 11 | 12 | # n random numbers 13 | n <- 30 14 | 15 | # some random multivariate data 16 | Y <- generateSiberGroup(n.obs = 30) 17 | 18 | 19 | ## ----warpdata, echo = TRUE----------------------------------------------- 20 | 21 | # plot this example data with column 2 by column 1 22 | plot(Y[,2] ~ Y[,1], type = "p", asp = 1, 23 | xlim = c(-4, 4), 24 | ylim = c(-4, 4)) 25 | 26 | # add an ellipse, in this case a 95% ellipse 27 | mu <- colMeans(Y) # centre of the ellipse 28 | Sigma <- cov(Y) # covariance matrix of the ellipse 29 | 30 | # percentile of the ellipse 31 | p <- 0.95 32 | 33 | # draw the ellipse 34 | tmp <- addEllipse(mu, Sigma, p.interval = p, col = "red", lty = 2) 35 | 36 | # Define a matrix of 5 data points to test against our ellipse. 37 | # For ease of interpretation of this code, I have built this matrix by 38 | # specifying each row on a separate line and do this by adding the option 39 | # byrow = FALSE (by default R fills down the rows first of a matrix). 40 | test.these <- matrix(c(-2, 2, 41 | 0, 0, 42 | -5, 2, 43 | 1, -2, 44 | 4, 0), 45 | byrow = TRUE, 46 | ncol = 2, nrow = 5) 47 | 48 | # transform these points onto ellipsoid coordinates 49 | Z <- pointsToEllipsoid(test.these, Sigma, mu) 50 | 51 | # determine whther or not these points are inside or outside the ellipse drawn 52 | # with same p as above (percentile). 53 | inside <- ellipseInOut(Z, p = p) 54 | 55 | # inside points are marked TRUE which corresponds to 1 in numeric terms, and 56 | # outside marked FALSE which corresponds to 0. So, below i set up my custom 57 | # colour order of ("red", "black") and then look up [inside + 1] which will 58 | # be [0 + 1 = 1 = "red" for inside is FALSE] and 59 | # [1 + 1 = 2 = "black" for inside is TRUE]. 60 | 61 | # and plot them with colour coding for whether they are inside or outside 62 | points(test.these[,2] ~ test.these[,1], 63 | col = c("red","black")[inside + 1], 64 | pch = "*", 65 | cex = 2) 66 | 67 | 68 | ## ----highdim, echo = TRUE------------------------------------------------ 69 | # set the random seed generator so we get consistent results each time 70 | # we run this code. 71 | #set.seed(2) 72 | 73 | # n random numbers 74 | n <- 10^4 75 | 76 | # number of dimensions 77 | d <- 3 78 | 79 | # vector of d means between -1 and +1 80 | mu <- stats::runif(d, -1, +1) 81 | 82 | # a (d x d) covariance matrix 83 | # pull a precision matrix from the wishart distribution and invert it to 84 | # get the corresponding covariance matrix. 85 | sigma <- solve(matrix(stats::rWishart(1, d, diag(d)), d, d)) 86 | 87 | # n-dimensional multivariate random numbers for this test 88 | Y <- mnormt::rmnorm(n, mu, sigma) 89 | 90 | # sample mean and covariance matrix 91 | mu <- colMeans(Y) # centre of the ellipse 92 | Sigma <- cov(Y) # covariance matrix of the ellipse 93 | 94 | # percentile of the ellipsoid to test 95 | p <- 0.95 96 | 97 | # here i am just going to test whether my actual data points are inside 98 | # or outside the 95% ellipsoid but you could replace with your own 99 | # data points as above 100 | 101 | # transform these points onto ellipsoid coordinates 102 | Z <- pointsToEllipsoid(Y, Sigma, mu) 103 | 104 | # determine whther or not these points are inside or outside the ellipse drawn 105 | # with same p as above (percentile). 106 | inside <- ellipseInOut(Z, p = p) 107 | 108 | # how many of our points are inside our ellipse? 109 | sum(inside) / length(inside) 110 | 111 | 112 | -------------------------------------------------------------------------------- /aj-content/vignettes/test_n_run-tree1_chain1.rda: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/andrewcparnell/simms_course/3052327e05aa93897fcfa31e2e8dd49fa3e45708/aj-content/vignettes/test_n_run-tree1_chain1.rda -------------------------------------------------------------------------------- /aj-content/vignettes/test_n_run-tree1_chain2.rda: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/andrewcparnell/simms_course/3052327e05aa93897fcfa31e2e8dd49fa3e45708/aj-content/vignettes/test_n_run-tree1_chain2.rda -------------------------------------------------------------------------------- /aj-content/vignettes/test_n_run-tree1_conv.rda: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/andrewcparnell/simms_course/3052327e05aa93897fcfa31e2e8dd49fa3e45708/aj-content/vignettes/test_n_run-tree1_conv.rda -------------------------------------------------------------------------------- /aj-content/vignettes/test_n_run-tree2_chain1.rda: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/andrewcparnell/simms_course/3052327e05aa93897fcfa31e2e8dd49fa3e45708/aj-content/vignettes/test_n_run-tree2_chain1.rda -------------------------------------------------------------------------------- /aj-content/vignettes/test_n_run-tree2_chain2.rda: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/andrewcparnell/simms_course/3052327e05aa93897fcfa31e2e8dd49fa3e45708/aj-content/vignettes/test_n_run-tree2_chain2.rda -------------------------------------------------------------------------------- /aj-content/vignettes/test_n_run-tree2_conv.rda: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/andrewcparnell/simms_course/3052327e05aa93897fcfa31e2e8dd49fa3e45708/aj-content/vignettes/test_n_run-tree2_conv.rda -------------------------------------------------------------------------------- /ap_notes/CHANGES.md: -------------------------------------------------------------------------------- 1 | # Stable Isotope Mixing Models course - changes to AP notes 2 | 3 | Changes to make: 4 | 5 | - Remove the section on GLMs in the reg_and_simms part 6 | - Fix plot on slide 4 of siar_stats to have the axes the correct way round (or swap this for a new lecture simmr_stats) 7 | - Swap fitting and convergence in reg_and_simms 8 | -------------------------------------------------------------------------------- /ap_notes/Thomas_Bayes.pdf: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/andrewcparnell/simms_course/3052327e05aa93897fcfa31e2e8dd49fa3e45708/ap_notes/Thomas_Bayes.pdf -------------------------------------------------------------------------------- /ap_notes/complex_simms/Geese2Harmonic.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/andrewcparnell/simms_course/3052327e05aa93897fcfa31e2e8dd49fa3e45708/ap_notes/complex_simms/Geese2Harmonic.png -------------------------------------------------------------------------------- /ap_notes/complex_simms/Geese2IsoSpace.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/andrewcparnell/simms_course/3052327e05aa93897fcfa31e2e8dd49fa3e45708/ap_notes/complex_simms/Geese2IsoSpace.png -------------------------------------------------------------------------------- /ap_notes/complex_simms/GeeseConsumers2.csv: -------------------------------------------------------------------------------- 1 | Bird,d15NPl,d13CPl,julianday 1,10.22,-11.36,299.75 2,10.37,-11.88,299.75 3,10.44,-10.6,299.75 4,10.52,-11.25,299.75 5,10.19,-11.66,299.75 6,10.45,-10.41,299.75 7,9.91,-10.88,299.75 8,11.27,-14.73,299.75 9,9.34,-11.52,299.75 10,11.68,-15.89,12 11,12.29,-14.79,12 12,11.04,-17.64,12 13,11.46,-16.97,12 14,11.73,-17.25,12 15,12.29,-14.77,12 16,11.79,-15.67,13 17,11.49,-15.34,13 18,11.73,-15.53,13 19,11.1,-17.27,13 20,11.36,-15.63,13 21,12.19,-15.94,13 22,11.03,-14.88,13 23,11.21,-15.9,13 25,10.58,-17.11,13 26,11.61,-14.93,13 27,12.16,-16.26,13 28,10.7,-17.5,13 29,11.47,-16.37,13 30,12.07,-15.21,13 31,11.75,-15.43,13 32,11.86,-16.54,13 33,12.33,-15,13 34,12.36,-16.41,13 35,11.13,-15.09,13 36,10.92,-18.06,13 37,12.42,-16.27,13 38,10.95,-15.08,13 39,12.28,-14.39,14 40,11.04,-21.45,47.42 41,10.76,-22.52,47.42 42,10.99,-21.25,47.42 43,10.78,-21.84,47.42 44,11.07,-22.51,47.42 45,10.2,-21.97,48.42 46,11.67,-20.23,48.42 48,7.53,-21.64,48.42 50,10.65,-22.49,48.42 52,10.58,-21.91,48.42 53,11.13,-21.65,48.42 55,7.73,-21.37,48.42 57,10.79,-22.9,48.42 58,10.47,-21.13,48.42 61,10.82,-19.33,48.42 62,10.41,-20.29,48.42 64,11.1,-20.56,48.42 66,10.95,-20.87,48.42 67,10.76,-21.07,48.42 68,10.83,-21.69,48.42 71,10.25,-21.17,48.42 72,10.52,-21.74,48.42 74,9.94,-22.69,48.42 76,9.94,-21.06,48.42 78,10.65,-21.5,48.42 80,10.76,-20.15,48.42 82,11.11,-21.99,48.42 83,10.2,-22.3,48.42 85,11.27,-21.71,48.42 87,10.21,-22.48,48.42 89,10.88,-21.86,48.42 90,11.21,-21.68,48.42 91,11.36,-20.97,48.42 93,10.75,-21.91,48.42 95,12.38,-19.05,48.42 97,11.16,-22.78,48.42 102,11.57,-22.36,48.42 104,10.79,-22.46,48.42 106,11.13,-21.52,48.42 107,10.72,-21.84,48.42 110,10.99,-21.3,48.42 112,10.38,-21.39,48.42 114,10.95,-22.1,48.42 116,10.75,-21.59,48.42 117,10.75,-20.14,48.42 119,11.05,-20.67,48.42 120,10.66,-20.31,48.42 123,10.61,-20.07,48.42 124,10.9,-21.2,48.42 125,11.14,-20.44,48.42 127,10.33,-22.06,48.42 131,10.83,-22.05,48.42 133,10.75,-21.44,48.42 142,9.18,-21.93,48.42 143,9.03,-22.47,48.42 144,9.05,-22.27,48.42 145,8.6,-22.19,48.42 146,8.29,-22.81,48.42 147,10.32,-20.48,49.42 148,10.28,-22.47,49.42 149,6.47,-18.06,49.42 150,11.36,-20.72,49.42 151,10.75,-20.97,49.42 152,11.13,-19.11,49.42 154,11.37,-18.4,49.42 155,10.86,-20.45,49.42 156,10.54,-21.2,49.42 157,10.39,-19.74,49.42 158,10.66,-20.48,49.42 160,9.99,-21.48,49.42 162,11.65,-17.81,49.42 163,11.02,-19.77,49.42 164,10.67,-22.56,49.42 165,8.15,-14.72,321.17 166,11.12,-12.21,321.17 167,10.95,-12.35,321.17 168,11.2,-13.88,322.17 169,10.76,-14.43,322.17 170,11.32,-14.65,322.17 171,10.85,-13.9,322.17 172,11.74,-14.12,322.17 173,10.46,-10.88,323.17 174,10.93,-10.44,323.17 175,12.3,-15.33,349.58 176,10.67,-13.78,349.58 177,11.51,-13.98,349.58 178,10.56,-15.22,349.58 179,12.51,-15.25,349.58 180,13.51,-15.76,349.58 181,11.98,-15.78,349.58 182,12.2,-15.49,349.58 183,10.48,-13.02,349.58 184,12.4,-15.3,349.58 185,13,-15.55,349.58 186,11.36,-14.35,349.58 187,12.08,-14.99,349.58 188,12.39,-14.83,349.58 189,12.28,-16.18,349.58 190,12.6,-15.01,349.58 191,11.3,-12.87,349.58 192,11.1,-14.67,349.58 193,11.42,-13.84,349.58 194,11.49,-14.89,349.58 195,12,-13.33,349.58 196,13.35,-15.04,349.58 197,11.97,-14.29,349.58 198,13.35,-15.62,349.58 199,12.75,-13.99,349.58 200,12.55,-15.06,349.58 201,12.3,-15.06,349.58 202,12.51,-15,349.58 203,12.61,-14.55,349.58 204,10.98,-13.32,349.58 205,11.82,-14.34,349.58 206,12.27,-14.47,349.58 207,12.11,-14.31,349.58 208,12.11,-14.18,349.58 209,12.89,-16.18,349.58 211,12.29,-15.92,350.58 212,11.89,-15.35,350.58 213,12.74,-14.29,350.58 214,12.29,-15.92,350.58 215,11.89,-15.35,350.58 218,10.56,-20.22,28 222,10.54,-19.97,28 225,10.97,-20.78,28 230,10.46,-20.61,28 249,10.56,-20.58,28 255,10.86,-20.19,28 260,10.9,-20.71,28 270,11.06,-20.59,28 271,10.76,-20.09,28 275,10.64,-19.37,28 283,10.94,-20.41,28 289,10.85,-20.84,28 295,10.45,-20.75,28 300,11.15,-20.29,28 303,11.23,-20.89,28 304,11.16,-19.69,28 305,10.94,-20.41,28 306,11.2,-21.24,28 307,10.71,-19.33,28 308,9.55,-25.87,48.42 309,8.6,-25.4,48.42 310,9.67,-27.23,48.42 311,8.17,-27.52,48.42 312,9.81,-24.55,48.42 313,10.94,-17.36,48.42 314,9.49,-24.7,48.42 315,9.46,-27.76,48.42 316,7.94,-28.92,48.42 317,9.77,-25.98,48.42 318,8.07,-26.77,48.42 319,8.39,-28.76,48.42 320,8.95,-27.7,48.42 321,9.83,-24.75,48.42 322,8.51,-25.47,48.42 323,8.86,-26.58,48.42 324,7.93,-28.94,48.42 325,8,-29.13,48.42 326,8.33,-26.65,48.42 327,8,-28.04,48.42 328,9.39,-27.5,48.42 329,8.01,-29.28,48.42 330,7.59,-27.85,48.42 331,8.26,-27.41,48.42 332,9.49,-27.57,48.42 333,8.23,-29.06,48.42 334,9.1,-25.98,48.42 335,8.21,-28.21,48.42 336,9.59,-25.27,48.42 337,9.37,-14.43,48.42 338,9.47,-27.4,48.42 339,8.6,-27.76,48.42 340,8.23,-28.45,99.25 346,8.39,-27.35,99.25 342,8.24,-28.83,99.25 347,8.34,-29.39,99.25 341,8.36,-28.86,99.25 344,7.22,-28.61,99.25 343,7.13,-29.27,99.25 345,10.64,-20.32,99.25 351,8.06,-28.21,99.25 348,8.22,-26.3,99.25 349,8.92,-28.27,99.25 350,9.35,-27.75,99.25 352,7.32,-28.55,99.25 353,7.66,-27.38,99.25 354,8.09,-29.13,99.25 355,7.3,-28.66,99.25 357,7.33,-29.02,99.25 356,7.33,-26.04,99.25 362,7.36,-26.06,99.25 358,7.49,-28.52,99.25 360,8.07,-28.51,99.25 359,8.84,-27.93,99.25 361,7.93,-29.07,99.25 363,7.94,-28.41,99.25 373,8.74,-26.42,100.25 372,8.26,-27.71,100.25 369,9.63,-27.75,100.25 368,8.85,-24.28,100.25 367,7.55,-28.43,100.25 370,10.05,-25.94,100.25 371,8.23,-28,100.25 365,7.74,-28.59,100.25 366,9.12,-22.61,100.25 374,7.33,-27.34,101.25 376,7.54,-27.35,101.25 375,8.8,-29.14,101.25 -------------------------------------------------------------------------------- /ap_notes/complex_simms/complex_SIMMs.pdf: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/andrewcparnell/simms_course/3052327e05aa93897fcfa31e2e8dd49fa3e45708/ap_notes/complex_simms/complex_SIMMs.pdf -------------------------------------------------------------------------------- /ap_notes/complex_simms/geese2_jags_output.rda: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/andrewcparnell/simms_course/3052327e05aa93897fcfa31e2e8dd49fa3e45708/ap_notes/complex_simms/geese2_jags_output.rda -------------------------------------------------------------------------------- /ap_notes/complex_simms/plot_geese_output.R: -------------------------------------------------------------------------------- 1 | 2 | # Plot the Geese output from the JAGS model in module 8 3 | rm(list=ls()) 4 | 5 | # Set the working directory 6 | #setwd("/Volumes/MacintoshHD2/GDrive/Conferences&Talks/SIAR_Glasgow/mod_8_timeseries_SIMMs") 7 | #setwd("~/GDrive/Conferences&Talks/SIAR_Glasgow/mod_8_complex_SIMMs") 8 | #setwd("~/transfer/SIMM_Glasgow") 9 | 10 | # Load in packages 11 | library(simmr) 12 | library(readxl) 13 | library(compositions) 14 | 15 | # Load in the data and the output 16 | path = system.file("extdata", "geese_data.xls", package = "simmr") 17 | geese_data = lapply(excel_sheets(path), read_excel, path = path) 18 | geese_consumers <- read.csv('ap_notes/complex_simms/GeeseConsumers2.csv') 19 | julianday = geese_consumers$julianday 20 | con = geese_data[[1]] 21 | sources = geese_data[[2]] 22 | TDF = geese_data[[3]] 23 | Conc = geese_data[[4]] 24 | 25 | # Some useful bits from the data 26 | N = nrow(con) 27 | K = nrow(sources) 28 | 29 | # Load in the jags output 30 | load(file='ap_notes/complex_simms/geese2_jags_output.rda') 31 | out = do.call(rbind,output) 32 | 33 | # Sort out the days to plot them properly 34 | newday = julianday 35 | newday[newday<299.75] = abs(newday[newday<299.75])+365 36 | 37 | # Create grid of predicted values 38 | new_grid = c(290:365,1:110) 39 | plot_grid = seq(300,300+length(new_grid)-1,length=length(new_grid)) 40 | X_new = cbind(1,sin(2*pi*new_grid/365),cos(2*pi*new_grid/365)) 41 | 42 | # Get the beta values - should be three betas - beta[1,k], beta[2,k], beta[3,k] 43 | beta_out = list(length=K) 44 | for(k in 1:K) beta_out[[k]] = out[,(3*k-2):(3*k)] 45 | 46 | # Now loop through the output creating predictions of the mean 47 | n_samples = nrow(out) 48 | f_pred = array(NA,dim=c(length(new_grid),K,n_samples)) 49 | for(s in 1:n_samples) { 50 | for(k in 1:K) { 51 | f_pred[,k,s] = X_new%*%beta_out[[k]][s,] 52 | } 53 | } 54 | 55 | # Now create predicted proportions 56 | p_pred = array(NA,dim=c(length(new_grid),K,n_samples)) 57 | for(s in 1:n_samples) { 58 | p_pred[,,s] = clrInv(f_pred[,,s]) 59 | } 60 | p_upper = apply(p_pred,c(1,2),'quantile',probs=0.9) 61 | p_median = apply(p_pred,c(1,2),'quantile',probs=0.5) 62 | p_lower = apply(p_pred,c(1,2),'quantile',probs=0.1) 63 | 64 | ########################## 65 | 66 | # Finally create the plot 67 | 68 | # Start plot 69 | par(mar=c(3,3,2,1), mgp=c(2,.7,0), tck=-.01,las=1) 70 | plot(newday,newday,type='n',xlab='Julian Day',ylim=c(0,1.1),las=1,xaxt='n',ylab='Dietary Proportion',pch=19,main='Dietary proportions by Julian day') 71 | 72 | # Add in a specialised axis 73 | grid() 74 | axis(1,at=seq(300,365,by=20),labels=seq(300,365,by=20)) 75 | axis(1,at=seq(365,500,by=20),labels=seq(365,500,by=20)%%365) 76 | 77 | # Create some colours 78 | transp = 0.5 79 | #mycols = c(rgb(1,0,0,transp),rgb(0,0,1,transp),rgb(0,1,0,transp),rgb(1,1,0,transp)) 80 | mycols = viridisLite::viridis(4, alpha = transp) 81 | 82 | # And finally add lines 83 | for(k in 1:K) { 84 | polygon(c(plot_grid,rev(plot_grid)),c(p_lower[,k],rev(p_upper[,k])),col=mycols[k],border=NA) 85 | lines(plot_grid,p_median[,k],lwd=3,col=mycols[k]) 86 | } 87 | 88 | # Add in some dates 89 | mylinelocs = c(1+365,32+365,61+365,92+365,306,336) 90 | myoffset = -0.5 91 | mylineheight=0.9 92 | lines(c(mylinelocs[1],mylinelocs[1]),c(0,1)) # 1/1 93 | text(mylinelocs[1]+myoffset,mylineheight,labels='1st Jan',srt=90,pos=3,offset=0) 94 | lines(c(mylinelocs[2],mylinelocs[2]),c(0,1)) # 1/2 95 | text(mylinelocs[2]+myoffset,mylineheight,labels='1st Feb',srt=90,pos=3,offset=0) 96 | lines(c(mylinelocs[3],mylinelocs[3]),c(0,1)) # 1/3 97 | text(mylinelocs[3]+myoffset,mylineheight,labels='1st Mar',srt=90,pos=3,offset=0) 98 | lines(c(mylinelocs[4],mylinelocs[4]),c(0,1)) # 1/4 99 | text(mylinelocs[4]+myoffset,mylineheight,labels='1st Apr',srt=90,pos=3,offset=0) 100 | lines(c(mylinelocs[5],mylinelocs[5]),c(0,1)) # 1/11 101 | text(mylinelocs[5]+myoffset,mylineheight,labels='1st Nov',srt=90,pos=3,offset=0) 102 | lines(c(mylinelocs[6],mylinelocs[6]),c(0,1)) # 1/12 103 | text(mylinelocs[6]+myoffset,mylineheight,labels='1st Dec',srt=90,pos=3,offset=0) 104 | legend('top',legend=as.matrix(sources[,1]),pch=19,col=mycols,horiz=T) 105 | 106 | -------------------------------------------------------------------------------- /ap_notes/complex_simms/run_geese_harmonic.R: -------------------------------------------------------------------------------- 1 | library(rjags) 2 | library(simmr) 3 | library(readxl) 4 | 5 | # Set wd 6 | #setwd("~/transfer/SIMM_Glasgow") 7 | 8 | # Load in the data and the output 9 | path = system.file("extdata", "geese_data.xls", package = "simmr") 10 | geese_data = lapply(excel_sheets(path), read_excel, path = path) 11 | geese_consumers <- read.csv('ap_notes/complex_simms/GeeseConsumers2.csv') 12 | 13 | modelstring =' 14 | model { 15 | for(i in 1:N) { 16 | for(j in 1:J) { 17 | y[i,j] ~ dnorm(inprod(p[i,]*q[,j],s[,j]+c[,j])/inprod(p[i,],q[,j]),1/pow(sigma[j],2)) 18 | } 19 | } 20 | for(k in 1:K) { 21 | for(j in 1:J) { 22 | s[k,j] ~ dnorm(s_mean[k,j],s_prec[k,j]) 23 | c[k,j] ~ dnorm(c_mean[k,j],c_prec[k,j]) 24 | } 25 | } 26 | for(i in 1:N) { 27 | p[i,1:K] <- expf[i,]/sum(expf[i,]) 28 | for(k in 1:K) { 29 | expf[i,k] <- exp(f[i,k]) 30 | } 31 | } 32 | for(k in 1:K) { 33 | f[1:N,k] <- X[1:N,1:L]%*%beta[1:L,k] #~ dnorm(mu_f[i,k],1/pow(sigma_f[k],2)) 34 | #mu_f[1:N,k] <- X[1:N,1:L]%*%beta[1:L,k] 35 | #sigma_f[k] ~ dgamma(2,1) 36 | } 37 | for(l in 1:L) { 38 | for(k in 1:K) { beta[l,k] ~ dnorm(0,1) } 39 | } 40 | for(j in 1:J) { sigma[j] ~ dunif(0,10) } 41 | } 42 | ' 43 | X = cbind(1,sin(2*pi*geese_consumers[,4]/365),cos(2*pi*geese_consumers[,4]/365)) 44 | data=list(y=geese_consumers[,2:3], 45 | s_mean=geese_data[[2]][,2:3], 46 | s_prec=1/geese_data[[2]][,4:5]^2, 47 | c_mean=geese_data[[3]][,2:3], 48 | c_prec=1/geese_data[[3]][,4:5]^2, 49 | q=geese_data[[4]][,2:3], 50 | N=nrow(geese_consumers), 51 | K=nrow(geese_data[[2]]), 52 | J=2,X=X,L=ncol(X)) 53 | init = function() { 54 | list( 55 | 'beta'=matrix(rnorm(data$L*data$K,0,0.1),ncol=data$K,nrow=data$L), 56 | 'sigma'=runif(data$J,0,1) 57 | ) 58 | } 59 | model=jags.model(textConnection(modelstring), data=data, n.chains=3,init=init) 60 | stop() 61 | output=coda.samples(model=model,variable.names=c('beta'),n.iter=10000,thin=10) 62 | save(output,file='geese2_jags_output.rda') 63 | gelman.diag(output,multivariate=FALSE) 64 | -------------------------------------------------------------------------------- /ap_notes/cosimmr/cosimmr_presentation.pdf: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/andrewcparnell/simms_course/3052327e05aa93897fcfa31e2e8dd49fa3e45708/ap_notes/cosimmr/cosimmr_presentation.pdf -------------------------------------------------------------------------------- /ap_notes/header.tex: -------------------------------------------------------------------------------- 1 | \usepackage{graphicx} 2 | \usepackage{amsmath,amsfonts,amssymb,amsthm,amscd, mathrsfs} 3 | \setbeamertemplate{navigation symbols}{} %%removes bottom line 4 | \setbeamertemplate{footline}[frame number] -------------------------------------------------------------------------------- /ap_notes/intro_bayes/Thomas_Bayes.gif: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/andrewcparnell/simms_course/3052327e05aa93897fcfa31e2e8dd49fa3e45708/ap_notes/intro_bayes/Thomas_Bayes.gif -------------------------------------------------------------------------------- /ap_notes/intro_bayes/intro_bayes.pdf: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/andrewcparnell/simms_course/3052327e05aa93897fcfa31e2e8dd49fa3e45708/ap_notes/intro_bayes/intro_bayes.pdf -------------------------------------------------------------------------------- /ap_notes/maynooth_uni_logo.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/andrewcparnell/simms_course/3052327e05aa93897fcfa31e2e8dd49fa3e45708/ap_notes/maynooth_uni_logo.jpg -------------------------------------------------------------------------------- /ap_notes/old_slides/building_simms/Create_spline_data.R: -------------------------------------------------------------------------------- 1 | # Create some data for a spline type model 2 | 3 | # Clear the workspace 4 | rm(list=ls()) 5 | 6 | # Set the working directory 7 | #setwd("~/GDrive/Conferences&Talks/SIAR_Glasgow_Jan16/ap_notes/mod_9_building_simms") 8 | #setwd("/Volumes/MacintoshHD2/GDrive/Conferences&Talks/SIAR_Glasgow/mod_9_building_SIMMs") 9 | 10 | # Set the seed 11 | set.seed(123) 12 | 13 | # Source in the basis functions 14 | source('bases.r') 15 | 16 | # Create some data 17 | N = 100 18 | t = 1:N 19 | 20 | # Create basis functions 21 | B = bbase(t) 22 | 23 | # Simulate some parmeters 24 | sd_beta = 0.2 25 | beta = cumsum(rnorm(ncol(B),0,sd_beta)) 26 | 27 | # Noise sd 28 | sigma = 0.1 29 | 30 | # Data 31 | y = rnorm(N,mean=B%*%beta,sigma) 32 | # Re-scale so in the range 0,1 33 | p = (y-min(y)+2)/(max(y)-min(y)+4) 34 | 35 | # Plot it 36 | par(mar=c(3,3,2,1), mgp=c(2,.7,0), tck=-.01,las=1) 37 | #plot(t,p) 38 | -------------------------------------------------------------------------------- /ap_notes/old_slides/building_simms/Geese2Spline.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/andrewcparnell/simms_course/3052327e05aa93897fcfa31e2e8dd49fa3e45708/ap_notes/old_slides/building_simms/Geese2Spline.png -------------------------------------------------------------------------------- /ap_notes/old_slides/building_simms/SIMM_spline_output.rda: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/andrewcparnell/simms_course/3052327e05aa93897fcfa31e2e8dd49fa3e45708/ap_notes/old_slides/building_simms/SIMM_spline_output.rda -------------------------------------------------------------------------------- /ap_notes/old_slides/building_simms/bases.r: -------------------------------------------------------------------------------- 1 | tpower <- function(x, t, p) 2 | # Truncated p-th power function 3 | (x - t) ^ p * (x > t) 4 | 5 | 6 | bbase <- function(x, xl = min(x), xr = max(x), nseg = 10, deg = 3){ 7 | # Construct B-spline basis 8 | dx <- (xr - xl) / nseg 9 | knots <- seq(xl - deg * dx, xr + deg * dx, by = dx) 10 | P <- outer(x, knots, tpower, deg) 11 | n <- dim(P)[2] 12 | D <- diff(diag(n), diff = deg + 1) / (gamma(deg + 1) * dx ^ deg) 13 | B <- (-1) ^ (deg + 1) * P %*% t(D) 14 | B } 15 | 16 | gauss <- function(x, mu, sig) { 17 | # Gaussian-shaped function 18 | u <- (x - mu) / sig 19 | y <- exp(- u * u / 2) 20 | y } 21 | 22 | gbase <- function(x, mus) { 23 | # Construct Gaussian basis 24 | sig <- (mus[2] - mus[1]) / 2 25 | G <- outer(x, mus, gauss, sig) 26 | G } 27 | 28 | pbase <- function(x, n) { 29 | # Construct polynomial basis 30 | u <- (x - min(x)) / (max(x) - min(x)) 31 | u <- 2 * (u - 0.5); 32 | P <- outer(u, seq(0, n, by = 1), "^") 33 | P } 34 | -------------------------------------------------------------------------------- /ap_notes/old_slides/building_simms/building_SIMMs.pdf: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/andrewcparnell/simms_course/3052327e05aa93897fcfa31e2e8dd49fa3e45708/ap_notes/old_slides/building_simms/building_SIMMs.pdf -------------------------------------------------------------------------------- /ap_notes/old_slides/building_simms/run_spline_geese.R: -------------------------------------------------------------------------------- 1 | 2 | # Plot the Geese output from the JAGS model in module 8 3 | rm(list=ls()) 4 | 5 | # Set the working directory 6 | #setwd("/Volumes/MacintoshHD2/GDrive/Conferences&Talks/SIAR_Glasgow/mod_9_building_SIMMs") 7 | setwd("~/GDrive/Conferences&Talks/SIAR_Glasgow/mod_9_building_SIMMs") 8 | 9 | # Load in packages 10 | library(siar) 11 | library(compositions) 12 | 13 | # Source in bases.r functions 14 | source('bases.r') 15 | 16 | # Load in the data and the output 17 | data(geese1demo,sourcesdemo,correctionsdemo,concdepdemo) 18 | sources = as.matrix(sourcesdemo[,2:5]) 19 | tefs = as.matrix(correctionsdemo[,2:5]) 20 | cd = as.matrix(concdepdemo[,c(2,4)]) 21 | con = read.csv('../mod_8_complex_SIMMs/GeeseConsumers2.csv') 22 | 23 | # Some useful bits from the data 24 | N = nrow(con) 25 | K = nrow(sources) 26 | 27 | # Run the JAGS SIMM Spline model 28 | modelstring =' 29 | model { 30 | for(i in 1:N) { 31 | for(j in 1:J) { 32 | y[i,j] ~ dnorm(inprod(p[i,]*q[,j],s[,j]+c[,j])/inprod(p[i,],q[,j]),1/pow(sigma[j],2)) 33 | } 34 | } 35 | for(k in 1:K) { 36 | for(j in 1:J) { 37 | s[k,j] ~ dnorm(s_mean[k,j],s_prec[k,j]) 38 | c[k,j] ~ dnorm(c_mean[k,j],c_prec[k,j]) 39 | } 40 | } 41 | for(i in 1:N) { 42 | p[i,1:K] <- expf[i,]/sum(expf[i,]) 43 | for(k in 1:K) { 44 | expf[i,k] <- exp(f[i,k]) 45 | } 46 | } 47 | for(k in 1:K) { 48 | f[1:N,k] <- B%*%beta[,k] 49 | beta[1,k] ~ dnorm(0,0.001) 50 | for(l in 2:L) { beta[l,k] ~ dnorm(beta[l-1,k],1/pow(sigma_beta[k],2)) } 51 | sigma_beta[k] ~ dunif(0,10) 52 | } 53 | for(j in 1:J) { sigma[j] ~ dunif(0,10) } 54 | } 55 | ' 56 | 57 | # Run model 58 | con = read.csv('../mod_8_complex_SIMMs/GeeseConsumers2.csv') 59 | data(geese1demo,sourcesdemo,correctionsdemo,concdepdemo) 60 | sources = as.matrix(sourcesdemo[,2:5]) 61 | tefs = as.matrix(correctionsdemo[,2:5]) 62 | cd = as.matrix(concdepdemo[,c(2,4)]) 63 | julianday_rescaled = (con$julianday-299.75)%%365 64 | B = bbase(julianday_rescaled) 65 | data=list(y=con[,2:3],s_mean=sources[,c(1,3)],s_prec=1/sources[,c(2,4)]^2, 66 | c_mean=tefs[,c(1,3)],c_prec=1/tefs[,c(2,4)]^2, 67 | q=cd,N=nrow(con),K=nrow(sources), 68 | J=ncol(con[,2:3]),B=B,L=ncol(B)) 69 | model=jags.model(textConnection(modelstring), data=data, n.chains=3) 70 | stop() 71 | output=coda.samples(model=model,variable.names=c('sigma_beta','beta','sigma'),n.iter=10000,thin=10) 72 | save(output,file='SIMM_spline_output.rda') 73 | #load(file='SIMM_spline_output.rda') 74 | gelman.diag(output,multivariate = FALSE) 75 | -------------------------------------------------------------------------------- /ap_notes/old_slides/prac_using_siar/prac_using_siar.pdf: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/andrewcparnell/simms_course/3052327e05aa93897fcfa31e2e8dd49fa3e45708/ap_notes/old_slides/prac_using_siar/prac_using_siar.pdf -------------------------------------------------------------------------------- /ap_notes/old_slides/using_mixsiar/MixSIAR_model.txt: -------------------------------------------------------------------------------- 1 | # source$data_type: means 2 | # source$by_factor: 1 3 | # random effects: 2 4 | # fixed effects: 0 5 | # nested factors: FALSE TRUE 6 | # factors: Region Pack 7 | # continuous effects: 0 8 | # error structure: Residual * Process 9 | # source$conc_dep: FALSE 10 | 11 | model{ 12 | for(src in 1:n.sources){ 13 | for(f1 in 1:source_factor_levels){ 14 | for(iso in 1:n.iso){ 15 | src_mu[src,iso,f1] ~ dnorm(MU_array[src,iso,f1], n_array[src,f1]/SIG2_array[src,iso,f1]); # Eqn 3.8 but with precision instead of variance 16 | tmp.X[src,iso,f1] ~ dchisqr(n_array[src,f1]); 17 | src_tau[src,iso,f1] <- tmp.X[src,iso,f1]/(SIG2_array[src,iso,f1]*(n_array[src,f1] - 1)); # Eqn 3.9, following the simulation on p.580 18 | } 19 | } 20 | } 21 | 22 | # draw p.global (global proportion means) from an uninformative Dirichlet, 23 | # then ilr.global is the ILR-transform of p.global 24 | p.global[1:n.sources] ~ ddirch(alpha[1:n.sources]); 25 | for(src in 1:(n.sources-1)){ 26 | gmean[src] <- prod(p.global[1:src])^(1/src); 27 | ilr.global[src] <- sqrt(src/(src+1))*log(gmean[src]/p.global[src+1]); # page 296, Egozcue 2003 28 | } 29 | 30 | fac1.sig ~ dunif(0,20); 31 | fac1.invSig2 <- 1/(fac1.sig*fac1.sig); 32 | # draw the fac1 (region) specific ILR terms (random effect) 33 | for(f1 in 1:factor1_levels) { 34 | for(src in 1:(n.sources-1)) { 35 | ilr.fac1[f1,src] ~ dnorm(0,fac1.invSig2); 36 | } 37 | } 38 | 39 | fac2.sig ~ dunif(0,20); 40 | fac2.invSig2 <- 1/(fac2.sig*fac2.sig); 41 | # draw the fac2-specific ILR terms (random effect) 42 | for(f2 in 1:factor2_levels){ 43 | for(src in 1:(n.sources-1)){ 44 | ilr.fac2[f2,src] ~ dnorm(0,fac2.invSig2); 45 | } 46 | } 47 | 48 | # DON'T generate individual deviates from the global/region/pack mean (but keep same model structure) 49 | for(i in 1:N) { 50 | for(src in 1:(n.sources-1)) { 51 | ilr.ind[i,src] <- 0; 52 | ilr.tot[i,src] <- ilr.global[src] + ilr.fac1[Factor.1[i],src] + ilr.fac2[Factor.2[i],src] + ilr.ind[i,src]; # add all effects together for each individual (in ilr-space) 53 | } 54 | } 55 | 56 | # Inverse ILR math (equation 24, page 294, Egozcue 2003) 57 | for(i in 1:N){ 58 | for(j in 1:(n.sources-1)){ 59 | cross[i,,j] <- (e[,j]^ilr.tot[i,j])/sum(e[,j]^ilr.tot[i,j]); 60 | } 61 | for(src in 1:n.sources){ 62 | tmp.p[i,src] <- prod(cross[i,src,]); 63 | } 64 | for(src in 1:n.sources){ 65 | p.ind[i,src] <- tmp.p[i,src]/sum(tmp.p[i,]); 66 | } 67 | } 68 | 69 | for(src in 1:n.sources) { 70 | for(i in 1:N){ 71 | # these are weights for variances 72 | p2[i,src] <- p.ind[i,src]*p.ind[i,src]; 73 | } 74 | } 75 | 76 | # Transform ilr.fac1 into p.fac1 (fac1 not nested within fac2) 77 | for(f1 in 1:factor1_levels) { 78 | for(src in 1:(n.sources-1)) { 79 | ilr.fac1.tot[f1,src] <- ilr.global[src] + ilr.fac1[f1,src]; 80 | cross.fac1[f1,,src] <- (e[,src]^ilr.fac1.tot[f1,src])/sum(e[,src]^ilr.fac1.tot[f1,src]); 81 | } 82 | for(src in 1:n.sources) { 83 | tmp.p.fac1[f1,src] <- prod(cross.fac1[f1,src,]); 84 | } 85 | for(src in 1:n.sources){ 86 | p.fac1[f1,src] <- tmp.p.fac1[f1,src]/sum(tmp.p.fac1[f1,]); 87 | } 88 | } 89 | 90 | # Transform ilr.fac2 into p.fac2 (fac2 nested within fac1) 91 | for(f2 in 1:factor2_levels){ 92 | for(src in 1:(n.sources-1)){ 93 | ilr.fac2.tot[f2,src] <- ilr.global[src] + ilr.fac1[factor1_lookup[f2],src] + ilr.fac2[f2,src]; 94 | cross.fac2[f2,,src] <- (e[,src]^ilr.fac2.tot[f2,src])/sum(e[,src]^ilr.fac2.tot[f2,src]); 95 | } 96 | for(src in 1:n.sources) { 97 | tmp.p.fac2[f2,src] <- prod(cross.fac2[f2,src,]); 98 | } 99 | for(src in 1:n.sources){ 100 | p.fac2[f2,src] <- tmp.p.fac2[f2,src]/sum(tmp.p.fac2[f2,]); 101 | } 102 | } 103 | 104 | 105 | # for each isotope and population, calculate the predicted mixtures 106 | for(iso in 1:n.iso) { 107 | for(i in 1:N) { 108 | 109 | mix.mu[iso,i] <- inprod(src_mu[,iso,Factor.1[i]],p.ind[i,]) + inprod(frac_mu[,iso],p.ind[i,]); 110 | } 111 | } 112 | 113 | # Multiplicative residual error 114 | for(iso in 1:n.iso){ 115 | resid.prop[iso] ~ dunif(0,20); 116 | } 117 | 118 | 119 | # Calculate process variance for each isotope and population 120 | for(iso in 1:n.iso) { 121 | for(i in 1:N) { 122 | 123 | process.var[iso,i] <- inprod(1/src_tau[,iso,Factor.1[i]],p2[i,]) + inprod(frac_sig2[,iso],p2[i,]); 124 | } 125 | } 126 | 127 | # Construct Sigma, the mixture precision matrix 128 | for(ind in 1:N){ 129 | for(i in 1:n.iso){ 130 | for(j in 1:n.iso){ 131 | Sigma.ind[ind,i,j] <- equals(i,j)/(process.var[i,ind]*resid.prop[i]); 132 | } 133 | } 134 | } 135 | 136 | # Likelihood 137 | for(i in 1:N) { 138 | 139 | X_iso[i,] ~ dmnorm(mix.mu[,i], Sigma.ind[i,,]); 140 | } 141 | } # end model 142 | 143 | -------------------------------------------------------------------------------- /ap_notes/old_slides/using_mixsiar/matrix_plot.tiff: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/andrewcparnell/simms_course/3052327e05aa93897fcfa31e2e8dd49fa3e45708/ap_notes/old_slides/using_mixsiar/matrix_plot.tiff -------------------------------------------------------------------------------- /ap_notes/old_slides/using_mixsiar/mixsiar_screen.tiff: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/andrewcparnell/simms_course/3052327e05aa93897fcfa31e2e8dd49fa3e45708/ap_notes/old_slides/using_mixsiar/mixsiar_screen.tiff -------------------------------------------------------------------------------- /ap_notes/old_slides/using_mixsiar/mixsiar_screen_isospace.tiff: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/andrewcparnell/simms_course/3052327e05aa93897fcfa31e2e8dd49fa3e45708/ap_notes/old_slides/using_mixsiar/mixsiar_screen_isospace.tiff -------------------------------------------------------------------------------- /ap_notes/old_slides/using_mixsiar/mixsiar_screen_mix.tiff: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/andrewcparnell/simms_course/3052327e05aa93897fcfa31e2e8dd49fa3e45708/ap_notes/old_slides/using_mixsiar/mixsiar_screen_mix.tiff -------------------------------------------------------------------------------- /ap_notes/old_slides/using_mixsiar/mixsiar_screen_source.tiff: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/andrewcparnell/simms_course/3052327e05aa93897fcfa31e2e8dd49fa3e45708/ap_notes/old_slides/using_mixsiar/mixsiar_screen_source.tiff -------------------------------------------------------------------------------- /ap_notes/old_slides/using_mixsiar/overall_p.tiff: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/andrewcparnell/simms_course/3052327e05aa93897fcfa31e2e8dd49fa3e45708/ap_notes/old_slides/using_mixsiar/overall_p.tiff -------------------------------------------------------------------------------- /ap_notes/old_slides/using_mixsiar/re_sds.tiff: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/andrewcparnell/simms_course/3052327e05aa93897fcfa31e2e8dd49fa3e45708/ap_notes/old_slides/using_mixsiar/re_sds.tiff -------------------------------------------------------------------------------- /ap_notes/old_slides/using_mixsiar/trace.tiff: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/andrewcparnell/simms_course/3052327e05aa93897fcfa31e2e8dd49fa3e45708/ap_notes/old_slides/using_mixsiar/trace.tiff -------------------------------------------------------------------------------- /ap_notes/old_slides/using_mixsiar/using_mixsiar.pdf: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/andrewcparnell/simms_course/3052327e05aa93897fcfa31e2e8dd49fa3e45708/ap_notes/old_slides/using_mixsiar/using_mixsiar.pdf -------------------------------------------------------------------------------- /ap_notes/prac_jags/practical_R_jags_and_lr.R: -------------------------------------------------------------------------------- 1 | # Practical on using linear regression in JAGS 2 | 3 | # Call in the package we need 4 | library(R2jags) 5 | 6 | # Here is some data on sea level rise 7 | sea_level = read.csv('https://raw.githubusercontent.com/andrewcparnell/tsme_course/master/data/church_and_white_global_tide_gauge.csv') 8 | head(sea_level) 9 | 10 | # Create a plot of the data 11 | #with(sea_level,plot(year_AD,sea_level_m)) 12 | # nicer plotting syntax ;) that matches the call to lm 13 | plot(sea_level_m ~ year_AD, data = sea_level) 14 | 15 | # Let's fit a linear regression with sea_level_m as the response and year as the covariate 16 | 17 | # Fit a standard linear regression 18 | lr_1 = lm(sea_level_m ~ year_AD, data = sea_level) 19 | summary(lr_1) 20 | 21 | plot(lr_1$residuals ~ lr_1$fitted.values) 22 | 23 | # Now look at the JAGS code for this model 24 | model_code = ' 25 | model 26 | { 27 | # Likelihood 28 | for (i in 1:n) { 29 | y[i] ~ dnorm(alpha + beta * x[i], sigma^-2) 30 | } 31 | 32 | # Priors 33 | alpha ~ dnorm(0, 100^-2) 34 | beta ~ dnorm(0, 100^-2) 35 | sigma ~ dunif(0, 10) 36 | } 37 | ' 38 | 39 | # Set up the data - these match the data objects in the jags code 40 | model_data = list(n = nrow(sea_level), 41 | y = sea_level$sea_level_m, 42 | x = sea_level$year_AD) 43 | 44 | # Choose which parameters to save 45 | model_parameters = c('alpha', 'beta', 46 | 'sigma') 47 | 48 | # Run the model 49 | model_run = jags(data = model_data, 50 | parameters.to.save = model_parameters, 51 | model.file = textConnection(model_code)) 52 | 53 | # Look at the output 54 | print(model_run) 55 | 56 | # Things to do: 57 | # 1 Compare the lm output to the jags output to see they match 58 | # 2 Change the prior distributions - make them narrower? 59 | # 3 Change the code/results to get mm per year 60 | 61 | # Further output ---------------------------------------------------------- 62 | 63 | plot(model_run) 64 | traceplot(model_run) 65 | 66 | # We can access the full posterior data frame 67 | head(model_run$BUGSoutput$sims.matrix) 68 | 69 | # Or you can access just the posterior means 70 | model_run$BUGSoutput$mean$alpha 71 | model_run$BUGSoutput$mean$beta 72 | 73 | # We can plot the posterior distributions of the parameters 74 | plot(density(model_run$BUGSoutput$sims.list$alpha)) 75 | plot(density(model_run$BUGSoutput$sims.list$beta)) 76 | 77 | # Create a 90% interval for the sea level rate in mm per year 78 | quantile(model_run$BUGSoutput$sims.list$beta, 79 | probs = c(0.05, 0.95)) * 1000 80 | 81 | # We can create a plot of the posterior mean line 82 | alpha_mean = model_run$BUGSoutput$mean$alpha[1] 83 | beta_mean = model_run$BUGSoutput$mean$beta[1] 84 | #x = sea_level$year_AD 85 | plot(sea_level_m ~ year_AD, data = sea_level) 86 | abline(a = alpha_mean , b = beta_mean) 87 | #lines(x, alpha_mean + beta_mean * x, col = 'red') 88 | legend('topleft', 89 | legend = c('Data', 'Posterior mean'), 90 | lty=c(-1,1), 91 | pch=c(1,-1), 92 | col=c('black','red')) 93 | 94 | -------------------------------------------------------------------------------- /ap_notes/prac_jags/practical_jags_logistic.R: -------------------------------------------------------------------------------- 1 | # Practical session on using JAGS for logistic regression 2 | 3 | # Start up code - clear workspace and call packages 4 | rm(list=ls()) # Clear the workspace 5 | library(R2jags) 6 | library(boot) # Package contains the logit transform 7 | 8 | # Data -------------------------------------------------------------------- 9 | 10 | # Adapted data from Royle and Dorazio (Chapter 2) 11 | # Moth mortality data 12 | N = 12 # Number of experiments 13 | K = 20 # Number of moths in each experiment 14 | y = c(1,4,9,13,18,20, 0,2,6,10,12,16) # Number died 15 | sex = c(rep('male',6), rep('female',6)) # Sex 16 | dose = rep(0:5, 2) # Dose of ??? 17 | sex_male = as.integer(sex == 'male') # Turn sex into an integer 18 | # The key questions is: what are the effects of dose and sex? 19 | 20 | # Quick exploratory plot 21 | plot(dose, y, col = sex_male + 1) 22 | legend('topleft', legend = c('Female', 'Male'), 23 | col = 1:2, pch = 1) 24 | 25 | # Or better plot of proportion died 26 | plot(dose, y/K, col = sex_male + 1, las = 1) 27 | legend('topleft', legend = c('Female', 'Male'), 28 | col = 1:2, pch = 1) 29 | 30 | # JAGS code --------------------------------------------------------------- 31 | 32 | # Jags code to fit the model to the simulated data 33 | model_code = ' 34 | model 35 | { 36 | # Likelihood 37 | for (i in 1:N) { 38 | y[i] ~ dbin(p_died[i], K) 39 | logit(p_died[i]) <- alpha + beta_male * x_male[i] + beta_dose * x_dose[i] 40 | } 41 | 42 | # Priors 43 | alpha ~ dnorm(0, 10^-2) 44 | beta_male ~ dnorm(0, 10^-2) 45 | beta_dose ~ dnorm(0, 10^-2) 46 | } 47 | ' 48 | 49 | # Run the model ----------------------------------------------------------- 50 | 51 | # Set up the data 52 | model_data = list(N = N, y = y, x_male = sex_male, x_dose = dose, K = K) 53 | 54 | # Choose the parameters to watch 55 | model_parameters = c("alpha", "beta_male", "beta_dose") 56 | 57 | # Run the model 58 | model_run = jags(data = model_data, 59 | parameters.to.save = model_parameters, 60 | model.file = textConnection(model_code)) 61 | 62 | # Look at output ---------------------------------------------------------- 63 | 64 | print(model_run) 65 | plot(model_run) 66 | 67 | # Look at effect of sex - quantified by beta_male 68 | hist(model_run$BUGSoutput$sims.list$beta_male, breaks = 30) 69 | # Seems positive - males more likely to die 70 | 71 | # Can vconvert this into odds: 72 | hist(exp(model_run$BUGSoutput$sims.list$beta_male), breaks = 30) 73 | # 3 times higher chance of dying if you're male! 74 | 75 | # Look at the posterior 76 | head(model_run$BUGSoutput$sims.matrix) 77 | 78 | # Extract the mean values of the parameters 79 | alpha_mean = model_run$BUGSoutput$mean$alpha[1] 80 | beta_male_mean = model_run$BUGSoutput$mean$beta_male[1] 81 | beta_dose_mean = model_run$BUGSoutput$mean$beta_dose[1] 82 | 83 | # What about effect of dose? 84 | o = order(dose) 85 | plot(dose, y/K, col = sex_male + 1, las = 1) 86 | # Males 87 | with(model_run, 88 | lines(dose[o], 89 | inv.logit(alpha_mean + beta_male_mean * 1 + 90 | beta_dose_mean * dose[o]), 91 | col = 'red')) 92 | # Females 93 | with(model_run, 94 | lines(dose[o], 95 | inv.logit(alpha_mean + beta_male_mean * 0 + 96 | beta_dose_mean * dose[o]), 97 | col = 'blue')) 98 | 99 | # Legend 100 | legend('topleft', 101 | legend = c('Males', 'Females'), 102 | lty = 1, 103 | col = c('red','blue')) 104 | 105 | 106 | # Other tasks ------------------------------------------------------------- 107 | 108 | # 1) See if there is an interaction between sex and dose in the above example. To do this add an extra term in the model beta_int * x_male[i] * x_dose[i]. Don't forget to include beta_int in your parameters to watch vector. Is beta_int precisely estimated in the posterior? 109 | # 2) It almost always the case that the death rate increases with dose. Try changing the prior distribution on beta_dose to reflect the fact that the parameter should be positive. How much effect does this have on the posterior ditribution of beta_dose? 110 | # 3) (Harder) A common task is to estimate the LD-50, the dose at which 50% of the animals have died. See if you can estimate the LD-50 for males and females with uncertainty. (hint: either estimate a 95% CI for the LD50 for each group or, better yet, produce a full posterior distribution) 111 | 112 | -------------------------------------------------------------------------------- /ap_notes/prac_jags/practical_jags_poisson.R: -------------------------------------------------------------------------------- 1 | # Practical session on using JAGS for Poisson regression 2 | 3 | # Start up code - clear workspace and call packages 4 | rm(list=ls()) # Clear the workspace 5 | library(R2jags) 6 | 7 | # Data -------------------------------------------------------------------- 8 | 9 | butterflies = data.frame(garden = factor(c(1, 1, 1, 1, 1, 1, 1, 1, 2, 2, 2, 10 | 2, 2, 2, 2, 2, 3, 3, 3, 3, 3, 3, 11 | 3, 3)), 12 | monarchs = c(0, 4, 2, 2, 0, 6, 0, 0, 5, 9, 7, 13 | 5, 7, 5, 9, 5, 10, 14, 12, 12, 14 | 10, 16, 10, 10)) 15 | 16 | # A simple plot 17 | boxplot(monarchs ~ garden, data = butterflies) 18 | 19 | # JAGS code --------------------------------------------------------------- 20 | 21 | # Jags code to fit the model to the simulated data 22 | model_code = ' 23 | model 24 | { 25 | # Likelihood 26 | for (i in 1:N) { 27 | y[i] ~ dpois(rate[i]) 28 | log(rate[i]) <- alpha + beta_garden2 * x_garden2[i] + 29 | beta_garden3 * x_garden3[i] 30 | } 31 | # Priors 32 | alpha ~ dnorm(0, 10^-2) 33 | beta_garden2 ~ dnorm(0, 10^-2) 34 | beta_garden3 ~ dnorm(0, 10^-2) 35 | } 36 | ' 37 | 38 | # Run the model ----------------------------------------------------------- 39 | 40 | # Set up the data 41 | model_data = with(butterflies, 42 | list(N = length(garden), y = monarchs, 43 | x_garden2 = as.integer(garden==2), 44 | x_garden3 = as.integer(garden==3))) 45 | 46 | # Choose the parameters to watch 47 | model_parameters = c("alpha", "beta_garden2", "beta_garden3") 48 | 49 | # Run the model 50 | model_run = jags(data = model_data, 51 | parameters.to.save = model_parameters, 52 | model.file = textConnection(model_code)) 53 | 54 | # Look at output ---------------------------------------------------------- 55 | 56 | print(model_run) 57 | plot(model_run) 58 | 59 | # Extract tbe output of each garden and plot them against the data 60 | garden_1 = exp(model_run$BUGSoutput$sims.list$alpha) 61 | garden_2 = exp(model_run$BUGSoutput$sims.list$beta_garden2) 62 | garden_3 = exp(model_run$BUGSoutput$sims.list$beta_garden3) 63 | n_samples = length(garden_3) 64 | 65 | posterior = data.frame(garden = rep(1:3, each = n_samples), 66 | monarchs = c(garden_1, garden_2, garden_3)) 67 | 68 | #boxplot(monarchs ~ garden, data = butterflies) 69 | boxplot(monarchs ~ garden, data = posterior) 70 | 71 | # Other tasks ------------------------------------------------------------- 72 | 73 | # 1) Think carefully about how the model is set up and what the parameters actually represent. Perhpas we could change the model to get a parameter that represents the rate for each garden? 74 | # 2) The Poisson distribution unrealistically assumes that the mean and the variance are equal. Do you know any other count distributions that might fit these data better. If so fit them! 75 | 76 | -------------------------------------------------------------------------------- /ap_notes/prac_mixsiar_and_jags/mixsiar_script.R: -------------------------------------------------------------------------------- 1 | # A script to run MixSIAR on the Wolves example 2 | # The full versions are in the MixSIAR manual: https://github.com/brianstock/MixSIAR/blob/master/Manual/mixsiar_manual.pdf 3 | # and the MixSIAR vignettes: https://cran.r-project.org/web/packages/MixSIAR/vignettes/ 4 | 5 | library(MixSIAR) 6 | library(tidyr) # For pivoting later in new output_jags 7 | library(ggplot2) 8 | 9 | # My new output function 10 | source("extra_code/output_jags_new.R") 11 | 12 | # Find the data 13 | mix.filename = system.file("extdata", 14 | "wolves_consumer.csv", 15 | package = "MixSIAR") 16 | #system(paste('open',mix.filename)) 17 | 18 | # Load into MixSIAR 19 | mix = load_mix_data(filename="mix.filename", 20 | iso_names=c("d13C","d15N"), 21 | factors=c("Region","Pack"), 22 | fac_random=c(TRUE,TRUE), 23 | fac_nested=c(FALSE,TRUE), 24 | cont_effects=NULL) 25 | 26 | # Have a look at the mixture data 27 | # mix.data = read.csv(mix.filename) 28 | # str(mix.data) 29 | 30 | # Replace the system.file call with the path to your file 31 | source.filename = system.file("extdata", 32 | "wolves_sources.csv", 33 | package = "MixSIAR") 34 | 35 | # Load the source data 36 | source = load_source_data(filename=source.filename, 37 | source_factors="Region", 38 | conc_dep=FALSE, 39 | data_type="means", 40 | mix) 41 | 42 | # Replace the system.file call with the path to your file 43 | discr.filename = system.file("extdata", 44 | "wolves_discrimination.csv", 45 | package = "MixSIAR") 46 | 47 | # Load the discrimination/TDF data 48 | discr = load_discr_data(filename=discr.filename, mix) 49 | 50 | # Isospace plot 51 | plot_data(filename="isospace_plot", 52 | plot_save_pdf = FALSE, 53 | plot_save_png = FALSE, 54 | mix = mix, 55 | source = source, 56 | discr = discr) 57 | 58 | # Alternative plot saving it and editing it 59 | p <- plot_data(filename="isospace_plot", 60 | plot_save_pdf = FALSE, 61 | plot_save_png = FALSE, 62 | mix = mix, 63 | source = source, 64 | discr = discr, return_obj = TRUE) 65 | 66 | # Can now do e.g. 67 | p + theme(legend.position = "None") 68 | 69 | # Plot the prior 70 | #plot_prior(alpha.prior=1,source) 71 | 72 | # Write the model out 73 | model_filename = "MixSIAR_model.txt" # Name of the JAGS model file 74 | write_JAGS_model(model_filename, 75 | resid_err = TRUE, 76 | process_err = TRUE, 77 | mix, source) 78 | 79 | # Test run 80 | jags.1 = run_model(run="test", 81 | mix, source, discr, 82 | model_filename, 83 | alpha.prior = c(1,1,1), 84 | resid_err = TRUE, 85 | process_err = TRUE) 86 | 87 | # Long run - change run to 'normal' if you have 5 hours to kill 88 | jags.2 = run_model(run="very short", 89 | mix, source, discr, 90 | model_filename, 91 | alpha.prior = 1, 92 | resid_err = TRUE, 93 | process_err = TRUE) 94 | 95 | # Get the output 96 | # output_JAGS(jags.1, mix = mix, source = source, 97 | # output_options = 98 | # list(summary_save = TRUE, 99 | # summary_name = "summary_statistics", 100 | # sup_post = FALSE, 101 | # plot_post_save_pdf = FALSE, 102 | # plot_post_name = "posterior_density", 103 | # sup_pairs = FALSE, 104 | # plot_pairs_save_pdf = FALSE, 105 | # plot_pairs_name = "pairs_plot", 106 | # sup_xy = FALSE, 107 | # plot_xy_save_pdf = FALSE, 108 | # plot_xy_name = "xy_plot", 109 | # gelman = FALSE, 110 | # heidel =FALSE, 111 | # geweke = FALSE, 112 | # diag_save = FALSE, 113 | # diag_name = "diagnostics", 114 | # indiv_effect = FALSE, 115 | # plot_post_save_png = FALSE, 116 | # plot_pairs_save_png = FALSE, 117 | # plot_xy_save_png = FALSE, 118 | # diag_save_ggmcmc = FALSE)) 119 | 120 | out <- output_JAGS(jags.1, 121 | mix = mix, 122 | source = source, 123 | c('summary_diagnostics')) 124 | 125 | 126 | out <- output_JAGS(jags.1, mix = mix, source = source, 127 | c('summary_diagnostics', 128 | 'summary_statistics', 129 | 'summary_quantiles', 130 | 'plot_global', 131 | 'plot_factors')) 132 | 133 | 134 | -------------------------------------------------------------------------------- /ap_notes/prac_mixsiar_and_jags/mixsiar_script_as_notebook.Rmd: -------------------------------------------------------------------------------- 1 | --- 2 | title: "MixSIAR script example as Rnotebook" 3 | output: html_notebook 4 | --- 5 | 6 | Here I recreate the mixsiar_script.R example but in a Rnotebook. Additionally I have grabbed the actual data files and put them in a folder `wolves_data/` within the folder where this Rmd file sits so that the analysis more closely resembles the analysis you might make with your own data. 7 | 8 | ## Setup 9 | 10 | Load libraries. 11 | 12 | ```{r} 13 | library(MixSIAR) 14 | library(tidyr) # For pivoting later in new output_jags 15 | library(ggplot2) 16 | ``` 17 | 18 | ## Load in the data 19 | 20 | First load the mixtures, i.e. the consumers. 21 | 22 | ```{r} 23 | # Load into MixSIAR 24 | mix = load_mix_data( 25 | filename="wolves_data/wolves_consumer.csv", 26 | iso_names=c("d13C","d15N"), 27 | factors=c("Region","Pack"), 28 | fac_random=c(TRUE,TRUE), 29 | fac_nested=c(FALSE,TRUE), 30 | cont_effects=NULL) 31 | ``` 32 | 33 | load the Source data 34 | 35 | ```{r} 36 | # Load the source data 37 | source = load_source_data( 38 | filename="wolves_data/wolves_sources.csv", 39 | source_factors="Region", 40 | conc_dep=FALSE, 41 | data_type="means", 42 | mix) 43 | ``` 44 | 45 | Load the trophic discrimination factors. 46 | 47 | ```{r} 48 | # Load the discrimination/TDF data 49 | discr = load_discr_data( 50 | filename="wolves_data/wolves_discrimination.csv", 51 | mix) 52 | ``` 53 | 54 | ## Plot the data 55 | 56 | ```{r} 57 | # Isospace plot 58 | plot_data(filename="isospace_plot", 59 | plot_save_pdf = FALSE, 60 | plot_save_png = FALSE, 61 | mix = mix, 62 | source = source, 63 | discr = discr) 64 | 65 | ``` 66 | 67 | Plot the same data but with a colour-blind friendly palette and hide the legend. 68 | 69 | ```{r} 70 | # Alternative plot saving it and editing it 71 | p <- plot_data(filename="isospace_plot", 72 | plot_save_pdf = FALSE, 73 | plot_save_png = FALSE, 74 | mix = mix, 75 | source = source, 76 | discr = discr, return_obj = TRUE) 77 | 78 | # We can now augment the ggplot object with our own 79 | # customisations. 80 | p + 81 | theme(legend.position = "None") + 82 | scale_color_viridis_d() 83 | ``` 84 | 85 | ## Use MixSIAR to construct the JAGS model 86 | 87 | ```{r} 88 | # Write the model out 89 | model_filename = "MixSIAR_model.txt" # Name of the JAGS model file 90 | write_JAGS_model(model_filename, 91 | resid_err = TRUE, 92 | process_err = TRUE, 93 | mix, source) 94 | ``` 95 | 96 | ## Run a very short test run of the model 97 | 98 | ```{r} 99 | # Test run 100 | jags.1 = run_model(run="test", 101 | mix, source, discr, 102 | model_filename, 103 | alpha.prior = c(1,1,1), 104 | resid_err = TRUE, 105 | process_err = TRUE) 106 | ``` 107 | 108 | 109 | ## Run a longer version but still very short 110 | 111 | N.B. This is not evaluated by default in case it takes a long time on your machine. Change chunk option `eval = TRUE` or copy and paste to the console window. 112 | 113 | ```{r, eval = FALSE} 114 | # Long run - change run to 'normal' 115 | # if you have 5 hours to kill 116 | jags.2 = run_model(run="very short", 117 | mix, source, discr, 118 | model_filename, 119 | alpha.prior = 1, 120 | resid_err = TRUE, 121 | process_err = TRUE) 122 | ``` 123 | 124 | ## Explore the output 125 | 126 | ```{r} 127 | out <- output_JAGS(jags.1, 128 | mix = mix, 129 | source = source, 130 | c('summary_diagnostics')) 131 | ``` 132 | 133 | Print and plot all (most?) of the optional output. 134 | 135 | N.B. not run by default. Change chunk option `eval = TRUE` or copy and paste to the console window. 136 | 137 | ```{r} 138 | out <- output_JAGS(jags.1, 139 | mix = mix, 140 | source = source, 141 | c('summary_diagnostics', 142 | 'summary_statistics', 143 | 'summary_quantiles', 144 | 'plot_global', 145 | 'plot_factors')) 146 | ``` 147 | 148 | 149 | 150 | 151 | 152 | 153 | -------------------------------------------------------------------------------- /ap_notes/prac_mixsiar_and_jags/prac_mixsiar_and_jags.pdf: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/andrewcparnell/simms_course/3052327e05aa93897fcfa31e2e8dd49fa3e45708/ap_notes/prac_mixsiar_and_jags/prac_mixsiar_and_jags.pdf -------------------------------------------------------------------------------- /ap_notes/prac_mixsiar_and_jags/wolves_data/wolves_consumer.csv: -------------------------------------------------------------------------------- 1 | d13C,d15N,Region,Pack 2 | -23.68,7.96,1,1 3 | -23.61,7.78,1,1 4 | -23.76,7.72,1,1 5 | -23.61,7.77,1,1 6 | -24.37,7.33,1,1 7 | -23.93,7.65,1,1 8 | -23.88,7.46,1,1 9 | -23.66,6.82,1,1 10 | -23.59,6.44,1,1 11 | -22.79,8.31,1,2 12 | -21.08,8.2,1,2 13 | -20.42,8.89,1,2 14 | -20.81,8.88,1,2 15 | -21.04,8.71,1,2 16 | -22.95,7.41,1,2 17 | -23.01,8.56,1,2 18 | -23.45,8.36,1,2 19 | -22.33,8.63,1,3 20 | -23.61,7.28,1,3 21 | -22.8,7.12,1,3 22 | -22.96,6.71,1,3 23 | -19.82,10.86,2,4 24 | -18.07,13.14,2,4 25 | -20.7,10.05,2,4 26 | -19.37,11.06,2,4 27 | -21.14,9.01,2,4 28 | -21.25,9.63,2,4 29 | -20.98,10.25,2,4 30 | -20.4,10.79,2,4 31 | -19.96,11.03,2,4 32 | -23.34,6.9,2,4 33 | -23.06,7.57,2,4 34 | -22.1,8.2,2,4 35 | -20.81,9.89,2,4 36 | -19.75,10.74,2,4 37 | -17.73,13.66,2,5 38 | -18.24,13.09,2,5 39 | -18.74,12.47,2,5 40 | -19.23,11.95,2,5 41 | -16.78,14.29,2,5 42 | -21.17,10.5,2,5 43 | -20.7,10.98,2,5 44 | -21.07,10.32,2,5 45 | -19.07,12.14,2,5 46 | -21.53,10.23,2,5 47 | -18.32,12.87,2,5 48 | -17.32,13.8,2,5 49 | -18.39,13.03,2,5 50 | -23.48,8.1,2,5 51 | -22.82,9.1,2,5 52 | -22.19,9.77,2,5 53 | -22.35,7.31,2,6 54 | -21.7,8.19,2,6 55 | -22.25,7.9,2,6 56 | -22.57,7.62,2,6 57 | -22.08,8.18,2,6 58 | -21.73,8.36,2,6 59 | -17.56,13.26,2,7 60 | -17.68,12.74,2,7 61 | -18.43,13.16,2,7 62 | -18.12,12.68,2,7 63 | -17.17,13.9,3,8 64 | -18.21,13.17,3,8 65 | -16.54,14.32,3,8 66 | -17.47,13.99,3,8 67 | -19.01,12.3,3,8 68 | -------------------------------------------------------------------------------- /ap_notes/prac_mixsiar_and_jags/wolves_data/wolves_discrimination.csv: -------------------------------------------------------------------------------- 1 | ,Meand13C,SDd13C,Meand15N,SDd15N 2 | Deer,2.1,0,2.6,0 3 | Salmon,2.1,0,2.6,0 4 | Marine Mammals,2.1,0,2.6,0 5 | -------------------------------------------------------------------------------- /ap_notes/prac_mixsiar_and_jags/wolves_data/wolves_sources.csv: -------------------------------------------------------------------------------- 1 | ,Region,Meand13C,SDd13C,Meand15N,SDd15N,n 2 | Deer,1,-26.88,1.1,3.07,1.35,24 3 | Deer,2,-27.15,0.67,2.8,1.14,37 4 | Deer,3,-27.47,0.75,2.76,2.32,9 5 | Salmon,1,-18.58,1.34,12.26,1.18,6 6 | Salmon,2,-22.38,2.85,11.92,1.12,5 7 | Salmon,3,-22.38,2.85,11.92,1.12,5 8 | Marine Mammals,1,-14.7,1.08,16.26,1.06,7 9 | Marine Mammals,2,-14.47,0.95,15.55,1.69,6 10 | Marine Mammals,3,-12.48,0.75,16.21,1.78,6 11 | -------------------------------------------------------------------------------- /ap_notes/prac_using_simmr/geese_data.xls: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/andrewcparnell/simms_course/3052327e05aa93897fcfa31e2e8dd49fa3e45708/ap_notes/prac_using_simmr/geese_data.xls -------------------------------------------------------------------------------- /ap_notes/prac_using_simmr/prac_using_simmr.pdf: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/andrewcparnell/simms_course/3052327e05aa93897fcfa31e2e8dd49fa3e45708/ap_notes/prac_using_simmr/prac_using_simmr.pdf -------------------------------------------------------------------------------- /ap_notes/prac_using_simmr/quick_start.Rmd: -------------------------------------------------------------------------------- 1 | --- 2 | title: "simmr: quick start guide" 3 | author: "Andrew Parnell" 4 | date: "`r Sys.Date()`" 5 | output: rmarkdown::html_vignette 6 | vignette: > 7 | %\VignetteIndexEntry{Vignette Title} 8 | %\VignetteEngine{knitr::rmarkdown} 9 | %\VignetteEncoding{UTF-8} 10 | --- 11 | 12 | ## Step 1: install `simmr` 13 | 14 | Use: 15 | ```{r, eval = FALSE} 16 | install.packages('simmr') 17 | ``` 18 | 19 | then 20 | ```{r, message=FALSE} 21 | library(simmr) 22 | ``` 23 | 24 | ## Step 2: load in the data 25 | 26 | Some geese isotope data is included with this package. Find where it is with: 27 | ```{r, eval = FALSE} 28 | system.file("extdata", "geese_data.xls", package = "simmr") 29 | ``` 30 | 31 | Load into R with: 32 | ```{r, echo = FALSE} 33 | if (!requireNamespace("readxl", quietly = TRUE)) { 34 | stop("readxl needed for this vignette to work. Please install it.", 35 | call. = FALSE) 36 | } 37 | ``` 38 | ```{r} 39 | library(readxl) 40 | path = system.file("extdata", "geese_data.xls", package = "simmr") 41 | geese_data = lapply(excel_sheets(path), read_excel, path = path) 42 | ``` 43 | 44 | Can now separate out the data into parts 45 | ```{r} 46 | targets = geese_data[[1]] 47 | sources = geese_data[[2]] 48 | TEFs = geese_data[[3]] 49 | concdep = geese_data[[4]] 50 | ``` 51 | 52 | ## Step 3: load the data into `simmr` 53 | 54 | ```{r} 55 | geese_simmr = simmr_load(mixtures = as.matrix(targets[, 1:2]), 56 | source_names = sources$Sources, 57 | source_means = sources[,2:3], 58 | source_sds = sources[,4:5], 59 | correction_means = TEFs[,2:3], 60 | correction_sds = TEFs[,4:5], 61 | concentration_means = concdep[,2:3], 62 | group = as.factor(paste('day', targets$Time))) 63 | ``` 64 | 65 | ## Step 4: plot the data 66 | 67 | ```{r, fig.width = 8, fig.height = 8} 68 | plot(geese_simmr, group = 1:8) 69 | ``` 70 | 71 | ## Step 5: run through `simmr` 72 | 73 | ```{r, results = 'hide', message = FALSE} 74 | geese_simmr_out = simmr_mcmc(geese_simmr) 75 | ``` 76 | 77 | ## Step 6: look at the output 78 | 79 | ```{r, fig.width = 8, fig.height = 8} 80 | compare_groups(geese_simmr_out, groups = 1:4, 81 | source_name = 'Enteromorpha') 82 | ``` 83 | 84 | For the many more options available to run and analyse output, see the main vignette via `vignette('simmr')` 85 | -------------------------------------------------------------------------------- /ap_notes/prac_using_simmr/simmr_from_excel.R: -------------------------------------------------------------------------------- 1 | # A simmr example from an Excel data set 2 | 3 | # Clear workspace 4 | rm(list = ls()) 5 | 6 | # Load packages 7 | library(simmr) 8 | library(readxl) # Run install.packages('readxl') if you don't have this 9 | library(httr) # Again run install.packages if you don't have this 10 | 11 | # Load in the data 12 | 13 | # Go to the files pane and find the file 'geese_data.xls' 14 | # Then click (in the files pane) on More > Set as WD 15 | # Copy the command from the console window into the line below 16 | 17 | # Find out what the sheet names are and load in all of them 18 | sheet_names = excel_sheets(path = 'ap_notes/prac_using_simmr/geese_data.xls') 19 | all = lapply(sheet_names, 20 | read_excel, path = 'ap_notes/prac_using_simmr/geese_data.xls') 21 | 22 | # Extract out the different pieces 23 | mix = all[[1]] 24 | source = all[[2]] 25 | TDF = all[[3]] 26 | Conc = all[[4]] 27 | 28 | # Get the data into simmr 29 | simmr_groups = simmr_load(mixtures=as.matrix(mix[,1:2]), 30 | source_names=unlist(source[,1]), 31 | source_means=as.matrix(source[,2:3]), 32 | source_sds=as.matrix(source[,4:5]), 33 | correction_means=as.matrix(TDF[,2:3]), 34 | correction_sds=as.matrix(TDF[,4:5]), 35 | concentration_means = as.matrix(Conc[,2:3]), 36 | group=paste('day', mix$Time)) 37 | 38 | # Plot the iso-space plot 39 | plot(simmr_groups, 40 | group=1:8, 41 | xlab=expression(paste(delta^13, "C (\u2030)",sep="")), 42 | ylab=expression(paste(delta^15, "N (\u2030)",sep="")), 43 | title='Isospace plot of Inger et al Geese data', 44 | mix_name='Geese') 45 | 46 | # RUN THE MODEL 47 | simmr_groups_out = simmr_mcmc(simmr_groups) 48 | 49 | # Check convergence 50 | summary(simmr_groups_out, type = 'diagnostics', group = 1:8) 51 | # Some of these are not good - longer run? 52 | 53 | # Example plots 54 | plot(simmr_groups_out, 55 | type = 'boxplot', 56 | group = 2, 57 | title = 'simmr output group 2') 58 | plot(simmr_groups_out, 59 | type = c('density', 'matrix'), 60 | group = 6, 61 | title = 'simmr output group 6') 62 | 63 | 64 | -------------------------------------------------------------------------------- /ap_notes/prac_using_simmr/simmr_with_priors.R: -------------------------------------------------------------------------------- 1 | # Taken from the help(simmr_elicit) file 2 | 3 | # Clear workspace and load in packages 4 | rm(list = ls()) 5 | library(simmr) 6 | 7 | # (Fake) data set: 10 observations, 2 tracers, 4 sources 8 | mix = matrix(c(-10.13, -10.72, -11.39, -11.18, -10.81, -10.7, -10.54, 9 | -10.48, -9.93, -9.37, 11.59, 11.01, 10.59, 10.97, 11.52, 11.89, 10 | 11.73, 10.89, 11.05, 12.3), ncol=2, nrow=10) 11 | colnames(mix) = c('d13C','d15N') 12 | s_names=c('Source A','Source B','Source C','Source D') 13 | s_means = matrix(c(-14, -15.1, -11.03, -14.44, 3.06, 7.05, 13.72, 5.96), ncol=2, nrow=4) 14 | s_sds = matrix(c(0.48, 0.38, 0.48, 0.43, 0.46, 0.39, 0.42, 0.48), ncol=2, nrow=4) 15 | c_means = matrix(c(2.63, 1.59, 3.41, 3.04, 3.28, 2.34, 2.14, 2.36), ncol=2, nrow=4) 16 | c_sds = matrix(c(0.41, 0.44, 0.34, 0.46, 0.46, 0.48, 0.46, 0.66), ncol=2, nrow=4) 17 | conc = matrix(c(0.02, 0.1, 0.12, 0.04, 0.02, 0.1, 0.09, 0.05), ncol=2, nrow=4) 18 | 19 | # Load into simmr 20 | simmr_1 = simmr_load(mixtures=mix, 21 | source_names=s_names, 22 | source_means=s_means, 23 | source_sds=s_sds, 24 | correction_means=c_means, 25 | correction_sds=c_sds, 26 | concentration_means = conc) 27 | 28 | # Iso-space plot 29 | plot(simmr_1) 30 | 31 | # MCMC run 32 | simmr_1_out = simmr_mcmc(simmr_1) 33 | 34 | # Summary 35 | summary(simmr_1_out,'quantiles') 36 | # A bit vague: 37 | # 2.5 38 | # Source A 0.029 0.115 0.203 0.312 0.498 39 | # Source B 0.146 0.232 0.284 0.338 0.453 40 | # Source C 0.216 0.255 0.275 0.296 0.342 41 | # Source D 0.032 0.123 0.205 0.299 0.465 42 | 43 | # Look at prior/posterior 44 | prior_viz(simmr_1_out) 45 | 46 | # Now suppose I had prior information that: 47 | # proportion means = 0.5,0.2,0.2,0.1 48 | # proportion sds = 0.08,0.02,0.01,0.02 49 | prior=simmr_elicit(n_sources = 4, 50 | c(0.5,0.2,0.2,0.1), 51 | c(0.08,0.02,0.01,0.02)) 52 | 53 | simmr_1a_out = simmr_mcmc(simmr_1, 54 | prior_control=list(means=prior$mean, 55 | sd=prior$sd, 56 | sigma_shape = c(3, 3), 57 | sigma_rate = c(3/50, 3/50))) 58 | 59 | summary(simmr_1a_out,'quantiles') 60 | # Much more precise: 61 | # 2.5 62 | # Source A 0.441 0.494 0.523 0.553 0.610 63 | # Source B 0.144 0.173 0.188 0.204 0.236 64 | # Source C 0.160 0.183 0.196 0.207 0.228 65 | # Source D 0.060 0.079 0.091 0.105 0.135 66 | 67 | # Look at prior/posterior 68 | prior_viz(simmr_1a_out) 69 | -------------------------------------------------------------------------------- /ap_notes/reg_and_simms/geese1demo.rda: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/andrewcparnell/simms_course/3052327e05aa93897fcfa31e2e8dd49fa3e45708/ap_notes/reg_and_simms/geese1demo.rda -------------------------------------------------------------------------------- /ap_notes/reg_and_simms/reg_and_simms.pdf: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/andrewcparnell/simms_course/3052327e05aa93897fcfa31e2e8dd49fa3e45708/ap_notes/reg_and_simms/reg_and_simms.pdf -------------------------------------------------------------------------------- /ap_notes/reg_and_simms/reg_and_simms_aj_playing.Rmd: -------------------------------------------------------------------------------- 1 | 2 | 3 | ## A simple SIMM in JAGS 4 | 5 | This basic example attempts to explore the (minor) discrepancy between prior and posterior distributions on the sources themselves. 6 | 7 | plot the data 8 | 9 | ```{r} 10 | # Load in the data 11 | data("geese_data_day1") 12 | consumers = geese_data_day1$mixtures[,1] 13 | source_means = geese_data_day1$source_means[1:2,1] 14 | source_sds = geese_data_day1$source_sds[1:2,1] 15 | con_grid = seq(-35,-5,length=500) 16 | plot(con_grid,dnorm(con_grid, 17 | mean=source_means[2],sd=source_sds[2]), 18 | type='l',col='red',xlab='d13C',ylab='Probability density') 19 | lines(con_grid,dnorm(con_grid 20 | ,mean=source_means[1],sd=source_sds[1]), 21 | col='blue') 22 | points(consumers,rep(0,9)) 23 | legend('topright',legend=c('Grass','Zostera','Consumers'), 24 | lty=c(1,1,-1),pch=c(-1,-1,1),col=c('red','blue','black')) 25 | ``` 26 | 27 | 28 | ```{r,results='hide'} 29 | 30 | 31 | model_code =' 32 | model { 33 | for(i in 1:N) { 34 | y[i] ~ dnorm(p_1*s_1+p_2*s_2,sigma^-2) 35 | } 36 | p_1 ~ dunif(0,1) 37 | p_2 <- 1-p_1 38 | s_1 ~ dnorm(s_1_mean,s_1_sd^-2) 39 | s_2 ~ dnorm(s_2_mean,s_2_sd^-2) 40 | sigma ~ dunif(0,10) 41 | } 42 | ' 43 | data=list(y=consumers,s_1_mean=source_means[1], 44 | s_1_sd=source_sds[1], 45 | s_2_mean=source_means[2],s_2_sd=source_sds[2], 46 | N=length(consumers)) 47 | model_parameters = c('p_1', 'p_2', "s_1", "s_2") 48 | model_run = jags(data = data, 49 | parameters.to.save = model_parameters, 50 | model.file = textConnection(model_code), 51 | n.iter = 20000, 52 | n.thin = 10) 53 | ``` 54 | 55 | ### Andrew Playing 56 | 57 | In the chunk above i have included monitors for `s_1` and `s_2`. Since these are effectively priors, we can look at their posterior. What is interesting i think is that although there is no data directly relating to these priors, they are informed by the mixing model itself, and so the posterior distribution of their estimates are not the same. In this case, presumably, the fact that the consumers sit very close to source 2 (zostera), it tends to pull it even closer? 58 | 59 | Granted they are very close, but I wonder whether the effect can be larger for different geometries or sample sizes, and whether this has any bearing on interpreation at all? 60 | 61 | In this example i increased the numebr of posterior draws and thinned it hard to make sure the posteriors were solid. 62 | 63 | 64 | ```{r post-source-means} 65 | 66 | prior_s1_mean = source_means[1] 67 | prior_s2_mean = source_means[2] 68 | 69 | post_s1_mean = mean(model_run$BUGSoutput$sims.list$s_1) 70 | post_s2_mean = mean(model_run$BUGSoutput$sims.list$s_2) 71 | 72 | 73 | test_out <- matrix(c(prior_s1_mean, prior_s2_mean, post_s1_mean, post_s2_mean), 74 | ncol = 2, nrow = 2) 75 | 76 | colnames(test_out) <- c("prior", "post") 77 | rownames(test_out) <- c("s1_mean", "s2_mean") 78 | 79 | print(test_out, digits = 4) 80 | 81 | ``` 82 | 83 | In fact, the discrepancy between the prior and posterior estimates for source 1 sd are more substantial. 84 | 85 | ```{r post-source-sds} 86 | 87 | prior_s1_sd = source_sds[1] 88 | prior_s2_sd = source_sds[2] 89 | 90 | post_s1_sd = sd(model_run$BUGSoutput$sims.list$s_1) 91 | post_s2_sd = sd(model_run$BUGSoutput$sims.list$s_2) 92 | 93 | 94 | test_out_sd <- matrix(c(prior_s1_sd, prior_s2_sd, post_s1_sd, post_s2_sd), 95 | ncol = 2, nrow = 2) 96 | 97 | colnames(test_out_sd) <- c("prior", "post") 98 | rownames(test_out_sd) <- c("s1_sd", "s2_sd") 99 | 100 | print(test_out_sd, digits = 4) 101 | 102 | ``` 103 | 104 | 105 | 106 | -------------------------------------------------------------------------------- /ap_notes/reg_and_simms/sourcesdemo.rda: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/andrewcparnell/simms_course/3052327e05aa93897fcfa31e2e8dd49fa3e45708/ap_notes/reg_and_simms/sourcesdemo.rda -------------------------------------------------------------------------------- /ap_notes/revision_of_likelihood_regression/revision_of_likelihood_and_regression.pdf: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/andrewcparnell/simms_course/3052327e05aa93897fcfa31e2e8dd49fa3e45708/ap_notes/revision_of_likelihood_regression/revision_of_likelihood_and_regression.pdf -------------------------------------------------------------------------------- /ap_notes/siar_mixsiar/siar_mixsiar.pdf: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/andrewcparnell/simms_course/3052327e05aa93897fcfa31e2e8dd49fa3e45708/ap_notes/siar_mixsiar/siar_mixsiar.pdf -------------------------------------------------------------------------------- /ap_notes/siar_stats/siar_stats.pdf: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/andrewcparnell/simms_course/3052327e05aa93897fcfa31e2e8dd49fa3e45708/ap_notes/siar_stats/siar_stats.pdf -------------------------------------------------------------------------------- /extra_code/DIY_isospace_plot.R: -------------------------------------------------------------------------------- 1 | # Create an iso-space plot for the geese data from siar 2 | 3 | # Note: for simplicity this ignores concentration dependence and enrichment factors 4 | 5 | library(siar) 6 | data("geese1demo",'sourcesdemo') 7 | 8 | # First create plot of sources based on means and 95% confidence intervals 9 | conf_level = 1.96 10 | d15N_low = sourcesdemo$Meand15N - conf_level*sourcesdemo$SDd15N 11 | d15N_high = sourcesdemo$Meand15N + conf_level*sourcesdemo$SDd15N 12 | d13C_low = sourcesdemo$Meand13C - conf_level*sourcesdemo$SDd13C 13 | d13C_high = sourcesdemo$Meand13C + conf_level*sourcesdemo$SDd13C 14 | 15 | # Find the range of values for the plotting limits 16 | d13C_low_all = min(d13C_low) 17 | d13C_high_all = max(d13C_high) 18 | d15N_low_all = min(d15N_low) 19 | d15N_high_all = max(d15N_high) 20 | 21 | # Set up a basic plot 22 | plot(1,1,type='n',xlab='d13C',ylab='d15N',xlim=c(d13C_low_all,d13C_high_all),ylim=c(d15N_low_all,d15N_high_all),las=1,bty='l') 23 | 24 | # Loop through sources to create plusses 25 | n_sources = nrow(sourcesdemo) 26 | for(i in 1:n_sources) { 27 | # Create horizontal lines 28 | lines(c(d13C_low[i],d13C_high[i]),c(sourcesdemo$Meand15N[i],sourcesdemo$Meand15N[i]),col=i) 29 | # Create vertical lines 30 | lines(c(sourcesdemo$Meand13C[i],sourcesdemo$Meand13C[i]),c(d15N_low[i],d15N_high[i]),col=i) 31 | # Add the means 32 | points(sourcesdemo$Meand13C[i],sourcesdemo$Meand15N[i],col=i,pch=i,cex=2) 33 | } 34 | 35 | 36 | # Add the consumers as dots 37 | points(geese1demo[,2],geese1demo[,1],pch=19) 38 | 39 | # Alternativel add them as a plus 40 | geese_means = apply(geese1demo,2,function(x) return(c(mean(x),sd(x)))) 41 | 42 | # Now create low and high values as above 43 | geese15N_low = geese_means[1,1] - conf_level*geese_means[2,1] 44 | geese15N_high = geese_means[1,1] + conf_level*geese_means[2,1] 45 | geese13C_low = geese_means[1,2] - conf_level*geese_means[2,2] 46 | geese13C_high = geese_means[1,2] + conf_level*geese_means[2,2] 47 | 48 | # Now plot lines - horizontal 49 | lines(c(geese13C_low,geese13C_high),c(geese_means[1,1],geese_means[1,1]),lwd=3) 50 | # Create vertical lines 51 | lines(c(geese_means[1,2],geese_means[1,2]),c(geese15N_low,geese15N_high),lwd=3) 52 | 53 | 54 | 55 | 56 | -------------------------------------------------------------------------------- /extra_code/mixsiar_examples/cladocera.R: -------------------------------------------------------------------------------- 1 | ## ---- eval=FALSE-------------------------------------------------------------- 2 | # library(MixSIAR) 3 | # mixsiar.dir <- find.package("MixSIAR") 4 | # paste0(mixsiar.dir,"/example_scripts") 5 | 6 | ## ---- eval=FALSE-------------------------------------------------------------- 7 | # source(paste0(mixsiar.dir,"/example_scripts/mixsiar_script_cladocera.R")) 8 | 9 | ## ----------------------------------------------------------------------------- 10 | library(MixSIAR) 11 | library(tidyr) # For pivoting later in new output_jags 12 | library(ggplot2) 13 | library(GGally) 14 | 15 | # My new output function 16 | source("extra_code/output_jags_new.R") 17 | 18 | ## ----------------------------------------------------------------------------- 19 | # Replace the system.file call with the path to your file 20 | mix.filename <- system.file("extdata", "cladocera_consumer.csv", package = "MixSIAR") 21 | 22 | mix <- load_mix_data(filename=mix.filename, 23 | iso_names=c("c14.0","c16.0","c16.1w9","c16.1w7","c16.2w4", 24 | "c16.3w3","c16.4w3","c17.0","c18.0","c18.1w9", 25 | "c18.1w7","c18.2w6","c18.3w6","c18.3w3","c18.4w3", 26 | "c18.5w3","c20.0","c22.0","c20.4w6","c20.5w3", 27 | "c22.6w3","BrFA"), 28 | factors="id", 29 | fac_random=FALSE, 30 | fac_nested=FALSE, 31 | cont_effects=NULL) 32 | 33 | ## ----------------------------------------------------------------------------- 34 | # Replace the system.file call with the path to your file 35 | source.filename <- system.file("extdata", "cladocera_sources.csv", package = "MixSIAR") 36 | 37 | source <- load_source_data(filename=source.filename, 38 | source_factors=NULL, 39 | conc_dep=FALSE, 40 | data_type="means", 41 | mix) 42 | 43 | ## ----------------------------------------------------------------------------- 44 | # Replace the system.file call with the path to your file 45 | discr.filename <- system.file("extdata", "cladocera_discrimination.csv", package = "MixSIAR") 46 | 47 | discr <- load_discr_data(filename=discr.filename, mix) 48 | 49 | ## ---- eval=FALSE-------------------------------------------------------------- 50 | # # default "UNINFORMATIVE" / GENERALIST prior (alpha = 1) 51 | # plot_prior(alpha.prior=1,source) 52 | 53 | ## ---- eval=FALSE-------------------------------------------------------------- 54 | # # Write the JAGS model file 55 | model_filename <- "MixSIAR_model.txt" 56 | resid_err <- FALSE 57 | process_err <- TRUE 58 | write_JAGS_model(model_filename, resid_err, process_err, mix, source) 59 | 60 | ## ---- eval=FALSE-------------------------------------------------------------- 61 | jags.1 <- run_model(run="test", mix, source, discr, model_filename) 62 | 63 | ## ---- eval=FALSE-------------------------------------------------------------- 64 | # jags.1 <- run_model(run="normal", mix, source, discr, model_filename) 65 | 66 | ## ---- eval=FALSE-------------------------------------------------------------- 67 | # output_JAGS(jags.1, mix, source, output_options) 68 | output_JAGS(jags.1, mix = mix, source = source, 69 | c('summary_diagnostics', 70 | 'summary_statistics', 71 | 'summary_quantiles', 72 | 'plot_global', 73 | 'plot_global_matrix', 74 | 'plot_factors')) 75 | -------------------------------------------------------------------------------- /extra_code/mixsiar_examples/geese.R: -------------------------------------------------------------------------------- 1 | ## ---- eval=FALSE-------------------------------------------------------------- 2 | # library(MixSIAR) 3 | # mixsiar.dir <- find.package("MixSIAR") 4 | # paste0(mixsiar.dir,"/example_scripts") 5 | 6 | ## ---- eval=FALSE-------------------------------------------------------------- 7 | # source(paste0(mixsiar.dir,"/example_scripts/mixsiar_script_geese.R")) 8 | 9 | ## ----------------------------------------------------------------------------- 10 | library(MixSIAR) 11 | library(tidyr) # For pivoting later in new output_jags 12 | library(ggplot2) 13 | library(GGally) 14 | 15 | # My new output function 16 | source("extra_code/output_jags_new.R") 17 | 18 | ## ----------------------------------------------------------------------------- 19 | # Replace the system.file call with the path to your file 20 | mix.filename <- system.file("extdata", "geese_consumer.csv", package = "MixSIAR") 21 | 22 | mix <- load_mix_data(filename=mix.filename, 23 | iso_names=c("d13C","d15N"), 24 | factors="Group", 25 | fac_random=FALSE, 26 | fac_nested=FALSE, 27 | cont_effects=NULL) 28 | 29 | ## ----------------------------------------------------------------------------- 30 | # Replace the system.file call with the path to your file 31 | source.filename <- system.file("extdata", "geese_sources.csv", package = "MixSIAR") 32 | 33 | source <- load_source_data(filename=source.filename, 34 | source_factors=NULL, 35 | conc_dep=TRUE, 36 | data_type="means", 37 | mix) 38 | 39 | ## ----------------------------------------------------------------------------- 40 | # Replace the system.file call with the path to your file 41 | discr.filename <- system.file("extdata", "geese_discrimination.csv", package = "MixSIAR") 42 | 43 | discr <- load_discr_data(filename=discr.filename, mix) 44 | 45 | ## ---- eval=FALSE-------------------------------------------------------------- 46 | # # Make an isospace plot 47 | # plot_data(filename="isospace_plot", plot_save_pdf=TRUE, plot_save_png=FALSE, mix,source,discr) 48 | 49 | ## ----------------------------------------------------------------------------- 50 | # Calculate the convex hull area, standardized by source variance 51 | calc_area(source=source,mix=mix,discr=discr) 52 | 53 | ## ---- eval=FALSE-------------------------------------------------------------- 54 | # # default "UNINFORMATIVE" / GENERALIST prior (alpha = 1) 55 | # plot_prior(alpha.prior=1,source) 56 | 57 | ## ---- eval=FALSE-------------------------------------------------------------- 58 | # # Write the JAGS model file 59 | model_filename <- "MixSIAR_model.txt" 60 | resid_err <- TRUE 61 | process_err <- FALSE 62 | write_JAGS_model(model_filename, resid_err, process_err, mix, source) 63 | 64 | ## ---- eval=FALSE-------------------------------------------------------------- 65 | jags.1 <- run_model(run="test", mix, source, discr, model_filename, 66 | alpha.prior = 1, resid_err, process_err) 67 | 68 | ## ---- eval=FALSE-------------------------------------------------------------- 69 | # jags.1 <- run_model(run="short", mix, source, discr, model_filename, 70 | # alpha.prior = 1, resid_err, process_err) 71 | 72 | ## ---- eval=FALSE-------------------------------------------------------------- 73 | # output_JAGS(jags.1, mix, source, output_options) 74 | output_JAGS(jags.1, mix = mix, source = source, 75 | c('summary_diagnostics', 76 | 'summary_statistics', 77 | 'summary_quantiles', 78 | 'plot_global', 79 | 'plot_factors'), 80 | search_par = "p.") 81 | -------------------------------------------------------------------------------- /extra_code/mixsiar_examples/geese2.R: -------------------------------------------------------------------------------- 1 | ## ---- eval=FALSE-------------------------------------------------------------- 2 | # library(MixSIAR) 3 | # mixsiar.dir <- find.package("MixSIAR") 4 | # paste0(mixsiar.dir,"/example_scripts") 5 | 6 | ## ---- eval=FALSE-------------------------------------------------------------- 7 | # source(paste0(mixsiar.dir,"/example_scripts/mixsiar_script_geese.R")) 8 | 9 | ## ----------------------------------------------------------------------------- 10 | library(MixSIAR) 11 | library(tidyr) # For pivoting later in new output_jags 12 | library(ggplot2) 13 | library(GGally) 14 | 15 | # My new output function 16 | source("extra_code/output_jags_new.R") 17 | 18 | ## ----------------------------------------------------------------------------- 19 | # Replace the system.file call with the path to your file 20 | #mix.filename <- system.file("extdata", "geese_consumer.csv", package = "MixSIAR") 21 | mix.filename <- "extra_code/mixsiar_examples/geese_consumer.csv" 22 | 23 | mix <- load_mix_data(filename=mix.filename, 24 | iso_names=c("d13C","d15N"), 25 | factors="Group", 26 | fac_random=TRUE, 27 | fac_nested=FALSE, 28 | cont_effects="Net.Wt") 29 | mix$data <- mix$data[,c(1:3,9)] 30 | 31 | ## ----------------------------------------------------------------------------- 32 | # Replace the system.file call with the path to your file 33 | source.filename <- system.file("extdata", "geese_sources.csv", package = "MixSIAR") 34 | 35 | source <- load_source_data(filename=source.filename, 36 | source_factors=NULL, 37 | conc_dep=TRUE, 38 | data_type="means", 39 | mix) 40 | 41 | ## ----------------------------------------------------------------------------- 42 | # Replace the system.file call with the path to your file 43 | discr.filename <- system.file("extdata", "geese_discrimination.csv", package = "MixSIAR") 44 | 45 | discr <- load_discr_data(filename=discr.filename, mix) 46 | 47 | ## ---- eval=FALSE-------------------------------------------------------------- 48 | # # Make an isospace plot 49 | # plot_data(filename="isospace_plot", plot_save_pdf=TRUE, plot_save_png=FALSE, mix,source,discr) 50 | 51 | ## ----------------------------------------------------------------------------- 52 | # Calculate the convex hull area, standardized by source variance 53 | calc_area(source=source,mix=mix,discr=discr) 54 | 55 | ## ---- eval=FALSE-------------------------------------------------------------- 56 | # # default "UNINFORMATIVE" / GENERALIST prior (alpha = 1) 57 | # plot_prior(alpha.prior=1,source) 58 | 59 | ## ---- eval=FALSE-------------------------------------------------------------- 60 | # # Write the JAGS model file 61 | model_filename <- "MixSIAR_model.txt" 62 | resid_err <- TRUE 63 | process_err <- FALSE 64 | write_JAGS_model(model_filename, resid_err, process_err, mix, source) 65 | 66 | ## ---- eval=FALSE-------------------------------------------------------------- 67 | jags.1 <- run_model(run="test", mix, source, discr, model_filename, 68 | alpha.prior = 1, resid_err, process_err) 69 | 70 | ## ---- eval=FALSE-------------------------------------------------------------- 71 | # jags.1 <- run_model(run="short", mix, source, discr, model_filename, 72 | # alpha.prior = 1, resid_err, process_err) 73 | 74 | ## ---- eval=FALSE-------------------------------------------------------------- 75 | # output_JAGS(jags.1, mix, source, output_options) 76 | my_output <- output_JAGS(jags.1, mix = mix, source = source, 77 | c('summary_diagnostics', 78 | 'summary_statistics', 79 | 'summary_quantiles', 80 | 'plot_global', 81 | 'plot_global_matrix', 82 | 'plot_factors')) 83 | 84 | # To get the full list of parameters use: 85 | rownames(my_output$summary_statistics) 86 | -------------------------------------------------------------------------------- /extra_code/mixsiar_examples/isopod.R: -------------------------------------------------------------------------------- 1 | ## ---- eval=FALSE-------------------------------------------------------------- 2 | # library(MixSIAR) 3 | # mixsiar.dir <- find.package("MixSIAR") 4 | # paste0(mixsiar.dir,"/example_scripts") 5 | 6 | ## ---- eval=FALSE-------------------------------------------------------------- 7 | # source(paste0(mixsiar.dir,"/example_scripts/mixsiar_script_isopod.R")) 8 | 9 | ## ----------------------------------------------------------------------------- 10 | library(MixSIAR) 11 | library(tidyr) # For pivoting later in new output_jags 12 | library(ggplot2) 13 | library(GGally) 14 | 15 | # My new output function 16 | source("extra_code/output_jags_new.R") 17 | 18 | ## ----------------------------------------------------------------------------- 19 | # Replace the system.file call with the path to your file 20 | mix.filename <- system.file("extdata", "isopod_consumer.csv", package = "MixSIAR") 21 | 22 | mix <- load_mix_data(filename=mix.filename, 23 | iso_names=c("c16.4w3","c18.2w6","c18.3w3","c18.4w3","c20.4w6","c20.5w3","c22.5w3","c22.6w3"), 24 | factors="Site", 25 | fac_random=TRUE, 26 | fac_nested=FALSE, 27 | cont_effects=NULL) 28 | 29 | ## ----------------------------------------------------------------------------- 30 | # Replace the system.file call with the path to your file 31 | source.filename <- system.file("extdata", "isopod_sources.csv", package = "MixSIAR") 32 | 33 | source <- load_source_data(filename=source.filename, 34 | source_factors=NULL, 35 | conc_dep=FALSE, 36 | data_type="means", 37 | mix) 38 | 39 | ## ----------------------------------------------------------------------------- 40 | # Replace the system.file call with the path to your file 41 | discr.filename <- system.file("extdata", "isopod_discrimination.csv", package = "MixSIAR") 42 | 43 | discr <- load_discr_data(filename=discr.filename, mix) 44 | 45 | ## ---- eval=FALSE-------------------------------------------------------------- 46 | # # Make an isospace plot 47 | # plot_data(filename="isospace_plot", plot_save_pdf=TRUE, plot_save_png=FALSE, mix,source,discr) 48 | 49 | ## ---- eval=FALSE-------------------------------------------------------------- 50 | # # default "UNINFORMATIVE" / GENERALIST prior (alpha = 1) 51 | # plot_prior(alpha.prior=1,source) 52 | 53 | ## ---- eval=FALSE-------------------------------------------------------------- 54 | # # Write the JAGS model file 55 | model_filename <- "MixSIAR_model.txt" 56 | resid_err <- TRUE 57 | process_err <- FALSE 58 | write_JAGS_model(model_filename, resid_err, process_err, mix, source) 59 | 60 | ## ---- eval=FALSE-------------------------------------------------------------- 61 | jags.1 <- run_model(run="test", mix, source, discr, model_filename) 62 | 63 | ## ---- eval=FALSE-------------------------------------------------------------- 64 | # jags.1 <- run_model(run="normal", mix, source, discr, model_filename) 65 | 66 | ## ---- eval=FALSE-------------------------------------------------------------- 67 | # output_JAGS(jags.1, mix, source, output_options) 68 | output_JAGS(jags.1, mix = mix, source = source, 69 | c('summary_diagnostics', 70 | 'summary_statistics', 71 | 'summary_quantiles', 72 | 'plot_global', 73 | 'plot_factors')) -------------------------------------------------------------------------------- /extra_code/mixsiar_examples/killerwhale.R: -------------------------------------------------------------------------------- 1 | ## ---- eval=FALSE-------------------------------------------------------------- 2 | # library(MixSIAR) 3 | # mixsiar.dir <- find.package("MixSIAR") 4 | # paste0(mixsiar.dir,"/example_scripts") 5 | 6 | ## ---- eval=FALSE-------------------------------------------------------------- 7 | # source(paste0(mixsiar.dir,"/example_scripts/mixsiar_script_killerwhale.R")) 8 | 9 | ## ----------------------------------------------------------------------------- 10 | library(MixSIAR) 11 | library(tidyr) # For pivoting later in new output_jags 12 | library(ggplot2) 13 | library(GGally) 14 | 15 | # My new output function 16 | source("extra_code/output_jags_new.R") 17 | 18 | ## ----------------------------------------------------------------------------- 19 | # Replace the system.file call with the path to your file 20 | mix.filename <- system.file("extdata", "killerwhale_consumer.csv", package = "MixSIAR") 21 | 22 | mix <- load_mix_data(filename=mix.filename, 23 | iso_names=c("d13C","d15N"), 24 | factors=NULL, 25 | fac_random=NULL, 26 | fac_nested=NULL, 27 | cont_effects=NULL) 28 | 29 | ## ----------------------------------------------------------------------------- 30 | # Replace the system.file call with the path to your file 31 | source.filename <- system.file("extdata", "killerwhale_sources.csv", package = "MixSIAR") 32 | 33 | source <- load_source_data(filename=source.filename, 34 | source_factors=NULL, 35 | conc_dep=FALSE, 36 | data_type="means", 37 | mix) 38 | 39 | ## ----------------------------------------------------------------------------- 40 | # Replace the system.file call with the path to your file 41 | discr.filename <- system.file("extdata", "killerwhale_discrimination.csv", package = "MixSIAR") 42 | 43 | discr <- load_discr_data(filename=discr.filename, mix) 44 | 45 | ## ---- eval=FALSE-------------------------------------------------------------- 46 | # # Make an isospace plot 47 | # plot_data(filename="isospace_plot", plot_save_pdf=TRUE, plot_save_png=FALSE, mix,source,discr) 48 | 49 | ## ---- eval=FALSE-------------------------------------------------------------- 50 | # # Plot uninformative prior 51 | # plot_prior(alpha.prior=1, source, filename = "prior_plot_kw_uninf") 52 | # 53 | # # Define model structure and write JAGS model file 54 | model_filename <- "MixSIAR_model_kw_uninf.txt" # Name of the JAGS model file 55 | resid_err <- TRUE 56 | process_err <- TRUE 57 | write_JAGS_model(model_filename, resid_err, process_err, mix, source) 58 | # 59 | # # Run the JAGS model ("very long" took ~5 min) 60 | jags.uninf <- run_model(run="test",mix,source,discr,model_filename) 61 | # # jags.uninf <- run_model(run="very long",mix,source,discr,model_filename) 62 | # 63 | # # Process diagnostics, summary stats, and posterior plots 64 | # output_JAGS(jags.uninf, mix, source) 65 | output_JAGS(jags.uninf, mix = mix, source = source, 66 | c('summary_diagnostics', 67 | 'summary_statistics', 68 | 'summary_quantiles', 69 | 'plot_global_matrix', 70 | 'plot_global')) 71 | 72 | 73 | ## ---- eval=FALSE-------------------------------------------------------------- 74 | # # Our 14 fecal samples were 10, 1, 0, 0, 3 75 | kw.alpha <- c(10,1,0,0,3) 76 | # 77 | # # Generate alpha hyperparameters scaling sum(alpha)=n.sources 78 | kw.alpha <- kw.alpha*length(kw.alpha)/sum(kw.alpha) 79 | # 80 | # # the Dirichlet hyperparameters for the alpha.prior cannot be 0 (but can set = .01) 81 | kw.alpha[which(kw.alpha==0)] <- 0.01 82 | # 83 | # # Plot your informative prior 84 | # plot_prior(alpha.prior=kw.alpha, 85 | # source=source, 86 | # plot_save_pdf=TRUE, 87 | # plot_save_png=FALSE, 88 | # filename="prior_plot_kw_inf") 89 | # 90 | # # Define model structure and write JAGS model file 91 | model_filename <- "MixSIAR_model_kw_inf.txt" # Name of the JAGS model file 92 | resid_err <- TRUE 93 | process_err <- TRUE 94 | write_JAGS_model(model_filename, resid_err, process_err, mix, source) 95 | # 96 | # # Run the JAGS model ("very long" took ~5 min) 97 | jags.inf <- run_model(run="test",mix,source,discr,model_filename,alpha.prior=kw.alpha) 98 | # # jags.inf <- run_model(run="very long",mix,source,discr,model_filename,alpha.prior=kw.alpha) 99 | # 100 | # # Process diagnostics, summary stats, and posterior plots 101 | # output_JAGS(jags.inf, mix, source) 102 | output_JAGS(jags.inf, mix = mix, source = source, 103 | c('summary_diagnostics', 104 | 'summary_statistics', 105 | 'summary_quantiles', 106 | 'plot_global_matrix', 107 | 'plot_global')) 108 | -------------------------------------------------------------------------------- /extra_code/mixsiar_examples/lake.R: -------------------------------------------------------------------------------- 1 | ## ---- eval=FALSE-------------------------------------------------------------- 2 | # library(MixSIAR) 3 | # mixsiar.dir <- find.package("MixSIAR") 4 | # paste0(mixsiar.dir,"/example_scripts") 5 | 6 | ## ---- eval=FALSE-------------------------------------------------------------- 7 | # source(paste0(mixsiar.dir,"/example_scripts/mixsiar_script_lake.R")) 8 | 9 | ## ----------------------------------------------------------------------------- 10 | library(MixSIAR) 11 | library(tidyr) # For pivoting later in new output_jags 12 | library(ggplot2) 13 | library(GGally) 14 | library(dplyr) 15 | 16 | # My new output function 17 | source("extra_code/output_jags_new.R") 18 | 19 | ## ----------------------------------------------------------------------------- 20 | # Replace the system.file call with the path to your file 21 | mix.filename <- system.file("extdata", "lake_consumer.csv", package = "MixSIAR") 22 | 23 | mix <- load_mix_data(filename=mix.filename, 24 | iso_names=c("d13C","d15N"), 25 | factors=NULL, 26 | fac_random=NULL, 27 | fac_nested=NULL, 28 | cont_effects=NULL) 29 | 30 | ## ----------------------------------------------------------------------------- 31 | # Replace the system.file call with the path to your file 32 | source.filename <- system.file("extdata", "lake_sources.csv", package = "MixSIAR") 33 | 34 | source <- load_source_data(filename=source.filename, 35 | source_factors=NULL, 36 | conc_dep=FALSE, 37 | data_type="raw", 38 | mix) 39 | 40 | ## ----------------------------------------------------------------------------- 41 | # Replace the system.file call with the path to your file 42 | discr.filename <- system.file("extdata", "lake_discrimination.csv", package = "MixSIAR") 43 | 44 | discr <- load_discr_data(filename=discr.filename, mix) 45 | 46 | ## ---- eval=FALSE-------------------------------------------------------------- 47 | # # Make an isospace plot 48 | # plot_data(filename="isospace_plot", plot_save_pdf=TRUE, plot_save_png=FALSE, mix,source,discr) 49 | 50 | ## ----------------------------------------------------------------------------- 51 | # Calculate the convex hull area, standardized by source variance 52 | calc_area(source=source,mix=mix,discr=discr) 53 | 54 | ## ---- eval=FALSE-------------------------------------------------------------- 55 | # # default "UNINFORMATIVE" / GENERALIST prior (alpha = 1) 56 | # plot_prior(alpha.prior=1,source) 57 | 58 | ## ---- eval=FALSE-------------------------------------------------------------- 59 | # # Write the JAGS model file 60 | model_filename <- "MixSIAR_model.txt" 61 | resid_err <- TRUE 62 | process_err <- FALSE 63 | write_JAGS_model(model_filename, resid_err, process_err, mix, source) 64 | 65 | ## ---- eval=FALSE-------------------------------------------------------------- 66 | jags.1 <- run_model(run="test", mix, source, discr, model_filename) 67 | 68 | ## ---- eval=FALSE-------------------------------------------------------------- 69 | # jags.1 <- run_model(run="normal", mix, source, discr, model_filename) 70 | 71 | ## ---- eval=FALSE-------------------------------------------------------------- 72 | # output_JAGS(jags.1, mix, source, output_options) 73 | output <- output_JAGS(jags.1, mix = mix, source = source, 74 | c('summary_diagnostics', 75 | 'summary_statistics', 76 | 'summary_quantiles', 77 | 'plot_global_matrix', 78 | 'plot_global', 79 | 'plot_cont'), 80 | search_par = 'p.global') 81 | 82 | # Change around if required 83 | output$plot_cont + 84 | facet_wrap(~Source) + 85 | theme_bw() + 86 | theme(legend.position = "None") 87 | 88 | # Get the DIC (similar to AIC) find a model with the smallest DIC 89 | jags.1$BUGSoutput$DIC 90 | -------------------------------------------------------------------------------- /extra_code/mixsiar_examples/wolves.R: -------------------------------------------------------------------------------- 1 | ## ---- eval=FALSE-------------------------------------------------------------- 2 | # library(MixSIAR) 3 | # mixsiar.dir <- find.package("MixSIAR") 4 | # paste0(mixsiar.dir,"/example_scripts") 5 | 6 | ## ---- eval=FALSE-------------------------------------------------------------- 7 | # source(paste0(mixsiar.dir,"/example_scripts/mixsiar_script_wolves.R")) 8 | 9 | ## ----------------------------------------------------------------------------- 10 | library(MixSIAR) 11 | library(tidyr) # For pivoting later in new output_jags 12 | library(ggplot2) 13 | library(GGally) 14 | 15 | # My new output function 16 | source("extra_code/output_jags_new.R") 17 | # source("https://raw.githubusercontent.com/andrewcparnell/simms_course/master/extra_code/output_jags_new.R") 18 | 19 | ## ----------------------------------------------------------------------------- 20 | # Replace the system.file call with the path to your file 21 | mix.filename <- system.file("extdata", "wolves_consumer.csv", package = "MixSIAR") 22 | 23 | # Load the mixture/consumer data 24 | mix <- load_mix_data(filename=mix.filename, 25 | iso_names=c("d13C","d15N"), 26 | factors=c("Region","Pack"), 27 | fac_random=c(TRUE,TRUE), 28 | fac_nested=c(FALSE,TRUE), 29 | cont_effects=NULL) 30 | 31 | ## ----------------------------------------------------------------------------- 32 | # Replace the system.file call with the path to your file 33 | source.filename <- system.file("extdata", "wolves_sources.csv", package = "MixSIAR") 34 | 35 | # Load the source data 36 | source <- load_source_data(filename=source.filename, 37 | source_factors="Region", 38 | conc_dep=FALSE, 39 | data_type="means", 40 | mix) 41 | 42 | ## ----------------------------------------------------------------------------- 43 | # Replace the system.file call with the path to your file 44 | discr.filename <- system.file("extdata", "wolves_discrimination.csv", package = "MixSIAR") 45 | 46 | # Load the discrimination/TDF data 47 | discr <- load_discr_data(filename=discr.filename, mix) 48 | 49 | ## ---- eval=FALSE-------------------------------------------------------------- 50 | # # Make an isospace plot 51 | # plot_data(filename="isospace_plot", plot_save_pdf=TRUE, plot_save_png=FALSE, mix,source,discr) 52 | 53 | ## ----------------------------------------------------------------------------- 54 | # Calculate the convex hull area, standardized by source variance 55 | calc_area(source=source,mix=mix,discr=discr) 56 | 57 | ## ---- eval=FALSE-------------------------------------------------------------- 58 | # # default "UNINFORMATIVE" / GENERALIST prior (alpha = 1) 59 | # plot_prior(alpha.prior=1,source) 60 | 61 | ## ---- eval=FALSE-------------------------------------------------------------- 62 | # # Write the JAGS model file 63 | model_filename <- "MixSIAR_model.txt" # Name of the JAGS model file 64 | resid_err <- TRUE 65 | process_err <- TRUE 66 | write_JAGS_model(model_filename, resid_err, process_err, mix, source) 67 | 68 | ## ---- eval=FALSE-------------------------------------------------------------- 69 | # run <- list(chainLength=200000, burn=150000, thin=50, chains=3, calcDIC=TRUE) 70 | 71 | ## ---- eval=FALSE-------------------------------------------------------------- 72 | jags.1 <- run_model(run="test", mix, source, discr, model_filename) 73 | 74 | ## ---- eval=FALSE-------------------------------------------------------------- 75 | # jags.1 <- run_model(run="normal", mix, source, discr, model_filename) 76 | 77 | ## ---- eval=FALSE-------------------------------------------------------------- 78 | # output_options <- list(summary_save = TRUE, 79 | # summary_name = "summary_statistics", 80 | # sup_post = FALSE, 81 | # plot_post_save_pdf = TRUE, 82 | # plot_post_name = "posterior_density", 83 | # sup_pairs = FALSE, 84 | # plot_pairs_save_pdf = TRUE, 85 | # plot_pairs_name = "pairs_plot", 86 | # sup_xy = TRUE, 87 | # plot_xy_save_pdf = FALSE, 88 | # plot_xy_name = "xy_plot", 89 | # gelman = TRUE, 90 | # heidel = FALSE, 91 | # geweke = TRUE, 92 | # diag_save = TRUE, 93 | # diag_name = "diagnostics", 94 | # indiv_effect = FALSE, 95 | # plot_post_save_png = FALSE, 96 | # plot_pairs_save_png = FALSE, 97 | # plot_xy_save_png = FALSE, 98 | # diag_save_ggmcmc = FALSE) 99 | 100 | ## ---- eval=FALSE-------------------------------------------------------------- 101 | # output_JAGS(jags.1, mix, source, output_options) 102 | out_stuff <- output_JAGS(jags.1, mix = mix, source = source, 103 | c('summary_diagnostics', 104 | 'summary_statistics', 105 | 'summary_quantiles', 106 | 'plot_global', 107 | 'plot_global_matrix', 108 | 'plot_factors')) 109 | 110 | out_stuff$plot_factors[[1]] + theme_bw() + theme(legend.position = "None") 111 | out_stuff$plot_factors[[2]] + xlim(0, 1) 112 | -------------------------------------------------------------------------------- /extra_code/nick_code.R: -------------------------------------------------------------------------------- 1 | # Example of estimating TDFs for a simple system with known dietary proportions 2 | 3 | # Data set 1: 10 obs on 2 isos, 4 sources, with tefs and concdep 4 | # Assume p = c(0.25, 0.25, 0.25, 0.25) 5 | 6 | # Need the development version of simmr 7 | # Run this first (but you only need it once) 8 | # library(devtools) 9 | # install_github('andrewcparnell/simmr') 10 | library(simmr) 11 | 12 | # The data 13 | mix = matrix(c(-10.13, -10.72, -11.39, -11.18, -10.81, -10.7, -10.54, 14 | -10.48, -9.93, -9.37, 11.59, 11.01, 10.59, 10.97, 11.52, 11.89, 15 | 11.73, 10.89, 11.05, 12.3), ncol=2, nrow=10) 16 | colnames(mix) = c('d13C','d15N') 17 | s_names=c('Source A','Source B','Source C','Source D') 18 | s_means = matrix(c(-14, -15.1, -11.03, -14.44, 3.06, 7.05, 13.72, 5.96), ncol=2, nrow=4) 19 | s_sds = matrix(c(0.48, 0.38, 0.48, 0.43, 0.46, 0.39, 0.42, 0.48), ncol=2, nrow=4) 20 | conc = matrix(c(0.02, 0.1, 0.12, 0.04, 0.02, 0.1, 0.09, 0.05), ncol=2, nrow=4) 21 | 22 | 23 | 24 | # Load into simmr without any corrections 25 | simmr_tdf = simmr_load(mixtures=mix, 26 | source_names=s_names, 27 | source_means=s_means, 28 | source_sds=s_sds, 29 | concentration_means = conc) 30 | 31 | # Plot it - they should be slightly off centre as they're not corrected 32 | plot(simmr_tdf) 33 | 34 | # MCMC run - assumes they're know to have eaten equal proportions of sources 35 | simmr_tdf_out = simmr_mcmc_tdf(simmr_tdf, 36 | p = rep(1/simmr_tdf$n_sources, simmr_tdf$n_sources)) 37 | 38 | # Summary 39 | summary(simmr_tdf_out,type='diagnostics') 40 | summary(simmr_tdf_out,type='quantiles') 41 | 42 | # Now put these corrections back into the model and check the 43 | # iso-space plots and dietary output 44 | simmr_tdf_2 = simmr_load(mixtures=mix, 45 | source_names=s_names, 46 | source_means=s_means, 47 | source_sds=s_sds, 48 | correction_means = simmr_tdf_out$c_mean_est, 49 | correction_sds = simmr_tdf_out$c_sd_est, 50 | concentration_means = conc) 51 | 52 | # Plot with corrections now 53 | plot(simmr_tdf_2) 54 | 55 | # Now re-run 56 | simmr_tdf_2_out = simmr_mcmc(simmr_tdf_2) 57 | 58 | # Check convergence 59 | summary(simmr_tdf_2_out, type = 'diagnostics') 60 | 61 | # And see if it looks equal 62 | plot(simmr_tdf_2_out, type = 'boxplot') 63 | plot(simmr_tdf_2_out, type = 'matrix') 64 | 65 | -------------------------------------------------------------------------------- /extra_code/run_simmr_indeff.R: -------------------------------------------------------------------------------- 1 | # Some example code to run the individual effects version of simmr and interpret output 2 | 3 | library(simmr) 4 | 5 | # Load in the new MCMC function to produce individual effects 6 | source("https://raw.githubusercontent.com/andrewcparnell/simms_course/master/extra_code/simmr_mcmc_indeff.R") 7 | 8 | # A very basic data set taken from the vignette 9 | mix = matrix(c(-10.13, -10.72, -11.39, -11.18, -10.81, -10.7, -10.54, 10 | -10.48, -9.93, -9.37, 11.59, 11.01, 10.59, 10.97, 11.52, 11.89, 11 | 11.73, 10.89, 11.05, 12.3), ncol=2, nrow=10) 12 | colnames(mix) = c('d13C','d15N') 13 | s_names = c("Zostera", "Grass", "U.lactuca", "Enteromorpha") 14 | s_means = matrix(c(-14, -15.1, -11.03, -14.44, 3.06, 7.05, 13.72, 5.96), ncol=2, nrow=4) 15 | s_sds = matrix(c(0.48, 0.38, 0.48, 0.43, 0.46, 0.39, 0.42, 0.48), ncol=2, nrow=4) 16 | c_means = matrix(c(2.63, 1.59, 3.41, 3.04, 3.28, 2.34, 2.14, 2.36), ncol=2, nrow=4) 17 | c_sds = matrix(c(0.41, 0.44, 0.34, 0.46, 0.46, 0.48, 0.46, 0.66), ncol=2, nrow=4) 18 | conc = matrix(c(0.02, 0.1, 0.12, 0.04, 0.02, 0.1, 0.09, 0.05), ncol=2, nrow=4) 19 | 20 | # Load into simmr 21 | simmr_in = simmr_load(mixtures=mix, 22 | source_names=s_names, 23 | source_means=s_means, 24 | source_sds=s_sds, 25 | correction_means=c_means, 26 | correction_sds=c_sds, 27 | concentration_means = conc) 28 | 29 | # Run the individual effects version 30 | simmr_out = simmr_mcmc_indeff(simmr_in) 31 | 32 | # Summarise the output 33 | summary(simmr_out$outpu[[1]]) # Get means and quantiles 34 | 35 | # Traceplots and density plots of values 36 | plot(simmr_out$output[[1]]) 37 | 38 | # If you want the full posterior run 39 | post = do.call(rbind, simmr_out$output[[1]]) 40 | 41 | # Now if you want e.g. the mean for each parameter 42 | colMeans(post) 43 | 44 | -------------------------------------------------------------------------------- /extra_code/simmr_mcmc_indeff.R: -------------------------------------------------------------------------------- 1 | simmr_mcmc_indeff = function(simmr_in, 2 | prior_control=list(means=rep(0,simmr_in$n_sources), 3 | sd=rep(1,simmr_in$n_sources)), 4 | mcmc_control=list(iter=20000, 5 | burn=2000, 6 | thin=20, 7 | n.chain=4), 8 | individual_effects = TRUE) { 9 | 10 | # Main function to run simmr through JAGS 11 | # if(class(simmr_in)!='simmr_input') stop("Input argument simmr_in must have come from simmr_load") 12 | 13 | # Throw warning if n.chain =1 14 | if(mcmc_control$n.chain==1) warning("Running only 1 MCMC chain will cause an error in the convergence diagnostics") 15 | 16 | # Throw a warning if less than 4 observations in a group - 1 is ok as it wil do a solo run 17 | if(min(table(simmr_in$group))>1 & min(table(simmr_in$group))<4) warning("At least 1 group has less than 4 observations - either put each observation in an individual group or use informative prior information") 18 | 19 | # Set up the model string 20 | # model_string = ' 21 | # model { 22 | # # Likelihood 23 | # for (j in 1:J) { 24 | # for (i in 1:N) { 25 | # y[i,j] ~ dnorm(inprod(p*q[,j], s_mean[,j]+c_mean[,j]) / inprod(p,q[,j]), 1/var_y[j]) 26 | # } 27 | # var_y[j] <- inprod(pow(p*q[,j],2),pow(s_sd[,j],2)+pow(c_sd[,j],2))/pow(inprod(p,q[,j]),2) 28 | # + pow(sigma[j],2) 29 | # } 30 | # 31 | # # Prior on sigma 32 | # for(j in 1:J) { sigma[j] ~ dunif(0,sig_upp) } 33 | # 34 | # # CLR prior on p 35 | # p[1:K] <- expf/sum(expf) 36 | # for(k in 1:K) { 37 | # expf[k] <- exp(f[k]) 38 | # f[k] ~ dnorm(mu_f[k],1/pow(sigma_f[k],2)) 39 | # } 40 | # } 41 | # ' 42 | model_string = ' 43 | model { 44 | # Likelihood 45 | for (j in 1:J) { 46 | for (i in 1:N) { 47 | y[i,j] ~ dnorm(inprod(p_ind[i,]*q[,j], s_mean[,j]+c_mean[,j]) / inprod(p_ind[i,],q[,j]), 1/var_y[i,j]) 48 | var_y[i,j] <- inprod(pow(p_ind[i,]*q[,j],2),pow(s_sd[,j],2)+pow(c_sd[,j],2))/pow(inprod(p_ind[i,],q[,j]),2) + pow(sigma[j],2) 49 | 50 | } 51 | } 52 | 53 | # Prior on sigma 54 | for(j in 1:J) { sigma[j] ~ dunif(0,sig_upp) } 55 | 56 | # CLR prior on p 57 | for(i in 1:N) { 58 | p_ind[i, 1:K] <- expf[i, 1:K]/sum(expf[i, 1:K]) 59 | for(k in 1:K) { 60 | expf[i, k] <- exp(f[i, k]) 61 | f[i, k] ~ dnorm(mu_f[k],1/pow(sigma_f[k],2)) 62 | } 63 | } 64 | 65 | p[1:K] <- exp_f_mean[1:K]/sum(exp_f_mean[1:K]) 66 | for(k in 1:K) { 67 | exp_f_mean[k] <- exp(mu_f[k]) 68 | mu_f[k] ~ dnorm(mu_f_mean[k], sigma_f_sd[k]^-2) 69 | sigma_f[k] ~ dt(0, sigma_f_sd[k]^-2, 1)T(0,) 70 | } 71 | 72 | } 73 | ' 74 | 75 | 76 | output = output_2 = vector('list',length=simmr_in$n_groups) 77 | 78 | # Loop through all the groups 79 | for(i in 1:simmr_in$n_groups) { 80 | if(simmr_in$n_groups>1) cat(paste("\nRunning for group",i,'\n\n')) 81 | 82 | curr_rows = which(simmr_in$group_int==i) 83 | curr_mix = simmr_in$mixtures[curr_rows,,drop=FALSE] 84 | 85 | # Determine if a single observation or not 86 | if(nrow(curr_mix)==1) { 87 | cat('Only 1 mixture value, performing a simmr solo run...\n') 88 | solo=TRUE 89 | } else { 90 | solo=FALSE 91 | } 92 | 93 | # Create data object 94 | data = with(simmr_in,list( 95 | y=curr_mix, 96 | s_mean=source_means, 97 | s_sd=source_sds, 98 | N=nrow(curr_mix), 99 | J=n_tracers, 100 | c_mean=correction_means, 101 | c_sd = correction_sds, 102 | q=concentration_means, 103 | K=n_sources, 104 | mu_f_mean=prior_control$means, 105 | sigma_f_sd=prior_control$sd, 106 | sig_upp=ifelse(solo,0.001,1000))) 107 | 108 | # Run in JAGS 109 | model = rjags::jags.model(textConnection(model_string), 110 | data=data, 111 | n.chain=mcmc_control$n.chain, 112 | n.adapt=mcmc_control$burn) 113 | 114 | if(individual_effects) { 115 | vars_to_save = c("p", "sigma", "p_ind") 116 | } else { 117 | vars_to_save = c("p", "sigma") 118 | } 119 | output[[i]] = rjags::coda.samples(model=model, 120 | variable.names=vars_to_save, 121 | n.iter=mcmc_control$iter, 122 | thin=mcmc_control$thin) 123 | if(individual_effects) { 124 | curr_col_names = colnames(output[[i]][[1]]) 125 | curr_col_names[grep('p\\[', curr_col_names)] = simmr_in$source_names 126 | curr_col_names[grep('sigma', curr_col_names)] = paste0('sd_',colnames(simmr_in$mixtures)) 127 | for (j in 1:length(simmr_in$source_names)) { 128 | curr_col_names = gsub(paste0(',',j,'\\]'), paste0(',',simmr_in$source_names[j],']'), curr_col_names) 129 | } 130 | output_2[[i]] = lapply(output[[i]],"colnames<-", 131 | curr_col_names) 132 | } else { 133 | output_2[[i]] = lapply(output[[i]],"colnames<-", 134 | c(simmr_in$source_names, 135 | paste0('sd_',colnames(simmr_in$mixtures)))) 136 | } 137 | class(output_2[[i]]) = c('mcmc.list') 138 | } 139 | 140 | output_all = vector('list') 141 | output_all$input = simmr_in 142 | output_all$output = output_2 143 | if(individual_effects) { 144 | class(output_all) = c('simmr_output', 'simmr_output_individual') 145 | } else { 146 | class(output_all) = 'simmr_output' 147 | } 148 | 149 | return(output_all) 150 | 151 | } -------------------------------------------------------------------------------- /extra_code/simmr_with_factors_and_priors.R: -------------------------------------------------------------------------------- 1 | # This code runs simmr with a single categorical covariate and puts in informative 2 | # prior distributions on each of the factor levels 3 | 4 | # Load in simmr 5 | library(simmr) 6 | library(readxl) # Run install.packages('readxl') if you don't have this 7 | library(httr) # Again run install.packages if you don't have this 8 | 9 | # Load in the Geese data from the simmr_with_excel example 10 | sheet_names <- excel_sheets(path = "ap_notes/prac_using_simmr/geese_data.xls") 11 | all <- lapply(sheet_names, 12 | read_excel, 13 | path = "ap_notes/prac_using_simmr/geese_data.xls" 14 | ) 15 | 16 | # Load in the pieces 17 | mix <- all[[1]] 18 | source <- all[[2]] 19 | TDF <- all[[3]] 20 | conc <- all[[4]] 21 | 22 | # Use simmr_elicity to provide prior information to the model 23 | # Here I will use sex with two levels to provide prior information 24 | # First sex = 0 25 | sex0 <- simmr_elicit( 26 | n_source = 4, 27 | proportion_means = c(0.1, 0.2, 0.3, 0.4), 28 | proportion_sds = c(0.05, 0.05, 0.1, 0.1), 29 | ) 30 | # Now sex = 0 31 | sex1 <- simmr_elicit( 32 | n_source = 4, 33 | proportion_means = c(0.2, 0.2, 0.1, 0.5), 34 | proportion_sds = c(0.1, 0.1, 0.02, 0.2), 35 | ) 36 | 37 | 38 | 39 | # Write some JAGS code to run the model with a single factor covariate 40 | model_code <- " 41 | model{ 42 | # Likelihood 43 | for(j in 1:J) { 44 | for (i in 1:N) { 45 | y[i,j] ~ dnorm(inprod(p[i,1:K]*q[,j], s_mean[,j]+c_mean[,j]) / inprod(p[i,1:K],q[,j]), 1/var_y[i,j]) 46 | var_y[i,j] <- inprod(pow(p[i,1:K]*q[,j],2),pow(s_sd[,j],2)+pow(c_sd[,j],2))/pow(inprod(p[i,1:K],q[,j]),2) + pow(sigma[j],2) 47 | } 48 | } 49 | 50 | # Prior on sigma 51 | for(j in 1:J) { 52 | sigma[j] ~ dgamma(sigma_shape[j], sigma_rate[j]) 53 | } 54 | 55 | # CLR prior on p 56 | for (i in 1:N) { 57 | p[i,1:K] <- expf[i,1:K]/sum(expf[i,1:K]) 58 | for(k in 1:K) { 59 | expf[i,k] <- exp(f[i,k]) 60 | f[i,k] <- inprod(X[i,1:L], beta[k,1:L]) 61 | } 62 | } 63 | 64 | # Prior on beta 65 | for (k in 1:K) { 66 | for (l in 1:L) { 67 | beta[k,l] ~ dnorm(beta_mean[k,l], beta_sd[k,l]^-2) 68 | } 69 | } 70 | 71 | # Also get predictions 72 | for (i in 1:N_pred) { 73 | p_pred[i,1:K] <- expf_pred[i,1:K]/sum(expf_pred[i,1:K]) 74 | for(k in 1:K) { 75 | expf_pred[i,k] <- exp(f_pred[i,k]) 76 | f_pred[i,k] <- inprod(X_pred[i,1:L], beta[k,1:L]) 77 | } 78 | } 79 | }" 80 | 81 | # Set up the data 82 | model_data <- list( 83 | N = nrow(mix), 84 | J = 2, 85 | K = nrow(source), 86 | y = mix[,1:2], 87 | q = conc[,2:3], 88 | s_mean = source[,2:3], 89 | s_sd = source[,4:5], 90 | c_mean = TDF[,2:3], 91 | c_sd = TDF[,4:5], 92 | X = model.matrix(~factor(mix$Sex) - 1), 93 | L = ncol(model.matrix(~factor(mix$Sex) - 1)), 94 | X_pred = cbind(c(0, 1), c(1, 0)), 95 | N_pred = 2, 96 | beta_mean = cbind(sex0$mean, sex1$mean), # This is where the clever prior information comes in 97 | beta_sd = cbind(sex0$sd, sex1$sd), 98 | sigma_shape = rep(3, 2), 99 | sigma_rate = rep(3/50, 2) 100 | ) 101 | 102 | # Run this through simmr 103 | out <- jags( 104 | data = model_data, 105 | model = textConnection(model_code), 106 | parameters.to.save = c("p", "beta", "sigma", "f", "f_pred", "p_pred") 107 | ) 108 | 109 | plot(out) 110 | 111 | # Create a plot of the histograms for the two levels across the sources 112 | par(mfrow = c(2, 4)) 113 | source_names <- as.vector(source[,1]$Sources) 114 | for(i in 1:4) { 115 | hist(out$BUGSoutput$sims.list$p_pred[,1,i], breaks = 30, main = paste("Sex = 0;", source_names[i])) 116 | } 117 | for(i in 1:4) { 118 | hist(out$BUGSoutput$sims.list$p_pred[,2,i], breaks = 30, main = paste("Sex = 1;", source_names[i])) 119 | } 120 | par(mfrow = c(1, 1)) 121 | 122 | -------------------------------------------------------------------------------- /extra_code/test-sample-size-SIBER.Rmd: -------------------------------------------------------------------------------- 1 | --- 2 | title: "Explore SEA_B on small sample sizes" 3 | output: html_notebook 4 | --- 5 | 6 | 7 | ```{r setup} 8 | 9 | library(SIBER) 10 | library(mvtnorm) 11 | library(tidyverse) 12 | ``` 13 | 14 | 15 | Create some simulated data to work with: 16 | 17 | ```{r} 18 | 19 | n.iso <- 2 20 | 21 | samp_these <- as.list(3:10) 22 | 23 | iso_data <- purrr:::map(samp_these, rmvnorm, msean = rep(0,n.iso)) 24 | 25 | 26 | 27 | 28 | 29 | ``` 30 | 31 | -------------------------------------------------------------------------------- /index.md: -------------------------------------------------------------------------------- 1 | --- 2 | title: "Stable Isotope Mixing Models: course timetable" 3 | author: "Andrew Parnell and Andrew Jackson, with Emma Govan" 4 | date: "March 2024 - online" 5 | output: html_document 6 | --- 7 | 8 | Course pre-requisites can be found [here](https://andrewcparnell.github.io/simms_course/Prerequisites.html). All the raw files and code can be found [here](https://github.com/andrewcparnell/simms_course). Click 'Clone or Download' near the top right and then 'Download ZIP' if you want an offline copy of everything. 9 | 10 | As this module will be delivered online please install [Zoom](https://www.zoom.us) and [Slack](https://slack.com) to access the videos and interactive components of the course. All the Zoom links to the meeting will be posted to the Slack `#zoom-links` channel. 11 | 12 | Please note that the course will be recorded so that attendees in different time zones can catch up on material. **All times are UTC+0**. 13 | 14 | ## Day 1 15 | 16 | Introduction to SIA data and revision of basic statistical and R concepts 17 | 18 | Time | Class 19 | ------------- | ---------------------------------------------------- 20 | 09:30-10:30 | Session 1 [Introduction: why use a SIMM? (AJ & AP)](https://andrewcparnell.github.io/simms_course/aj-content/intro-why-use-a-SIMM.html) 21 | 10:30-10:45 | Coffee break 22 | 10:45-11:45 | Session 2 [Revision of likelihood and regression (AP)](https://andrewcparnell.github.io/simms_course/ap_notes/revision_of_likelihood_regression/revision_of_likelihood_and_regression.pdf) 23 | 11:45-12:00 | Break 24 | 12:00-13:00 | Session 3 [Guided practical: Revision of important R concepts (AP)](https://andrewcparnell.github.io/simms_course/ap_notes/revision_of_R/Revision_of_R.R) 25 | 13:00-14:00 | Lunch 26 | 14:00-15:00 | Session 4 [Guided practical: Intro to SI data](https://andrewcparnell.github.io/simms_course/aj-content/practicals/day-1-pm1/Exploring-Basic-SIA-Data.nb.html) and [biplots](https://andrewcparnell.github.io/simms_course/aj-content/practicals/day-1-pm1/first-biplot.nb.html) (AJ) 27 | 15:00-15:30 | Coffee break 28 | 15:30-17:00 | Session 5 [Guided Practical: Simple linear models to explain SIA data](https://andrewcparnell.github.io/simms_course/aj-content/practicals/day-1-pm2/basic-SIA-linear-models.nb.html) (AJ) 29 | 30 | ## Day 2 31 | 32 | Introduction to Bayes and SIMMs 33 | 34 | Time | Class 35 | ------------- | ---------------------------------------------------- 36 | 09:30-10:30 | Session 6 [An introduction to Bayesian statistics (AP)](https://andrewcparnell.github.io/simms_course/ap_notes/intro_bayes/intro_bayes.pdf) 37 | 10:30-10:45 | Coffee break 38 | 10:45-11:45 | Session 7 [Guided practical: R, JAGS, and linear regression (AP)](https://andrewcparnell.github.io/simms_course/ap_notes/prac_jags/practical_R_jags_and_lr.R) 39 | 11:45-12:00 | Break 40 | 12:00-13:00 | Session 8 [Differences between regression models and SIMMs (AP)](https://andrewcparnell.github.io/simms_course/ap_notes/reg_and_simms/reg_and_simms.pdf) 41 | 13:00-14:00 | Lunch 42 | 14:00-15:00 | Session 9 [Guided practical: intro to simmr (AP)](https://andrewcparnell.github.io/simms_course/ap_notes/prac_using_simmr/simmr_vignette_code.R) 43 | 15:00-15:30 | Coffee break 44 | 15:30-17:00 | Session 10 Practical: options are (1) run your data through AJ's plots from yesterday, or (2) get your data to run in `simmr`, or (3) go back and learn ggplot2 from [this script](https://andrewcparnell.github.io/simms_course/ap_notes/gg_plot_class.R) 45 | 46 | ## Day 3 47 | 48 | simmr / MixSIAR 49 | 50 | Time | Class 51 | ------------- | ---------------------------------------------------- 52 | 09:30-10:30 | Session 11 [The statistical model behind simmr (and SIAR) (AP)](https://andrewcparnell.github.io/simms_course/ap_notes/siar_stats/siar_stats.pdf) 53 | 10:30-10:45 | Coffee break 54 | 10:45-11:45 | Session 12 Guided Practical: using [MixSIAR](https://andrewcparnell.github.io/simms_course/ap_notes/prac_mixsiar_and_jags/mixsiar_script.R) and [incorporating prior information in simmr (AP)](https://andrewcparnell.github.io/simms_course/ap_notes/prac_using_simmr/simmr_with_priors.R) 55 | 11:45-12:00 | Break 56 | 12:00-13:00 | Session 13 [Dos and don'ts of using mixing models with discussion (AJ)](https://andrewcparnell.github.io/simms_course/aj-content/siar-dos-and-donts.html) 57 | 13:00-14:00 | Lunch 58 | 14:00-15:00 | Session 14 Dos and don'ts continued (AJ) 59 | 15:00-15:30 | Coffee break 60 | 15:30-17:00 | Session 15 [Practical: Source grouping, when and how? (AJ)](https://andrewcparnell.github.io/simms_course/aj-content/practicals/source-aggregation.nb.html) 61 | 62 | 63 | ## Day 4 64 | 65 | Source grouping, SIBER, and (new!) cosimmr 66 | 67 | Time | Class 68 | ------------- | ---------------------------------------------------- 69 | 09:30-10:30 | Session 16 [Creating and understanding Stable Isotope Bayesian Ellipses (SIBER) (AJ)](https://andrewcparnell.github.io/simms_course/aj-content/siber-intro-ellipses.html) 70 | 10:30-10:45 | Coffee break 71 | 10:45-11:45 | Session 17 [Guided Practical: Using SIBER to compare populations using ellipses (AJ)](https://andrewcparnell.github.io/simms_course/aj-content/practicals/siber-comparing-populations.nb.html) 72 | 11:45-12:00 | Break 73 | 12:00-13:00 | Session 18 [Guided Practical: Using SIBER to compare communities using convex hulls (AJ)](https://andrewcparnell.github.io/simms_course/aj-content/practicals/siber-comparing-communities.nb.html) 74 | 13:00-14:00 | Lunch 75 | 14:00-15:00 | Session 19 [Introduction to cosimmr](https://andrewcparnell.github.io/simms_course/ap_notes/cosimmr/cosimmr_presentation.pdf) and [practical](https://raw.githubusercontent.com/emmagovan/cosimmr/master/vignettes/cosimmr.Rmd) (EG & AP) 76 | 15:00-15:30 | Coffee break 77 | 15:30-17:00 | Session 20 [Practical: pick a MixSIAR example](https://github.com/brianstock/MixSIAR/tree/master/inst/example_scripts) and [look at the manual](https://github.com/brianstock/MixSIAR/blob/master/Manual/mixsiar_manual_3.1.pdf) (AP & AJ) 78 | 79 | -------------------------------------------------------------------------------- /my_plot.pdf: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/andrewcparnell/simms_course/3052327e05aa93897fcfa31e2e8dd49fa3e45708/my_plot.pdf -------------------------------------------------------------------------------- /my_prostate_file.rda: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/andrewcparnell/simms_course/3052327e05aa93897fcfa31e2e8dd49fa3e45708/my_prostate_file.rda -------------------------------------------------------------------------------- /my_prostate_file.rds: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/andrewcparnell/simms_course/3052327e05aa93897fcfa31e2e8dd49fa3e45708/my_prostate_file.rds -------------------------------------------------------------------------------- /papers/2007_Ecol_Schmidt et al.pdf: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/andrewcparnell/simms_course/3052327e05aa93897fcfa31e2e8dd49fa3e45708/papers/2007_Ecol_Schmidt et al.pdf -------------------------------------------------------------------------------- /papers/Bearhop2004Determining Trophic Niche.pdf: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/andrewcparnell/simms_course/3052327e05aa93897fcfa31e2e8dd49fa3e45708/papers/Bearhop2004Determining Trophic Niche.pdf -------------------------------------------------------------------------------- /papers/Cheung & Szpak 2020 J Arch Method & Theory Online.pdf: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/andrewcparnell/simms_course/3052327e05aa93897fcfa31e2e8dd49fa3e45708/papers/Cheung & Szpak 2020 J Arch Method & Theory Online.pdf -------------------------------------------------------------------------------- /papers/Cheung & Szpak 2021 J Arch Meth & Theory online.pdf: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/andrewcparnell/simms_course/3052327e05aa93897fcfa31e2e8dd49fa3e45708/papers/Cheung & Szpak 2021 J Arch Meth & Theory online.pdf -------------------------------------------------------------------------------- /papers/Fry-MEPS.pdf: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/andrewcparnell/simms_course/3052327e05aa93897fcfa31e2e8dd49fa3e45708/papers/Fry-MEPS.pdf -------------------------------------------------------------------------------- /papers/Jackson_SiBER_2011.pdf: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/andrewcparnell/simms_course/3052327e05aa93897fcfa31e2e8dd49fa3e45708/papers/Jackson_SiBER_2011.pdf -------------------------------------------------------------------------------- /papers/Layman et al 2007 Ecology Letters.pdf: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/andrewcparnell/simms_course/3052327e05aa93897fcfa31e2e8dd49fa3e45708/papers/Layman et al 2007 Ecology Letters.pdf -------------------------------------------------------------------------------- /papers/Layman2007SIRCommunityTrophicStructure.pdf: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/andrewcparnell/simms_course/3052327e05aa93897fcfa31e2e8dd49fa3e45708/papers/Layman2007SIRCommunityTrophicStructure.pdf -------------------------------------------------------------------------------- /papers/Newsome_et_al__Proof2.pdf: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/andrewcparnell/simms_course/3052327e05aa93897fcfa31e2e8dd49fa3e45708/papers/Newsome_et_al__Proof2.pdf -------------------------------------------------------------------------------- /papers/Pacella et al, 2013.pdf: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/andrewcparnell/simms_course/3052327e05aa93897fcfa31e2e8dd49fa3e45708/papers/Pacella et al, 2013.pdf -------------------------------------------------------------------------------- /papers/Parnell_BSimms_2013.pdf: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/andrewcparnell/simms_course/3052327e05aa93897fcfa31e2e8dd49fa3e45708/papers/Parnell_BSimms_2013.pdf -------------------------------------------------------------------------------- /papers/Parnell_SIAR_2010.pdf: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/andrewcparnell/simms_course/3052327e05aa93897fcfa31e2e8dd49fa3e45708/papers/Parnell_SIAR_2010.pdf -------------------------------------------------------------------------------- /papers/Phillips&Koch2002.pdf: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/andrewcparnell/simms_course/3052327e05aa93897fcfa31e2e8dd49fa3e45708/papers/Phillips&Koch2002.pdf -------------------------------------------------------------------------------- /papers/Phillips_SIMMadvice_2014.pdf: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/andrewcparnell/simms_course/3052327e05aa93897fcfa31e2e8dd49fa3e45708/papers/Phillips_SIMMadvice_2014.pdf -------------------------------------------------------------------------------- /papers/m490p285.pdf: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/andrewcparnell/simms_course/3052327e05aa93897fcfa31e2e8dd49fa3e45708/papers/m490p285.pdf -------------------------------------------------------------------------------- /simms_course.Rproj: -------------------------------------------------------------------------------- 1 | Version: 1.0 2 | ProjectId: 0a8137fd-3d8b-4c6e-81b7-b4ead5acc2d7 3 | 4 | RestoreWorkspace: Default 5 | SaveWorkspace: Default 6 | AlwaysSaveHistory: Default 7 | 8 | EnableCodeIndexing: Yes 9 | UseSpacesForTab: Yes 10 | NumSpacesForTab: 2 11 | Encoding: UTF-8 12 | 13 | RnwWeave: Sweave 14 | LaTeX: pdfLaTeX 15 | --------------------------------------------------------------------------------