Database Reference
In-Depth Information
3.
The results that come back from are a little complicated, but with some trial and
error, we can tease the answers back out, as follows:
user=>
(def x-sqr (.asList (r-source "chisqr-example.R")))
#'user/x-sqr
;; X-square
user=>
(.. x-sqr (at 0) asList (at "statistic") asDouble)
0.2166086470268894
;; degrees of freedon
user=>
(.. x-sqr (at 0) asList (at "parameter") asInteger)
2
;; p-value
user=>
(.. x-sqr (at 0) asList (at "p.value") asDouble)
0.897354468808211
How it works…
The most dificult part of this is to deal with the return value. After calling
r-source
, we
convert the output to an R list. We pull the
statistic
item from that and convert it to a double.
That's the
Χ²
value. The
parameter
item is the degrees of freedom. Also, the
p.value
item is
the p-value for the test.
Generally, when I'm picking out the results from their Java data structures, the REPL and
documentation are the biggest help. For example, the value
x-sqr
, when printed on the
REPL, displays this:
user=> x-sqr
[#<REXPGenericVector org.rosuda.REngine.REXPGenericVector@4e2f1185+[9]
named> #<REXPLogical org.rosuda.REngine.REXPLogical@43be5d17[1]>]
This tells me that the list's irst item is a generic R vector and the second item is an R logical
structure. Diving further into the irst item shows the names of the members it contains:
user=> (.. x-sqr (at 0) asList names)
["statistic" "parameter" "p.value" "method" "data.name" "observed"
"expected" "residuals" "stdres"]
This helps me pick out the values I'm looking for, and by using some test data and referring to
the documentation for the data types, I can easily write the code that is required to dig down
to the results.
There's more…
The documentation for R's Java data types is available at
http://rforge.net/org/docs/