GitLab at IIASA

index.html 17.3 KiB
Newer Older
<!DOCTYPE html>
<!-- Generated by pkgdown: do not edit by hand --><html lang="en">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<meta charset="utf-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Run FarmDyn from R and create sample farms • FarmDynR</title>
<!-- jquery --><script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.4.1/jquery.min.js" integrity="sha256-CSXorXvZcTkaix6Yvo6HppcZGetbYMGWSFlBw8HfCJo=" crossorigin="anonymous"></script><!-- Bootstrap --><link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/twitter-bootstrap/3.4.1/css/bootstrap.min.css" integrity="sha256-bZLfwXAP04zRMK2BjiO8iu9pf4FbLqX6zitd+tIvLhE=" crossorigin="anonymous">
<script src="https://cdnjs.cloudflare.com/ajax/libs/twitter-bootstrap/3.4.1/js/bootstrap.min.js" integrity="sha256-nuL8/2cJ5NDSSwnKD8VqreErSWHtnEP9E7AySL+1ev4=" crossorigin="anonymous"></script><!-- bootstrap-toc --><link rel="stylesheet" href="bootstrap-toc.css">
<script src="bootstrap-toc.js"></script><!-- Font Awesome icons --><link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/5.12.1/css/all.min.css" integrity="sha256-mmgLkCYLUQbXn0B1SRqzHar6dCnv9oZFPEC1g1cwlkk=" crossorigin="anonymous">
<link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/5.12.1/css/v4-shims.min.css" integrity="sha256-wZjR52fzng1pJHwx4aV2AO3yyTOXrcDW7jBpJtTwVxw=" crossorigin="anonymous">
<!-- clipboard.js --><script src="https://cdnjs.cloudflare.com/ajax/libs/clipboard.js/2.0.6/clipboard.min.js" integrity="sha256-inc5kl9MA1hkeYUt+EC3BhlIgyp/2jDIyBLS6k3UxPI=" crossorigin="anonymous"></script><!-- headroom.js --><script src="https://cdnjs.cloudflare.com/ajax/libs/headroom/0.11.0/headroom.min.js" integrity="sha256-AsUX4SJE1+yuDu5+mAVzJbuYNPHj/WroHuZ8Ir/CkE0=" crossorigin="anonymous"></script><script src="https://cdnjs.cloudflare.com/ajax/libs/headroom/0.11.0/jQuery.headroom.min.js" integrity="sha256-ZX/yNShbjqsohH1k95liqY9Gd8uOiE1S4vZc+9KQ1K4=" crossorigin="anonymous"></script><!-- pkgdown --><link href="pkgdown.css" rel="stylesheet">
<script src="pkgdown.js"></script><meta property="og:title" content="Run FarmDyn from R and create sample farms">
<meta property="og:description" content="This package allows the user to run FarmDyn from R, create sample farms from FADN data for use in FarmDyn, and includes useful functions to work with the results from FarmDyn.">
<!-- mathjax --><script src="https://cdnjs.cloudflare.com/ajax/libs/mathjax/2.7.5/MathJax.js" integrity="sha256-nvJJv9wWKEm88qvoQl9ekL2J+k/RWIsaSScxxlsrv8k=" crossorigin="anonymous"></script><script src="https://cdnjs.cloudflare.com/ajax/libs/mathjax/2.7.5/config/TeX-AMS-MML_HTMLorMML.js" integrity="sha256-84DKXVJXs0/F8OTMzX4UR909+jtl4G7SPypPavF+GfA=" crossorigin="anonymous"></script><!--[if lt IE 9]>
<script src="https://oss.maxcdn.com/html5shiv/3.7.3/html5shiv.min.js"></script>
<script src="https://oss.maxcdn.com/respond/1.4.2/respond.min.js"></script>
<![endif]-->
</head>
<body data-spy="scroll" data-target="#toc">
    

    <div class="container template-home">
      <header><div class="navbar navbar-default navbar-fixed-top" role="navigation">
  <div class="container">
    <div class="navbar-header">
      <button type="button" class="navbar-toggle collapsed" data-toggle="collapse" data-target="#navbar" aria-expanded="false">
        <span class="sr-only">Toggle navigation</span>
        <span class="icon-bar"></span>
        <span class="icon-bar"></span>
        <span class="icon-bar"></span>
      </button>
      <span class="navbar-brand">
        <a class="navbar-link" href="index.html">FarmDynR</a>
        <span class="version label label-default" data-toggle="tooltip" data-placement="bottom" title="">0.5.1</span>
      </span>
    </div>

    <div id="navbar" class="navbar-collapse collapse">
      <ul class="nav navbar-nav">
<li>
  <a href="reference/index.html">Reference</a>
</li>
<li>
  <a href="news/index.html">Changelog</a>
</li>
      </ul>
<ul class="nav navbar-nav navbar-right"></ul>
</div>
<!--/.nav-collapse -->
  </div>
<!--/.container -->
</div>
<!--/.navbar -->

      

      </header><div class="row">
  <div class="contents col-md-9">
<div class="section level1">
<div class="page-header"><h1 id="farmdynr">FarmDynR<a class="anchor" aria-label="anchor" href="#farmdynr"></a>
</h1></div>
<p>Developed by Hugo Scherer and Marc Müller at Wageningen Economic Research.</p>
<!-- badges: start -->
<!-- badges: end -->
<p>The goal of FarmDynR is to give the user the ability to aggregate FADN data to create representative farms for any grouping available, generate descriptive statistics, and to run FarmDyn from R.</p>
<p>Additionally, it includes useful functions to analyze the results of FarmDyn simulations.</p>
<div class="section level2">
<h2 id="requirements">Requirements<a class="anchor" aria-label="anchor" href="#requirements"></a>
</h2>
Hugo Scherer's avatar
Hugo Scherer committed
<p>This package requires that you have GAMS and FarmDyn installed. Additionally, the package imports from:</p>
<ul>
<li><p>gdxrrw (remember to load GAMS with <code>igdx()</code> with the path to your version of GAMS)</p></li>
Hugo Scherer's avatar
Hugo Scherer committed
<li><p>readr</p></li>
<li><p>dplyr</p></li>
<li><p>tidyr</p></li>
<li><p>readxl</p></li>
<li><p>data.table</p></li>
Hugo Scherer's avatar
Hugo Scherer committed
</ul>
<p>It also suggests:</p>
<ul>
<li>stringr</li>
</ul>
</div>
<div class="section level2">
<h2 id="installation">Installation<a class="anchor" aria-label="anchor" href="#installation"></a>
</h2>
<p>You can install the development version of FarmDynR like so:</p>
<div class="sourceCode" id="cb1"><pre class="downlit sourceCode r">
<code class="sourceCode R"><span> <span class="fu"><a href="https://rdrr.io/r/utils/install.packages.html" class="external-link">install.packages</a></span><span class="op">(</span><span class="st">'https://gitlab.iiasa.ac.at/mind-step/FarmDynR'</span><span class="op">)</span></span></code></pre></div>
</div>
<div class="section level2">
<h2 id="workflow">Workflow<a class="anchor" aria-label="anchor" href="#workflow"></a>
</h2>
Hugo Scherer's avatar
Hugo Scherer committed
<p>The workflow for this package is as follows:</p>
<ol style="list-style-type: decimal">
<li><p>Load the individual farm FADN data into R (with desired countries and years)</p></li>
<li>
<p>Run <code><a href="reference/fadn2fd.html">fadn2fd()</a></code> with the FADN data, farmbranch desired, the mapping and the option to save GDX files based on the mapping - Yields will be calculated and the FADN data will be prepared - Outliers are removed</p>
<ul>
<li><p>The mapping is a list of vectors, where each vector contains the names of the columns in the FADN data that correspond to the desired grouping. For example, if you want to group by NUTS0 and organic status, or NUTS0 or NUTS2, the mapping would be <code>list(c("NUTS0", "misc%OrganicCode"), "NUTS0", "NUTS2")</code>. The first vector is the grouping you want to use, and the following vectors are the groupings you want to aggregate to. Currently, depending on the farm branch selected, either dairy or arable, different subset conditions apply. In the case of dairy, more than 5 cows and those farms that have the top and bottom 10 % of milk yields are removed, also farm typology (TF14) of 45 is selected. In the case of arable, the farms are selected whose land is covered with 80% FarmDyn crops, also the Farm typologies 15, 16, 20 are selected.</p></li>
<li><p>From this data, the variable names are selected and/or computed for use in FarmDyn. At the same time, yield calculations per region are happening. If there is a region that does not have any yield it is imputed with the higher level region, country or European average. Once this is all done, the data is mapped using the provided list of mappings, so that different files can be generated for p_farmdata. If <code>save_gdx</code> is set to <code>TRUE</code>, the resulting files are in gdx format with a parameter p_farmdata and names depending on the provided mappings.</p></li>
<li><p>Once the gdx files are made, they have to be placed in the “dat” folder in FarmDyn</p></li>
</ul>
</li>
</ol>
<!-- --><ol start="3" style="list-style-type: decimal">
<li>Write batch file with <code><a href="reference/writeBatch.html">writeBatch()</a></code> and run FarmDyn with <code><a href="reference/runFarmDynfromBatch.html">runFarmDynfromBatch()</a></code> using the created batch file - Optional: Create descriptive statistics for reporting with <code><a href="reference/fd_desc.html">fd_desc()</a></code> with the farmbranch.
<ul>
<li>This will copy the segment towards the end of <code>runInc.gms</code> containing the settings to run FarmDyn from batch files and replace the scenario description with the mapping, the farmData file with the format “farmData_{ mapping }.gdx”, as is created in the FarmDynR package using <code><a href="reference/fadn2fd.html">fadn2fd()</a></code>, and will include all the farmIds from this gdx file</li>
<li>This only works for one mapping, however, a list of mappings will not work like it does for the aggregation. However, a single mapping made of different categories or characteristics can be accepted (for example <code>c("NUTS0", "misc%OrganicCode")</code>). The mappings have to match the ones created with <code><a href="reference/fadn2fd.html">fadn2fd()</a></code>
</li>
<li>Once the batch file is created, <code><a href="reference/runFarmDynfromBatch.html">runFarmDynfromBatch()</a></code> will run FarmDyn by giving the location of the batch file, the ini and the xml</li>
<li>The descriptive statistics are calculated using the weighted mean with the (summed up) SYS02 weights</li>
</ul>
</li>
Hugo Scherer's avatar
Hugo Scherer committed
</ol>
<p>Once the simulations have been run, the next step is to analyze the results. In FarmDynR there are many ways to do this. You can load parameters, scalars, and marginals from dump files, or access to results, automatically setting a column <code>sims</code> as the name of your simulation run. <code><a href="reference/scen_analysis.html">scen_analysis()</a></code> will load the results of the different scenarios (<code>p_res</code>) found in <code>res_{scenario name}_until_2010.gdx</code> by merging them together. <code><a href="reference/load_dumps.html">load_dumps()</a></code> does the same, but with dump files with the pattern <code>dump_{scenario name}_{farmId}.gdx</code>. From dump files, many other objects can be accessed, like variables (<code><a href="reference/vars_dump.html">vars_dump()</a></code>), scalars (<code><a href="reference/load_dump_scalar.html">load_dump_scalar()</a></code>), and marginals (<code><a href="reference/load_dump_marg.html">load_dump_marg()</a></code>), in case they are needed.</p>
<p>Taking advantage of the <code><a href="reference/scen_diff.html">scen_diff()</a></code> and <code><a href="reference/abs_diff.html">abs_diff()</a></code> functions, the relative and absolute differences compared to the reference scenario can be easily calculated. Technically, these latter functions can be used for any data, not just FarmDyn scenario results.</p>
</div>
<div class="section level2">
<h2 id="example">Example<a class="anchor" aria-label="anchor" href="#example"></a>
</h2>
<div class="sourceCode" id="cb2"><pre class="downlit sourceCode r">
<code class="sourceCode R"><span><span class="kw"><a href="https://rdrr.io/r/base/library.html" class="external-link">library</a></span><span class="op">(</span><span class="va"><a href="https://www.wur.nl/" class="external-link">FarmDynR</a></span><span class="op">)</span></span>
<span></span>
<span><span class="co"># Read in FADN data, for example using:</span></span>
<span><span class="va">FADN</span> <span class="op">&lt;-</span> <span class="fu">setDT</span><span class="op">(</span></span>
<span>  <span class="fu">ldply</span><span class="op">(</span></span>
<span>    <span class="va">FADNFiles</span>, <span class="va">fread</span>,</span>
<span>  <span class="op">)</span></span>
<span><span class="op">)</span></span>
<span></span>
<span><span class="co"># Create mapping</span></span>
<span><span class="va">mapping</span> <span class="op">&lt;-</span> <span class="fu"><a href="https://rdrr.io/r/base/list.html" class="external-link">list</a></span><span class="op">(</span><span class="fu"><a href="https://rdrr.io/r/base/c.html" class="external-link">c</a></span><span class="op">(</span><span class="st">"NUTS0"</span>, <span class="st">"misc%OrganicCode"</span><span class="op">)</span>, <span class="st">"NUTS0"</span>, <span class="st">"NUTS2"</span><span class="op">)</span></span>
<span></span>
<span><span class="co"># Create FarmDyn data</span></span>
<span><span class="va">fd_data</span> <span class="op">&lt;-</span> <span class="fu"><a href="reference/fadn2fd.html">fadn2fd</a></span><span class="op">(</span><span class="va">fadn</span>, <span class="st">"Dairy"</span>, <span class="va">mapping</span>, save_gdx <span class="op">=</span> <span class="cn">FALSE</span><span class="op">)</span></span>
<span></span>
<span><span class="co"># Create descriptive statistics</span></span>
<span><span class="fu"><a href="reference/fd_desc.html">fd_desc</a></span><span class="op">(</span><span class="va">fd_data</span>, farmbranch <span class="op">=</span> <span class="st">"dairy"</span>, csv <span class="op">=</span> <span class="cn">FALSE</span>, dir <span class="op">=</span> <span class="cn">NULL</span><span class="op">)</span></span>
<span></span>
<span><span class="co"># Write batch file</span></span>
<span><span class="fu"><a href="reference/writeBatch.html">writeBatch</a></span><span class="op">(</span><span class="st">"path/to/FarmDyn"</span>, <span class="va">mapping</span>, farmIds <span class="op">=</span> <span class="fu"><a href="https://rdrr.io/r/base/unique.html" class="external-link">unique</a></span><span class="op">(</span><span class="va">fd_data</span><span class="op">[[</span><span class="va">mapping</span><span class="op">]</span><span class="op">]</span><span class="op">$</span><span class="va">farmIds</span><span class="op">)</span><span class="op">)</span></span>
<span></span>
<span><span class="co"># Run FarmDyn</span></span>
<span><span class="fu"><a href="reference/runFarmDynfromBatch.html">runFarmDynfromBatch</a></span><span class="op">(</span><span class="st">"path/to/batch/file"</span>,</span>
<span>  IniFile <span class="op">=</span> <span class="st">"path/to/Ini.ini"</span>, XMLFile <span class="op">=</span> <span class="st">"path/to/xml.xml"</span>,</span>
<span><span class="op">)</span></span>
<span><span class="co"># Analyze the scenarios</span></span>
<span><span class="va">scenarios</span> <span class="op">&lt;-</span> <span class="fu"><a href="reference/scen_analysis.html">scen_analysis</a></span><span class="op">(</span>res_fold<span class="op">=</span><span class="fu"><a href="https://rdrr.io/r/base/file.path.html" class="external-link">file.path</a></span><span class="op">(</span><span class="st">"FarmDyn"</span>, <span class="st">"sample"</span>, <span class="st">"results"</span><span class="op">)</span>, scen_name <span class="op">=</span> <span class="fu"><a href="https://rdrr.io/r/base/c.html" class="external-link">c</a></span><span class="op">(</span><span class="st">"reference"</span>, <span class="st">"scenario1"</span>, <span class="st">"scenario2"</span><span class="op">)</span><span class="op">)</span></span>
<span></span>
<span><span class="co"># Calculate differences from reference</span></span>
<span><span class="va">selected_cols</span> <span class="op">&lt;-</span> <span class="fu"><a href="https://rdrr.io/r/base/c.html" class="external-link">c</a></span><span class="op">(</span><span class="st">"colname"</span>, <span class="st">"othercolnames"</span><span class="op">)</span></span>
<span><span class="va">other_cols</span> <span class="op">&lt;-</span> <span class="fu"><a href="https://rdrr.io/r/base/c.html" class="external-link">c</a></span><span class="op">(</span><span class="st">"colname1"</span>, <span class="st">"colname2"</span><span class="op">)</span></span>
<span></span>
<span><span class="va">scenarios</span> <span class="op">&lt;-</span> <span class="fu"><a href="reference/abs_diff.html">abs_diff</a></span><span class="op">(</span><span class="va">scenarios</span>, vars_to_diff <span class="op">=</span> <span class="va">selected_cols</span><span class="op">)</span></span>
<span><span class="va">scenarios</span> <span class="op">&lt;-</span> <span class="fu"><a href="reference/scen_diff.html">scen_diff</a></span><span class="op">(</span><span class="va">scenarios</span>, vars_to_diff <span class="op">=</span> <span class="va">other_cols</span><span class="op">)</span></span></code></pre></div>
</div>
</div>
  </div>

  <div class="col-md-3 hidden-xs hidden-sm" id="pkgdown-sidebar">
    <div class="license">
<h2 data-toc-skip>License</h2>
<ul class="list-unstyled">
<li><a href="LICENSE.html">Full license</a></li>
<li><small>GPL (&gt;= 3)</small></li>
</ul>
</div>


<div class="citation">
<h2 data-toc-skip>Citation</h2>
<ul class="list-unstyled">
<li><a href="authors.html#citation">Citing FarmDynR</a></li>
</ul>
</div>

<div class="developers">
<h2 data-toc-skip>Developers</h2>
<ul class="list-unstyled">
<li>Hugo Scherer <br><small class="roles"> Author, maintainer </small>  </li>
<li><a href="authors.html">More about authors...</a></li>
</ul>
</div>



  </div>
</div>


      <footer><div class="copyright">
  <p></p>
<p>Developed by Hugo Scherer.</p>
</div>

<div class="pkgdown">
  <p></p>
<p>Site built with <a href="https://pkgdown.r-lib.org/" class="external-link">pkgdown</a> 2.0.7.</p>
</div>

      </footer>
</div>

  


  

  </body>
</html>