fslaborg / RProvider

Access R packages from F#
http://fslab.org/RProvider/
Other
235 stars 69 forks source link

No Docs for how to extract values from test summaries #255

Open matthewcrews opened 2 years ago

matthewcrews commented 2 years ago

Is your feature request related to a problem? Please describe. I am trying to perform a binom_test and extract the p-value from the result. The documentation does not cover this scenario.

Describe the solution you'd like A simple example of how to extract values from a statistical test

Describe alternatives you've considered There are none that I have found

Additional context This is an example of what I am trying to do:

#r "nuget: RProvider"

open RProvider
open RProvider.stats

fsi.AddPrinter (fun (synexpr:RDotNet.SymbolicExpression) -> synexpr.Print())

let x = 10.0

let summary = R.binom_test(x, 100.0, 0.5, alternative="two.sided")
// What I would like to be able to do
let pValue : float = summary.GetValue "p.value"
nhirschey commented 2 years ago

Agreed this should be improved.

You can piece it together from these two links:

Example below.

#r "nuget:Rprovider"
#r "nuget:R.NET"

open RProvider
open RProvider.stats
open RDotNet

fsi.AddPrinter FSIPrinters.rValue

let x = 10.0
let summary = R.binom_test(x, 100.0, 0.5, alternative = "two.sided")

summary.AsList().Names
(*
val it: string[] =
  [|"statistic"; "parameter"; "p.value"; "conf.int"; "estimate"; "null.value";
    "alternative"; "method"; "data.name"|]
*)
summary.AsList().["p.value"].GetValue<float>()
// val it: float = 3.063290175e-17
matthewcrews commented 2 years ago

Thank you! The piece I was missing was the open RDotNet. I saw examples of AsList() but didn't notice the required open statement.

AndrewIOM commented 1 year ago

Documentation still needs to be clearer, but with the latest changes you should be able to directly access named list items without needing a reference to RDotNet as follows (by opening the RProvider.Operators module):

#r "nuget:RProvider"

open RProvider
open RProvider.Operators
open RProvider.stats

fsi.AddPrinter FSIPrinters.rValue

let x: float = 10.0
let summary = R.binom_test(x, 100.0, 0.5, alternative = "two.sided")
// val summary: SymbolicExpression =

//         Exact binomial test

// data:  10 and 100
// number of successes = 10, number of trials = 100, p-value < 2.2e-16
// alternative hypothesis: true probability of success is not equal to 0.5
// 95 percent confidence interval:
//  0.04900469 0.17622260
// sample estimates:
// probability of success 
//                    0.1 

summary?``p.value``
// val it: SymbolicExpression = [1] 3.06329e-17

summary?statistic
// val it: SymbolicExpression = number of successes 
//               10