Skip to content

Commit

Permalink
Deploy commit: Revert ml stuff (#1124)
Browse files Browse the repository at this point in the history
* Revert "eval=FALSE in c15"

This reverts commit 80872c3.

* Revert "store_backends"

This reverts commit e6f74bd.

* Revert "Remove cache from chunk 15-eco-26"

This reverts commit 7217843.

* Revert "Try evaluating train function"

This reverts commit 6302bc7.

* Revert "Update 15-tune.rds, fix actions (hopefully)"

This reverts commit 737cbfc.

* Revert "Do not evaluate failing mlr3::lrn chunk (#1111 hotfix)"

This reverts commit b402780.

* set eval=TRUE and save 15-tune.rds anew

* use new method of specifying a fallback learner (breaking change, see https://github.com/mlr-org/mlr3/releases/tag/v0.21.0)

* rebuild the autotuner bfb40b7
  • Loading branch information
Robinlovelace committed Sep 25, 2024
1 parent 64ead99 commit 870ea0c
Show file tree
Hide file tree
Showing 16 changed files with 150 additions and 96 deletions.
16 changes: 15 additions & 1 deletion 12-spatial-cv.md
Original file line number Diff line number Diff line change
Expand Up @@ -507,6 +507,18 @@ The classification\index{classification} task remains the same, hence we can sim
Learners implementing SVM can be found using the `list_mlr3learners()` command of the **mlr3extralearners**.


``` r
mlr3_learners = mlr3extralearners::list_mlr3learners()
#> This will take a few seconds.
mlr3_learners |>
dplyr::filter(class == "classif" & grepl("svm", id)) |>
dplyr::select(id, class, mlr3_package, required_packages)
#> id class mlr3_package required_packages
#> <char> <char> <char> <list>
#> 1: classif.ksvm classif mlr3extralearners mlr3,mlr3extralearners,kernlab
#> 2: classif.lssvm classif mlr3extralearners mlr3,mlr3extralearners,kernlab
#> 3: classif.svm classif mlr3learners mlr3,mlr3learners,e1071
```

Of the options, we will use `ksvm()` from the **kernlab** package [@karatzoglou_kernlab_2004].
To allow for non-linear relationships, we use the popular radial basis function (or Gaussian) kernel (`"rbfdot" `) which is also the default of `ksvm()`.
Expand All @@ -517,7 +529,9 @@ To make sure that the tuning does not stop because of one failing model, we addi
``` r
lrn_ksvm = mlr3::lrn("classif.ksvm", predict_type = "prob", kernel = "rbfdot",
type = "C-svc")
lrn_ksvm$fallback = lrn("classif.featureless", predict_type = "prob")
lrn_ksvm$encapsulate(method = "try",
fallback = lrn("classif.featureless",
predict_type = "prob"))
```

The next stage is to specify a resampling strategy.
Expand Down
12 changes: 11 additions & 1 deletion 13-transport.md
Original file line number Diff line number Diff line change
Expand Up @@ -570,7 +570,7 @@ routes_short_scenario = routes_short |>
mutate(bicycle = bicycle + car_driver * uptake,
car_driver = car_driver * (1 - uptake))
sum(routes_short_scenario$bicycle) - sum(routes_short$bicycle)
#> [1] 583
#> [1] 692
```

Having created a scenario in which approximately 4000 trips have switched from driving to cycling, we can now model where this updated modeled cycling activity will take place.
Expand All @@ -584,6 +584,11 @@ route_network_scenario = overline(routes_short_scenario, attrib = "bicycle")

The outputs of the two preceding code chunks are summarized in Figure \@ref(fig:rnetvis) below.


```
#> [plot mode] fit legend/component: Some legend items or map compoments do not fit well, and are therefore rescaled. Set the tmap option 'component.autoscale' to FALSE to disable rescaling.
```

<div class="figure" style="text-align: center">
<img src="figures/rnetvis-1.png" alt="Illustration of the percentage of car trips switching to cycling as a function of distance (left) and route network level results of this function (right)." width="49%" /><img src="figures/rnetvis-2.png" alt="Illustration of the percentage of car trips switching to cycling as a function of distance (left) and route network level results of this function (right)." width="49%" />
<p class="caption">(\#fig:rnetvis)Illustration of the percentage of car trips switching to cycling as a function of distance (left) and route network level results of this function (right).</p>
Expand Down Expand Up @@ -643,6 +648,11 @@ ways_centrality = ways_sfn |>
mutate(betweenness = tidygraph::centrality_edge_betweenness(lengths))
```


```
#> [plot mode] fit legend/component: Some legend items or map compoments do not fit well, and are therefore rescaled. Set the tmap option 'component.autoscale' to FALSE to disable rescaling.
```

<div class="figure" style="text-align: center">
<img src="figures/wayssln-1.png" alt="Illustration of route network datasets. The grey lines represent a simplified road network, with segment thickness proportional to betweenness. The green lines represent potential cycling flows (one way) calculated with the code above." width="100%" />
<p class="caption">(\#fig:wayssln)Illustration of route network datasets. The grey lines represent a simplified road network, with segment thickness proportional to betweenness. The green lines represent potential cycling flows (one way) calculated with the code above.</p>
Expand Down
15 changes: 11 additions & 4 deletions 15-eco.md
Original file line number Diff line number Diff line change
Expand Up @@ -442,22 +442,20 @@ Calling the `train()`-method of the `AutoTuner`-object finally runs the hyperpar

``` r
# hyperparameter tuning
set.seed(08012024)
set.seed(24092024)
autotuner_rf$train(task)
```





<!-- TODO: evaluate this when issue fixed upstream -->


``` r
autotuner_rf$tuning_result
#> mtry sample.fraction min.node.size learner_param_vals x_domain regr.rmse
#> <int> <num> <int> <list> <list> <num>
#> 1: 4 0.878 7 <list[4]> <list[3]> 0.368
#> 1: 4 0.784 10 <list[4]> <list[3]> 0.382
```

### Predictive mapping
Expand All @@ -469,6 +467,15 @@ To do so, we only need to run the `predict` method of our fitted `AutoTuner` obj
``` r
# predicting using the best hyperparameter combination
autotuner_rf$predict(task)
#> <PredictionRegr> for 84 observations:
#> row_ids truth response
#> 1 -1.084 -1.176
#> 2 -0.975 -1.176
#> 3 -0.912 -1.168
#> --- --- ---
#> 82 0.814 0.594
#> 83 0.814 0.746
#> 84 0.808 0.807
```

The `predict` method will apply the model to all observations used in the modeling.
Expand Down
8 changes: 4 additions & 4 deletions conclusion.html
Original file line number Diff line number Diff line change
Expand Up @@ -132,7 +132,7 @@ <h2>
</h2>
<p>A feature of R, and open source software in general, is that there are often multiple ways to achieve the same result.
The code chunk below illustrates this by using three functions, covered in Chapters <a href="attr.html#attr">3</a> and <a href="geometry-operations.html#geometry-operations">5</a>, to combine the 16 regions of New Zealand into a single geometry:</p>
<div class="sourceCode" id="cb510"><pre class="downlit sourceCode r">
<div class="sourceCode" id="cb513"><pre class="downlit sourceCode r">
<code class="sourceCode R"><span><span class="kw"><a href="https://rdrr.io/r/base/library.html">library</a></span><span class="op">(</span><span class="va"><a href="https://jakubnowosad.com/spData/">spData</a></span><span class="op">)</span></span>
<span><span class="va">nz_u1</span> <span class="op">=</span> <span class="fu">sf</span><span class="fu">::</span><span class="fu"><a href="https://r-spatial.github.io/sf/reference/geos_combine.html">st_union</a></span><span class="op">(</span><span class="va">nz</span><span class="op">)</span></span>
<span><span class="va">nz_u2</span> <span class="op">=</span> <span class="fu"><a href="https://rspatial.github.io/terra/reference/aggregate.html">aggregate</a></span><span class="op">(</span><span class="va">nz</span><span class="op">[</span><span class="st">"Population"</span><span class="op">]</span>, <span class="fu"><a href="https://rdrr.io/r/base/list.html">list</a></span><span class="op">(</span><span class="fu"><a href="https://rdrr.io/r/base/rep.html">rep</a></span><span class="op">(</span><span class="fl">1</span>, <span class="fu"><a href="https://rspatial.github.io/terra/reference/dimensions.html">nrow</a></span><span class="op">(</span><span class="va">nz</span><span class="op">)</span><span class="op">)</span><span class="op">)</span>, <span class="va">sum</span><span class="op">)</span></span>
Expand All @@ -154,7 +154,7 @@ <h2>
The same applies for all packages showcased in this book, although it can be helpful (when not distracting) to be aware of alternatives and being able to justify your choice of software.</p>
<p>A common choice, for which there is no simple answer, is between <strong>tidyverse</strong> and base R for geocomputation.
The following code chunk, for example, shows <strong>tidyverse</strong> and base R ways to extract the <code>Name</code> column from the <code>nz</code> object, as described in Chapter <a href="attr.html#attr">3</a>:</p>
<div class="sourceCode" id="cb511"><pre class="downlit sourceCode r">
<div class="sourceCode" id="cb514"><pre class="downlit sourceCode r">
<code class="sourceCode R"><span><span class="kw"><a href="https://rdrr.io/r/base/library.html">library</a></span><span class="op">(</span><span class="va"><a href="https://dplyr.tidyverse.org">dplyr</a></span><span class="op">)</span> <span class="co"># attach a tidyverse package</span></span>
<span><span class="va">nz_name1</span> <span class="op">=</span> <span class="va">nz</span><span class="op">[</span><span class="st">"Name"</span><span class="op">]</span> <span class="co"># base R approach</span></span>
<span><span class="va">nz_name2</span> <span class="op">=</span> <span class="va">nz</span> <span class="op">|&gt;</span> <span class="co"># tidyverse approach</span></span>
Expand Down Expand Up @@ -280,13 +280,13 @@ <h3>
You could simply ask how to do this in one of the places outlined in the previous section.
However, it is likely that you will get a better response if you provide a reproducible example of what you have tried so far.
The following code creates a map of the world with blue sea and green land, but the land is not filled in:</p>
<div class="sourceCode" id="cb512"><pre class="downlit sourceCode r">
<div class="sourceCode" id="cb515"><pre class="downlit sourceCode r">
<code class="sourceCode R"><span><span class="kw"><a href="https://rdrr.io/r/base/library.html">library</a></span><span class="op">(</span><span class="va"><a href="https://r-spatial.github.io/sf/">sf</a></span><span class="op">)</span></span>
<span><span class="kw"><a href="https://rdrr.io/r/base/library.html">library</a></span><span class="op">(</span><span class="va"><a href="https://jakubnowosad.com/spData/">spData</a></span><span class="op">)</span></span>
<span><span class="fu"><a href="https://rspatial.github.io/terra/reference/plot.html">plot</a></span><span class="op">(</span><span class="fu"><a href="https://r-spatial.github.io/sf/reference/st_geometry.html">st_geometry</a></span><span class="op">(</span><span class="va">world</span><span class="op">)</span>, col <span class="op">=</span> <span class="st">"green"</span><span class="op">)</span></span></code></pre></div>
<p>If you post this code in a forum, it is likely that you will get a more specific and useful response.
For example, someone might respond with the following code, which demonstrably solves the problem, as illustrated in Figure <a href="conclusion.html#fig:16-synthesis-reprex">16.1</a>:</p>
<div class="sourceCode" id="cb513"><pre class="downlit sourceCode r">
<div class="sourceCode" id="cb516"><pre class="downlit sourceCode r">
<code class="sourceCode R"><span><span class="kw"><a href="https://rdrr.io/r/base/library.html">library</a></span><span class="op">(</span><span class="va"><a href="https://r-spatial.github.io/sf/">sf</a></span><span class="op">)</span></span>
<span><span class="kw"><a href="https://rdrr.io/r/base/library.html">library</a></span><span class="op">(</span><span class="va"><a href="https://jakubnowosad.com/spData/">spData</a></span><span class="op">)</span></span>
<span><span class="co"># use the bg argument to fill in the land</span></span>
Expand Down
Loading

0 comments on commit 870ea0c

Please sign in to comment.