Example: Regression

As another example of RinRuby usage, consider the usage of RinRuby for simple linear regression below. The simulation parameters are defined in Ruby, computations are performed in R, and Ruby reports the results. In a more eloborate application, the simulation parameter might come from input from a graphical user interface, the statistical analysis might be more involved, and the results might be an HTML page or PDF report.

Code:

   require "rinruby"      
   n = 10
   beta_0 = 1
   beta_1 = 0.25
   alpha = 0.05
   seed = 23423
   R.x = (1..n).entries
   R.eval <<EOF
     set.seed(#{seed})
     y <- #{beta_0} + #{beta_1}*x + rnorm(#{n})
     fit <- lm( y ~ x )
     est <- round(coef(fit),3)
     pvalue <- summary(fit)$coefficients[2,4]
   EOF
   puts "E(y|x) ~= #{R.est[0]} + #{R.est[1]} * x"
   if R.pvalue < alpha
     puts "Reject the null hypothesis and conclude that x and y are related."
   else
     puts "There is insufficient evidence to conclude that x and y are related."
   end


Output:

   E(y|x) ~= 1.264 + 0.273 * x
   Reject the null hypothesis and conclude that x and y are related.