Skip to content

Commit

Permalink
fix figures
Browse files Browse the repository at this point in the history
  • Loading branch information
petr-pavlik committed Nov 27, 2024
1 parent 867a09e commit 4d72c6a
Show file tree
Hide file tree
Showing 11 changed files with 10,799 additions and 14,348 deletions.
131 changes: 3 additions & 128 deletions 04_interpolation.qmd
Original file line number Diff line number Diff line change
Expand Up @@ -79,7 +79,7 @@ plot(x = st_geometry(grid), lwd = 0.1, add = TRUE, reset = TRUE) # <2>

Now we compute the distances between the points using the `outer()` function

```{r, eval=TRUE, fig.align='center', fig.cap=''}
```{r, eval=TRUE, fig.align='center'}
distances <- sapply(1:nrow(st_coordinates(grid)), function(i) {
sqrt((st_coordinates(grid)[i, "X"] - st_coordinates(dom)[, "X"])^2 +
(st_coordinates(grid)[i, "Y"] - st_coordinates(dom)[, "Y"])^2)
Expand Down Expand Up @@ -151,7 +151,7 @@ plot(x = join,

There are many libraries listed in CRAN **geostatistics** task view. One of these is called gstat, it was developed and is maintained by Edzer Pebesma, who is also behind the `raster` and `terra` packages. The gstat package contains functions no olny for interpolations.

```{r, eval=TRUE}
```{r, eval=TRUE, fig.align='center'}
library(gstat)
idw_res <- gstat::idw(formula = value ~ 1,
locations = dom,
Expand Down Expand Up @@ -186,7 +186,7 @@ weighted_sum

Another interpolation technique is called **Krigging**. Opposing to the Inverse Distance Weighted method.

```{r, eval=TRUE}
```{r, eval=TRUE, fig.align='center'}
projected_crs <- 32633 # Replace with the appropriate UTM zone for your data
Expand Down Expand Up @@ -247,128 +247,3 @@ plot(
)
```

<!-- ### Data -->

<!-- Let's use some artificial data set for this example as well. -->

<!-- $$ -->
<!-- \mathrm{Distances} = \sqrt{(x_i - x_j)^2 + (y_i - y_j)^2}\qquad \mathrm{for}\:i, j = 1,2, \ldots,n -->
<!-- $$ -->

<!-- $$ -->
<!-- \mathrm{Differences} = \Delta Z_{i,j} = Z_i - Z_j\qquad \mathrm{for} -->
<!-- $$ -->

<!-- ### Semivariogram calculation -->

<!-- ```{r, fig.width=400} -->
<!-- # show.vgms() -->
<!-- ``` -->

<!-- $$ -->
<!-- \gamma(h) = \dfrac{1}{2N(h)}\sum\limits_{i=1}^{n}\sum\limits_{j=1}^{n}\Delta Z_{ij}(h)^2 -->
<!-- $$ where $\gamma(h)$ represents the semivariance lag at distance $h$. -->

<!-- ```{r, eval=FALSE} -->
<!-- df <- data.frame( -->
<!-- x = runif(10, 0, 50), -->
<!-- y = runif(10, 0, 50), -->
<!-- value = rnorm(10) -->
<!-- ) -->
<!-- n <- nrow(df) -->
<!-- dist_matrix <- as.matrix(dist(cbind(df$x, df$y))) -->
<!-- differences_matrix <- matrix(0, n, n) -->

<!-- for (i in 1:n) { -->
<!-- differences_matrix[i, ] <- df$value - df$value[i] -->
<!-- } -->
<!-- lag_tol <- 1 # Lag tolerance, you can adjust this -->
<!-- max_lag <- 10 # Maximum lag distance, you can adjust this -->

<!-- semivariance <- rep(0, max_lag / lag_tol) -->
<!-- num_pairs <- rep(0, max_lag / lag_tol) -->

<!-- for (i in 1:(n - 1)) { -->
<!-- for (j in (i + 1):n) { -->
<!-- distance_ij <- sqrt((df$x[i] - df$x[j])^2 + (df$y[i] - df$y[j])^2) -->
<!-- lag <- round(distance_ij / lag_tol) -->
<!-- if (lag <= max_lag / lag_tol) { -->
<!-- semivariance[lag] <- semivariance[lag] + (df$value[i] - df$value[j])^2 -->
<!-- num_pairs[lag] <- num_pairs[lag] + 1 -->
<!-- } -->
<!-- } -->
<!-- } -->

<!-- semivariance <- semivariance / (2 * num_pairs) -->

<!-- prediction_grid <- expand.grid( -->
<!-- x = seq(0, 50, by = 1), -->
<!-- y = seq(0, 50, by = 1) -->
<!-- ) -->

<!-- n_grid <- nrow(prediction_grid) -->
<!-- semivariance_at_grid <- rep(0, n_grid) -->

<!-- for (i in 1:n_grid) { -->
<!-- differences_to_data <- df$value - prediction_grid[i, ] -->
<!-- distances_to_data <- sqrt((df$x - prediction_grid[i, "x"])^2 + (df$y - prediction_grid[i, "y"])^2) -->
<!-- lag_values <- round(distances_to_data / lag_tol) -->
<!-- semivariance_at_grid[i] <- sum((differences_to_data)^2 / (2 * lag_values)) -->
<!-- } -->

<!-- n_lags <- length(semivariance) -->
<!-- kriged_values <- rep(0, n_grid) -->

<!-- for (i in 1:n_grid) { -->
<!-- kriging_weights <- rep(0, n) -->
<!-- distances_to_data <- sqrt((df$x - prediction_grid[i, "x"])^2 + (df$y - prediction_grid[i, "y"])^2) -->
<!-- lag_values <- round(distances_to_data / lag_tol) -->
<!-- for (j in 1:n) { -->
<!-- if (lag_values[j] <= n_lags) { -->
<!-- kriging_weights[j] <- (semivariance[lag_values[j]] - semivariance_at_grid[i]) / semivariance[lag_values[j]] -->
<!-- } -->
<!-- } -->
<!-- # Check if there are valid weights to avoid replacement with length zero -->
<!-- if (any(kriging_weights != 0)) { -->
<!-- kriged_values[i] <- sum(kriging_weights * df$value) -->
<!-- } -->
<!-- } -->


<!-- ``` -->

<!-- $$ -->
<!-- \left[ -->
<!-- \begin{matrix} -->
<!-- \mathbf{G} & \mathbf{j}\\ -->
<!-- \mathbf{j^\prime} & \mathbf{0}\\ -->
<!-- \end{matrix} -->
<!-- \right] -->
<!-- \left[ -->
<!-- \begin{matrix} -->
<!-- \lambda\\ -->
<!-- \mu_L -->
<!-- \end{matrix} -->
<!-- \right] = -->
<!-- \left[ -->
<!-- \begin{matrix} -->
<!-- \mathbf{g}\\ -->
<!-- 1 -->
<!-- \end{matrix} -->
<!-- \right] -->
<!-- $$ Where $\mathbf{G}$ is a rectangular matrix containing the values of variograph for all doubles of measured points, $\mathbf{1}$ is a vector of weights s with values of -->

<!-- ### Now we have to state the varigram model. We will calculate the semivariogram -->

<!-- to model the **spatial covariance** structure. Then calculate the pairwise distances and differences between data points. -->

<!-- ```{r, eval=FALSE} -->
<!-- n <- nrow(df) -->
<!-- dist_matrix <- as.matrix(dist(cbind(df$x, df$y))) -->
<!-- differences_matrix <- matrix(0, n, n) -->

<!-- for (i in 1:n) { -->
<!-- differences_matrix[i, ] <- df$value - df$value[i] -->
<!-- } -->
<!-- ``` -->
56 changes: 28 additions & 28 deletions docs/01_R.html
Original file line number Diff line number Diff line change
Expand Up @@ -340,19 +340,19 @@ <h3 data-number="2.2.2" class="anchored" data-anchor-id="special-values"><span c
<dl class="code-annotation-container-hidden code-annotation-container-grid">
<dt data-target-cell="annotated-cell-4" data-target-annotation="1">1</dt>
<dd>
<span data-code-lines="1" data-code-annotation="1" data-code-cell="annotated-cell-4">General sequence of numbers</span>
<span data-code-annotation="1" data-code-lines="1" data-code-cell="annotated-cell-4">General sequence of numbers</span>
</dd>
<dt data-target-cell="annotated-cell-4" data-target-annotation="2">2</dt>
<dd>
<span data-code-lines="2" data-code-annotation="2" data-code-cell="annotated-cell-4">change some elements to not assigned</span>
<span data-code-annotation="2" data-code-lines="2" data-code-cell="annotated-cell-4">change some elements to not assigned</span>
</dd>
<dt data-target-cell="annotated-cell-4" data-target-annotation="3">3</dt>
<dd>
<span data-code-lines="4" data-code-annotation="3" data-code-cell="annotated-cell-4">without removal</span>
<span data-code-annotation="3" data-code-lines="4" data-code-cell="annotated-cell-4">without removal</span>
</dd>
<dt data-target-cell="annotated-cell-4" data-target-annotation="4">4</dt>
<dd>
<span data-code-lines="5" data-code-annotation="4" data-code-cell="annotated-cell-4">and with removal</span>
<span data-code-annotation="4" data-code-lines="5" data-code-cell="annotated-cell-4">and with removal</span>
</dd>
</dl>
</div>
Expand Down Expand Up @@ -628,12 +628,12 @@ <h3 data-number="2.3.3" class="anchored" data-anchor-id="functions"><span class=
<dl class="code-annotation-container-hidden code-annotation-container-grid">
<dt data-target-cell="annotated-cell-9" data-target-annotation="1">1</dt>
<dd>
<span data-code-lines="2,3,4" data-code-annotation="1" data-code-cell="annotated-cell-9">Example of function, which seeks the neares number from a vector <code>x</code> to a certain referential <code>value</code>.</span>
<span data-code-annotation="1" data-code-lines="2,3,4" data-code-cell="annotated-cell-9">Example of function, which seeks the neares number from a vector <code>x</code> to a certain referential <code>value</code>.</span>
</dd>
</dl>
</div>
<div class="cell-output cell-output-stdout">
<pre><code>Hodnota nejblíže 0 z vektoru x je: 0.04803224</code></pre>
<pre><code>Hodnota nejblíže 0 z vektoru x je: -0.01590373</code></pre>
</div>
</div>
<p>We can test if we get the same result as the <em>primitive</em> function from R using <code>all.equal()</code> statement.</p>
Expand Down Expand Up @@ -810,15 +810,15 @@ <h4 class="unnumbered anchored" data-anchor-id="vectors">Vectors</h4>
<dl class="code-annotation-container-hidden code-annotation-container-grid">
<dt data-target-cell="annotated-cell-14" data-target-annotation="1">1</dt>
<dd>
<span data-code-lines="3" data-code-annotation="1" data-code-cell="annotated-cell-14">Adding two vectors while length of second is the multiple of the first</span>
<span data-code-annotation="1" data-code-lines="3" data-code-cell="annotated-cell-14">Adding two vectors while length of second is the multiple of the first</span>
</dd>
<dt data-target-cell="annotated-cell-14" data-target-annotation="2">2</dt>
<dd>
<span data-code-lines="4" data-code-annotation="2" data-code-cell="annotated-cell-14">Multiplying two vectors while length of second is the multiple of the first</span>
<span data-code-annotation="2" data-code-lines="4" data-code-cell="annotated-cell-14">Multiplying two vectors while length of second is the multiple of the first</span>
</dd>
<dt data-target-cell="annotated-cell-14" data-target-annotation="3">3</dt>
<dd>
<span data-code-lines="5" data-code-annotation="3" data-code-cell="annotated-cell-14">Multipling with single numeric value</span>
<span data-code-annotation="3" data-code-lines="5" data-code-cell="annotated-cell-14">Multipling with single numeric value</span>
</dd>
</dl>
</div>
Expand Down Expand Up @@ -849,35 +849,35 @@ <h5 class="unnumbered anchored" data-anchor-id="working-with-vectors">Working wi
<dl class="code-annotation-container-hidden code-annotation-container-grid">
<dt data-target-cell="annotated-cell-15" data-target-annotation="1">1</dt>
<dd>
<span data-code-lines="1,2,3,4,5,6,7" data-code-annotation="1" data-code-cell="annotated-cell-15">Vector creation <span class="math inline">\(\boldsymbol{\mathrm{x}}\)</span> by different approaches. Sequences, repeats, repetitions and sampling</span>
<span data-code-annotation="1" data-code-lines="1,2,3,4,5,6,7" data-code-cell="annotated-cell-15">Vector creation <span class="math inline">\(\boldsymbol{\mathrm{x}}\)</span> by different approaches. Sequences, repeats, repetitions and sampling</span>
</dd>
<dt data-target-cell="annotated-cell-15" data-target-annotation="2">2</dt>
<dd>
<span data-code-lines="8" data-code-annotation="2" data-code-cell="annotated-cell-15">Transposition of vector.</span>
<span data-code-annotation="2" data-code-lines="8" data-code-cell="annotated-cell-15">Transposition of vector.</span>
</dd>
<dt data-target-cell="annotated-cell-15" data-target-annotation="3">3</dt>
<dd>
<span data-code-lines="9" data-code-annotation="3" data-code-cell="annotated-cell-15">Naming elements of a vector.</span>
<span data-code-annotation="3" data-code-lines="9" data-code-cell="annotated-cell-15">Naming elements of a vector.</span>
</dd>
<dt data-target-cell="annotated-cell-15" data-target-annotation="4">4</dt>
<dd>
<span data-code-lines="10" data-code-annotation="4" data-code-cell="annotated-cell-15">Selection of elements from a vector based on a condition.</span>
<span data-code-annotation="4" data-code-lines="10" data-code-cell="annotated-cell-15">Selection of elements from a vector based on a condition.</span>
</dd>
<dt data-target-cell="annotated-cell-15" data-target-annotation="5">5</dt>
<dd>
<span data-code-lines="11" data-code-annotation="5" data-code-cell="annotated-cell-15">Selection of elements from a vector based on an index.</span>
<span data-code-annotation="5" data-code-lines="11" data-code-cell="annotated-cell-15">Selection of elements from a vector based on an index.</span>
</dd>
</dl>
</div>
<div class="cell-output cell-output-stdout">
<pre><code> [,1] [,2] [,3] [,4] [,5] [,6] [,7] [,8]
[1,] 96.97616 834.4318 707.3374 1212.165 1288.363 16.33978 315.1559 608.6296
<pre><code> [,1] [,2] [,3] [,4] [,5] [,6] [,7] [,8]
[1,] 110.3749 5.84625 693.2721 147.1503 9.649926 1766.76 7.561956 1.109624
[,9] [,10]
[1,] 22.63338 199.5815
D E F H I
34.816167 35.893779 4.042249 24.670419 4.757455
[1,] 148.2652 0.350399
A B E F G I J
10.5059478 2.4179021 3.1064330 42.0328426 2.7499010 12.1764216 0.5919451
A B C
-9.847647 -28.886533 -26.595815 </code></pre>
10.505948 2.417902 -26.330061 </code></pre>
</div>
</div>
<div class="cell">
Expand Down Expand Up @@ -953,7 +953,7 @@ <h4 class="unnumbered anchored" data-anchor-id="matrices-and-arrays">Matrices an
<dl class="code-annotation-container-hidden code-annotation-container-grid">
<dt data-target-cell="annotated-cell-17" data-target-annotation="1">1</dt>
<dd>
<span data-code-lines="2" data-code-annotation="1" data-code-cell="annotated-cell-17">Conversion to <span class="math inline">\(2\times 2\)</span> dimension</span>
<span data-code-annotation="1" data-code-lines="2" data-code-cell="annotated-cell-17">Conversion to <span class="math inline">\(2\times 2\)</span> dimension</span>
</dd>
</dl>
</div>
Expand All @@ -972,9 +972,9 @@ <h4 class="unnumbered anchored" data-anchor-id="matrices-and-arrays">Matrices an
<span id="cb24-4"><a href="#cb24-4" aria-hidden="true" tabindex="-1"></a>M[<span class="fu">c</span>(<span class="dv">1</span>,<span class="dv">3</span>), <span class="dv">1</span><span class="sc">:</span><span class="dv">2</span>] <span class="ot">&lt;-</span> <span class="fu">rnorm</span>(<span class="dv">2</span>) <span class="co"># store random numbers to first two rows</span></span>
<span id="cb24-5"><a href="#cb24-5" aria-hidden="true" tabindex="-1"></a></span>
<span id="cb24-6"><a href="#cb24-6" aria-hidden="true" tabindex="-1"></a><span class="fu">colMeans</span>(M) </span>
<span id="cb24-7"><a href="#cb24-7" aria-hidden="true" tabindex="-1"></a><span class="do">## [1] 1.4745562 0.5745562</span></span>
<span id="cb24-7"><a href="#cb24-7" aria-hidden="true" tabindex="-1"></a><span class="do">## [1] 0.88685608 -0.01314392</span></span>
<span id="cb24-8"><a href="#cb24-8" aria-hidden="true" tabindex="-1"></a><span class="fu">rowSums</span>(M)</span>
<span id="cb24-9"><a href="#cb24-9" aria-hidden="true" tabindex="-1"></a><span class="do">## [1] 3.704401 1.500000 2.041160 1.500000 1.500000</span></span></code><button title="Copy to Clipboard" class="code-copy-button"><i class="bi"></i></button></pre></div>
<span id="cb24-9"><a href="#cb24-9" aria-hidden="true" tabindex="-1"></a><span class="do">## [1] -1.837653 1.500000 1.706214 1.500000 1.500000</span></span></code><button title="Copy to Clipboard" class="code-copy-button"><i class="bi"></i></button></pre></div>
</details>
</div>
<p>It is possible to have matrices containing any data type, e.g.</p>
Expand Down Expand Up @@ -1080,7 +1080,7 @@ <h5 class="unnumbered anchored" data-anchor-id="if"><code>if()</code></h5>
<dl class="code-annotation-container-hidden code-annotation-container-grid">
<dt data-target-cell="annotated-cell-24" data-target-annotation="1">1</dt>
<dd>
<span data-code-lines="2,3,4,5,6" data-code-annotation="1" data-code-cell="annotated-cell-24">The chain of conditions will close <strong>at the first evaluation which happens to be TRUE</strong>.</span>
<span data-code-annotation="1" data-code-lines="2,3,4,5,6" data-code-cell="annotated-cell-24">The chain of conditions will close <strong>at the first evaluation which happens to be TRUE</strong>.</span>
</dd>
</dl>
</div>
Expand Down Expand Up @@ -1119,11 +1119,11 @@ <h4 class="unnumbered anchored" data-anchor-id="switch"><code>switch()</code></h
<dl class="code-annotation-container-hidden code-annotation-container-grid">
<dt data-target-cell="annotated-cell-26" data-target-annotation="1">1</dt>
<dd>
<span data-code-lines="4" data-code-annotation="1" data-code-cell="annotated-cell-26">“A” variant did not happen,</span>
<span data-code-annotation="1" data-code-lines="4" data-code-cell="annotated-cell-26">“A” variant did not happen,</span>
</dd>
<dt data-target-cell="annotated-cell-26" data-target-annotation="2">2</dt>
<dd>
<span data-code-lines="5" data-code-annotation="2" data-code-cell="annotated-cell-26">instead the “B” variant is truthful, so the expression is evaluated as <span class="math inline">\(2\cdot 3 = 6\)</span></span>
<span data-code-annotation="2" data-code-lines="5" data-code-cell="annotated-cell-26">instead the “B” variant is truthful, so the expression is evaluated as <span class="math inline">\(2\cdot 3 = 6\)</span></span>
</dd>
</dl>
</div>
Expand Down Expand Up @@ -1237,11 +1237,11 @@ <h4 class="unnumbered anchored" data-anchor-id="repeat-cycle"><code>repeat</code
<dl class="code-annotation-container-hidden code-annotation-container-grid">
<dt data-target-cell="annotated-cell-30" data-target-annotation="1">1</dt>
<dd>
<span data-code-lines="2,3,4,6" data-code-annotation="1" data-code-cell="annotated-cell-30">Execute in loop,</span>
<span data-code-annotation="1" data-code-lines="2,3,4,6" data-code-cell="annotated-cell-30">Execute in loop,</span>
</dd>
<dt data-target-cell="annotated-cell-30" data-target-annotation="2">2</dt>
<dd>
<span data-code-lines="5" data-code-annotation="2" data-code-cell="annotated-cell-30">if a condition is met, <code>break</code> stops the cycle.</span>
<span data-code-annotation="2" data-code-lines="5" data-code-cell="annotated-cell-30">if a condition is met, <code>break</code> stops the cycle.</span>
</dd>
</dl>
</div>
Expand Down
Loading

0 comments on commit 4d72c6a

Please sign in to comment.