テストデータでscipy、rpyのglmの使い方を勉強してみる。

出展はハーバード大学講義テキスト
生物統計学入門のChapter 19
データは公開されているものなのでOKかと

コードはこんな感じ

from scipy import *
from rpy import *
import csv

# low_infants.txtは下の「データ」のテキストファイル
rd1 = csv.reader(file("low_infants.txt")) 

low_inf = []

for line in rd1:
  low_inf.append(line)

# この時点では文字列のarray
low_infa = array(low_inf)

# 文字列を64精度の数値に変換
low_infa = low_infa.astype(float64)

x1=low_infa[:,4].tolist()
x2=low_infa[:,2].tolist()
y=low_inf[:,1].tolist()

ret = r.glm(r("y~x1+x2"),data=r.data_frame(x1=x1,x2=x2,y=y,family='gaussian'))

ret['coefficients']

係数は本に載っていたように出てきた。
一応、うまく行ってる?



データ

27,41,29,1360,37,0
29,40,31,1490,34,0
30,38,33,1490,32,0
28,38,31,1180,37,0
29,38,30,1200,29,1
23,32,25,680,19,0
22,33,27,620,20,1
26,38,29,1060,25,0
27,30,28,1320,27,0
25,34,29,830,32,1
23,32,26,880,26,0
26,39,30,1130,29,0
27,38,29,1140,24,0
27,39,29,1350,26,0
26,37,29,950,25,0
27,39,29,1220,25,0
26,38,29,980,28,0
29,42,33,1480,30,0
28,39,33,1250,31,1
27,38,29,1250,37,0
25,36,28,920,31,0
25,38,30,1020,21,0
24,34,27,750,21,0
31,42,33,1480,30,0
28,37,32,1140,23,0
23,32,28,670,33,1
26,36,29,1150,18,0
25,34,28,1030,20,0
23,33,29,560,29,0
28,37,30,1260,26,0
35,36,31,900,23,0
24,31,30,620,33,0
33,39,31,1440,26,0
28,39,29,1350,38,0
26,37,27,1170,23,0
26,36,27,1170,23,0
26,37,27,1170,26,0
28,40,32,1420,27,0
29,43,31,1475,27,0
28,37,28,1200,19,0
27,36,30,970,30,1
27,38,29,1190,27,0
28,39,28,1350,31,0
28,41,31,1460,29,0
22,32,27,570,23,1
22,32,25,620,35,0
28,40,30,1200,27,0
24,32,28,660,35,0
28,40,28,1280,28,0
23,33,25,830,21,0
22,31,23,680,26,0
23,32,27,640,21,0
28,39,28,1440,29,0
25,38,27,1150,32,0
27,35,27,850,32,0
24,29,26,760,24,0
23,20,25,620,19,0
21,32,23,660,41,0
25,35,26,985,33,0
22,33,24,690,17,0
27,38,29,1200,26,1
28,40,29,1370,20,0
26,35,27,1170,25,0
28,38,30,1200,30,0
27,39,30,1100,32,0
31,43,32,1480,33,0
30,38,33,1350,34,1
26,38,27,1160,23,0
27,38,31,1330,30,0
24,36,26,960,22,0
25,39,27,1080,35,0
25,36,27,1000,26,0
29,41,35,1490,34,1
25,35,28,880,18,1
29,41,30,1370,23,0
31,40,31,1325,18,1
29,39,30,1400,20,0
26,37,27,1240,29,0
23,31,25,660,18,0
23,34,25,780,25,0
25,37,26,950,14,0
25,36,29,850,39,0
28,40,29,1200,32,0
30,42,34,1440,35,1
26,38,30,1100,23,1
26,38,29,1250,18,0
29,42,33,1420,28,1
28,38,30,1400,19,0
27,36,29,1420,36,0
24,34,24,900,29,0
29,38,33,1160,33,1
23,34,25,820,39,0
28,41,32,1410,29,1
27,39,31,1300,33,1
26,38,31,1110,30,0
26,37,31,820,30,1
27,40,29,1150,28,0
28,35,32,880,35,1
28,41,33,1320,36,1
26,38,28,1080,36,0

タグ:

+ タグ編集
  • タグ:

このサイトはreCAPTCHAによって保護されており、Googleの プライバシーポリシー利用規約 が適用されます。

最終更新:2007年05月24日 23:48