Current section
3 Versions
Jump to
Current section
3 Versions
Compare versions
5
files changed
+30
additions
-18
deletions
| @@ -1,3 +1,4 @@ | |
| 1 | + [](https://hex.pm/packages/statistex_robust) [](https://hexdocs.pm/statistex_robust/) |
| 1 2 | |
| 2 3 | # Statistex.Robust |
| @@ -1,15 +1,14 @@ | |
| 1 1 | {<<"links">>, |
| 2 2 | [{<<"GitHub">>,<<"https://github.com/chazzka/statistex_robust">>}]}. |
| 3 3 | {<<"name">>,<<"statistex_robust">>}. |
| 4 | - {<<"version">>,<<"0.1.0">>}. |
| 4 | + {<<"version">>,<<"0.1.1">>}. |
| 5 5 | {<<"description">>,<<"Robust statistics based on Statistex library">>}. |
| 6 6 | {<<"elixir">>,<<"~> 1.17">>}. |
| 7 7 | {<<"app">>,<<"statistex_robust">>}. |
| 8 8 | {<<"licenses">>,[<<"MIT">>]}. |
| 9 9 | {<<"files">>, |
| 10 10 | [<<"lib">>,<<"lib/statistex_robust.ex">>,<<"lib/bootstrap">>, |
| 11 | - <<"lib/bootstrap/bootstrap.ex">>,<<"lib/medcouple.R">>,<<"mix.exs">>, |
| 12 | - <<"README.md">>]}. |
| 11 | + <<"lib/bootstrap/bootstrap.ex">>,<<"mix.exs">>,<<"README.md">>]}. |
| 13 12 | {<<"requirements">>, |
| 14 13 | [[{<<"name">>,<<"statistex">>}, |
| 15 14 | {<<"app">>,<<"statistex">>}, |
| @@ -1,11 +0,0 @@ | |
| 1 | - # Load the required library |
| 2 | - library(robustbase) |
| 3 | - |
| 4 | - # Capture the command line arguments as a numeric vector |
| 5 | - args <- commandArgs(trailingOnly = TRUE) |
| 6 | - arr <- as.list(args) |
| 7 | - options(mc_doScale_quiet=TRUE) |
| 8 | - # Compute the medcouple using the mc function from robustbase |
| 9 | - result <- mc(c(arr)) |
| 10 | - # Print the result to be captured by Elixir |
| 11 | - cat(sprintf("%.10f", result)) |
| \ No newline at end of file |
| @@ -38,6 +38,7 @@ defmodule Statistex.Robust do | |
| 38 38 | Medcouple is a robust statistic that measures the skewness of a univariate distribution. |
| 39 39 | |
| 40 40 | Depends on Rscript (https://www.rdocumentation.org/packages/utils/versions/3.6.2/topics/Rscript). |
| 41 | + Depends on library robustbase (https://cran.r-project.org/web/packages/robustbase/index.html) |
| 41 42 | |
| 42 43 | Rscript has to be available in shell path. |
| 43 44 | |
| @@ -48,10 +49,32 @@ defmodule Statistex.Robust do | |
| 48 49 | 0.4 |
| 49 50 | """ |
| 50 51 | def medcouple(arr) do |
| 51 | - {result, _exit_code} = |
| 52 | - System.cmd("Rscript", ["lib/medcouple.R"] ++ (arr |> Enum.map(fn x -> "#{x}" end))) |
| 52 | + args = arr |> Enum.map(&to_string/1) |
| 53 53 | |
| 54 | - String.trim(result) |> String.to_float() |
| 54 | + # Capture both stdout and stderr |
| 55 | + {result, exit_code} = |
| 56 | + System.cmd("Rscript", ["-e", r_script()] ++ args, stderr_to_stdout: true) |
| 57 | + |
| 58 | + if exit_code != 0 do |
| 59 | + # Rscript ended with an error, handle it accordingly |
| 60 | + {:error, "Rscript failed with exit code #{exit_code}: #{String.trim(result)}"} |
| 61 | + else |
| 62 | + String.trim(result) |> String.to_float() |
| 63 | + end |
| 64 | + end |
| 65 | + |
| 66 | + defp r_script do |
| 67 | + """ |
| 68 | + if (!requireNamespace("robustbase", quietly = TRUE)) { |
| 69 | + stop("The 'robustbase' package is required but not installed. Please install it using install.packages('robustbase').") |
| 70 | + } |
| 71 | + library(robustbase) |
| 72 | + args <- commandArgs(trailingOnly = TRUE) |
| 73 | + arr <- as.numeric(args) |
| 74 | + options(mc_doScale_quiet=TRUE) |
| 75 | + result <- mc(arr) |
| 76 | + cat(sprintf("%.10f", result)) |
| 77 | + """ |
| 55 78 | end |
| 56 79 | |
| 57 80 | @doc """ |
| @@ -5,7 +5,7 @@ defmodule StatistexRobust.MixProject do | |
| 5 5 | [ |
| 6 6 | app: :statistex_robust, |
| 7 7 | description: "Robust statistics based on Statistex library", |
| 8 | - version: "0.1.0", |
| 8 | + version: "0.1.1", |
| 9 9 | elixir: "~> 1.17", |
| 10 10 | start_permanent: Mix.env() == :prod, |
| 11 11 | deps: deps(), |