statsample-glm 0.0.1 → 0.1.0

Sign up to get free protection for your applications and to get access to all the features.
Files changed (43) hide show
  1. checksums.yaml +7 -0
  2. data/.gitignore +51 -0
  3. data/.rspec +1 -0
  4. data/.travis.yml +2 -9
  5. data/Gemfile +2 -20
  6. data/LICENSE.txt +1 -1
  7. data/README.rdoc +14 -11
  8. data/Rakefile +16 -24
  9. data/lib/statsample-glm.rb +1 -11
  10. data/lib/statsample-glm/{regression.rb → glm.rb} +13 -46
  11. data/lib/statsample-glm/glm/base.rb +99 -0
  12. data/lib/statsample-glm/glm/irls/base.rb +54 -0
  13. data/lib/statsample-glm/glm/irls/logistic.rb +46 -0
  14. data/lib/statsample-glm/glm/irls/poisson.rb +48 -0
  15. data/lib/statsample-glm/glm/logistic.rb +16 -0
  16. data/lib/statsample-glm/glm/mle/base.rb +157 -0
  17. data/lib/statsample-glm/glm/mle/logistic.rb +113 -0
  18. data/lib/statsample-glm/glm/mle/normal.rb +94 -0
  19. data/lib/statsample-glm/glm/mle/probit.rb +100 -0
  20. data/lib/statsample-glm/glm/normal.rb +17 -0
  21. data/lib/statsample-glm/glm/poisson.rb +17 -0
  22. data/lib/statsample-glm/glm/probit.rb +16 -0
  23. data/lib/statsample-glm/version.rb +5 -0
  24. data/spec/data/logistic.csv +51 -0
  25. data/spec/data/logistic_mle.csv +201 -0
  26. data/spec/data/normal.csv +30 -0
  27. data/spec/logistic_spec.rb +37 -0
  28. data/spec/normal_spec.rb +15 -0
  29. data/spec/poisson_spec.rb +32 -0
  30. data/spec/probit_spec.rb +19 -0
  31. data/spec/spec_helper.rb +50 -0
  32. data/statsample-glm.gemspec +35 -0
  33. metadata +71 -145
  34. data/VERSION +0 -1
  35. data/features/bio-statsample-glm.feature +0 -9
  36. data/features/step_definitions/bio-statsample-glm_steps.rb +0 -0
  37. data/features/support/env.rb +0 -15
  38. data/lib/statsample-glm/regression/logistic.rb +0 -108
  39. data/lib/statsample-glm/regression/poisson.rb +0 -90
  40. data/test/helper.rb +0 -87
  41. data/test/test_glm.rb +0 -4
  42. data/test/test_glm_logistic.rb +0 -23
  43. data/test/test_glm_poisson.rb +0 -25
@@ -0,0 +1,100 @@
1
+ require 'statsample-glm/glm/mle/base'
2
+
3
+ module Statsample
4
+ module GLM
5
+ module MLE
6
+ # Probit MLE estimation.
7
+ # == Usage:
8
+ #
9
+ # mle = Statsample::MLE::Probit.new
10
+ # mle.newton_raphson(x,y)
11
+ # beta = mle.parameters
12
+ # likelihood = mle.likelihood(x,y,beta)
13
+ # iterations = mle.iterations
14
+ class Probit < Statsample::GLM::MLE::Base
15
+
16
+ protected
17
+ def measurement data_set, coefficients
18
+ (data_set * coefficients).map { |x| Distribution::Normal.cdf(x) }
19
+ end
20
+ # F(B'Xi)
21
+ if Statsample.has_gsl?
22
+ # F(B'Xi)
23
+ def f(b,x)
24
+ p_bx=(x*b)[0,0]
25
+ GSL::Cdf::ugaussian_P(p_bx)
26
+ end
27
+ # f(B'Xi)
28
+ def ff(b,x)
29
+ p_bx=(x*b)[0,0]
30
+ GSL::Ran::ugaussian_pdf(p_bx)
31
+ end
32
+ else
33
+ def f(b,x) #:nodoc:
34
+ p_bx=(x*b)[0,0]
35
+ Distribution::Normal.cdf(p_bx)
36
+ end
37
+ def ff(b,x) #:nodoc:
38
+ p_bx=(x*b)[0,0]
39
+ Distribution::Normal.pdf(p_bx)
40
+ end
41
+ end
42
+ # Log Likehood for x_i vector, y_i scalar and b parameters
43
+ def log_likelihood_i(xi,yi,b)
44
+ fbx=f(b,xi)
45
+ (yi.to_f*Math::log(fbx))+((1.0-yi.to_f)*Math::log(1.0-fbx))
46
+ end
47
+ # First derivative of log-likelihood probit function
48
+ # x: Matrix (NxM)
49
+ # y: Matrix (Nx1)
50
+ # p: Matrix (Mx1)
51
+ def first_derivative(x,y,b)
52
+ raise "x.rows!=y.rows" if x.row_size!=y.row_size
53
+ raise "x.columns!=p.rows" if x.column_size!=b.row_size
54
+ n = x.row_size
55
+ k = x.column_size
56
+ fd = Array.new(k)
57
+ k.times {|i| fd[i] = [0.0]}
58
+ n.times do |i|
59
+ xi = Matrix.rows([x.row(i).to_a])
60
+ fbx=f(b,xi)
61
+ value1 = (y[i,0]-fbx)/ ( fbx*(1-fbx))*ff(b,xi)
62
+ k.times do |j|
63
+ fd[j][0] += value1*xi[0,j]
64
+ end
65
+ end
66
+ Matrix.rows(fd, true)
67
+ end
68
+ # Second derivative of log-likelihood probit function
69
+ # x: Matrix (NxM)
70
+ # y: Matrix (Nx1)
71
+ # p: Matrix (Mx1)
72
+
73
+ def second_derivative(x,y,b)
74
+ raise "x.rows!=y.rows" if x.row_size!=y.row_size
75
+ raise "x.columns!=p.rows" if x.column_size!=b.row_size
76
+ n = x.row_size
77
+ k = x.column_size
78
+ if Statsample.has_gsl?
79
+ sum=GSL::Matrix.zeros(k)
80
+ else
81
+ sum=Matrix.zero(k)
82
+ end
83
+ n.times do |i|
84
+ xi=Matrix.rows([x.row(i).to_a])
85
+ fbx=f(b,xi)
86
+ val=((ff(b,xi)**2) / (fbx*(1.0-fbx)))*xi.t*xi
87
+ if Statsample.has_gsl?
88
+ val=val.to_gsl
89
+ end
90
+ sum-=val
91
+ end
92
+ if Statsample.has_gsl?
93
+ sum=sum.to_matrix
94
+ end
95
+ sum
96
+ end
97
+ end # Probit
98
+ end # MLE
99
+ end # GLM
100
+ end # Statsample
@@ -0,0 +1,17 @@
1
+ require 'statsample-glm/glm/base'
2
+
3
+ module Statsample
4
+ module GLM
5
+
6
+ class Normal < Statsample::GLM::Base
7
+
8
+ def initialize data_set, dependent, opts={}
9
+ super data_set, dependent, opts
10
+ end
11
+
12
+ def to_s
13
+ "Statsample::GLM::Normal"
14
+ end
15
+ end
16
+ end
17
+ end
@@ -0,0 +1,17 @@
1
+ require 'statsample-glm/glm/base'
2
+
3
+ module Statsample
4
+ module GLM
5
+
6
+ class Poisson < Statsample::GLM::Base
7
+
8
+ def initialize data_set, dependent, opts={}
9
+ super data_set, dependent, opts
10
+ end
11
+
12
+ def to_s
13
+ "Statsample::GLM::Poisson"
14
+ end
15
+ end
16
+ end
17
+ end
@@ -0,0 +1,16 @@
1
+ require 'statsample-glm/glm/base'
2
+
3
+ module Statsample
4
+ module GLM
5
+ class Probit < Statsample::GLM::Base
6
+
7
+ def initialize data_set, dependent, opts={}
8
+ super data_set, dependent, opts
9
+ end
10
+
11
+ def to_s
12
+ "Statsample::GLM::Probit"
13
+ end
14
+ end
15
+ end
16
+ end
@@ -0,0 +1,5 @@
1
+ module Statsample
2
+ module GLM
3
+ VERSION = "0.1.0"
4
+ end
5
+ end
@@ -0,0 +1,51 @@
1
+ x1,x2,y
2
+ 0.537322309644812,-0.866655707911859,0
3
+ -0.717124209978434,-0.367820249977585,0
4
+ -0.519166718891331,0.361486610435,1
5
+ 0.434970973986765,0.857332626245179,0
6
+ -0.761822002215759,0.133438466268095,1
7
+ 1.51170030921189,0.716104533073575,1
8
+ 0.883854199811195,1.77206093023382,1
9
+ -0.908689798854196,-0.10136697295802,1
10
+ 1.70331977539793,-0.777086491435508,0
11
+ -0.246971150634099,-0.204573554913706,1
12
+ -1.59077593922623,0.963353531412233,1
13
+ -0.721548040910253,-1.10103024900542,1
14
+ 0.467025703920194,-0.404372761837392,1
15
+ -0.510132788447137,-0.230226345183469,0
16
+ 0.430106510266798,0.0363730246866971,1
17
+ -0.144353683251536,-0.838265540390497,0
18
+ -1.54943800728303,1.12543549657924,1
19
+ 0.849307651309298,-0.57929175648001,1
20
+ -0.640304240933579,-0.747060244805248,0
21
+ 1.31462478279425,0.58946979365152,1
22
+ -0.399783455165345,-0.531952663697324,0
23
+ 0.0453055645017902,1.53338594419818,1
24
+ -2.58212161987746,0.521992029051441,1
25
+ -1.16484414309359,1.41631763288724,1
26
+ -1.08829266466281,0.611402316795129,1
27
+ -0.243893919684792,-0.518355638373296,0
28
+ -1.96655661929441,-0.515192557101107,0
29
+ 0.301335373291024,-0.672697937866108,1
30
+ -0.665832694463588,1.84347042325327,1
31
+ -0.0120650855753837,-0.21195540664804,0
32
+ 1.5116066367604,-0.269869371631611,0
33
+ 0.557300353673344,0.296155694010096,1
34
+ 1.12829931872045,-2.18097898069634,0
35
+ 0.234443748015922,-1.21314663927206,0
36
+ -2.03486690662651,1.49193669881581,1
37
+ 0.275544751380246,1.38969280369493,1
38
+ -0.231465849558696,-0.400680808117106,0
39
+ -0.356880153225012,-1.87282814976479,0
40
+ -0.57746647541923,1.82394870451051,1
41
+ 1.35758352580655,0.637864732838274,1
42
+ 1.23971669378224,-0.141155946382493,0
43
+ -0.662466275100489,0.0699950644281617,1
44
+ 0.313263561921793,1.32568550595165,1
45
+ -1.08783223256362,-0.412599258349398,1
46
+ 1.41964722846899,0.14436832227506,1
47
+ 1.29325100940785,-1.16507785388489,0
48
+ 0.72153880625103,-2.16782049922428,0
49
+ 0.440580131022748,0.24318371493798,0
50
+ 0.0351917814720056,0.258954871320764,1
51
+ -0.142353224879252,-0.151966534521183,1
@@ -0,0 +1,201 @@
1
+ a,b,c,y
2
+ 0.751712137198122,-3.26835910896471,1.70092606756102,0.0
3
+ 0.554214068711979,-2.95659724374009,2.66368360383625,0.0
4
+ -1.85331644460541,-2.8293733798982,3.34679611669776,0.0
5
+ -2.88610159022917,-0.738982407138072,4.74970154250585,0.0
6
+ -2.60553098049408,0.561020310042032,5.48308397712906,0.0
7
+ -4.27353214430849,1.62383436799538,5.35813425193809,0.0
8
+ -4.77012590041537,1.22025583429631,6.41070111604149,0.0
9
+ -6.92314832885029,2.86547174912175,8.7318591921429,0.0
10
+ -7.56419504045061,4.94028695088613,8.94193466569898,0.0
11
+ -8.63093667936286,4.27420502079032,9.27002100933965,0.0
12
+ -8.99111149517962,5.10389362675139,11.7669513163404,0.0
13
+ -9.99057638012989,7.87484596043453,12.4794035020575,0.0
14
+ -10.3818782968703,8.84300238382604,13.7498993372395,0.0
15
+ -11.0476824276485,9.44613324059569,13.502502696779,0.0
16
+ -12.4344241411336,9.7051587072806,15.122117356301,0.0
17
+ -13.6272942288038,10.419034297173,16.3289942692609,0.0
18
+ -15.6202224973741,11.3788332186947,17.7367653402519,0.0
19
+ -16.2922390869162,13.1516565107205,18.6939344625842,0.0
20
+ -16.7159137753851,14.9076297530125,18.0246863680123,0.0
21
+ -17.9501251836104,15.8533651031786,20.6826094110231,0.0
22
+ -18.9898843433331,15.4331557188719,20.9101142300728,0.0
23
+ -19.9085085832061,16.8542366574895,22.0721145894251,0.0
24
+ -21.1466526906367,18.6785324946036,23.4977598550022,0.0
25
+ -21.3675743976537,18.3208056358668,23.9121114020162,0.0
26
+ -22.131396135351,20.7616214854992,24.1683442928502,0.0
27
+ -23.1636319405886,21.1293492055766,25.2695476916398,0.0
28
+ -24.1360762823224,21.7035705688561,27.9161820459799,0.0
29
+ -25.3860721945057,23.3588003206916,27.8755285811136,0.0
30
+ -27.2546274134222,24.9201403553661,28.9810564843169,0.0
31
+ -28.84506179655,25.1681854417924,29.6749936376452,0.0
32
+ -29.5648703289208,26.3989365511582,30.3290447782847,0.0
33
+ -30.9473334996246,26.9014594846656,32.5460486188393,0.0
34
+ -30.6333205969831,27.9838524246205,32.63981307435,0.0
35
+ -31.1288464401156,28.0475385515748,34.4913247020856,0.0
36
+ -32.9192755199859,30.8664195945179,35.0830004548623,0.0
37
+ -33.5126863006676,31.7135111883763,35.963262553823,0.0
38
+ -35.0626135760564,31.0828516323826,36.6124285861271,0.0
39
+ -36.9524020008844,33.9722904006922,37.1501952879747,0.0
40
+ -36.8859458347289,34.1203333509726,38.5388292299586,0.0
41
+ -38.3029057124175,34.3054755372445,40.9141143587977,0.0
42
+ -38.4946443648172,35.0506904211858,40.2609817417396,0.0
43
+ -39.3511324722213,36.8560827180564,41.727133714198,0.0
44
+ -41.7530250817957,37.750760484498,43.8282650147001,0.0
45
+ -42.3509555518331,39.6673203525628,44.870018186039,0.0
46
+ -42.244528415626,39.0788755186789,45.7949681462311,0.0
47
+ -43.4340872344621,41.1905511155583,45.845002676574,0.0
48
+ -45.6641407465385,42.0427797845611,46.7401996025804,0.0
49
+ -46.3384022695138,42.4548123590202,47.6088880049696,0.0
50
+ -47.2110032014801,43.9466417934236,49.2425272385793,0.0
51
+ -48.5809364095671,45.2438778157098,49.7183364732398,0.0
52
+ -48.8367941243197,45.8093002387181,51.8176699160374,0.0
53
+ -49.3632780402121,47.2173660517208,51.910626136845,0.0
54
+ -51.3304857529218,48.2292571877905,53.890768221757,0.0
55
+ -52.6713212093626,48.0055890605259,54.2008024542977,0.0
56
+ -53.0492822384067,50.9227140239038,55.8288448459473,0.0
57
+ -53.5475407502719,50.399466736719,56.6653737429534,0.0
58
+ -54.9713103390392,52.2174375936081,57.5892087088816,0.0
59
+ -55.3824555656592,52.1624080567189,58.9048271122945,0.0
60
+ -57.2392061954268,53.0027225338458,59.7821645733191,0.0
61
+ -58.9104229152484,54.9892539015416,60.4219388424604,0.0
62
+ -58.1824120199409,56.3542717685635,61.2574760633137,0.0
63
+ -60.341564759061,56.7219808335487,61.0889655818828,0.0
64
+ -61.7134385451754,58.6296683714548,63.962361122351,0.0
65
+ -61.4485849309236,59.2722211731019,64.3931122256424,0.0
66
+ -63.0320403297596,60.7473007790999,64.4546114078189,0.0
67
+ -64.1041544802386,61.8740389113319,65.9649824945026,0.0
68
+ -65.4439008347463,62.1787709650214,67.730652575126,0.0
69
+ -65.3433374939793,63.9649079050439,67.2579323919331,0.0
70
+ -66.1146018531461,64.8632381781096,69.639884237254,0.0
71
+ -68.447747797377,65.4474036538936,69.6078430641903,0.0
72
+ -69.294418370038,65.4181483428477,70.0720760179831,0.0
73
+ -70.2849681665589,66.6959106050402,71.7102828712744,0.0
74
+ -70.4280941800939,67.6086197232511,72.3531894807932,1.0
75
+ -71.4902330755333,68.6959578032103,73.2224551274211,0.0
76
+ -72.0927102365517,69.6523144077888,74.0072309103283,0.0
77
+ -73.8239369250543,70.9727748471512,75.0623744862556,0.0
78
+ -74.0048051089996,72.4351875425075,76.9734643262443,0.0
79
+ -75.3894569624969,73.2005662031078,78.2395878278339,0.0
80
+ -77.2087700153069,73.873044622658,79.2174499824634,1.0
81
+ -78.3312007339737,74.3177101419062,80.4500892602852,0.0
82
+ -78.0152851918014,76.0879642141581,81.7807815886821,1.0
83
+ -79.6348564839424,76.2685721204187,82.516078707618,0.0
84
+ -81.9637989522169,77.0024986767795,82.4661448054153,0.0
85
+ -81.7384252154184,79.6668712051742,84.6703671298337,0.0
86
+ -83.6602393343142,80.4913108235325,85.1069139287146,0.0
87
+ -84.5817566138972,80.0267527066757,85.7126502390058,0.0
88
+ -84.1500643836875,82.9742935504215,87.7889543910899,0.0
89
+ -85.4066570769733,82.3347246270609,87.2294546291805,0.0
90
+ -87.7637024688064,83.3797423673815,88.7428383996369,1.0
91
+ -87.8532422216585,84.5508112461818,90.4419761299396,1.0
92
+ -89.4596482634063,86.7455889667711,90.0173299461518,0.0
93
+ -90.0512708441363,87.7184551425266,92.6086387957092,0.0
94
+ -91.7774688537336,88.3362578318951,92.5323490472281,0.0
95
+ -91.8941758249984,89.1756154247095,93.1997545484098,0.0
96
+ -93.6063748804845,89.6466643235472,95.9725910658342,0.0
97
+ -94.0398325903283,90.3812804437975,96.7702387180417,0.0
98
+ -94.8766180342569,91.4799550730711,97.2352819068861,1.0
99
+ -96.6522324427766,93.3217737988283,97.4817099617913,1.0
100
+ -96.2874457012073,93.6988909891918,98.8166711820106,0.0
101
+ -97.7197245583556,95.1054536364693,100.672788663657,0.0
102
+ -98.1481566037241,95.1218250594043,100.732200143903,0.0
103
+ -99.2071669041095,96.2150468128895,101.714216717689,0.0
104
+ -100.936418858708,98.4045788153991,102.613459593007,1.0
105
+ -102.443560747664,99.9196460296025,103.024089431854,1.0
106
+ -103.253024963442,100.800612294368,104.827681828628,1.0
107
+ -103.749612770423,101.647413918771,105.735965604858,1.0
108
+ -104.328852482254,101.52632439617,106.261555245069,1.0
109
+ -106.909854948268,103.417639882281,107.655384160196,0.0
110
+ -107.146277874964,103.735504363015,109.010106082004,1.0
111
+ -108.299680591253,105.221040236174,109.433289426765,1.0
112
+ -108.483982549154,106.450953357466,110.204651921964,1.0
113
+ -109.852652647883,107.528286824546,111.038226237901,1.0
114
+ -110.908323481598,107.109110976433,113.280776112873,1.0
115
+ -112.467924011291,108.685764539417,113.830693757837,1.0
116
+ -113.205608139008,110.215691301935,114.433169300046,0.0
117
+ -114.088065910725,111.855183307649,115.490962636794,1.0
118
+ -114.932088811468,112.821293025765,116.840371472296,1.0
119
+ -116.346270551874,112.158706089612,117.351225094843,0.0
120
+ -117.586523469426,113.660859727683,119.786169957249,0.0
121
+ -117.399020777901,115.140026386953,119.538050647704,0.0
122
+ -119.742251706502,116.297893641214,121.055886377645,1.0
123
+ -120.811568959087,116.442419636884,122.285416410297,1.0
124
+ -121.87197183902,117.772559468586,123.134175983163,0.0
125
+ -121.203532711179,118.217415486896,124.033131679149,1.0
126
+ -122.799770233652,119.066944773458,125.957266439052,0.0
127
+ -123.25939666012,121.092648631783,126.957392689029,1.0
128
+ -124.665594116895,122.943504248815,126.726922729708,1.0
129
+ -126.45629792163,123.680027227716,128.578446920242,1.0
130
+ -126.478395127934,124.860787507164,129.382354701582,1.0
131
+ -128.287481953834,124.904366629726,130.857704333381,1.0
132
+ -129.634518688906,126.002157605703,130.664679201595,1.0
133
+ -130.832869120258,126.312676682549,131.028091005298,1.0
134
+ -131.824704198069,128.491040094009,132.438881937649,1.0
135
+ -131.027748413386,128.666245091681,134.386819143277,1.0
136
+ -133.251129013031,129.041770903105,135.442572078667,0.0
137
+ -134.69199358524,131.797330031224,135.990306488139,1.0
138
+ -134.269836322383,131.114035752663,137.455991445092,1.0
139
+ -136.322562482609,133.458573512031,137.051219239798,1.0
140
+ -137.181797709245,134.690639765394,139.80399233003,1.0
141
+ -138.996622047529,135.735702581706,140.012525838337,1.0
142
+ -139.215574081352,135.215412440418,141.384571880875,1.0
143
+ -140.25643517594,136.693217029473,141.619094359002,1.0
144
+ -141.091696435808,138.544186252854,143.447467807219,1.0
145
+ -141.615279226692,139.247015471284,143.781551942357,1.0
146
+ -143.738916186876,140.442376021513,145.690446407267,1.0
147
+ -143.406661315583,141.611755086021,146.834954933569,1.0
148
+ -145.539994351071,141.864759154179,146.821868669993,1.0
149
+ -146.759792237469,143.72389809576,147.648189754648,1.0
150
+ -147.742745321685,143.773042536024,148.335412957969,1.0
151
+ -147.827110918347,144.891157822512,150.991195838727,1.0
152
+ -149.385928352478,146.506476517532,151.284254745032,1.0
153
+ -150.977776481931,146.021859562023,152.768419217875,1.0
154
+ -150.312752816396,148.730155452661,152.038272157343,1.0
155
+ -151.558966920835,148.800062079608,153.169105510546,1.0
156
+ -152.600091618579,150.820711027947,154.708689228766,1.0
157
+ -153.408136649531,151.844749166301,156.485789011581,1.0
158
+ -155.851442988913,152.727616801779,157.051864933563,1.0
159
+ -155.804122660613,153.080067542298,157.928538460402,1.0
160
+ -157.073427576551,153.343498427848,158.067904946476,1.0
161
+ -157.107508909122,154.26235452322,160.34811507316,1.0
162
+ -159.484841744779,155.071935569765,161.331016311668,1.0
163
+ -160.494393486944,156.832393661499,162.766796789861,1.0
164
+ -160.993630097021,157.641448785465,162.16966187911,1.0
165
+ -162.552759845799,159.539687734098,163.085996347117,1.0
166
+ -163.564091719115,159.348960279159,164.704726412269,1.0
167
+ -163.726283737347,161.928264713823,165.777638968284,1.0
168
+ -165.875733565084,162.238166456333,167.748146433357,1.0
169
+ -166.396130106934,163.168469688741,167.777541018587,1.0
170
+ -166.362936492002,164.060990842406,168.793540054343,1.0
171
+ -167.190830641697,164.69633527615,169.716276093277,1.0
172
+ -169.856337102559,165.948746195011,171.767907484416,1.0
173
+ -170.437574285639,166.585793448766,172.423441930238,1.0
174
+ -171.364589341033,167.713387609531,172.424258150602,1.0
175
+ -171.719858914268,168.568258270241,174.186728478313,1.0
176
+ -172.222432925774,169.341495037779,175.014592418557,1.0
177
+ -174.925024864504,170.009200154178,175.823166747739,1.0
178
+ -174.440609108192,172.585679891303,177.497935948773,1.0
179
+ -175.685134632762,172.597439708769,178.865842811381,1.0
180
+ -177.037735766697,174.337033018417,178.526154196112,1.0
181
+ -177.695754636999,175.158079880788,179.090845324338,1.0
182
+ -178.530965125958,175.998360662267,180.756244303945,1.0
183
+ -179.33859605694,177.055963520977,182.537104759468,1.0
184
+ -181.051026538749,178.749994896864,182.511752447157,1.0
185
+ -181.910324655635,178.191126997934,183.205490333945,1.0
186
+ -182.658814367512,179.821227820741,184.077256331919,1.0
187
+ -184.92643232869,181.744602320009,185.895789796461,1.0
188
+ -185.336918062898,181.363095955327,186.129937394056,1.0
189
+ -185.267288986526,182.500514904395,188.252778726315,1.0
190
+ -186.872953708712,184.965877299035,188.828219832583,1.0
191
+ -188.918306879815,185.051974487609,190.899813628421,1.0
192
+ -189.763521615511,185.68824128269,190.220429056165,1.0
193
+ -190.644174012638,187.750343044182,191.493983126918,1.0
194
+ -190.321219894606,187.817497818665,193.40572238254,1.0
195
+ -192.79354360347,188.86697228881,193.159805548127,1.0
196
+ -192.025564246072,189.147494777077,194.034017243282,1.0
197
+ -193.32579637306,190.629543307974,195.316361684183,1.0
198
+ -194.09803396593,192.184663556046,196.170525930209,1.0
199
+ -196.951286736902,192.950569139836,198.865411666979,1.0
200
+ -197.273285018295,193.455170048879,199.984457526702,1.0
201
+ -197.367054178611,194.765784862729,199.831724804912,1.0
@@ -0,0 +1,30 @@
1
+ ROLL,UNEM,HGRAD,INC
2
+ 5501,8.1,9552,1923
3
+ 5945,7,9680,1961
4
+ 6629,7.3,9731,1979
5
+ 7556,7.5,11666,2030
6
+ 8716,7,14675,2112
7
+ 9369,6.4,15265,2192
8
+ 9920,6.5,15484,2235
9
+ 10167,6.4,15723,2351
10
+ 11084,6.3,16501,2411
11
+ 12504,7.7,16890,2475
12
+ 13746,8.2,17203,2524
13
+ 13656,7.5,17707,2674
14
+ 13850,7.4,18108,2833
15
+ 14145,8.2,18266,2863
16
+ 14888,10.1,19308,2839
17
+ 14991,9.2,18224,2898
18
+ 14836,7.7,18997,3123
19
+ 14478,5.7,19505,3195
20
+ 14539,6.5,19800,3239
21
+ 14395,7.5,19546,3129
22
+ 14599,7.3,19117,3100
23
+ 14969,9.2,18774,3008
24
+ 15107,10.1,17813,2983
25
+ 14831,7.5,17304,3069
26
+ 15081,8.8,16756,3151
27
+ 15127,9.1,16749,3127
28
+ 15856,8.8,16925,3179
29
+ 15938,7.8,17231,3207
30
+ 16081,7,16816,3345
@@ -0,0 +1,37 @@
1
+ require 'spec_helper.rb'
2
+
3
+ describe Statsample::GLM::Logistic do
4
+ context "IRLS algorithm" do
5
+ before do
6
+ @data_set = Statsample::CSV.read "spec/data/logistic.csv"
7
+ @glm = Statsample::GLM.compute @data_set, 'y', :logistic, {constant: 1}
8
+ end
9
+
10
+ it "reports correct coefficients as an array" do
11
+ expect_similar_vector(@glm.coefficients,[-0.312493754568903,
12
+ 2.28671333346264,0.675603176233325])
13
+
14
+ end
15
+
16
+ it "reports correct coefficients as a hash" do
17
+ expect_similar_hash(@glm.coefficients(:hash), {:constant => 0.675603176233325,
18
+ :x1 => -0.312493754568903, :x2 => 2.28671333346264})
19
+ end
20
+ end
21
+
22
+ context "MLE algorithm" do
23
+ before do
24
+ @data_set = Statsample::CSV.read("spec/data/logistic_mle.csv")
25
+ @glm = Statsample::GLM.compute @data_set,'y', :logistic, {constant: 1, algorithm: :mle}
26
+ end
27
+
28
+ it "reports correct regression values as an array" do
29
+ expect(@glm.log_likelihood).to be_within(0.001).of(-38.8669)
30
+
31
+ expect_similar_vector(@glm.coefficients, [0.3270, 0.8147, -0.4031,-5.3658],0.001)
32
+ expect_similar_vector(@glm.standard_error, [0.4390, 0.4270, 0.3819,1.9045],0.001)
33
+
34
+ expect(@glm.iterations).to eq(7)
35
+ end
36
+ end
37
+ end