{"id":628,"date":"2012-10-18T13:40:19","date_gmt":"2012-10-18T13:40:19","guid":{"rendered":"http:\/\/garysieling.com\/blog\/?p=628"},"modified":"2012-10-18T13:40:19","modified_gmt":"2012-10-18T13:40:19","slug":"building-a-statistical-significance-testing-web-service-powered-by-r","status":"publish","type":"post","link":"https:\/\/www.garysieling.com\/blog\/building-a-statistical-significance-testing-web-service-powered-by-r\/","title":{"rendered":"Building a statistical significance testing web service powered by R"},"content":{"rendered":"<p><a href=\"http:\/\/www.r-project.org\/\">R<\/a> is a programming language focused on solving statistical and mathematical calculations. R programs often operate on <a href=\"http:\/\/garysieling.com\/blog\/marking-time-in-r\">large<\/a>, <a href=\"http:\/\/garysieling.com\/blog\/onset-detection-with-r\">in-memory<\/a> <a href=\"http:\/\/garysieling.com\/blog\/detecting-pitches-in-music-with-r\">data sets<\/a>, which feels somewhat similar to database programming. Examples in the <a href=\"http:\/\/garysieling.com\/blog\/book-review-r-cookbook\">R Cookbook<\/a> bear a resemblence to functional programming in clojure, <a href=\"http:\/\/www.bioinformaticszen.com\/post\/simple-functional-programming-in-r\/\">as others have noted<\/a>.<\/p>\n<p>I&#8217;ve been exploring the language to gain insight into related, but disparate technologies that I use with regularity (e.g. Postgres), but for this to be really useful, I&#8217;d like to see R behind a webservice. Looking through the official website, there are many defunct attempts at using R in this manner, often abandoned once the maintainer finishes their masters.<\/p>\n<p>A couple have survived, notably <a href=\"http:\/\/cran.r-project.org\/web\/packages\/Rook\/index.html\">Rook<\/a> and <a href=\"http:\/\/biostat.mc.vanderbilt.edu\/rapache\/\">rApache<\/a>. Rook is a web server inside of R, and rApache, as you might guess, is an Apache module that calls R. I&#8217;ve chosen rApache, as I&#8217;d like to have a battle-tested front-end for this &#8211; while R seems to have very committed maintainers, there do not seem to be very many of them, and I have yet to find examples of anyone running this as a production application.<\/p>\n<p>Inspired by WolframAlpha&#8217;s APIs, I built a small web service to test statistical significance. In the future I intend to do tests on performance and security, as well as available JSON libraries.<\/p>\n<p>Here is the installation procedure:<\/p>\n<pre>apt-get upgrade\napt-get update\napt-get install r-base r-base-dev \napt-get install apache2-mpm-prefork apache2-prefork-dev \napt-get install git-core\ngit clone https:\/\/github.com\/jeffreyhorner\/rapache.git\ncd rapache\n.\/configure\nmake\nmake test\nmake install\nvi \/etc\/apache2\/httpd.conf<\/pre>\n<p>Apache configuration settings:<\/p>\n<pre lang=\"xml\">\n\nLoadModule R_module \/usr\/lib\/apache2\/modules\/mod_R.so\n\n<Location \/RApacheInfo>\nSetHandler r-info\n<\/Location>\n\nROutputErrors\n\n<Directory \/var\/www\/R>\n        SetHandler r-script\n        RHandler sys.source\n<\/Directory>\n\n<\/pre>\n<pre>\/etc\/init.d\/apache2 restart<\/pre>\n<p>And these are the contents of ws.R:<\/p>\n<pre lang=\"R\">\n\nsetContentType(\"application\/json\")\n\nzscore<-function(p, pc, N, Nc){ (p-pc) \n     \/ sqrt(p * (1-p) \/ N + pc * (1-pc) \/ Nc) }\nsignificant<-function(p, pc, N, Nc){ \n     zscore(p, pc, N, Nc) > 1.65 }\n\nvalid<-function(x){ nchar(x) < 10 }\n\nif (!valid(GET$pc) \n || !valid(GET$p) \n || !valid(GET$N) \n || !valid(GET$Nc)) {\n  cat('error:arg length')\n} else {\ncat(significant(as.numeric(GET$p), \n                as.numeric(GET$pc), \n                as.numeric(GET$N), \n                as.numeric(GET$Nc)))\n}\n\nOK\n\n<\/pre>\n<p>For instance, the output of http:\/\/localhost:8080\/R\/ws.R?p=.15&pc=.10&N=1000&Nc=1100<br \/>\nis \"TRUE\"<\/p>\n","protected":false},"excerpt":{"rendered":"<p>R is a programming language focused on solving statistical and mathematical calculations. R programs often operate on large, in-memory data sets, which feels somewhat similar to database programming. Examples in the R Cookbook bear a resemblence to functional programming in clojure, as others have noted. I&#8217;ve been exploring the language to gain insight into related, &hellip; <\/p>\n<p class=\"link-more\"><a href=\"https:\/\/www.garysieling.com\/blog\/building-a-statistical-significance-testing-web-service-powered-by-r\/\" class=\"more-link\">Continue reading<span class=\"screen-reader-text\"> &#8220;Building a statistical significance testing web service powered by R&#8221;<\/span><\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"om_disable_all_campaigns":false,"_monsterinsights_skip_tracking":false,"_monsterinsights_sitenote_active":false,"_monsterinsights_sitenote_note":"","_monsterinsights_sitenote_category":0,"footnotes":""},"categories":[4,6,27],"tags":[440,450,452,559,587],"aioseo_notices":[],"amp_enabled":true,"_links":{"self":[{"href":"https:\/\/www.garysieling.com\/blog\/wp-json\/wp\/v2\/posts\/628"}],"collection":[{"href":"https:\/\/www.garysieling.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.garysieling.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.garysieling.com\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.garysieling.com\/blog\/wp-json\/wp\/v2\/comments?post=628"}],"version-history":[{"count":0,"href":"https:\/\/www.garysieling.com\/blog\/wp-json\/wp\/v2\/posts\/628\/revisions"}],"wp:attachment":[{"href":"https:\/\/www.garysieling.com\/blog\/wp-json\/wp\/v2\/media?parent=628"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.garysieling.com\/blog\/wp-json\/wp\/v2\/categories?post=628"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.garysieling.com\/blog\/wp-json\/wp\/v2\/tags?post=628"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}