Skip to content
Snippets Groups Projects
Commit fa42cc52 authored by lucas.longour_ird.fr's avatar lucas.longour_ird.fr
Browse files

add introduction to R

parent 07cd4eca
No related branches found
No related tags found
No related merge requests found
Pipeline #1067 passed
Showing
with 283 additions and 97 deletions
......@@ -4,6 +4,104 @@ bibliography: references.bib
# Introduction {#introductionx}
## Use of R
### Installation
::: callout-note
The installation part is based on "[An Introduction to R](https://intro2r.com/)" book writed by *Alex Douglas, Deon Roos, Francesca Mancini, Ana Couto & David Lusseau*
:::
#### R
##### Windows users
For Windows users select the 'Download R for Windows' link and then click on the 'base' link and finally the download link 'Download R 4.2.1 for Windows'. This will begin the download of the '.exe' installation file. When the download has completed double click on the R executable file and follow the on-screen instructions. Full installation instructions can be found at the [CRAN website](https://cran.r-project.org/bin/windows/).
##### Mac users
For Mac users select the '[Download R for (Mac) OS X](https://cran.r-project.org/bin/macosx/)' link. The binary can be downloaded by selecting the 'R-4.2.1.pkg'. Once downloaded, double click on the file icon and follow the on-screen instructions to guide you through the necessary steps. See the '[R for Mac OS X FAQ](https://cran.r-project.org/bin/macosx/RMacOSX-FAQ.html)' for further information on installation.
##### Linux users
For Linux users, the installation method will depend on which flavour of Linux you are using. There are reasonably comprehensive instruction [here](https://cran.r-project.org/bin/linux/) for Debian, Redhat, Suse and Ubuntu. In most cases you can just use your OS package manager to install R from the official repository. On Ubuntu fire up a shell (Terminal) and use (you will need root permission to do this):
```{bash eval=FALSE}
sudo apt update
sudo apt install r-base r-base-dev
```
which will install base R and also the development version of base R (you only need this if you want to compile R packages from source but it doesn't hurt to have it).
If you receive an error after running the code above you may need to add a 'source.list' entry to your etc/apt/sources.list file. To do this open the terminal and enter this:
```{bash eval=FALSE}
sudo apt install -y --no-install-recommends software-properties-common dirmngr
# Add keys
wget -qO- https://cloud.r-project.org/bin/linux/ubuntu/marutter_pubkey.asc | sudo tee -a /etc/apt/trusted.gpg.d/cran_ubuntu_key.asc
sudo add-apt-repository "deb https://cloud.r-project.org/bin/linux/ubuntu $(lsb_release -cs)-cran40/"
```
Once you have done this then re-run the apt commands above and you should be good to go.
Install the following packages to allow for future spatial data analysis:
```{bash eval=FALSE}
sudo apt install -y libgdal-dev libproj-dev libgeos-dev libudunits2-dev libv8-dev libnode-dev libcairo2-dev libnetcdf-dev
```
#### RStudio
Whilst its eminently possible to just use the base installation of R (many people do), we will be using a popular Integrated Development Environment (IDE) called RStudio. RStudio can be thought of as an add-on to R which provides a more user-friendly interface, incorporating the R Console, a script editor and other useful functionality (like R markdown and Git Hub integration). You can find more information about RStudio [here](https://rstudio.com/).
RStudio is freely available for Windows, Mac and Linux operating systems and can be downloaded from the [RStudio site](https://rstudio.com/products/rstudio/download). You should select the 'RStudio Desktop' version. Note: you must install R before you install RStudio.
##### Windows and Mac users
For Windows and Mac users you should be presented with the appropriate link for downloading. Click on this link and once downloaded run the installer and follow the instructions. If you don't see the link then scroll down to the 'All Installers' section and choose the link manually.
##### Linux users
For Linux users scroll down to the 'All Installers' section and choose the appropriate link to download the binary for your Linux operating system. RStudio for Ubuntu (and Debian) is available as a `*.deb` package.
To install the `*.deb` file navigate to where you downloaded the file and then enter the following command with root permission
```{bash eval=FALSE}
sudo apt install ./rstudio-2022.07.2-576-amd64.deb
```
You can then start RStudio from the Console by simply typing
```{bash eval=FALSE}
rstudio
```
or you can create a shortcut on you Desktop for easy startup.
### Help
The R help is very useful for the use of functions.
```{r eval=FALSE}
?plot #displays the help page for the plot function
help("*") #for unconventional characters
```
Calling the help opens a page (the exact behavior depends on the operating system) with information and usage examples about the documented function(s) or operators.
### Functions
The basic syntax is:
```{r}
afunction <- function(arg1, arg2){
arg1 + arg2
}
afunction(10, 5)
```
## Spatial in R : History and evolutions
Historically, 4 packages make it possible to import, manipulate and transform spatial data:
......
This diff is collapsed.
02-data_acquisition_files/figure-html/display_point-1.png

975 KiB

02-data_acquisition_files/figure-html/display_sf-1.png

972 KiB

No preview for this file type
This diff is collapsed.
This diff is collapsed.
public/02-data_acquisition_files/figure-html/display_point-1.png

1.11 MiB | W: | H:

public/02-data_acquisition_files/figure-html/display_point-1.png

1.11 MiB | W: | H:

public/02-data_acquisition_files/figure-html/display_point-1.png
public/02-data_acquisition_files/figure-html/display_point-1.png
public/02-data_acquisition_files/figure-html/display_point-1.png
public/02-data_acquisition_files/figure-html/display_point-1.png
  • 2-up
  • Swipe
  • Onion skin
public/02-data_acquisition_files/figure-html/display_sf-1.png

976 KiB | W: | H:

public/02-data_acquisition_files/figure-html/display_sf-1.png

976 KiB | W: | H:

public/02-data_acquisition_files/figure-html/display_sf-1.png
public/02-data_acquisition_files/figure-html/display_sf-1.png
public/02-data_acquisition_files/figure-html/display_sf-1.png
public/02-data_acquisition_files/figure-html/display_sf-1.png
  • 2-up
  • Swipe
  • Onion skin
public/03-vector_data_files/figure-html/osrmroute-1.png

231 KiB | W: | H:

public/03-vector_data_files/figure-html/osrmroute-1.png

231 KiB | W: | H:

public/03-vector_data_files/figure-html/osrmroute-1.png
public/03-vector_data_files/figure-html/osrmroute-1.png
public/03-vector_data_files/figure-html/osrmroute-1.png
public/03-vector_data_files/figure-html/osrmroute-1.png
  • 2-up
  • Swipe
  • Onion skin
......@@ -2,7 +2,7 @@
<html xmlns="http://www.w3.org/1999/xhtml" lang="en" xml:lang="en"><head>
<meta charset="utf-8">
<meta name="generator" content="quarto-1.1.189">
<meta name="generator" content="quarto-1.1.251">
<meta name="viewport" content="width=device-width, initial-scale=1.0, user-scalable=yes">
......@@ -490,14 +490,14 @@ Moran’s I test
Model used when sampling: Poisson
Number of simulations: 499
Statistic: 0.1566449
p-value : 0.012 </code></pre>
p-value : 0.016 </code></pre>
</div>
<div class="sourceCode cell-code" id="cb11"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb11-1"><a href="#cb11-1" aria-hidden="true" tabindex="-1"></a><span class="fu">plot</span>(m_test)</span></code><button title="Copy to Clipboard" class="code-copy-button"><i class="bi"></i></button></pre></div>
<div class="cell-output-display">
<p><img src="07-basic_statistics_files/figure-html/MoransI-1.png" class="img-fluid" width="768"></p>
</div>
</div>
<p>The Moran’s statistics is here <span class="math inline">\(I =\)</span> 0.16. When comparing its value to the H0 distribution (built under 499 simulations), the probability of observing such a I value under the null hypothesis, i.e.&nbsp;the distribution of cases is spatially independent, is <span class="math inline">\(p_{value} =\)</span> 0.012. We therefore reject H0 with error risk of <span class="math inline">\(\alpha = 5\%\)</span>. The distribution of cases is therefore autocorrelated across districts in Cambodia.</p>
<p>The Moran’s statistics is here <span class="math inline">\(I =\)</span> 0.16. When comparing its value to the H0 distribution (built under 499 simulations), the probability of observing such a I value under the null hypothesis, i.e.&nbsp;the distribution of cases is spatially independent, is <span class="math inline">\(p_{value} =\)</span> 0.016. We therefore reject H0 with error risk of <span class="math inline">\(\alpha = 5\%\)</span>. The distribution of cases is therefore autocorrelated across districts in Cambodia.</p>
</section>
<section id="morans-i-local-test" class="level4" data-number="7.2.2.2">
<h4 data-number="7.2.2.2" class="anchored" data-anchor-id="morans-i-local-test"><span class="header-section-number">7.2.2.2</span> Moran’s I local test</h4>
......@@ -695,7 +695,7 @@ Statistical test
<span id="cb31-7"><a href="#cb31-7" aria-hidden="true" tabindex="-1"></a><span class="fu">print</span>(df_secondary_clusters)</span></code><button title="Copy to Clipboard" class="code-copy-button"><i class="bi"></i></button></pre></div>
<div class="cell-output cell-output-stdout">
<pre class="code-out"><code> SMR number.of.cases expected.cases p.value
1 3.767698 16 4.246625 0.008</code></pre>
1 3.767698 16 4.246625 0.01</code></pre>
</div>
</div>
<p>We only have one secondary cluster composed of one district.</p>
......
public/07-basic_statistics_files/figure-html/LocalMoransI-1.png

14.5 KiB | W: | H:

public/07-basic_statistics_files/figure-html/LocalMoransI-1.png

15.2 KiB | W: | H:

public/07-basic_statistics_files/figure-html/LocalMoransI-1.png
public/07-basic_statistics_files/figure-html/LocalMoransI-1.png
public/07-basic_statistics_files/figure-html/LocalMoransI-1.png
public/07-basic_statistics_files/figure-html/LocalMoransI-1.png
  • 2-up
  • Swipe
  • Onion skin
public/07-basic_statistics_files/figure-html/LocalMoransI_plt-1.png

43.3 KiB | W: | H:

public/07-basic_statistics_files/figure-html/LocalMoransI_plt-1.png

287 KiB | W: | H:

public/07-basic_statistics_files/figure-html/LocalMoransI_plt-1.png
public/07-basic_statistics_files/figure-html/LocalMoransI_plt-1.png
public/07-basic_statistics_files/figure-html/LocalMoransI_plt-1.png
public/07-basic_statistics_files/figure-html/LocalMoransI_plt-1.png
  • 2-up
  • Swipe
  • Onion skin
public/07-basic_statistics_files/figure-html/MoransI-1.png

18 KiB | W: | H:

public/07-basic_statistics_files/figure-html/MoransI-1.png

18.1 KiB | W: | H:

public/07-basic_statistics_files/figure-html/MoransI-1.png
public/07-basic_statistics_files/figure-html/MoransI-1.png
public/07-basic_statistics_files/figure-html/MoransI-1.png
public/07-basic_statistics_files/figure-html/MoransI-1.png
  • 2-up
  • Swipe
  • Onion skin
public/07-basic_statistics_files/figure-html/cases_visualization-1.png

38 KiB | W: | H:

public/07-basic_statistics_files/figure-html/cases_visualization-1.png

255 KiB | W: | H:

public/07-basic_statistics_files/figure-html/cases_visualization-1.png
public/07-basic_statistics_files/figure-html/cases_visualization-1.png
public/07-basic_statistics_files/figure-html/cases_visualization-1.png
public/07-basic_statistics_files/figure-html/cases_visualization-1.png
  • 2-up
  • Swipe
  • Onion skin
public/07-basic_statistics_files/figure-html/distribution-1.png

20.4 KiB | W: | H:

public/07-basic_statistics_files/figure-html/distribution-1.png

66.2 KiB | W: | H:

public/07-basic_statistics_files/figure-html/distribution-1.png
public/07-basic_statistics_files/figure-html/distribution-1.png
public/07-basic_statistics_files/figure-html/distribution-1.png
public/07-basic_statistics_files/figure-html/distribution-1.png
  • 2-up
  • Swipe
  • Onion skin
public/07-basic_statistics_files/figure-html/inc_visualization-1.png

52.2 KiB | W: | H:

public/07-basic_statistics_files/figure-html/inc_visualization-1.png

349 KiB | W: | H:

public/07-basic_statistics_files/figure-html/inc_visualization-1.png
public/07-basic_statistics_files/figure-html/inc_visualization-1.png
public/07-basic_statistics_files/figure-html/inc_visualization-1.png
public/07-basic_statistics_files/figure-html/inc_visualization-1.png
  • 2-up
  • Swipe
  • Onion skin
public/07-basic_statistics_files/figure-html/incidence_visualization-1.png

52.4 KiB

0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment