diff --git a/README.md b/README.md index 69d2cda..b4fff08 100644 --- a/README.md +++ b/README.md @@ -24,8 +24,6 @@ Text and figures are licensed under Creative Commons Attribution [CC BY 4.0](htt + Replace crazy function using cosinus by prediction w/ rainfall/temperature (beware standardisation); Or do both -+ Debug GLMM practical - + Complete example on survival, w/ hypothesis testing, illustrate with Bayes factors? See https://rstudio-pubs-static.s3.amazonaws.com/358672_09291d0b37ce43f08cf001cfd25c16c2.html, https://stackoverflow.com/questions/60278806/bayes-factor-in-r-with-jaggs, https://www.martinmodrak.cz/2021/03/28/three-ways-to-compute-a-bayes-factor/, http://yourdomain.com/statistics,/modeling/2017/07/07/BF_computation.html ou encore https://michael-franke.github.io/statistics,/modeling/2017/07/07/BF_computation.html + Properly introduce GLMs; Illustrate how to find p from logit(p) @@ -44,8 +42,6 @@ Text and figures are licensed under Creative Commons Attribution [CC BY 4.0](htt + Finish up writing that book -+ ~~Add a plot with several lines from posterior distribution of regression parameters to a plot of mean response function of a covariate; then get the credible interval on the prediction~~ - + Prior predictive check for logistic storks and lmm plants sample_mu <- rnorm( 1e4 , 178 , 20 ) sample_sigma <- runif( 1e4 , 0 , 50 ) @@ -65,6 +61,9 @@ dens( prior_h ) + Add something on equivalence w/ MLE: say binomial lik $Bin(n,k)$ and beta prior $Beta(a,b)$ then posterior is beta $Beta(a+k, b+n-k)$; posterior mean is $(a+k)/(a+b+n)$ which can be written $(1-w)(a/a+b) + w k/n$. Posterior mean is weighted average of prior mean and MLE. When sample size is big, $n$ tends to infinity and posterior mean tends to MLE, whatever the prior. Same reasoning with variance shows that Bayes gives reasonable results, even w/ small sample size ++ ~~Debug GLMM practical~~ + ++ ~~Add a plot with several lines from posterior distribution of regression parameters to a plot of mean response function of a covariate; then get the credible interval on the prediction~~ + ~~Make code/outputs fit in slides~~ diff --git a/docs/practicals/practical9.Rmd b/docs/practicals/practical9.Rmd index 2b51a8d..82727b6 100644 --- a/docs/practicals/practical9.Rmd +++ b/docs/practicals/practical9.Rmd @@ -196,6 +196,47 @@ round(fit$BUGSoutput$summary[, -c(4,6)], 3) --- +Convert regression coefficients from scaled to non-scaled and compare to values used to generate data (from ) + +```{r} +sbzero <- fit$BUGSoutput$sims.matrix[,'mu.a'] +sbun <- fit$BUGSoutput$sims.matrix[,'b[1]'] +sbdeux <- fit$BUGSoutput$sims.matrix[,'b[2]'] + +mu <- mean(boo) +sg <- sd(boo) +``` + +--- + +```{r} +bzero <- sbzero - sbun*mu/sg + sbdeux*mu^2/(sg^2) +hist(bzero) +abline(v = -14, col = "red", lwd = 2) +mean(bzero) +``` + +--- + +```{r} +bun <- sbdeux/sg - 2 * sbdeux * mu / (sg^2) +hist(bun) +abline(v = 1.8, col = "red", lwd = 2) +mean(bun) +``` + +--- + +```{r} +bdeux <- sbdeux/(sg^2) +hist(bdeux) +abline(v = - 0.045, col = "red", lwd = 2) +mean(bdeux) +``` + +--- +## Frequentist approach + .tiny-font[ ```{r} library(lme4) @@ -213,3 +254,4 @@ Parameter estimates obtained with both approaches are very similar. visreg::visreg(fit_lme4, xvar = 'Temp') ``` ] + diff --git a/docs/practicals/practical9.html b/docs/practicals/practical9.html index e900af5..b39d43b 100644 --- a/docs/practicals/practical9.html +++ b/docs/practicals/practical9.html @@ -19,7 +19,7 @@ ### Olivier Gimenez ] .date[ -### last updated: 2023-11-11 +### last updated: 2023-11-12 ] --- @@ -142,13 +142,13 @@ ``` ``` -## Transect Temperature Anemones Temp -## 1 1 20.33973 53 0.1338975 -## 2 1 20.47855 48 0.2125261 -## 3 1 20.61737 40 0.2911548 -## 4 1 20.75619 41 0.3697834 -## 5 1 20.89500 32 0.4484121 -## 6 1 21.03382 35 0.5270407 +## Transect Temperature Anemones Temp +## 1 1 18.31928 48 -0.9742340 +## 2 1 18.31673 56 -0.9757048 +## 3 1 18.31417 51 -0.9771756 +## 4 1 18.31162 51 -0.9786464 +## 5 1 18.30906 48 -0.9801172 +## 6 1 18.30651 39 -0.9815880 ``` ] @@ -237,26 +237,98 @@ ``` ## mean sd 2.5% 50% 97.5% Rhat n.eff -## a[1] 3.748 0.046 3.659 3.750 3.838 1.001 3000 -## a[2] 3.612 0.054 3.508 3.612 3.716 1.001 3000 -## a[3] 4.096 0.035 4.027 4.097 4.163 1.001 3000 -## a[4] 4.486 0.050 4.386 4.487 4.581 1.001 3000 -## a[5] 4.057 0.042 3.978 4.056 4.138 1.001 3000 -## a[6] 3.401 0.053 3.295 3.400 3.507 1.002 1700 -## a[7] 3.621 0.068 3.484 3.621 3.754 1.003 1100 -## a[8] 3.562 0.038 3.486 3.563 3.636 1.002 910 -## a[9] 3.866 0.033 3.798 3.867 3.930 1.001 1800 -## a[10] 3.853 0.041 3.774 3.854 3.934 1.001 3000 -## b[1] -0.040 0.028 -0.094 -0.039 0.014 1.001 3000 -## b[2] -0.129 0.017 -0.163 -0.129 -0.095 1.001 3000 -## deviance 1333.022 4.910 1325.304 1332.328 1344.842 1.001 3000 -## mu.a 3.828 0.131 3.570 3.829 4.068 1.001 3000 -## sigma.a 0.377 0.121 0.229 0.351 0.664 1.006 3000 +## a[1] 4.142 0.050 4.045 4.142 4.240 1.001 3000 +## a[2] 4.290 0.059 4.177 4.289 4.405 1.002 1100 +## a[3] 4.372 0.049 4.276 4.371 4.468 1.001 3000 +## a[4] 3.579 0.098 3.391 3.578 3.767 1.001 1900 +## a[5] 4.286 0.028 4.228 4.286 4.341 1.001 3000 +## a[6] 4.840 0.030 4.782 4.840 4.898 1.001 3000 +## a[7] 3.366 0.046 3.276 3.366 3.453 1.001 3000 +## a[8] 4.175 0.032 4.113 4.175 4.237 1.001 2600 +## a[9] 4.630 0.023 4.585 4.631 4.677 1.001 3000 +## a[10] 3.563 0.043 3.476 3.563 3.648 1.001 3000 +## b[1] -0.023 0.029 -0.079 -0.023 0.035 1.001 2800 +## b[2] -0.153 0.023 -0.196 -0.153 -0.109 1.001 3000 +## deviance 1401.831 4.854 1394.333 1401.129 1413.014 1.002 1100 +## mu.a 4.125 0.191 3.746 4.127 4.500 1.001 3000 +## sigma.a 0.568 0.167 0.341 0.535 0.980 1.007 580 ``` ] --- +Convert regression coefficients from scaled to non-scaled and compare to values used to generate data (from <https://stats.stackexchange.com/questions/361995/how-to-convert-coefficients-from-quadratic-function-from-scaled-to-not-scaled-co>) + + +```r +sbzero <- fit$BUGSoutput$sims.matrix[,'mu.a'] +sbun <- fit$BUGSoutput$sims.matrix[,'b[1]'] +sbdeux <- fit$BUGSoutput$sims.matrix[,'b[2]'] + +mu <- mean(boo) +sg <- sd(boo) +``` + +--- + + +```r +bzero <- sbzero - sbun*mu/sg + sbdeux*mu^2/(sg^2) +hist(bzero) +abline(v = -14, col = "red", lwd = 2) +``` + +![](practical9_files/figure-html/unnamed-chunk-11-1.png)<!-- --> + +```r +mean(bzero) +``` + +``` +## [1] -15.89737 +``` + +--- + + +```r +bun <- sbdeux/sg - 2 * sbdeux * mu / (sg^2) +hist(bun) +abline(v = 1.8, col = "red", lwd = 2) +``` + +![](practical9_files/figure-html/unnamed-chunk-12-1.png)<!-- --> + +```r +mean(bun) +``` + +``` +## [1] 1.939693 +``` + +--- + + +```r +bdeux <- sbdeux/(sg^2) +hist(bdeux) +abline(v = - 0.045, col = "red", lwd = 2) +``` + +![](practical9_files/figure-html/unnamed-chunk-13-1.png)<!-- --> + +```r +mean(bdeux) +``` + +``` +## [1] -0.05066283 +``` + +--- +## Frequentist approach + .tiny-font[ ```r @@ -271,15 +343,15 @@ ## Family: poisson ( log ) ## Formula: Anemones ~ Temp + I(Temp^2) + (1 | Transect) ## Data: data -## AIC BIC logLik deviance df.resid -## 1382.2781 1395.4714 -687.1391 1374.2781 196 +## AIC BIC logLik deviance df.resid +## 1461.916 1475.109 -726.958 1453.916 196 ## Random effects: ## Groups Name Std.Dev. -## Transect (Intercept) 0.3026 +## Transect (Intercept) 0.4552 ## Number of obs: 200, groups: Transect, 10 ## Fixed Effects: ## (Intercept) Temp I(Temp^2) -## 3.83058 -0.04063 -0.12902 +## 4.12492 -0.02394 -0.15259 ``` ] @@ -293,8 +365,9 @@ visreg::visreg(fit_lme4, xvar = 'Temp') ``` -![](practical9_files/figure-html/unnamed-chunk-11-1.svg)<!-- --> +![](practical9_files/figure-html/unnamed-chunk-15-1.svg)<!-- --> ] + diff --git a/docs/practicals/practical9_files/figure-html/unnamed-chunk-11-1.png b/docs/practicals/practical9_files/figure-html/unnamed-chunk-11-1.png index 79d8e4d..9f244b4 100644 Binary files a/docs/practicals/practical9_files/figure-html/unnamed-chunk-11-1.png and b/docs/practicals/practical9_files/figure-html/unnamed-chunk-11-1.png differ diff --git a/docs/practicals/practical9_files/figure-html/unnamed-chunk-11-1.svg b/docs/practicals/practical9_files/figure-html/unnamed-chunk-11-1.svg index 7c8b2bf..1f37c2d 100644 --- a/docs/practicals/practical9_files/figure-html/unnamed-chunk-11-1.svg +++ b/docs/practicals/practical9_files/figure-html/unnamed-chunk-11-1.svg @@ -30,18 +30,15 @@ - - - - + - + - + @@ -72,64 +69,60 @@ - - - - - - - - - - + + + + + + - - + + - + + - + - + - - - - - - - - - + + + + + + + + - + - + - + - + - + - + - - - - + + + + @@ -140,206 +133,206 @@ - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + diff --git a/docs/practicals/practical9_files/figure-html/unnamed-chunk-12-1.png b/docs/practicals/practical9_files/figure-html/unnamed-chunk-12-1.png new file mode 100644 index 0000000..19beb10 Binary files /dev/null and b/docs/practicals/practical9_files/figure-html/unnamed-chunk-12-1.png differ diff --git a/docs/practicals/practical9_files/figure-html/unnamed-chunk-13-1.png b/docs/practicals/practical9_files/figure-html/unnamed-chunk-13-1.png new file mode 100644 index 0000000..b320bad Binary files /dev/null and b/docs/practicals/practical9_files/figure-html/unnamed-chunk-13-1.png differ diff --git a/docs/practicals/practical9_files/figure-html/unnamed-chunk-15-1.svg b/docs/practicals/practical9_files/figure-html/unnamed-chunk-15-1.svg new file mode 100644 index 0000000..1d2e8d5 --- /dev/null +++ b/docs/practicals/practical9_files/figure-html/unnamed-chunk-15-1.svg @@ -0,0 +1,333 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + diff --git a/docs/practicals/practical9_files/figure-html/unnamed-chunk-2-1.svg b/docs/practicals/practical9_files/figure-html/unnamed-chunk-2-1.svg index b81caca..e1ddf5c 100644 --- a/docs/practicals/practical9_files/figure-html/unnamed-chunk-2-1.svg +++ b/docs/practicals/practical9_files/figure-html/unnamed-chunk-2-1.svg @@ -36,18 +36,24 @@ - + - + - + + + + + + + @@ -75,26 +81,34 @@ - - - - - + + + + + - - + + + + - - + + + + - - + + + + - - + + + + @@ -105,7 +119,7 @@ - + diff --git a/docs/practicals/practical9_files/figure-html/unnamed-chunk-3-1.svg b/docs/practicals/practical9_files/figure-html/unnamed-chunk-3-1.svg index 7a47d81..8431bb1 100644 --- a/docs/practicals/practical9_files/figure-html/unnamed-chunk-3-1.svg +++ b/docs/practicals/practical9_files/figure-html/unnamed-chunk-3-1.svg @@ -6,25 +6,31 @@ - + - + - + + + + + + + - + @@ -33,123 +39,134 @@ - + - + - + - + - + - + - + - + - + - + - + + + + - - - - - - - - - - + + + + + + - - + + + + - - + + + + - - + + + + - - - - - - - - - - - + + + + + + + + + + + - - - + + + + - - - + + + + - - - + + + + - - - + + + + - - - + + + + - - - + + + + - + - - - - - - - - + + + + + + + + diff --git a/docs/practicals/practical9_files/figure-html/unnamed-chunk-4-1.svg b/docs/practicals/practical9_files/figure-html/unnamed-chunk-4-1.svg index 989b40c..93c699a 100644 --- a/docs/practicals/practical9_files/figure-html/unnamed-chunk-4-1.svg +++ b/docs/practicals/practical9_files/figure-html/unnamed-chunk-4-1.svg @@ -9,51 +9,45 @@ - + - + - + - + - - - - - - - + - + - + - + - + - + - + - + - + @@ -111,287 +105,284 @@ - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + - - + + - - + + - - + + - - + + + + + + + + + + - - + + - - + + - - - - - - - - + + - - + + - - + + + - - + + + - - - + + + - - - - - - - - - - - - - - - - + + + + + + + + + + + + + + + + diff --git a/docs/search.json b/docs/search.json index e5dd972..1b0de56 100644 --- a/docs/search.json +++ b/docs/search.json @@ -5,13 +5,13 @@ "title": "Bayesian statistics with R", "author": [], "contents": "\n\nContents\nSchedule\nLearning objectives\nRequirements\nCredits\nCorrections\nReuse\n\n\nWho: Olivier Gimenez\nWhen:\nMarch 22-23-25-26, 2021Nov 25-Dec 2-Dec 9, 2021April 11-12, 2022Sept 22-23, 2022Nov 24-Dec 1-Dec 8, 2022\nNov 23-Nov 30-Dec 7, 2023\nWhere:\nZoomBiodiversity Ecology Evolution Master, Montpellier UniversityCollège Doctoral, Montpellier UniversityCollège Doctoral, Montpellier University\nBiodiversity Ecology Evolution Master, Montpellier University\n\n\nSchedule\nAn introduction to Bayesian inference [lecture | practical 1 | video]\nThe likelihood [lecture | practical 2 | video]\nBayesian analyses by hand [lecture | video]\nA detour to explore priors [lecture | practical 3 | practical 4| video]\nMarkov chains Monte Carlo methods (MCMC) [lecture | video]\nBayesian analyses in R with the Jags software [lecture | R script | practical 5 | practical 6 | video]\nContrast scientific hypotheses with model selection [lecture | practical 7 | video]\nHeterogeneity and multilevel models (aka mixed models) [lecture | R script | practical 8 | practical 9 | video]\nLearning objectives\nTry and demystify Bayesian statistics, and MCMC methods\nMake the difference between Bayesian and Frequentist analyses\nUnderstand the Methods section of a paper that does Bayesian stuff\nRun Bayesian analyses with R (in Jags)\nRequirements\nYou need to have R and RStudio installed\nDownload Jags from sourceforge and install it\nInstall the following R packages: tidyverse, here, lme4, visreg, lattice, brms and R2jags. You can install them all at once by running the following code in the R command line:\n\n\ninstall.packages(c(\"tidyverse\", \"R2jags\", \"here\", \"lme4\", \"visreg\", \"lattice\", \"brms\"))\n\n\nCredits\nMany slides are from a workshop we used to run a loooong time ago with Ruth King, Byron Morgan and Steve Brooks. I also re-used or adapted slides by Richard McElreath, Kerrie Mengersen, Francisco Rodriguez Sanchez, Jim Albert and Jingchen Hu, Tristan Marh, Jason Matthiopoulos, a paper by Michael McCarthy and Pip Masters, Andrés Lopez-Sepulcre and John Kruschke’ book cover. The sources for the images are James Kulich, Matt Buck, xkcd and Mike West.\nCorrections\nIf you spot a typo or an error, find a bug, want to suggest changes, or have trouble running the code, please file an issue or get back to me\nReuse\nText and figures are licensed under Creative Commons Attribution CC BY 4.0. Any computer code (R, HTML, CSS, etc.) in slides and worksheets, including in slide and worksheet sources, is also licensed under MIT.\n\n\n\n", - "last_modified": "2023-11-12T08:37:48+01:00" + "last_modified": "2023-11-12T08:51:05+01:00" }, { "path": "LICENSE.html", "author": [], "contents": "\n\nContents\nMIT License\n\nMIT License\nCopyright (c) 2021 Olivier Gimenez\nPermission is hereby granted, free of charge, to any person obtaining a copy\nof this software and associated documentation files (the “Software”), to deal\nin the Software without restriction, including without limitation the rights\nto use, copy, modify, merge, publish, distribute, sublicense, and/or sell\ncopies of the Software, and to permit persons to whom the Software is\nfurnished to do so, subject to the following conditions:\nThe above copyright notice and this permission notice shall be included in all\ncopies or substantial portions of the Software.\nTHE SOFTWARE IS PROVIDED “AS IS”, WITHOUT WARRANTY OF ANY KIND, EXPRESS OR\nIMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,\nFITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE\nAUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER\nLIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,\nOUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE\nSOFTWARE.\n\n\n", - "last_modified": "2023-11-12T08:37:49+01:00" + "last_modified": "2023-11-12T08:51:05+01:00" } ], "collections": [] diff --git a/practicals/practical9.Rmd b/practicals/practical9.Rmd index 2b51a8d..82727b6 100644 --- a/practicals/practical9.Rmd +++ b/practicals/practical9.Rmd @@ -196,6 +196,47 @@ round(fit$BUGSoutput$summary[, -c(4,6)], 3) --- +Convert regression coefficients from scaled to non-scaled and compare to values used to generate data (from ) + +```{r} +sbzero <- fit$BUGSoutput$sims.matrix[,'mu.a'] +sbun <- fit$BUGSoutput$sims.matrix[,'b[1]'] +sbdeux <- fit$BUGSoutput$sims.matrix[,'b[2]'] + +mu <- mean(boo) +sg <- sd(boo) +``` + +--- + +```{r} +bzero <- sbzero - sbun*mu/sg + sbdeux*mu^2/(sg^2) +hist(bzero) +abline(v = -14, col = "red", lwd = 2) +mean(bzero) +``` + +--- + +```{r} +bun <- sbdeux/sg - 2 * sbdeux * mu / (sg^2) +hist(bun) +abline(v = 1.8, col = "red", lwd = 2) +mean(bun) +``` + +--- + +```{r} +bdeux <- sbdeux/(sg^2) +hist(bdeux) +abline(v = - 0.045, col = "red", lwd = 2) +mean(bdeux) +``` + +--- +## Frequentist approach + .tiny-font[ ```{r} library(lme4) @@ -213,3 +254,4 @@ Parameter estimates obtained with both approaches are very similar. visreg::visreg(fit_lme4, xvar = 'Temp') ``` ] + diff --git a/practicals/practical9.html b/practicals/practical9.html index e900af5..b39d43b 100644 --- a/practicals/practical9.html +++ b/practicals/practical9.html @@ -19,7 +19,7 @@ ### Olivier Gimenez ] .date[ -### last updated: 2023-11-11 +### last updated: 2023-11-12 ] --- @@ -142,13 +142,13 @@ ``` ``` -## Transect Temperature Anemones Temp -## 1 1 20.33973 53 0.1338975 -## 2 1 20.47855 48 0.2125261 -## 3 1 20.61737 40 0.2911548 -## 4 1 20.75619 41 0.3697834 -## 5 1 20.89500 32 0.4484121 -## 6 1 21.03382 35 0.5270407 +## Transect Temperature Anemones Temp +## 1 1 18.31928 48 -0.9742340 +## 2 1 18.31673 56 -0.9757048 +## 3 1 18.31417 51 -0.9771756 +## 4 1 18.31162 51 -0.9786464 +## 5 1 18.30906 48 -0.9801172 +## 6 1 18.30651 39 -0.9815880 ``` ] @@ -237,26 +237,98 @@ ``` ## mean sd 2.5% 50% 97.5% Rhat n.eff -## a[1] 3.748 0.046 3.659 3.750 3.838 1.001 3000 -## a[2] 3.612 0.054 3.508 3.612 3.716 1.001 3000 -## a[3] 4.096 0.035 4.027 4.097 4.163 1.001 3000 -## a[4] 4.486 0.050 4.386 4.487 4.581 1.001 3000 -## a[5] 4.057 0.042 3.978 4.056 4.138 1.001 3000 -## a[6] 3.401 0.053 3.295 3.400 3.507 1.002 1700 -## a[7] 3.621 0.068 3.484 3.621 3.754 1.003 1100 -## a[8] 3.562 0.038 3.486 3.563 3.636 1.002 910 -## a[9] 3.866 0.033 3.798 3.867 3.930 1.001 1800 -## a[10] 3.853 0.041 3.774 3.854 3.934 1.001 3000 -## b[1] -0.040 0.028 -0.094 -0.039 0.014 1.001 3000 -## b[2] -0.129 0.017 -0.163 -0.129 -0.095 1.001 3000 -## deviance 1333.022 4.910 1325.304 1332.328 1344.842 1.001 3000 -## mu.a 3.828 0.131 3.570 3.829 4.068 1.001 3000 -## sigma.a 0.377 0.121 0.229 0.351 0.664 1.006 3000 +## a[1] 4.142 0.050 4.045 4.142 4.240 1.001 3000 +## a[2] 4.290 0.059 4.177 4.289 4.405 1.002 1100 +## a[3] 4.372 0.049 4.276 4.371 4.468 1.001 3000 +## a[4] 3.579 0.098 3.391 3.578 3.767 1.001 1900 +## a[5] 4.286 0.028 4.228 4.286 4.341 1.001 3000 +## a[6] 4.840 0.030 4.782 4.840 4.898 1.001 3000 +## a[7] 3.366 0.046 3.276 3.366 3.453 1.001 3000 +## a[8] 4.175 0.032 4.113 4.175 4.237 1.001 2600 +## a[9] 4.630 0.023 4.585 4.631 4.677 1.001 3000 +## a[10] 3.563 0.043 3.476 3.563 3.648 1.001 3000 +## b[1] -0.023 0.029 -0.079 -0.023 0.035 1.001 2800 +## b[2] -0.153 0.023 -0.196 -0.153 -0.109 1.001 3000 +## deviance 1401.831 4.854 1394.333 1401.129 1413.014 1.002 1100 +## mu.a 4.125 0.191 3.746 4.127 4.500 1.001 3000 +## sigma.a 0.568 0.167 0.341 0.535 0.980 1.007 580 ``` ] --- +Convert regression coefficients from scaled to non-scaled and compare to values used to generate data (from <https://stats.stackexchange.com/questions/361995/how-to-convert-coefficients-from-quadratic-function-from-scaled-to-not-scaled-co>) + + +```r +sbzero <- fit$BUGSoutput$sims.matrix[,'mu.a'] +sbun <- fit$BUGSoutput$sims.matrix[,'b[1]'] +sbdeux <- fit$BUGSoutput$sims.matrix[,'b[2]'] + +mu <- mean(boo) +sg <- sd(boo) +``` + +--- + + +```r +bzero <- sbzero - sbun*mu/sg + sbdeux*mu^2/(sg^2) +hist(bzero) +abline(v = -14, col = "red", lwd = 2) +``` + +![](practical9_files/figure-html/unnamed-chunk-11-1.png)<!-- --> + +```r +mean(bzero) +``` + +``` +## [1] -15.89737 +``` + +--- + + +```r +bun <- sbdeux/sg - 2 * sbdeux * mu / (sg^2) +hist(bun) +abline(v = 1.8, col = "red", lwd = 2) +``` + +![](practical9_files/figure-html/unnamed-chunk-12-1.png)<!-- --> + +```r +mean(bun) +``` + +``` +## [1] 1.939693 +``` + +--- + + +```r +bdeux <- sbdeux/(sg^2) +hist(bdeux) +abline(v = - 0.045, col = "red", lwd = 2) +``` + +![](practical9_files/figure-html/unnamed-chunk-13-1.png)<!-- --> + +```r +mean(bdeux) +``` + +``` +## [1] -0.05066283 +``` + +--- +## Frequentist approach + .tiny-font[ ```r @@ -271,15 +343,15 @@ ## Family: poisson ( log ) ## Formula: Anemones ~ Temp + I(Temp^2) + (1 | Transect) ## Data: data -## AIC BIC logLik deviance df.resid -## 1382.2781 1395.4714 -687.1391 1374.2781 196 +## AIC BIC logLik deviance df.resid +## 1461.916 1475.109 -726.958 1453.916 196 ## Random effects: ## Groups Name Std.Dev. -## Transect (Intercept) 0.3026 +## Transect (Intercept) 0.4552 ## Number of obs: 200, groups: Transect, 10 ## Fixed Effects: ## (Intercept) Temp I(Temp^2) -## 3.83058 -0.04063 -0.12902 +## 4.12492 -0.02394 -0.15259 ``` ] @@ -293,8 +365,9 @@ visreg::visreg(fit_lme4, xvar = 'Temp') ``` -![](practical9_files/figure-html/unnamed-chunk-11-1.svg)<!-- --> +![](practical9_files/figure-html/unnamed-chunk-15-1.svg)<!-- --> ] + diff --git a/practicals/practical9_files/figure-html/unnamed-chunk-11-1.png b/practicals/practical9_files/figure-html/unnamed-chunk-11-1.png index 79d8e4d..9f244b4 100644 Binary files a/practicals/practical9_files/figure-html/unnamed-chunk-11-1.png and b/practicals/practical9_files/figure-html/unnamed-chunk-11-1.png differ diff --git a/practicals/practical9_files/figure-html/unnamed-chunk-11-1.svg b/practicals/practical9_files/figure-html/unnamed-chunk-11-1.svg index 7c8b2bf..1f37c2d 100644 --- a/practicals/practical9_files/figure-html/unnamed-chunk-11-1.svg +++ b/practicals/practical9_files/figure-html/unnamed-chunk-11-1.svg @@ -30,18 +30,15 @@ - - - - + - + - + @@ -72,64 +69,60 @@ - - - - - - - - - - + + + + + + - - + + - + + - + - + - - - - - - - - - + + + + + + + + - + - + - + - + - + - + - - - - + + + + @@ -140,206 +133,206 @@ - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + diff --git a/practicals/practical9_files/figure-html/unnamed-chunk-12-1.png b/practicals/practical9_files/figure-html/unnamed-chunk-12-1.png new file mode 100644 index 0000000..19beb10 Binary files /dev/null and b/practicals/practical9_files/figure-html/unnamed-chunk-12-1.png differ diff --git a/practicals/practical9_files/figure-html/unnamed-chunk-13-1.png b/practicals/practical9_files/figure-html/unnamed-chunk-13-1.png new file mode 100644 index 0000000..b320bad Binary files /dev/null and b/practicals/practical9_files/figure-html/unnamed-chunk-13-1.png differ diff --git a/practicals/practical9_files/figure-html/unnamed-chunk-15-1.svg b/practicals/practical9_files/figure-html/unnamed-chunk-15-1.svg new file mode 100644 index 0000000..1d2e8d5 --- /dev/null +++ b/practicals/practical9_files/figure-html/unnamed-chunk-15-1.svg @@ -0,0 +1,333 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + diff --git a/practicals/practical9_files/figure-html/unnamed-chunk-2-1.svg b/practicals/practical9_files/figure-html/unnamed-chunk-2-1.svg index b81caca..e1ddf5c 100644 --- a/practicals/practical9_files/figure-html/unnamed-chunk-2-1.svg +++ b/practicals/practical9_files/figure-html/unnamed-chunk-2-1.svg @@ -36,18 +36,24 @@ - + - + - + + + + + + + @@ -75,26 +81,34 @@ - - - - - + + + + + - - + + + + - - + + + + - - + + + + - - + + + + @@ -105,7 +119,7 @@ - + diff --git a/practicals/practical9_files/figure-html/unnamed-chunk-3-1.svg b/practicals/practical9_files/figure-html/unnamed-chunk-3-1.svg index 7a47d81..8431bb1 100644 --- a/practicals/practical9_files/figure-html/unnamed-chunk-3-1.svg +++ b/practicals/practical9_files/figure-html/unnamed-chunk-3-1.svg @@ -6,25 +6,31 @@ - + - + - + + + + + + + - + @@ -33,123 +39,134 @@ - + - + - + - + - + - + - + - + - + - + - + + + + - - - - - - - - - - + + + + + + - - + + + + - - + + + + - - + + + + - - - - - - - - - - - + + + + + + + + + + + - - - + + + + - - - + + + + - - - + + + + - - - + + + + - - - + + + + - - - + + + + - + - - - - - - - - + + + + + + + + diff --git a/practicals/practical9_files/figure-html/unnamed-chunk-4-1.svg b/practicals/practical9_files/figure-html/unnamed-chunk-4-1.svg index 989b40c..93c699a 100644 --- a/practicals/practical9_files/figure-html/unnamed-chunk-4-1.svg +++ b/practicals/practical9_files/figure-html/unnamed-chunk-4-1.svg @@ -9,51 +9,45 @@ - + - + - + - + - - - - - - - + - + - + - + - + - + - + - + - + @@ -111,287 +105,284 @@ - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + - - + + - - + + - - + + - - + + + + + + + + + + - - + + - - + + - - - - - - - - + + - - + + - - + + + - - + + + - - - + + + - - - - - - - - - - - - - - - - + + + + + + + + + + + + + + + +