stat 0.0.1 → 0.0.2

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,18 @@
1
+ #!/usr/bin/env octave -qf
2
+
3
+ printf ("%s", program_name());
4
+
5
+ for i = 1:nargin
6
+ printf (" %s", argv(){i});
7
+ end
8
+
9
+ printf ("\n");
10
+ printf ("Hello, world!\n");
11
+
12
+ r = rand(50) * 100;
13
+ whos
14
+
15
+ setenv("GNUTERM", "x11");
16
+ hist(r);
17
+ sleep(8);
18
+
@@ -0,0 +1,14 @@
1
+ #!/usr/bin/env ruby
2
+
3
+ require_relative 'stat'
4
+ require 'octave'
5
+
6
+ engine = Octave::Engine.new
7
+ engine.eval "123.456 * 789.101112"
8
+ engine.rand(10)
9
+
10
+ matrix = Octave::Matrix.new(20, 400)
11
+ 20.times { |m| 400.times { |n| matrix[m, n] = rand } }
12
+ engine.put_variable("m", matrix)
13
+
14
+ engine.save "/tmp/20_x_400_matrix"
@@ -0,0 +1,68 @@
1
+ #!/usr/bin/env ruby
2
+
3
+ require_relative 'stat'
4
+
5
+ p MY_DIR = `pwd`.strip
6
+ p R >> 'getwd()'
7
+
8
+ # converse: casts the result with 'to_ruby'
9
+ p R.converse "mean(c(1,2,3))" # -> 2.0
10
+
11
+ # convert: returns the raw Rserve::REXP result (like eval)
12
+ rexp = R.convert "mean(c(1,2,3))" # -> #<Rserve::REXP::Double:0x00000002296498 @payload=[2.0], @attr=nil>
13
+ rexp.as_doubles # -> [2.0]
14
+ p rexp.to_ruby # -> 2.0
15
+
16
+ # command: tell R what to do and only expect boolean reply
17
+ R.command "z <- mean(c(1,2,3))" # -> true
18
+ # all variables are persistent in the session and can be retrieved later
19
+ p R >> "z" # -> 2.0
20
+
21
+ p 'Correlation:'
22
+ p R.>> "cor(a,b)", a: [1, 2, 3], b: [1, 2, 3]
23
+
24
+ p hash = {
25
+ var1: [1, 2, 3, 4],
26
+ fac1: [3, 4, 5, 6],
27
+ res1: [4, 5, 6, 7]
28
+ }
29
+
30
+ ## convert with hash.to_dataframe or Rserve::DataFrame.new(hash)
31
+
32
+ R.command(dfr: hash.to_dataframe) do
33
+ %Q[
34
+ pdf("#{MY_DIR}/out/r.pdf")
35
+ hist(dfr$var1)
36
+ dev.off()
37
+ pdf("#{MY_DIR}/out/s.pdf")
38
+ plot(dfr)
39
+ dev.off()
40
+ ]
41
+ end
42
+
43
+ R.>> "pdf('#{MY_DIR}/out/t.pdf')",
44
+ "plot(c(1,2,3),c(4,5,6))",
45
+ "dev.off()"
46
+
47
+ DataRow = Struct.new(:var1, :fac1, :res1)
48
+
49
+ structs = [
50
+ DataRow.new(1, 3, 4),
51
+ DataRow.new(2, 4, 5),
52
+ DataRow.new(3, 5, 6),
53
+ DataRow.new(4, 6, 7)
54
+ ]
55
+
56
+ datafr = Rserve::DataFrame.from_structs(structs)
57
+ reply = R.converse('summary(df)', df: datafr).each_cons(6).to_a
58
+ datafr.colnames.zip(reply) { |name, data| puts [name].+(data).join("\n ") }
59
+
60
+ R >> 'plot(c(1,2,3), c(4,5,6))'
61
+ R >> 'dev.hold'
62
+ sleep(2)
63
+
64
+ R >> 'r = rand(50) * 100'
65
+ R >> 'hist(r)'
66
+ sleep(6)
67
+
68
+ #R.pause
@@ -6,3 +6,4 @@ require 'bundler/setup'
6
6
  $LOAD_PATH.unshift File.expand_path(File.dirname(__FILE__) + '/../lib')
7
7
 
8
8
  require 'stat'
9
+ require 'rserve/simpler/R'
@@ -1,3 +1,3 @@
1
1
  module Stat
2
- VERSION = "0.0.1"
2
+ VERSION = "0.0.2"
3
3
  end
metadata CHANGED
@@ -2,14 +2,14 @@
2
2
  name: stat
3
3
  version: !ruby/object:Gem::Version
4
4
  prerelease:
5
- version: 0.0.1
5
+ version: 0.0.2
6
6
  platform: ruby
7
7
  authors:
8
8
  - arvicco
9
9
  autorequire:
10
10
  bindir: bin
11
11
  cert_chain: []
12
- date: 2012-04-30 00:00:00.000000000 Z
12
+ date: 2012-05-01 00:00:00.000000000 Z
13
13
  dependencies:
14
14
  - !ruby/object:Gem::Dependency
15
15
  name: bundler
@@ -27,6 +27,22 @@ dependencies:
27
27
  none: false
28
28
  prerelease: false
29
29
  type: :runtime
30
+ - !ruby/object:Gem::Dependency
31
+ name: rserve-simpler
32
+ version_requirements: !ruby/object:Gem::Requirement
33
+ requirements:
34
+ - - ! '>='
35
+ - !ruby/object:Gem::Version
36
+ version: 0.0.6
37
+ none: false
38
+ requirement: !ruby/object:Gem::Requirement
39
+ requirements:
40
+ - - ! '>='
41
+ - !ruby/object:Gem::Version
42
+ version: 0.0.6
43
+ none: false
44
+ prerelease: false
45
+ type: :runtime
30
46
  description: Support for doing stats in Ruby
31
47
  email:
32
48
  - arvicco@gmail.com
@@ -35,15 +51,13 @@ extensions: []
35
51
  extra_rdoc_files: []
36
52
  files:
37
53
  - !binary |-
38
- YmluL3N0YXQucmI=
39
- - !binary |-
40
- YmluL3ByZWcvY29udmVydC5yYg==
54
+ YmluL29jdGF2ZV90ZXN0
41
55
  - !binary |-
42
- YmluL3ByZWcvZmlyc3QucmI=
56
+ YmluL29jdGF2ZV90ZXN0LnJi
43
57
  - !binary |-
44
- YmluL3ByZWcvaGVscGVyLnJi
58
+ YmluL3JfdGVzdC5yYg==
45
59
  - !binary |-
46
- bGliL3ByZWcucmI=
60
+ YmluL3N0YXQucmI=
47
61
  - !binary |-
48
62
  bGliL3N0YXQucmI=
49
63
  - !binary |-
@@ -1,11 +0,0 @@
1
- #!/usr/bin/env ruby
2
-
3
- require_relative 'helper'
4
-
5
- output_file = ARGV[0] || "#{DATA_DIR}/preg.csv"
6
-
7
- preg = Pregnancies.new.read #(100)
8
-
9
- File.open(output_file, 'w') do |output|
10
- output.puts preg.to_csv
11
- end
@@ -1,32 +0,0 @@
1
- #!/usr/bin/env ruby
2
-
3
- require_relative 'helper'
4
-
5
- resp = Respondents.new.read.recode #(10)
6
- puts 'Number of respondents', resp.length
7
-
8
- preg = Pregnancies.new.read.recode #(10)
9
- puts 'Number of pregnancies', preg.length
10
-
11
- #p preg.records[1]
12
-
13
- live_births = preg.records.find_all { |rec| rec.outcome == 1 }
14
- puts "Live births: #{live_births.size}"
15
-
16
- first_births = live_births.find_all { |rec| rec.birthord == 1 }
17
- first_avg_length = first_births.inject(0.0) { |sum, rec| sum + rec.prglength } / first_births.size
18
- puts "First births: #{first_births.size} total, #{first_avg_length} average length"
19
-
20
- other_births = live_births.find_all { |rec| rec.birthord != 1 }
21
- other_avg_length = other_births.inject(0.0) { |sum, rec| sum + rec.prglength } / other_births.size
22
- puts "Other births: #{other_births.size} total, #{other_avg_length} average length"
23
-
24
- puts 'Difference first-others (weeks)'
25
- p (first_avg_length - other_avg_length)
26
-
27
- puts 'Difference (hours)'
28
- p (first_avg_length - other_avg_length) * 7 * 24
29
-
30
- puts 'Difference %'
31
-
32
- p (first_avg_length - other_avg_length) / other_avg_length * 100
@@ -1,6 +0,0 @@
1
- require_relative '../stat'
2
- require 'preg'
3
- include Preg
4
-
5
- DATA_DIR = 'data/preg'
6
-
@@ -1,54 +0,0 @@
1
- require 'stat'
2
-
3
- # This module contains code for use with "Think Stats" pregnancy examples
4
- module Preg
5
-
6
- class Respondent < Stat::Record
7
- end
8
-
9
- class Pregnancy < Stat::Record
10
- end
11
-
12
- class Respondents < Stat::Table
13
- def read n=nil
14
- super 'data/preg/2002FemResp.dat', Respondent, n
15
- end
16
-
17
- def fields
18
- [['caseid', 1, 12, :i]
19
- ]
20
- end
21
- end
22
-
23
- class Pregnancies < Stat::Table
24
- def read n=nil
25
- super 'data/preg/2002FemPreg.dat', Pregnancy, n
26
- end
27
-
28
- def fields
29
- [['caseid', 1, 12, :i],
30
- ['nbrnaliv', 22, 22, :i],
31
- ['babysex', 56, 56, :i],
32
- ['birthwgt_lb', 57, 58, :i],
33
- ['birthwgt_oz', 59, 60, :i],
34
- ['prglength', 275, 276, :i],
35
- ['outcome', 277, 277, :i],
36
- ['birthord', 278, 279, :i],
37
- ['agepreg', 284, 287, :i],
38
- ['finalwgt', 423, 440, :f],
39
- ]
40
- end
41
-
42
- def recode
43
- @records.each do |rec|
44
- rec.agepreg /= 100.0 if rec.agepreg
45
- if rec.birthwgt_lb && rec.birthwgt_oz &&
46
- rec.birthwgt_lb < 20 && rec.birthwgt_oz <= 16
47
- rec.totalwgt_oz = rec.birthwgt_lb * 16 + rec.birthwgt_oz
48
- end
49
- end
50
-
51
- super
52
- end
53
- end # class Pregnancies
54
- end # module Preg