<?xml version="1.0" encoding="utf-8" standalone="yes" ?>
<rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom">
  <channel>
    <title>Modeling | Alex Baecher</title>
    <link>https://questlab.eco/tag/modeling/</link>
      <atom:link href="https://questlab.eco/tag/modeling/index.xml" rel="self" type="application/rss+xml" />
    <description>Modeling</description>
    <generator>Hugo Blox Builder (https://hugoblox.com)</generator><language>en-us</language><copyright>© 2026 Alex Baecher</copyright><lastBuildDate>Wed, 08 Oct 2025 00:00:00 +0000</lastBuildDate>
    <image>
      <url>https://questlab.eco/media/icon_hu16270048066519736882.png</url>
      <title>Modeling</title>
      <link>https://questlab.eco/tag/modeling/</link>
    </image>
    
    <item>
      <title>Linking hosts, landscapes, and climate to advance zoonotic arbovirus forecasting</title>
      <link>https://questlab.eco/publication/anand-2026-scireports/</link>
      <pubDate>Wed, 08 Oct 2025 00:00:00 +0000</pubDate>
      <guid>https://questlab.eco/publication/anand-2026-scireports/</guid>
      <description>&lt;html&gt;
  &lt;style&gt;
    section {
        background: white;
        color: black;
        border-radius: 1em;
        padding: 1em;
        left: 50% }
    #inner {
        display: inline-block;
        display: flex;
        align-items: center;
        justify-content: center }
  &lt;/style&gt;
  &lt;section&gt;
    &lt;div id=&#34;inner&#34;&gt;
      &lt;script type=&#39;text/javascript&#39; src=&#39;https://d1bxh8uas1mnw7.cloudfront.net/assets/embed.js&#39;&gt;&lt;/script&gt;
        &lt;span style=&#34;float:left&#34;; 
          class=&#34;__dimensions_badge_embed__&#34; 
          data-doi=&#34;	10.1038/s41598-026-46902-2&#34; 
          data-hide-zero-citations=&#34;true&#34; 
          data-legend=&#34;always&#34;&gt;
        &lt;/span&gt;
      &lt;script async src=&#34;https://badge.dimensions.ai/badge.js&#34; charset=&#34;utf-8&#34;&gt;&lt;/script&gt;
        &lt;div  style=&#34;float:right&#34;; 
          data-link-target=&#34;_blank&#34; 
          data-badge-details=&#34;right&#34; 
          data-badge-type=&#34;medium-donut&#34;
          data-doi=&#34;	10.1038/s41598-026-46902-2&#34;   
          data-condensed=&#34;true&#34; 
          data-hide-no-mentions=&#34;true&#34; 
          class=&#34;altmetric-embed&#34;&gt;
        &lt;/div&gt;
  &lt;/section&gt;</description>
    </item>
    
    <item>
      <title>Toward ecological forecasting of West Nile virus in Florida: Insights from two decades of sentinel chicken surveillance</title>
      <link>https://questlab.eco/publication/baecher-2025-stoten/</link>
      <pubDate>Tue, 09 Sep 2025 00:00:00 +0000</pubDate>
      <guid>https://questlab.eco/publication/baecher-2025-stoten/</guid>
      <description>&lt;html&gt;
  &lt;style&gt;
    section {
        background: white;
        color: black;
        border-radius: 1em;
        padding: 1em;
        left: 50% }
    #inner {
        display: inline-block;
        display: flex;
        align-items: center;
        justify-content: center }
  &lt;/style&gt;
  &lt;section&gt;
    &lt;div id=&#34;inner&#34;&gt;
      &lt;script type=&#39;text/javascript&#39; src=&#39;https://d1bxh8uas1mnw7.cloudfront.net/assets/embed.js&#39;&gt;&lt;/script&gt;
        &lt;span style=&#34;float:left&#34;; 
          class=&#34;__dimensions_badge_embed__&#34; 
          data-doi=&#34;10.1016/j.scitotenv.2025.180308&#34; 
          data-hide-zero-citations=&#34;true&#34; 
          data-legend=&#34;always&#34;&gt;
        &lt;/span&gt;
      &lt;script async src=&#34;https://badge.dimensions.ai/badge.js&#34; charset=&#34;utf-8&#34;&gt;&lt;/script&gt;
        &lt;div  style=&#34;float:right&#34;; 
          data-link-target=&#34;_blank&#34; 
          data-badge-details=&#34;right&#34; 
          data-badge-type=&#34;medium-donut&#34;
          data-doi=&#34;10.1016/j.scitotenv.2025.180308&#34;   
          data-condensed=&#34;true&#34; 
          data-hide-no-mentions=&#34;true&#34; 
          class=&#34;altmetric-embed&#34;&gt;
        &lt;/div&gt;
  &lt;/section&gt;
</description>
    </item>
    
    <item>
      <title>Citizen Science for Monitoring Birds</title>
      <link>https://questlab.eco/project/modeling-bird-populations/</link>
      <pubDate>Fri, 01 Aug 2025 00:00:00 +0000</pubDate>
      <guid>https://questlab.eco/project/modeling-bird-populations/</guid>
      <description>&lt;img src=&#34;https://questlab.eco/media/birds_western.jpg&#34; alt=&#34;Citizen science data for high priority migratory birds in the western US&#34;&gt;
</description>
    </item>
    
    <item>
      <title>Integrated population modeling</title>
      <link>https://questlab.eco/project/integrated-population-modeling/</link>
      <pubDate>Sat, 15 Feb 2025 00:00:00 +0000</pubDate>
      <guid>https://questlab.eco/project/integrated-population-modeling/</guid>
      <description></description>
    </item>
    
    <item>
      <title>Biodiversity redistribution</title>
      <link>https://questlab.eco/project/climate-species-redistribution/</link>
      <pubDate>Fri, 01 Mar 2024 00:00:00 +0000</pubDate>
      <guid>https://questlab.eco/project/climate-species-redistribution/</guid>
      <description></description>
    </item>
    
    <item>
      <title>Spatiotemporal statistics</title>
      <link>https://questlab.eco/project/geostatistical-modeling/</link>
      <pubDate>Thu, 15 Feb 2024 00:00:00 +0000</pubDate>
      <guid>https://questlab.eco/project/geostatistical-modeling/</guid>
      <description>&lt;p&gt;&lt;strong&gt;Spatiotemporal modeling&lt;/strong&gt;
Basic generalized linear mixed effects models can account for spatial (and spatiotemporal) autocorrelation using latent Gaussian random fields (GRFs). However, GRFs are computationally intense due to costly covariance functions. Stochastic Partial Differential Equations (SPDE) can be used to minimize the scale of this problem by approximating GRFs with Gaussian Markcov random fields (GMRFs) with a Matern covariance function (Lindgrin et al., 2011; Krainski et al. 2018). This solution has been adopted by the Integrated Nested Laplace Approximation (INLA) software, and has later been implimented in Template Model Builder (TMB) software (Thorson et al., 2015).&lt;/p&gt;
</description>
    </item>
    
    <item>
      <title>Landscape connectivity</title>
      <link>https://questlab.eco/project/landscape-ecology/</link>
      <pubDate>Wed, 15 Mar 2023 00:00:00 +0000</pubDate>
      <guid>https://questlab.eco/project/landscape-ecology/</guid>
      <description>&lt;p&gt;&lt;strong&gt;Publications&lt;/strong&gt;&lt;br&gt;
&lt;em&gt;&lt;strong&gt;Forthcoming&lt;/strong&gt;&lt;/em&gt;&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;Baecher, JA, et al. .&lt;strong&gt;In review&lt;/strong&gt;. Isolating what limits the spread of an invasive predator. &lt;em&gt;Journal of Applied Ecology&lt;/em&gt; &lt;a href=&#34;https://advance.sagepub.com/doi/full/10.22541/au.171248322.23946990/v1&#34; target=&#34;_blank&#34; rel=&#34;noopener&#34;&gt;https://advance.sagepub.com/doi/full/10.22541/au.171248322.23946990/v1&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;Baecher, JA, et al. .&lt;em&gt;In prep&lt;/em&gt;. Informing species range shifts with global connectivity. &lt;em&gt;TBA&lt;/em&gt;&lt;/li&gt;
&lt;/ul&gt;
</description>
    </item>
    
    <item>
      <title>Linear regression with gradient descent</title>
      <link>https://questlab.eco/post/gradient-descent/</link>
      <pubDate>Wed, 22 Sep 2021 00:40:04 -0700</pubDate>
      <guid>https://questlab.eco/post/gradient-descent/</guid>
      <description>&lt;h2 id=&#34;introduction-linear-regression-with-gradient-descent&#34;&gt;Introduction linear regression with gradient descent&lt;/h2&gt;
&lt;p&gt;This tutorial is a rough introduction into using gradient descent algorithms to estimate parameters (slope and intercept) for standard linear regressions, as an alternative to ordinary least squares (OLS) regression with a maximum likelihood estimator. To begin, I simulate data to perform a standard OLS regression with maximum likelihood using sums of squares. Once explained, I then demonstrate how to substitute gradient descent simply and interpret results.&lt;/p&gt;
&lt;pre&gt;&lt;code&gt;library(tidyverse)

## -- Attaching packages --------------------------------------- tidyverse 1.3.1 --

## v ggplot2 3.3.5     v purrr   0.3.4
## v tibble  3.1.3     v dplyr   1.0.7
## v tidyr   1.1.3     v stringr 1.4.0
## v readr   2.0.1     v forcats 0.5.1

## Warning: package &#39;readr&#39; was built under R version 4.1.1

## -- Conflicts ------------------------------------------ tidyverse_conflicts() --
## x dplyr::filter() masks stats::filter()
## x dplyr::lag()    masks stats::lag()
&lt;/code&gt;&lt;/pre&gt;
&lt;h1 id=&#34;ordinary-least-square-regression&#34;&gt;Ordinary Least Square Regression&lt;/h1&gt;
&lt;h2 id=&#34;simulate-data&#34;&gt;Simulate data&lt;/h2&gt;
&lt;h3 id=&#34;generate-random-data-in-which-y-is-a-noisy-function-of-x&#34;&gt;Generate random data in which y is a noisy function of x&lt;/h3&gt;
&lt;pre&gt;&lt;code&gt;set.seed(123)

x &amp;lt;- runif(1000, -5, 5)
y &amp;lt;- x + rnorm(1000) + 3
&lt;/code&gt;&lt;/pre&gt;
&lt;h3 id=&#34;fit-a-linear-model&#34;&gt;Fit a linear model&lt;/h3&gt;
&lt;pre&gt;&lt;code&gt;lm &amp;lt;- lm( y ~ x ) # Ordinary Least Squares regression with General Linear Model 
mod &amp;lt;- print(lm)

## 
## Call:
## lm(formula = y ~ x)
## 
## Coefficients:
## (Intercept)            x  
##      3.0118       0.9942

mod

## 
## Call:
## lm(formula = y ~ x)
## 
## Coefficients:
## (Intercept)            x  
##      3.0118       0.9942
&lt;/code&gt;&lt;/pre&gt;
&lt;h3 id=&#34;plot-the-data-and-the-model&#34;&gt;Plot the data and the model&lt;/h3&gt;
&lt;pre&gt;&lt;code&gt;plot(x,y, col = &amp;quot;grey80&amp;quot;, main=&#39;Regression using lm()&#39;, xlim = c(-2, 5), ylim = c(0,10)); 
text(0, 8, paste(&amp;quot;Intercept = &amp;quot;, round(mod$coefficients[1], 2), sep = &amp;quot;&amp;quot;));
text(4, 2, paste(&amp;quot;Slope = &amp;quot;, round(mod$coefficients[2], 2), sep = &amp;quot;&amp;quot;));
abline(v = 0, col = &amp;quot;grey80&amp;quot;); # line for y-intercept
abline(h = mod$coefficients[1], col = &amp;quot;grey80&amp;quot;) # plot horizontal line at intercept value
abline(a = mod$coefficients[1], b = mod$coefficients[2], col=&#39;blue&#39;, lwd=2) # use slope and intercept to plot best fit line
&lt;/code&gt;&lt;/pre&gt;


















&lt;figure  &gt;
  &lt;div class=&#34;d-flex justify-content-center&#34;&gt;
    &lt;div class=&#34;w-100&#34; &gt;&lt;img alt=&#34; &#34; srcset=&#34;
               /media/posts/lr_files/figure-markdown_strict/unnamed-chunk-4-1_hu8551663495961673042.webp 400w,
               /media/posts/lr_files/figure-markdown_strict/unnamed-chunk-4-1_hu9486235095446895088.webp 760w,
               /media/posts/lr_files/figure-markdown_strict/unnamed-chunk-4-1_hu8859505797292339637.webp 1200w&#34;
               src=&#34;https://questlab.eco/media/posts/lr_files/figure-markdown_strict/unnamed-chunk-4-1_hu8551663495961673042.webp&#34;
               width=&#34;672&#34;
               height=&#34;480&#34;
               loading=&#34;lazy&#34; data-zoomable /&gt;&lt;/div&gt;
  &lt;/div&gt;&lt;figcaption&gt;
      
    &lt;/figcaption&gt;&lt;/figure&gt;

&lt;h3 id=&#34;calculate-intercept-and-slope-using-sum-of-squares&#34;&gt;Calculate intercept and slope using sum of squares&lt;/h3&gt;
&lt;pre&gt;&lt;code&gt;x_bar &amp;lt;- mean(x) # calculate mean of independent variable
y_bar &amp;lt;- mean(y) # calculate mean of dependent variable

slope &amp;lt;- sum((x - x_bar)*(y - y_bar))/sum((x - x_bar)^2) # calculate sum of differences between x &amp;amp; y, and divide by sum of squares of x
slope

## [1] 0.9941662

intercept &amp;lt;- y_bar - (slope * x_bar) # calculate difference of y_bar across the linear predictor
intercept

## [1] 3.011774
&lt;/code&gt;&lt;/pre&gt;
&lt;h3 id=&#34;plot-data-using-manually-calculated-parameters&#34;&gt;Plot data using manually calculated parameters&lt;/h3&gt;
&lt;pre&gt;&lt;code&gt;plot(x,y, col = &amp;quot;grey80&amp;quot;, main=&#39;Regression with manual calculations&#39;, xlim = c(-2, 5), ylim = c(0,10)); 
abline(a = intercept, b = slope, col=&#39;blue&#39;, lwd=2)
&lt;/code&gt;&lt;/pre&gt;


















&lt;figure  &gt;
  &lt;div class=&#34;d-flex justify-content-center&#34;&gt;
    &lt;div class=&#34;w-100&#34; &gt;&lt;img alt=&#34; &#34; srcset=&#34;
               /media/posts/lr_files/figure-markdown_strict/unnamed-chunk-6-1_hu2949314009040170375.webp 400w,
               /media/posts/lr_files/figure-markdown_strict/unnamed-chunk-6-1_hu15171377414764716868.webp 760w,
               /media/posts/lr_files/figure-markdown_strict/unnamed-chunk-6-1_hu14793946180658159100.webp 1200w&#34;
               src=&#34;https://questlab.eco/media/posts/lr_files/figure-markdown_strict/unnamed-chunk-6-1_hu2949314009040170375.webp&#34;
               width=&#34;672&#34;
               height=&#34;480&#34;
               loading=&#34;lazy&#34; data-zoomable /&gt;&lt;/div&gt;
  &lt;/div&gt;&lt;figcaption&gt;
      
    &lt;/figcaption&gt;&lt;/figure&gt;

&lt;h1 id=&#34;gradient-descent&#34;&gt;Gradient Descent:&lt;/h1&gt;
&lt;h2 id=&#34;using-the-same-simulated-data-as-before-we-will-estimate-parameters-using-a-machine-learning-algorithm&#34;&gt;Using the same simulated data as before, we will estimate parameters using a machine learning algorithm&lt;/h2&gt;
&lt;h3 id=&#34;heres-some-figures-i-found-helpful-while-trying-to-understand-how-gradient-descent-works&#34;&gt;Here&amp;rsquo;s some figures I found helpful while trying to understand how gradient descent works:&lt;/h3&gt;


















&lt;figure  &gt;
  &lt;div class=&#34;d-flex justify-content-center&#34;&gt;
    &lt;div class=&#34;w-100&#34; &gt;&lt;img alt=&#34; &#34; srcset=&#34;
               /media/posts/lr_files/figure-markdown_strict/hiking_analogy_hu15159196870089607687.webp 400w,
               /media/posts/lr_files/figure-markdown_strict/hiking_analogy_hu8643088186133544508.webp 760w,
               /media/posts/lr_files/figure-markdown_strict/hiking_analogy_hu16647294568684891759.webp 1200w&#34;
               src=&#34;https://questlab.eco/media/posts/lr_files/figure-markdown_strict/hiking_analogy_hu15159196870089607687.webp&#34;
               width=&#34;700&#34;
               height=&#34;465&#34;
               loading=&#34;lazy&#34; data-zoomable /&gt;&lt;/div&gt;
  &lt;/div&gt;&lt;figcaption&gt;
      
    &lt;/figcaption&gt;&lt;/figure&gt;



















&lt;figure  &gt;
  &lt;div class=&#34;d-flex justify-content-center&#34;&gt;
    &lt;div class=&#34;w-100&#34; &gt;&lt;img alt=&#34; &#34; srcset=&#34;
               /media/posts/lr_files/figure-markdown_strict/lr_diagram_hu2448971957808616965.webp 400w,
               /media/posts/lr_files/figure-markdown_strict/lr_diagram_hu12845061643091946287.webp 760w,
               /media/posts/lr_files/figure-markdown_strict/lr_diagram_hu17409504747599895843.webp 1200w&#34;
               src=&#34;https://questlab.eco/media/posts/lr_files/figure-markdown_strict/lr_diagram_hu2448971957808616965.webp&#34;
               width=&#34;760&#34;
               height=&#34;473&#34;
               loading=&#34;lazy&#34; data-zoomable /&gt;&lt;/div&gt;
  &lt;/div&gt;&lt;figcaption&gt;
      
    &lt;/figcaption&gt;&lt;/figure&gt;

&lt;h3 id=&#34;to-determine-the-goodness-of-fit-for-a-given-set-of-parameters-we-will-empliment-a-squared-error-cost-function-a-way-to-calculate-the-degree-of-error-for-a-guess-for-slope-and-intercept&#34;&gt;To determine the goodness of fit for a given set of parameters, we will empliment a Squared error cost function (a way to calculate the degree of error for a guess for slope and intercept)&lt;/h3&gt;
&lt;pre&gt;&lt;code&gt;cost &amp;lt;- function(X, y, theta) {
  sum( (X %*% theta - y)^2 ) / (2*length(y))
}
&lt;/code&gt;&lt;/pre&gt;
&lt;h3 id=&#34;we-must-also-set-two-additional-parameters-learning-rate-and-iteration-limit&#34;&gt;We must also set two additional parameters: learning rate and iteration limit&lt;/h3&gt;
&lt;pre&gt;&lt;code&gt;alpha &amp;lt;- 0.01
num_iters &amp;lt;- 1000

# keep history
cost_history &amp;lt;- double(num_iters)
theta_history &amp;lt;- list(num_iters)

# initialize coefficients
theta &amp;lt;- matrix(c(0,0), nrow=2)

# add a column of 1&#39;s for the intercept coefficient
X &amp;lt;- cbind(1, matrix(x))

# gradient descent
for (i in 1:num_iters) {
  error &amp;lt;- (X %*% theta - y)
  delta &amp;lt;- t(X) %*% error / length(y)
  theta &amp;lt;- theta - alpha * delta
  cost_history[i] &amp;lt;- cost(X, y, theta)
  theta_history[[i]] &amp;lt;- theta
}

print(theta)

##           [,1]
## [1,] 3.0116439
## [2,] 0.9941657
&lt;/code&gt;&lt;/pre&gt;
&lt;h3 id=&#34;plot-data-and-converging-fit&#34;&gt;Plot data and converging fit&lt;/h3&gt;
&lt;pre&gt;&lt;code&gt;iters &amp;lt;- c((1:31)^2, 1000)
cols &amp;lt;- rev(terrain.colors(num_iters))
library(gifski)
png(&amp;quot;frame%03d.png&amp;quot;)
par(ask = FALSE)

for (i in iters) {
  plot(x,y, col=&amp;quot;grey80&amp;quot;, main=&#39;Linear regression using Gradient Descent&#39;)
  text(x = -3, y = 10, paste(&amp;quot;slope = &amp;quot;, round(theta_history[[i]][2], 3), sep = &amp;quot; &amp;quot;), adj = 0)
  text(x = -3, y = 8, paste(&amp;quot;intercept = &amp;quot;, round(theta_history[[i]][1], 3), sep = &amp;quot; &amp;quot;), adj = 0)
  abline(coef=theta_history[[i]], col=cols[i], lwd = 2)
}

dev.off()

## png 
##   2

png_files &amp;lt;- sprintf(&amp;quot;frame%03d.png&amp;quot;, 1:32)
gif_file &amp;lt;- gifski(png_files, delay = 0.1)
unlink(png_files)
utils::browseURL(gif_file)
&lt;/code&gt;&lt;/pre&gt;
&lt;h3 id=&#34;calculate-intercept-and-slope-using-gradient-descent-machine-learning&#34;&gt;Calculate intercept and slope using gradient descent (Machine Learning):&lt;/h3&gt;


















&lt;figure  &gt;
  &lt;div class=&#34;d-flex justify-content-center&#34;&gt;
    &lt;div class=&#34;w-100&#34; &gt;&lt;img alt=&#34; &#34;
           src=&#34;https://questlab.eco/media/posts/lr_files/figure-markdown_strict/lrgd.gif&#34;
           loading=&#34;lazy&#34; data-zoomable /&gt;&lt;/div&gt;
  &lt;/div&gt;&lt;figcaption&gt;
      
    &lt;/figcaption&gt;&lt;/figure&gt;

&lt;pre&gt;&lt;code&gt;plot(cost_history, type=&#39;line&#39;, col=&#39;blue&#39;, lwd=2, main=&#39;Cost function&#39;, ylab=&#39;cost&#39;, xlab=&#39;Iterations&#39;)

## Warning in plot.xy(xy, type, ...): plot type &#39;line&#39; will be truncated to first
## character
&lt;/code&gt;&lt;/pre&gt;


















&lt;figure  &gt;
  &lt;div class=&#34;d-flex justify-content-center&#34;&gt;
    &lt;div class=&#34;w-100&#34; &gt;&lt;img alt=&#34; &#34; srcset=&#34;
               /media/posts/lr_files/figure-markdown_strict/unnamed-chunk-10-1_hu15226046985714386052.webp 400w,
               /media/posts/lr_files/figure-markdown_strict/unnamed-chunk-10-1_hu13749904028915559076.webp 760w,
               /media/posts/lr_files/figure-markdown_strict/unnamed-chunk-10-1_hu11794761284791704341.webp 1200w&#34;
               src=&#34;https://questlab.eco/media/posts/lr_files/figure-markdown_strict/unnamed-chunk-10-1_hu15226046985714386052.webp&#34;
               width=&#34;672&#34;
               height=&#34;480&#34;
               loading=&#34;lazy&#34; data-zoomable /&gt;&lt;/div&gt;
  &lt;/div&gt;&lt;figcaption&gt;
      
    &lt;/figcaption&gt;&lt;/figure&gt;

&lt;h1 id=&#34;using-gradient-descent-with-real-data&#34;&gt;Using gradient descent with real data&lt;/h1&gt;
&lt;p&gt;I&amp;rsquo;ll demonstrate it&amp;rsquo;s features using an existing dataset from Bruno Oliveria: &amp;ldquo;Amphibio&amp;rdquo;:&lt;br&gt;
• Link to publication: &lt;a href=&#34;https://www.nature.com/articles/sdata2017123&#34; target=&#34;_blank&#34; rel=&#34;noopener&#34;&gt;https://www.nature.com/articles/sdata2017123&lt;/a&gt;&lt;br&gt;
• Link to data: &lt;a href=&#34;https://ndownloader.figstatic.com/files/8828578&#34; target=&#34;_blank&#34; rel=&#34;noopener&#34;&gt;https://ndownloader.figstatic.com/files/8828578&lt;/a&gt;&lt;/p&gt;
&lt;h3 id=&#34;load-amphibio-data&#34;&gt;Load amphibio data!&lt;/h3&gt;
&lt;pre&gt;&lt;code&gt;install.packages(&amp;quot;downloader&amp;quot;)
library(downloader)

url &amp;lt;- &amp;quot;https://ndownloader.figstatic.com/files/8828578&amp;quot;
download(url, dest=&amp;quot;lrgb/amphibio.zip&amp;quot;, mode=&amp;quot;wb&amp;quot;) 
unzip(&amp;quot;lrgb/amphibio.zip&amp;quot;, exdir = &amp;quot;./lrgb&amp;quot;)

df &amp;lt;- read_csv(&amp;quot;AmphiBIO_v1.csv&amp;quot;) %&amp;gt;%
  select(&amp;quot;Order&amp;quot;,
         &amp;quot;Body_mass_g&amp;quot;,
         &amp;quot;Body_size_mm&amp;quot;,
         &amp;quot;Size_at_maturity_min_mm&amp;quot;,
         &amp;quot;Size_at_maturity_max_mm&amp;quot;,
         &amp;quot;Litter_size_min_n&amp;quot;,
         &amp;quot;Litter_size_max_n&amp;quot;,
         &amp;quot;Reproductive_output_y&amp;quot;) %&amp;gt;%
  na.omit %&amp;gt;%
  mutate_if(is.numeric, ~ log(.))

## Rows: 6776 Columns: 38

## -- Column specification --------------------------------------------------------
## Delimiter: &amp;quot;,&amp;quot;
## chr  (6): id, Order, Family, Genus, Species, OBS
## dbl (31): Fos, Ter, Aqu, Arb, Leaves, Flowers, Seeds, Arthro, Vert, Diu, Noc...
## lgl  (1): Fruits

## 
## i Use `spec()` to retrieve the full column specification for this data.
## i Specify the column types or set `show_col_types = FALSE` to quiet this message.

plot(df$Body_size_mm, df$Size_at_maturity_max_mm, col = &amp;quot;grey80&amp;quot;, main=&#39;Correlation of amphibian traits&#39;, xlab = &amp;quot;Body size (mm)&amp;quot;, ylab = &amp;quot;Max size at maturity (mm)&amp;quot;); 
&lt;/code&gt;&lt;/pre&gt;


















&lt;figure  &gt;
  &lt;div class=&#34;d-flex justify-content-center&#34;&gt;
    &lt;div class=&#34;w-100&#34; &gt;&lt;img alt=&#34; &#34; srcset=&#34;
               /media/posts/lr_files/figure-markdown_strict/unnamed-chunk-11-1_hu893240730196825435.webp 400w,
               /media/posts/lr_files/figure-markdown_strict/unnamed-chunk-11-1_hu13026316878176312818.webp 760w,
               /media/posts/lr_files/figure-markdown_strict/unnamed-chunk-11-1_hu14878789998347573284.webp 1200w&#34;
               src=&#34;https://questlab.eco/media/posts/lr_files/figure-markdown_strict/unnamed-chunk-11-1_hu893240730196825435.webp&#34;
               width=&#34;672&#34;
               height=&#34;480&#34;
               loading=&#34;lazy&#34; data-zoomable /&gt;&lt;/div&gt;
  &lt;/div&gt;&lt;figcaption&gt;
      
    &lt;/figcaption&gt;&lt;/figure&gt;

&lt;h3 id=&#34;fit-a-linear-model-1&#34;&gt;Fit a linear model&lt;/h3&gt;
&lt;pre&gt;&lt;code&gt;lm &amp;lt;- lm(Size_at_maturity_max_mm ~ Body_size_mm, data = df) # Ordinary Least Squares regression with General Linear Model 
mod &amp;lt;- print(lm)

## 
## Call:
## lm(formula = Size_at_maturity_max_mm ~ Body_size_mm, data = df)
## 
## Coefficients:
##  (Intercept)  Body_size_mm  
##       0.6237        0.7265

mod

## 
## Call:
## lm(formula = Size_at_maturity_max_mm ~ Body_size_mm, data = df)
## 
## Coefficients:
##  (Intercept)  Body_size_mm  
##       0.6237        0.7265
&lt;/code&gt;&lt;/pre&gt;
&lt;h3 id=&#34;plot-the-data-and-the-model-1&#34;&gt;Plot the data and the model&lt;/h3&gt;
&lt;pre&gt;&lt;code&gt;plot(df$Body_size_mm, df$Size_at_maturity_max_mm, col = &amp;quot;grey80&amp;quot;, main=&#39;Linear Regression using Sum of Squares&#39;, xlab = &amp;quot;Body size (mm)&amp;quot;, ylab = &amp;quot;Max size at maturity (mm)&amp;quot;); 
text(4, 5, paste(&amp;quot;Intercept = &amp;quot;, round(mod$coefficients[1], 2), sep = &amp;quot;&amp;quot;));
text(6, 3, paste(&amp;quot;Slope = &amp;quot;, round(mod$coefficients[2], 2), sep = &amp;quot;&amp;quot;));
abline(a = mod$coefficients[1], b = mod$coefficients[2], col=&#39;blue&#39;, lwd=2) # use slope and intercept to plot best fit line
&lt;/code&gt;&lt;/pre&gt;


















&lt;figure  &gt;
  &lt;div class=&#34;d-flex justify-content-center&#34;&gt;
    &lt;div class=&#34;w-100&#34; &gt;&lt;img alt=&#34; &#34; srcset=&#34;
               /media/posts/lr_files/figure-markdown_strict/unnamed-chunk-13-1_hu1969989005240059808.webp 400w,
               /media/posts/lr_files/figure-markdown_strict/unnamed-chunk-13-1_hu12803367816218472457.webp 760w,
               /media/posts/lr_files/figure-markdown_strict/unnamed-chunk-13-1_hu3570476343014275261.webp 1200w&#34;
               src=&#34;https://questlab.eco/media/posts/lr_files/figure-markdown_strict/unnamed-chunk-13-1_hu1969989005240059808.webp&#34;
               width=&#34;672&#34;
               height=&#34;480&#34;
               loading=&#34;lazy&#34; data-zoomable /&gt;&lt;/div&gt;
  &lt;/div&gt;&lt;figcaption&gt;
      
    &lt;/figcaption&gt;&lt;/figure&gt;

&lt;h3 id=&#34;calculate-intercept-and-slope-using-sum-of-squares-1&#34;&gt;Calculate intercept and slope using sum of squares&lt;/h3&gt;
&lt;pre&gt;&lt;code&gt;x &amp;lt;- df$Body_size_mm
y &amp;lt;- df$Size_at_maturity_max_mm
x_bar &amp;lt;- mean(x) # calculate mean of independent variable
y_bar &amp;lt;- mean(y) # calculate mean of dependent variable

slope &amp;lt;- sum((x - x_bar)*(y - y_bar))/sum((x - x_bar)^2) # calculate sum of differences between x &amp;amp; y, and divide by sum of squares of x
slope

## [1] 0.7264703

intercept &amp;lt;- y_bar - (slope * x_bar) # calculate difference of y_bar across the linear predictor
intercept

## [1] 0.6237047

### plot data using manually calculated parameters
plot(x,y, col = &amp;quot;grey80&amp;quot;, main=&#39;Linear Regression using Ordinary Least Squares&#39;, xlab = &amp;quot;Body size (mm)&amp;quot;, ylab = &amp;quot;Max size at maturity (mm)&amp;quot;); 
abline(a = intercept, b = slope, col=&#39;blue&#39;, lwd=2)
&lt;/code&gt;&lt;/pre&gt;


















&lt;figure  &gt;
  &lt;div class=&#34;d-flex justify-content-center&#34;&gt;
    &lt;div class=&#34;w-100&#34; &gt;&lt;img alt=&#34; &#34; srcset=&#34;
               /media/posts/lr_files/figure-markdown_strict/unnamed-chunk-14-1_hu16688363695195450873.webp 400w,
               /media/posts/lr_files/figure-markdown_strict/unnamed-chunk-14-1_hu11796089762525742180.webp 760w,
               /media/posts/lr_files/figure-markdown_strict/unnamed-chunk-14-1_hu6382700030778350265.webp 1200w&#34;
               src=&#34;https://questlab.eco/media/posts/lr_files/figure-markdown_strict/unnamed-chunk-14-1_hu16688363695195450873.webp&#34;
               width=&#34;672&#34;
               height=&#34;480&#34;
               loading=&#34;lazy&#34; data-zoomable /&gt;&lt;/div&gt;
  &lt;/div&gt;&lt;figcaption&gt;
      
    &lt;/figcaption&gt;&lt;/figure&gt;

&lt;h3 id=&#34;calculate-intercept-and-slope-using-gradient-descent-machine-learning-1&#34;&gt;Calculate intercept and slope using gradient descent (Machine Learning)&lt;/h3&gt;
&lt;h3 id=&#34;squared-error-cost-function-a-way-to-calculate-the-degree-of-error-for-a-guess-for-slope-and-intercept&#34;&gt;Squared error cost function (a way to calculate the degree of error for a guess for slope and intercept)&lt;/h3&gt;
&lt;pre&gt;&lt;code&gt;### learning rate and iteration limit
alpha &amp;lt;- 0.001
num_iters &amp;lt;- 1000

### keep history
cost_history &amp;lt;- double(num_iters)
theta_history &amp;lt;- list(num_iters)

### initialize coefficients
theta &amp;lt;- matrix(c(0,0), nrow=2)

### add a column of 1&#39;s for the intercept coefficient
X &amp;lt;- cbind(1, matrix(x))

# gradient descent
for (i in 1:num_iters) {
  error &amp;lt;- (X %*% theta - y)
  delta &amp;lt;- t(X) %*% error / length(y)
  theta &amp;lt;- theta - alpha * delta
  cost_history[i] &amp;lt;- cost(X, y, theta)
  theta_history[[i]] &amp;lt;- theta
}

print(theta)

##           [,1]
## [1,] 0.1816407
## [2,] 0.8175962
&lt;/code&gt;&lt;/pre&gt;
&lt;h3 id=&#34;plot-data-and-converging-fit-1&#34;&gt;Plot data and converging fit&lt;/h3&gt;
&lt;pre&gt;&lt;code&gt;plot(x,y, col=&amp;quot;grey80&amp;quot;, main=&#39;Linear regression using Gradient Descent&#39;, xlab = &amp;quot;Body size (mm)&amp;quot;, ylab = &amp;quot;Max size at maturity (mm)&amp;quot;)
for (i in c((1:31)^2, 1000)) {
  abline(coef=theta_history[[i]], col=&amp;quot;red&amp;quot;)
}
abline(coef=theta, col=&amp;quot;blue&amp;quot;, lwd = 2)
&lt;/code&gt;&lt;/pre&gt;


















&lt;figure  &gt;
  &lt;div class=&#34;d-flex justify-content-center&#34;&gt;
    &lt;div class=&#34;w-100&#34; &gt;&lt;img alt=&#34; &#34; srcset=&#34;
               /media/posts/lr_files/figure-markdown_strict/unnamed-chunk-16-1_hu5100640028345161415.webp 400w,
               /media/posts/lr_files/figure-markdown_strict/unnamed-chunk-16-1_hu3901677043462376067.webp 760w,
               /media/posts/lr_files/figure-markdown_strict/unnamed-chunk-16-1_hu8737325766736597265.webp 1200w&#34;
               src=&#34;https://questlab.eco/media/posts/lr_files/figure-markdown_strict/unnamed-chunk-16-1_hu5100640028345161415.webp&#34;
               width=&#34;672&#34;
               height=&#34;480&#34;
               loading=&#34;lazy&#34; data-zoomable /&gt;&lt;/div&gt;
  &lt;/div&gt;&lt;figcaption&gt;
      
    &lt;/figcaption&gt;&lt;/figure&gt;

&lt;pre&gt;&lt;code&gt;plot(cost_history, type=&#39;line&#39;, col=&#39;blue&#39;, lwd=2, main=&#39;Cost function&#39;, ylab=&#39;cost&#39;, xlab=&#39;Iterations&#39;)

## Warning in plot.xy(xy, type, ...): plot type &#39;line&#39; will be truncated to first
## character
&lt;/code&gt;&lt;/pre&gt;


















&lt;figure  &gt;
  &lt;div class=&#34;d-flex justify-content-center&#34;&gt;
    &lt;div class=&#34;w-100&#34; &gt;&lt;img alt=&#34; &#34; srcset=&#34;
               /media/posts/lr_files/figure-markdown_strict/unnamed-chunk-17-1_hu17270757539472694698.webp 400w,
               /media/posts/lr_files/figure-markdown_strict/unnamed-chunk-17-1_hu11148887372339103562.webp 760w,
               /media/posts/lr_files/figure-markdown_strict/unnamed-chunk-17-1_hu14433026510257978825.webp 1200w&#34;
               src=&#34;https://questlab.eco/media/posts/lr_files/figure-markdown_strict/unnamed-chunk-17-1_hu17270757539472694698.webp&#34;
               width=&#34;672&#34;
               height=&#34;480&#34;
               loading=&#34;lazy&#34; data-zoomable /&gt;&lt;/div&gt;
  &lt;/div&gt;&lt;figcaption&gt;
      
    &lt;/figcaption&gt;&lt;/figure&gt;

</description>
    </item>
    
  </channel>
</rss>
