Skip to content

Instantly share code, notes, and snippets.

@alienrobotwizard
Created July 14, 2010 15:42
Show Gist options
  • Save alienrobotwizard/475594 to your computer and use it in GitHub Desktop.
Save alienrobotwizard/475594 to your computer and use it in GitHub Desktop.
#!/usr/bin/env ruby
#
# Installing rsruby gem:
#
# sudo apt-get install r-base
# sudo gem install rsruby -- --with-R-dir=/usr/lib/R --with-R-include=/usr/share/R/include
# sudo ln -s /usr/lib/R/lib/libR.so /usr/lib/libR.so
# export R_HOME=/usr/lib/R
#
require 'rubygems'
require 'rsruby'
class RplotXY
attr_accessor :r
def initialize
@r = RSRuby.instance
end
def plot x,y
x.map!{|f| Math.log(f) rescue 0.0} # pay no attention here, move along
y.map!{|f| Math.log(f) rescue 0.0} # pay no attention here, move along
r.png("data.png")
fit = r.lsfit(x,y) # this is absurd, it returns every goddamned point too
args = {
'x' => x,
'y' => y,
'pch' => '.',
'xlab' => 'log(trstrank)',
'ylab' => 'log(followers)'}
r.plot(args)
intercept = fit["coefficients"]["Intercept"]
slope = fit["coefficients"]["X"]
r.abline(intercept, slope, 'col' => "red")
r.title('main' => "Followers vs Raw TrstRank")
r.text(r.c(2), r.c(12), "y = #{slope}*x + #{intercept}")
r.eval_R("dev.off()")
end
end
artist = RplotXY.new
data = File.readlines(ARGV[0]).map{|line| line.strip.split("\t").map{|t| t.to_f}}
x_points = data.map{|row| row.first}
y_points = data.map{|row| row.last }
artist.plot x_points, y_points
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment