{"id":28089280,"url":"https://github.com/rbotafogo/scicom","last_synced_at":"2025-05-13T12:55:53.165Z","repository":{"id":18037274,"uuid":"21079101","full_name":"rbotafogo/scicom","owner":"rbotafogo","description":"Scientific Computing for JRuby","archived":false,"fork":false,"pushed_at":"2016-05-10T17:31:52.000Z","size":33810,"stargazers_count":24,"open_issues_count":1,"forks_count":1,"subscribers_count":5,"default_branch":"master","last_synced_at":"2025-04-22T17:00:51.247Z","etag":null,"topics":["jruby","mdarray","r","ruby","rubydatascience"],"latest_commit_sha":null,"homepage":null,"language":"Ruby","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"gpl-3.0","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/rbotafogo.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":"LICENSE.txt","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null}},"created_at":"2014-06-21T21:21:10.000Z","updated_at":"2023-06-22T20:19:04.000Z","dependencies_parsed_at":"2022-08-20T16:10:45.005Z","dependency_job_id":null,"html_url":"https://github.com/rbotafogo/scicom","commit_stats":null,"previous_names":[],"tags_count":2,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/rbotafogo%2Fscicom","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/rbotafogo%2Fscicom/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/rbotafogo%2Fscicom/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/rbotafogo%2Fscicom/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/rbotafogo","download_url":"https://codeload.github.com/rbotafogo/scicom/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":253948335,"owners_count":21988953,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["jruby","mdarray","r","ruby","rubydatascience"],"created_at":"2025-05-13T12:55:52.464Z","updated_at":"2025-05-13T12:55:53.153Z","avatar_url":"https://github.com/rbotafogo.png","language":"Ruby","readme":"\r\n## The SciCom “language”\r\n\r\nSciCom also allows for implementing R scripts in a “language” that is\r\njust like Ruby, so that the developer does not need to know that she\r\nis actually writing an R script.  All R methods are accessible through\r\nan R namespace.\r\n\r\nThe next script is the same baseball model done in R above using\r\nSciCom ‘language’:\r\n\r\n    require ‘scicom’\r\n    # This dataset comes from Baseball-Reference.com.\r\n    baseball = R.read__csv(\"baseball.csv\")\r\n    # Lets look at the data available for Momeyball.\r\n    moneyball = baseball.subset(baseball.Year \u003c 2002)\r\n\r\n    # Let's see if we can predict the number of wins, by looking at\r\n    # runs allowed (RA) and runs scored (RS).  RD is the runs difference.\r\n    # We are making a linear model for predicting wins (W) based on RD\r\n    moneyball.RD = moneyball.RS - moneyball.RA\r\n    wins_reg = R.lm(\"W ~ RD\", data: moneyball)\r\n    wins_reg.summary.pp\r\n\r\nWe show bellow an example of calculating the correlation matrix\r\nwithout using the build-in functions.  First this is done in an R\r\nscript and then using SciCom:\r\n\r\n    # Create a matrix and give it rownames and colnames\r\n    set.seed(42)\r\n    Xij \u003c- matrix(sample(seq(0, 9), 40, replace = TRUE), ncol = 4)\r\n    rownames(Xij) \u003c- paste(\"S\", seq(1, dim(Xij)[1]), sep = \"\")\r\n    colnames(Xij) \u003c- paste(\"V\", seq(1, dim(Xij)[2]), sep = \"\")\r\n \r\n    # find the means of the columns\r\n    n \u003c- dim(Xij)[1]\r\n    one \u003c- rep(1, n)\r\n    X.means \u003c- t(one) %*% Xij/n\r\n \r\n    # find the covariance of the matrix\r\n    X.diff \u003c- Xij - one %*% X.means\r\n    X.cov \u003c- t(X.diff) %*% X.diff/(n - 1)\r\n    round(X.cov, 2)\r\n \r\n    # find the correlation \r\n    sdi \u003c- diag(1/sqrt(diag(X.cov)))\r\n    rownames(sdi) \u003c- colnames(sdi) \u003c- colnames(X.cov)\r\n    round(sdi, 2)\r\n    X.cor \u003c- sdi %*% X.cov %*% sdi\r\n    rownames(X.cor) \u003c- colnames(X.cor) \u003c- colnames(X.cov)\r\n    round(X.cor, 2)\r\n\r\nNow the same code using SciCom\r\n\r\n    require ‘scicom’\r\n    # Create a matrix and give it rownames and colnames\r\n    R.set__seed(42)\r\n\tR.seq(0,9).sample(40, replace: TRUE).matrix(ncol: 4)\r\n\t  .fassign(:rownames, R.paste(\"S\", R.seq(1, xij.attr.dim[1]), sep: \"\"))\r\n      .fassign(:colnames, R.paste(\"V\", R.seq(1, xij.attr.dim[2]), sep: \"\"))\r\n \r\n    # find the means of the columns\r\n    n = xij.dim[1]\r\n    one = R.rep(1, n)\r\n    x_means = one.t._ :*, xij/n\r\n \r\n    # find the covariance of the matrix\r\n    x_diff = xij - (one._ :*, x_means)\r\n    x_cov = (x_diff.t._ :*, x_diff/(n - 1)).round(2)\r\n \r\n    # find the correlation \r\n    sdi = (1 / x_cov.diag.sqrt).diag.round(2)\r\n    sdi.fassign(:rownames, x_cov.rownames)\r\n    sdi.fassign(:colnames, x_cov.colnames)\r\n    x_cor = ((sdi._ :*, x_cov)._ :*, sdi)\r\n      .round(2)\r\n      .fassign(:rownames, x_cov.rownames)\r\n      .fassign(:colnames, x_cov.colnames)\r\n\r\nAs another example, here is a SciCom script to print the number of\r\ndays for every month is 2005:\r\n\r\n    require ‘scicom’\r\n    everyday = R.seq(from: R.as__Date('2005-1-1'), to: R.as__Date('2005-12-31'), by: 'day')\r\n    cmonth = everyday.format('%b')\r\n    cmonth\r\n      .factor(levels: cmonth.unique, ordered: TRUE)\r\n      .table\r\n      .pp\r\n\r\nAs can be seen from these examples, R methods can be accessed through\r\nthe R namespace in SciCom, so, R method ‘seq’ is called in SciCom as\r\n‘R.seq’.  R methods that are applied on objects can be called in two\r\nways, either using the R namespace as in ‘R.factor’ or directly on the\r\nobject, as in this case we did ‘cmonth.factor’.  This last example\r\nshows how SciCom allows method chaining, which is not possible in an R\r\nscript.\r\n\r\n## R Functions\r\n\r\nSciCom allows programmers to access any R function in the R namespace.  For instance, as shown\r\nabove, function _c_ in R can be access in SciCom by a call to R.c.  As another example,\r\nmethod _seq_ in R is accessed in SciCom by a call to R.seq.\r\n\r\nParameters can be passed to R functions normaly.  For example, the code bellow creates a vector\r\nwith with doubles:\r\n\r\n\t\u003e vec = R.c(2.4, 5.55, 10, 18.27, 34.45)\r\n\t\u003e vec.pp\r\n\r\n\t[1]   2,4  5,55    10 18,27 34,45\r\n\r\nRuby variable can also be passed as arguments to R methods:\r\n\r\n\t\u003e vec2 = R.c(vec, 3.5)\r\n\t\u003e vec2.pp\r\n\r\n\t[1]   2,4  5,55    10 18,27 34,45   3,5\r\n\r\n\t\u003e dbl = 3.5\r\n\t\u003e vec3 = R.c(vec2, dbl)\r\n\t\u003e vec3.pp\r\n\r\n\t[1]   2,4  5,55    10 18,27 34,45   3,5  5,75\r\n\r\nMore complex Ruby classes, such ar Ruby hashes, of course, cannot be passed as argument to R\r\nmethods.  SciCom, in principle, should support every Ruby method that is available in\r\nRenjin.  Note that Renjin is still under development and not all methods and libraries are\r\navailable.\r\n\r\nSome methods and variables in R have a '.' in their names.  This is standard R notation;\r\nhowever, '.' in Ruby is interpreted as a method call and thus cannot be part of a\r\nvariable name or function.  In order to access names in R that have a '.' on them\r\nin SciCom, the '.' is substituted by '__':\r\n\r\n\t\u003e # variable defined in R with a '.' in the name\r\n\t\u003e R.eval(\"r.d = 10.35\")\r\n\t\u003e # access the variable 'r.d' in Ruby by using '__' notation\r\n\t\u003e R.r__d.pp\r\n\r\n\t[1] 10,35\r\n\r\nAccessing method _as.complex_ in R is also done by using '__' notation:\r\n\r\n\t# acess R method 'as.complex' using 'as__complex' notation\r\n\t\u003e comp = R.as__complex(-1)\r\n\t\u003e p R.Re(comp).gz\r\n\r\n\t-1.0\r\n\t\r\n\t\u003e p R.Im(comp).gz\r\n\r\n\t0.0\r\n\r\n\t\u003e # now method 'is.complex'\r\n\t\u003e p R.is__complex(comp).gt\r\n\r\n\ttrue\r\n\r\n### Method Chaining\r\n\r\nSciCom allows methods to be chained.  The code above can be written as:\r\n\r\n\t# prints the real part of a complex number by using method chaining\r\n\t\u003e R.as__complex(-1)\r\n\t\t.Re\r\n\t\t.pp\r\n\r\n\t[1] -1\r\n\r\nWe will see more examples of method chaining later in this documentation.\r\nIn principle, we will try to use method chaining whenever possible, but\r\nin some situations normal use of R methods through the 'R.' notation will\r\nbe used to remind the reader that this notation is also possible.\r\n\r\n### Named Parameters\r\n\r\nR allows the use of named parameters in function calls.  SciCom also allows\r\nfor named parameters.  Named parameters in SciCom require the use of\r\nRuby hashes in the normal Ruby way.\r\n\r\n    \u003e # R code to create a complex number\r\n\t\u003e R.eval(\"comp = complex(real = 0, imaginary = 1)\")\r\n\t\u003e R.eval(\"print(comp)\")\r\n\r\n\t[0.0+1.0i]\r\n\r\n\t\u003e # Same code as above in SciCom notation, with chaining\r\n\t\u003e R.complex(real: 0, imaginary: 1)\r\n\t\u003e   .pp\r\n\r\n\t[0.0+1.0i]\r\n\r\n## SciCom with Standard R Interface\r\n\r\nSciCom allows R programmers to use R commands inside a Ruby script in\r\na way similar to RinRuby by calling method eval and passing to it an R\r\nscript:\r\n\r\n    # Basic integration with R can always be done by calling eval and passing it a valid\r\n    # R expression.\r\n    \u003e R.eval(\"r.i = 10L\")\r\n    \u003e R.eval(\"print(r.i)\")\r\n\r\n    [1] 10\r\n\r\n    \u003e R.eval(\"vec = c(10, 20, 30, 40, 50)\")\r\n    \u003e R.eval(\"print(vec)\")\r\n\r\n    [1] 10 20 30 40 50\r\n\r\n    \u003e R.eval(\"print(vec[1])\")\r\n\r\n    [1] 10\r\n\r\nProgrammers can also use here docs to integrate an R script inside a\r\nRuby script.  The next example show a model for predicting baseball\r\nwins based on runs allowed and runs scored.  The data comes from\r\nBaseball-Reference.com.\r\n\r\n      R.eval \u003c\u003cEOF\r\n\r\n        # This dataset comes from Baseball-Reference.com.\r\n        baseball = read.csv(\"baseball.csv\")\r\n        # str has a bug in Renjin\r\n        # str(data)\r\n\r\n        # Lets look at the data available for Momeyball.\r\n        moneyball = subset(baseball, Year \u003c 2002)\r\n\r\n        # Let's see if we can predict the number of wins, by lookin at\r\n        # runs allowed (RA) and runs scored (RS).  RD is the runs difference.\r\n        # We are making a linear model from predicting wins (W) based on RD\r\n        moneyball$RD = moneyball$RS - moneyball$RA\r\n        WinsReg = lm(W ~ RD, data=moneyball)\r\n        print(summary(WinsReg))\r\n\r\n    EOF\r\n\r\nThe output of the program above is:\r\n\r\n    Call:\r\n    lm(data = moneyball, formula = W ~ RD)\r\n\r\n    Residuals:\r\n        Min      1Q  Median      3Q     Max\r\n    -14,266  -2,651   0,123   2,936  11,657\r\n\r\n    Coefficients:\r\n                Estimate   Std. Error t value    Pr(\u003e|t|)             \r\n    (Intercept) 80,881     0,131      616,675    \u003c0         ***       \r\n             RD 0,106      0,001       81,554    \u003c0         ***       \r\n    ---\r\n    Signif. codes:  0 '***' 0,001 '**' 0,01 '*' 0,05 '.' 0,1 ' ' 1 \r\n\r\n    Residual standard error: 3,939 on 900 degrees of freedom\r\n    Multiple R-squared: 0,8808,\tAdjusted R-squared: 0,8807 \r\n    F-statistic: 6.650,9926 on 1 and 900 DF,  p-value: \u003c 0\r\n","funding_links":[],"categories":[],"sub_categories":[],"project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Frbotafogo%2Fscicom","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Frbotafogo%2Fscicom","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Frbotafogo%2Fscicom/lists"}