-
Notifications
You must be signed in to change notification settings - Fork 7
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Documenter.jl
committed
Mar 6, 2024
0 parents
commit e1780bf
Showing
50 changed files
with
2,285 additions
and
0 deletions.
There are no files selected for viewing
Large diffs are not rendered by default.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,126 @@ | ||
<!DOCTYPE html> | ||
<html lang="en"><head><meta charset="UTF-8"/><meta name="viewport" content="width=device-width, initial-scale=1.0"/><title>R Interface · JuliaBUGS.jl</title><script data-outdated-warner src="../assets/warner.js"></script><link href="https://cdnjs.cloudflare.com/ajax/libs/lato-font/3.0.0/css/lato-font.min.css" rel="stylesheet" type="text/css"/><link href="https://cdnjs.cloudflare.com/ajax/libs/juliamono/0.045/juliamono.min.css" rel="stylesheet" type="text/css"/><link href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/5.15.4/css/fontawesome.min.css" rel="stylesheet" type="text/css"/><link href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/5.15.4/css/solid.min.css" rel="stylesheet" type="text/css"/><link href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/5.15.4/css/brands.min.css" rel="stylesheet" type="text/css"/><link href="https://cdnjs.cloudflare.com/ajax/libs/KaTeX/0.13.24/katex.min.css" rel="stylesheet" type="text/css"/><script>documenterBaseURL=".."</script><script src="https://cdnjs.cloudflare.com/ajax/libs/require.js/2.3.6/require.min.js" data-main="../assets/documenter.js"></script><script src="../siteinfo.js"></script><script src="../../versions.js"></script><link class="docs-theme-link" rel="stylesheet" type="text/css" href="../assets/themes/documenter-dark.css" data-theme-name="documenter-dark" data-theme-primary-dark/><link class="docs-theme-link" rel="stylesheet" type="text/css" href="../assets/themes/documenter-light.css" data-theme-name="documenter-light" data-theme-primary/><script src="../assets/themeswap.js"></script></head><body><div id="documenter"><nav class="docs-sidebar"><div class="docs-package-name"><span class="docs-autofit"><a href="../">JuliaBUGS.jl</a></span></div><form class="docs-search" action="../search/"><input class="docs-search-query" id="documenter-search-query" name="q" type="text" placeholder="Search docs"/></form><ul class="docs-menu"><li><a class="tocitem" href="../">Introduction</a></li><li><a class="tocitem" href="../example/">Example</a></li><li><span class="tocitem">API</span><ul><li><a class="tocitem" href="../api/">General</a></li><li><a class="tocitem" href="../functions/">Functions</a></li><li><a class="tocitem" href="../distributions/">Distributions</a></li><li><a class="tocitem" href="../user_defined_functions/">User-defined Functions and Distributions</a></li></ul></li><li><a class="tocitem" href="../graph_plotting/">Plotting</a></li><li class="is-active"><a class="tocitem" href>R Interface</a><ul class="internal"><li><a class="tocitem" href="#Reading-BUGS-data-and-init-from-R-like-lists"><span>Reading BUGS <code>data</code> and <code>init</code> from R like lists</span></a></li></ul></li><li><a class="tocitem" href="../pitfalls/">Pitfalls</a></li><li><span class="tocitem">For Developers</span><ul><li><a class="tocitem" href="../parser/">Parser</a></li><li><a class="tocitem" href="../BUGS_notes/">Notes on BUGS Implementations</a></li></ul></li></ul><div class="docs-version-selector field has-addons"><div class="control"><span class="docs-label button is-static is-size-7">Version</span></div><div class="docs-selector control is-expanded"><div class="select is-fullwidth is-size-7"><select id="documenter-version-selector"></select></div></div></div></nav><div class="docs-main"><header class="docs-navbar"><nav class="breadcrumb"><ul class="is-hidden-mobile"><li class="is-active"><a href>R Interface</a></li></ul><ul class="is-hidden-tablet"><li class="is-active"><a href>R Interface</a></li></ul></nav><div class="docs-right"><a class="docs-edit-link" href="https://github.com/TuringLang/JuliaBUGS.jl/blob/master/docs/src/R_interface.md" title="Edit on GitHub"><span class="docs-icon fab"></span><span class="docs-label is-hidden-touch">Edit on GitHub</span></a><a class="docs-settings-button fas fa-cog" id="documenter-settings-button" href="#" title="Settings"></a><a class="docs-sidebar-button fa fa-bars is-hidden-desktop" id="documenter-sidebar-button" href="#"></a></div></header><article class="content" id="documenter-page"><h1 id="Integrating-R-in-Julia"><a class="docs-heading-anchor" href="#Integrating-R-in-Julia">Integrating R in Julia</a><a id="Integrating-R-in-Julia-1"></a><a class="docs-heading-anchor-permalink" href="#Integrating-R-in-Julia" title="Permalink"></a></h1><p>Julia offers a seamless interface to the <a href="https://www.r-project.org/about.html"><code>R</code> language</a>. </p><ul><li>The <a href="https://github.com/JuliaInterop/RCall.jl"><code>RCall.jl</code></a> package enables interaction with R functions in Julia.</li><li>The <a href="https://github.com/JuliaData/RData.jl"><code>RData.jl</code></a> package allows interfacing with R data in Julia.</li></ul><h2 id="Reading-BUGS-data-and-init-from-R-like-lists"><a class="docs-heading-anchor" href="#Reading-BUGS-data-and-init-from-R-like-lists">Reading BUGS <code>data</code> and <code>init</code> from R like lists</a><a id="Reading-BUGS-data-and-init-from-R-like-lists-1"></a><a class="docs-heading-anchor-permalink" href="#Reading-BUGS-data-and-init-from-R-like-lists" title="Permalink"></a></h2><blockquote><p><strong>Warning</strong>: The data layout in BUGS assumes that the data is stored in row-major order, while R uses column-major order. This discrepancy can lead to issues. <a href="https://mc-stan.org/"><code>Stan</code></a> developers have transformed the data and initializations of example BUGS models for R, which can be found <a href="https://github.com/stan-dev/example-models/tree/master/bugs_examples">here</a>.</p></blockquote><h3 id="Reading-the-list-data-structure-from-R"><a class="docs-heading-anchor" href="#Reading-the-list-data-structure-from-R">Reading the <code>list</code> data structure from R</a><a id="Reading-the-list-data-structure-from-R-1"></a><a class="docs-heading-anchor-permalink" href="#Reading-the-list-data-structure-from-R" title="Permalink"></a></h3><p>The data for <code>Rats</code> is available <a href="https://chjackson.github.io/openbugsdoc/Examples/Ratsdata.html">here</a>. </p><p>In Julia, we can read this data into a Julia dictionary using the <code>RCall.jl</code> package.</p><pre><code class="language-julia-repl hljs">julia> using RCall | ||
|
||
julia> data = R" | ||
list( | ||
x = c(8.0, 15.0, 22.0, 29.0, 36.0), xbar = 22, N = 30, T = 5, | ||
Y = structure( | ||
.Data = c( | ||
151, 199, 246, 283, 320, | ||
145, 199, 249, 293, 354, | ||
147, 214, 263, 312, 328, | ||
155, 200, 237, 272, 297, | ||
135, 188, 230, 280, 323, | ||
159, 210, 252, 298, 331, | ||
141, 189, 231, 275, 305, | ||
159, 201, 248, 297, 338, | ||
177, 236, 285, 350, 376, | ||
134, 182, 220, 260, 296, | ||
160, 208, 261, 313, 352, | ||
143, 188, 220, 273, 314, | ||
154, 200, 244, 289, 325, | ||
171, 221, 270, 326, 358, | ||
163, 216, 242, 281, 312, | ||
160, 207, 248, 288, 324, | ||
142, 187, 234, 280, 316, | ||
156, 203, 243, 283, 317, | ||
157, 212, 259, 307, 336, | ||
152, 203, 246, 286, 321, | ||
154, 205, 253, 298, 334, | ||
139, 190, 225, 267, 302, | ||
146, 191, 229, 272, 302, | ||
157, 211, 250, 285, 323, | ||
132, 185, 237, 286, 331, | ||
160, 207, 257, 303, 345, | ||
169, 216, 261, 295, 333, | ||
157, 205, 248, 289, 316, | ||
137, 180, 219, 258, 291, | ||
153, 200, 244, 286, 324 | ||
), | ||
.Dim = c(30, 5) | ||
) | ||
) | ||
" | ||
RObject{VecSxp} | ||
$x | ||
[1] 8 15 22 29 36 | ||
|
||
$xbar | ||
[1] 22 | ||
|
||
$N | ||
[1] 30 | ||
|
||
$T | ||
[1] 5 | ||
|
||
$Y | ||
[,1] [,2] [,3] [,4] [,5] | ||
[1,] 151 141 154 157 132 | ||
[2,] 199 189 200 212 185 | ||
[3,] 246 231 244 259 237 | ||
[4,] 283 275 289 307 286 | ||
[5,] 320 305 325 336 331 | ||
[6,] 145 159 171 152 160 | ||
[7,] 199 201 221 203 207 | ||
[8,] 249 248 270 246 257 | ||
[9,] 293 297 326 286 303 | ||
[10,] 354 338 358 321 345 | ||
[11,] 147 177 163 154 169 | ||
[12,] 214 236 216 205 216 | ||
[13,] 263 285 242 253 261 | ||
[14,] 312 350 281 298 295 | ||
[15,] 328 376 312 334 333 | ||
[16,] 155 134 160 139 157 | ||
[17,] 200 182 207 190 205 | ||
[18,] 237 220 248 225 248 | ||
[19,] 272 260 288 267 289 | ||
[20,] 297 296 324 302 316 | ||
[21,] 135 160 142 146 137 | ||
[22,] 188 208 187 191 180 | ||
[23,] 230 261 234 229 219 | ||
[24,] 280 313 280 272 258 | ||
[25,] 323 352 316 302 291 | ||
[26,] 159 143 156 157 153 | ||
[27,] 210 188 203 211 200 | ||
[28,] 252 220 243 250 244 | ||
[29,] 298 273 283 285 286 | ||
[30,] 331 314 317 323 324</code></pre><p>alternatively, <code>reval(s::String)</code> will produce the same result in this case.</p><p>If the data is stores in a file, user can use function (may require customizing the function to fit specific needs)</p><pre><code class="language-julia hljs">function read_rlist_to_dictionary(filepath::String) | ||
r_data = open(filepath) do f | ||
s = read(f, String) | ||
reval(s) | ||
end | ||
return rcopy(r_data) | ||
end</code></pre><p>, and save the result to a Julia variable and access the data as a Julia dictionary</p><pre><code class="language-julia-repl hljs">julia> rcopy(data) | ||
OrderedDict{Symbol, Any} with 5 entries: | ||
:x => [8.0, 15.0, 22.0, 29.0, 36.0] | ||
:xbar => 22.0 | ||
:N => 30.0 | ||
:T => 5.0 | ||
:Y => [151.0 141.0 … 157.0 132.0; 199.0 189.0 … 212.0 185.0; … ; 298.0 273.0 … 285.0 286.0; 331.0 314.0 … 323.0 324.0]</code></pre><p>It is worth noting that <code>rcopy</code> will automatically convert data names contains <code>.</code> to <code>_</code> in Julia. E.g.</p><pre><code class="language-julia hljs">julia> rcopy(R"list(a.b = 1)") | ||
OrderedDict{Symbol, Any} with 1 entry: | ||
:a_b => 1.0</code></pre><h3 id="Transform-Dta-read-from-R-to-Julia-convention"><a class="docs-heading-anchor" href="#Transform-Dta-read-from-R-to-Julia-convention">Transform Dta read from R to Julia convention</a><a id="Transform-Dta-read-from-R-to-Julia-convention-1"></a><a class="docs-heading-anchor-permalink" href="#Transform-Dta-read-from-R-to-Julia-convention" title="Permalink"></a></h3><p>If you want to load data using the R interface, but the data source is in the same layout as BUGS, you can process the data in Julia, for instance</p><pre><code class="language-julia-repl hljs"># define a row-major reshape function, because Julia's `reshape` is column-major | ||
julia> function rreshape(v::Vector, dim) | ||
return permutedims(reshape(v, reverse(dim)), length(dim):-1:1) | ||
end | ||
rreshape (generic function with 1 method) | ||
|
||
julia> rreshape(vcat(data[:Y]...), (30, 5)) | ||
30×5 Matrix{Float64}: | ||
151.0 199.0 246.0 283.0 320.0 | ||
145.0 199.0 249.0 293.0 354.0 | ||
147.0 214.0 263.0 312.0 328.0 | ||
155.0 200.0 237.0 272.0 297.0 | ||
135.0 188.0 230.0 280.0 323.0 | ||
159.0 210.0 252.0 298.0 331.0 | ||
141.0 189.0 231.0 275.0 305.0 | ||
159.0 201.0 248.0 297.0 338.0 | ||
⋮ | ||
146.0 191.0 229.0 272.0 302.0 | ||
157.0 211.0 250.0 285.0 323.0 | ||
132.0 185.0 237.0 286.0 331.0 | ||
160.0 207.0 257.0 303.0 345.0 | ||
169.0 216.0 261.0 295.0 333.0 | ||
157.0 205.0 248.0 289.0 316.0 | ||
137.0 180.0 219.0 258.0 291.0 | ||
153.0 200.0 244.0 286.0 324.0</code></pre><p>Please always verify the data before using.</p></article><nav class="docs-footer"><a class="docs-footer-prevpage" href="../graph_plotting/">« Plotting</a><a class="docs-footer-nextpage" href="../pitfalls/">Pitfalls »</a><div class="flexbox-break"></div><p class="footer-message">Powered by <a href="https://github.com/JuliaDocs/Documenter.jl">Documenter.jl</a> and the <a href="https://julialang.org/">Julia Programming Language</a>.</p></nav></div><div class="modal" id="documenter-settings"><div class="modal-background"></div><div class="modal-card"><header class="modal-card-head"><p class="modal-card-title">Settings</p><button class="delete"></button></header><section class="modal-card-body"><p><label class="label">Theme</label><div class="select"><select id="documenter-themepicker"><option value="documenter-light">documenter-light</option><option value="documenter-dark">documenter-dark</option></select></div></p><hr/><p>This document was generated with <a href="https://github.com/JuliaDocs/Documenter.jl">Documenter.jl</a> version 0.27.25 on <span class="colophon-date" title="Monday 4 March 2024 19:15">Monday 4 March 2024</span>. Using Julia version 1.10.2.</p></section><footer class="modal-card-foot"></footer></div></div></div></body></html> |
Oops, something went wrong.