statsample 1.3.1 → 1.4.0

Sign up to get free protection for your applications and to get access to all the features.
@@ -1,14 +0,0 @@
1
- module Statsample
2
- module Regression
3
- module Binomial
4
- # Logistic Regression class.
5
- # See Statsample::Regression::Binomial::BaseEngine for documentation
6
- class Logit < BaseEngine
7
- def initialize(ds,y_var)
8
- model=Statsample::MLE::Logit.new
9
- super(ds,y_var,model)
10
- end
11
- end
12
- end
13
- end
14
- end
@@ -1,14 +0,0 @@
1
- module Statsample
2
- module Regression
3
- module Binomial
4
- # Probit Regression
5
- # See Statsample::Regression::Binomial::BaseEngine for documentation
6
- class Probit < BaseEngine
7
- def initialize(ds,y_var)
8
- model=Statsample::MLE::Probit.new
9
- super(ds,y_var,model)
10
- end
11
- end
12
- end
13
- end
14
- end
@@ -1,47 +0,0 @@
1
- 79.1 151 1 9.1 58 56 510 950 33 301 108 41 394 261
2
- 163.5 143 0 11.3 103 95 583 1012 13 102 96 36 557 194
3
- 57.8 142 1 8.9 45 44 533 969 18 219 94 33 318 250
4
- 196.9 136 0 12.1 149 141 577 994 157 80 102 39 673 167
5
- 123.4 141 0 12.1 109 101 591 985 18 30 91 20 578 174
6
- 68.2 121 0 11.0 118 115 547 964 25 44 84 29 689 126
7
- 96.3 127 1 11.1 82 79 519 982 4 139 97 38 620 168
8
- 155.5 131 1 10.9 115 109 542 969 50 179 79 35 472 206
9
- 85.6 157 1 9.0 65 62 553 955 39 286 81 28 421 239
10
- 70.5 140 0 11.8 71 68 632 1029 7 15 100 24 526 174
11
- 167.4 124 0 10.5 121 116 580 966 101 106 77 35 657 170
12
- 84.9 134 0 10.8 75 71 595 972 47 59 83 31 580 172
13
- 51.1 128 0 11.3 67 60 624 972 28 10 77 25 507 206
14
- 66.4 135 0 11.7 62 61 595 986 22 46 77 27 529 190
15
- 79.8 152 1 8.7 57 53 530 986 30 72 92 43 405 264
16
- 94.6 142 1 8.8 81 77 497 956 33 321 116 47 427 247
17
- 53.9 143 0 11.0 66 63 537 977 10 6 114 35 487 166
18
- 92.9 135 1 10.4 123 115 537 978 31 170 89 34 631 165
19
- 75.0 130 0 11.6 128 128 536 934 51 24 78 34 627 135
20
- 122.5 125 0 10.8 113 105 567 985 78 94 130 58 626 166
21
- 74.2 126 0 10.8 74 67 602 984 34 12 102 33 557 195
22
- 43.9 157 1 8.9 47 44 512 962 22 423 97 34 288 276
23
- 121.6 132 0 9.6 87 83 564 953 43 92 83 32 513 227
24
- 96.8 131 0 11.6 78 73 574 1038 7 36 142 42 540 176
25
- 52.3 130 0 11.6 63 57 641 984 14 26 70 21 486 196
26
- 199.3 131 0 12.1 160 143 631 1071 3 77 102 41 674 152
27
- 34.2 135 0 10.9 69 71 540 965 6 4 80 22 564 139
28
- 121.6 152 0 11.2 82 76 571 1018 10 79 103 28 537 215
29
- 104.3 119 0 10.7 166 157 521 938 168 89 92 36 637 154
30
- 69.6 166 1 8.9 58 54 521 973 46 254 72 26 396 237
31
- 37.3 140 0 9.3 55 54 535 1045 6 20 135 40 453 200
32
- 75.4 125 0 10.9 90 81 586 964 97 82 105 43 617 163
33
- 107.2 147 1 10.4 63 64 560 972 23 95 76 24 462 233
34
- 92.3 126 0 11.8 97 97 542 990 18 21 102 35 589 166
35
- 65.3 123 0 10.2 97 87 526 948 113 76 124 50 572 158
36
- 127.2 150 0 10.0 109 98 531 964 9 24 87 38 559 153
37
- 83.1 177 1 8.7 58 56 638 974 24 349 76 28 382 254
38
- 56.6 133 0 10.4 51 47 599 1024 7 40 99 27 425 225
39
- 82.6 149 1 8.8 61 54 515 953 36 165 86 35 395 251
40
- 115.1 145 1 10.4 82 74 560 981 96 126 88 31 488 228
41
- 88.0 148 0 12.2 72 66 601 998 9 19 84 20 590 144
42
- 54.2 141 0 10.9 56 54 523 968 4 2 107 37 489 170
43
- 82.3 162 1 9.9 75 70 522 996 40 208 73 27 496 224
44
- 103.0 136 0 12.1 95 96 574 1012 29 36 111 37 622 162
45
- 45.5 139 1 8.8 46 41 480 968 19 49 135 53 457 249
46
- 50.8 126 0 10.4 106 97 599 989 40 24 78 25 593 171
47
- 84.9 130 0 12.1 90 91 623 1049 3 22 113 40 588 160
@@ -1,201 +0,0 @@
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
@@ -1,65 +0,0 @@
1
- require(File.expand_path(File.dirname(__FILE__)+'/helpers_tests.rb'))
2
- $:.unshift("/home/cdx/dev/rserve-client/lib")
3
- class StatsampleLogitTestCase < MiniTest::Unit::TestCase
4
- context Statsample::Regression::Binomial::Logit do
5
- should "return correct values for example" do
6
- crime=File.dirname(__FILE__)+'/fixtures/test_binomial.csv'
7
- ds=Statsample::CSV.read(crime)
8
- lr=Statsample::Regression::Binomial::Logit.new(ds,'y')
9
- assert_in_delta(-38.8669,lr.log_likehood,0.001)
10
- assert_in_delta(-5.3658,lr.constant,0.001)
11
-
12
- exp_coeffs={"a"=>0.3270,"b"=>0.8147, "c"=>-0.4031}
13
- exp_coeffs.each{|k,v|
14
- assert_in_delta(v,lr.coeffs[k],0.001)
15
- }
16
- exp_errors={'a'=>0.4390,'b'=>0.4270,'c'=>0.3819}
17
- exp_errors.each{|k,v|
18
- assert_in_delta(v,lr.coeffs_se[k],0.001)
19
- }
20
- assert_equal(7,lr.iterations)
21
- end
22
- end
23
- begin
24
- require 'rserve'
25
- require 'statsample/rserve_extension'
26
- should "return same similat values to as R gml" do
27
-
28
- r=Rserve::Connection.new
29
- ran=Distribution::Normal.rng
30
- samples=100
31
- a,b,c=ran.call,ran.call,ran.call
32
- logit=lambda {|x| Math.exp(x) / (1+Math.exp(x))}
33
-
34
- x1=Statsample::Vector.new_scale(samples) {ran.call}
35
- x2=Statsample::Vector.new_scale(samples) {ran.call}
36
- x3=Statsample::Vector.new_scale(samples) {ran.call}
37
-
38
- y= Statsample::Vector.new_scale(samples) {|i| logit.call(x1[i]*a+x2[i]*b+x3[i]*c+ran.call)}
39
- # Generate R object
40
- ds={'x1'=>x1,'x2'=>x2,'x3'=>x3,'y'=>y}.to_dataset
41
- r.assign('ds',ds)
42
- r.eval("mylogit<- glm(ds$y~ds$x1+ds$x2+ds$x3, family=binomial(link='logit'), na.action=na.pass)")
43
-
44
- r_logit=r.eval('summary(mylogit)')
45
- r_coeffs=r_logit.as_list['coefficients'].to_ruby
46
- ruby_logit=Statsample::Regression::Binomial::Logit.new(ds,'y')
47
-
48
- assert_in_delta(r_coeffs[0,0], ruby_logit.constant,1e-4)
49
- assert_in_delta(r_coeffs[0,1], ruby_logit.constant_se,5e-3)
50
-
51
- %w{x1 x2 x3}.each_with_index do |f,i|
52
- assert_in_delta(r_coeffs[i+1,0], ruby_logit.coeffs[f],1e-4)
53
- assert_in_delta(r_coeffs[i+1,1], ruby_logit.coeffs_se[f],5e-3)
54
-
55
- end
56
-
57
- r.close
58
-
59
- end
60
-
61
- rescue LoadError
62
- puts "Require rserve extension"
63
-
64
- end
65
- end
@@ -1,94 +0,0 @@
1
- require(File.expand_path(File.dirname(__FILE__)+'/helpers_tests.rb'))
2
- class StatsampleMLETestCase < MiniTest::Unit::TestCase
3
- def setup
4
- @file_binomial=File.dirname(__FILE__)+'/fixtures/test_binomial.csv'
5
- @crime=File.dirname(__FILE__)+'/fixtures/crime.txt'
6
- @cases=100
7
- a=Array.new()
8
- b=Array.new()
9
- c=Array.new()
10
- y=Array.new()
11
-
12
- @cases.times{|i|
13
- a.push(2*rand()-i)
14
- b.push(2*rand()-5+i)
15
- c.push(2*rand()+i)
16
- y_val=i+(rand()*@cases.quo(2) - @cases.quo(4))
17
- y.push(y_val<(@cases/2.0) ? 0.0 : 1.0)
18
- }
19
- a=a.to_vector(:scale)
20
- b=b.to_vector(:scale)
21
- c=c.to_vector(:scale)
22
- y=y.to_vector(:scale)
23
-
24
- @ds_indep={'a'=>a,'b'=>b,'c'=>c}.to_dataset
25
- constant=([1.0]*@cases).to_vector(:scale)
26
- @ds_indep_2={'constant'=>constant,'a'=>a,'b'=>b,'c'=>c}.to_dataset
27
- @ds_indep_2.fields=%w{constant a b c}
28
- @mat_x=@ds_indep_2.to_matrix
29
- @mat_y=y.to_matrix(:vertical)
30
- @ds=@ds_indep.dup
31
- @ds.add_vector('y',y)
32
- end
33
- def test_normal
34
- y=Array.new()
35
- y=@ds_indep.collect{|row|
36
- row['a']*5+row['b']+row['c']+rand()*3
37
- }
38
- constant=([1]*@cases).to_vector(:scale)
39
- ds_indep_2=@ds_indep.dup
40
- ds_indep_2['constant']=constant
41
- ds_indep_2.fields=%w{constant a b c}
42
- mat_x=ds_indep_2.to_matrix
43
- mat_y=y.to_matrix(:vertical)
44
- mle=Statsample::MLE::Normal.new()
45
- mle.verbose=false
46
- coeffs_nr=mle.newton_raphson(mat_x,mat_y)
47
- #p coeffs_nr
48
- ds=@ds_indep.dup
49
- ds.add_vector('y',y)
50
- lr=Statsample::Regression.multiple(ds, 'y')
51
- lr_constant = lr.constant
52
- lr_coeffs = lr.coeffs
53
- assert_in_delta(coeffs_nr[0,0], lr_constant,0.0000001)
54
- assert_in_delta(coeffs_nr[1,0], lr_coeffs["a"],0.0000001)
55
- assert_in_delta(coeffs_nr[2,0], lr_coeffs["b"],0.0000001)
56
- assert_in_delta(coeffs_nr[3,0], lr_coeffs["c"],0.0000001)
57
- end
58
-
59
- def test_probit
60
- ds=Statsample::CSV.read(@file_binomial)
61
- constant=([1.0]*ds.cases).to_vector(:scale)
62
- ds_indep={'constant'=>constant, 'a'=>ds['a'],'b'=>ds['b'], 'c'=>ds['c']}.to_dataset(%w{constant a b c})
63
- mat_x=ds_indep.to_matrix
64
- mat_y=ds['y'].to_matrix(:vertical)
65
- mle=Statsample::MLE::Probit.new
66
- b_probit=mle.newton_raphson(mat_x,mat_y)
67
- ll=mle.log_likehood(mat_x,mat_y,b_probit)
68
-
69
- b_exp=[-3.0670,0.1763,0.4483,-0.2240]
70
- b_exp.each_index{|i|
71
- assert_in_delta(b_exp[i], b_probit[i,0], 0.001)
72
- }
73
- assert_in_delta(-38.31559,ll,0.0001)
74
- end
75
- def test_logit_crime
76
- ds=Statsample::PlainText.read(@crime, %w{crimerat maleteen south educ police60 police59 labor males pop nonwhite unemp1 unemp2 median belowmed})
77
- constant=([1.0]*ds.cases).to_vector(:scale)
78
- ds2=ds.dup(%w{maleteen south educ police59})
79
- ds2['constant']=constant
80
- ds2.fields=%w{constant maleteen south educ police59}
81
- mat_x=ds2.to_matrix
82
- mat_y=(ds.compute "(crimerat>=110) ? 1:0").to_matrix(:vertical)
83
- mle=Statsample::MLE::Logit.new
84
- b=mle.newton_raphson(mat_x,mat_y)
85
- ll=mle.log_likehood(mat_x,mat_y,b)
86
- assert_in_delta(-18.606959,ll,0.001)
87
- exp=[-17.701, 0.0833,-1.117, 0.0229, 0.0581]
88
- exp.each_index{|i|
89
- assert_in_delta(exp[i],b[i,0],0.001)
90
- }
91
- assert_equal(5,mle.iterations)
92
- end
93
- end
94
-