annotate best_regression_subsets.py @ 2:4f33ec73e445 default tip

Corrected version string.
author devteam <devteam@galaxyproject.org>
date Thu, 10 Apr 2014 13:47:30 -0400
parents e769cde223a5
children
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
0
54c7a01a2cc7 Imported from capsule None
devteam
parents:
diff changeset
1 #!/usr/bin/env python
54c7a01a2cc7 Imported from capsule None
devteam
parents:
diff changeset
2
54c7a01a2cc7 Imported from capsule None
devteam
parents:
diff changeset
3 import sys
54c7a01a2cc7 Imported from capsule None
devteam
parents:
diff changeset
4 from rpy import *
54c7a01a2cc7 Imported from capsule None
devteam
parents:
diff changeset
5 import numpy
54c7a01a2cc7 Imported from capsule None
devteam
parents:
diff changeset
6
54c7a01a2cc7 Imported from capsule None
devteam
parents:
diff changeset
7 def stop_err(msg):
54c7a01a2cc7 Imported from capsule None
devteam
parents:
diff changeset
8 sys.stderr.write(msg)
54c7a01a2cc7 Imported from capsule None
devteam
parents:
diff changeset
9 sys.exit()
54c7a01a2cc7 Imported from capsule None
devteam
parents:
diff changeset
10
54c7a01a2cc7 Imported from capsule None
devteam
parents:
diff changeset
11
54c7a01a2cc7 Imported from capsule None
devteam
parents:
diff changeset
12 infile = sys.argv[1]
54c7a01a2cc7 Imported from capsule None
devteam
parents:
diff changeset
13 y_col = int(sys.argv[2])-1
54c7a01a2cc7 Imported from capsule None
devteam
parents:
diff changeset
14 x_cols = sys.argv[3].split(',')
54c7a01a2cc7 Imported from capsule None
devteam
parents:
diff changeset
15 outfile = sys.argv[4]
54c7a01a2cc7 Imported from capsule None
devteam
parents:
diff changeset
16 outfile2 = sys.argv[5]
54c7a01a2cc7 Imported from capsule None
devteam
parents:
diff changeset
17 print "Predictor columns: %s; Response column: %d" % ( x_cols, y_col+1 )
54c7a01a2cc7 Imported from capsule None
devteam
parents:
diff changeset
18 fout = open(outfile,'w')
54c7a01a2cc7 Imported from capsule None
devteam
parents:
diff changeset
19
54c7a01a2cc7 Imported from capsule None
devteam
parents:
diff changeset
20 for i, line in enumerate( file ( infile )):
54c7a01a2cc7 Imported from capsule None
devteam
parents:
diff changeset
21 line = line.rstrip('\r\n')
54c7a01a2cc7 Imported from capsule None
devteam
parents:
diff changeset
22 if len( line )>0 and not line.startswith( '#' ):
54c7a01a2cc7 Imported from capsule None
devteam
parents:
diff changeset
23 elems = line.split( '\t' )
54c7a01a2cc7 Imported from capsule None
devteam
parents:
diff changeset
24 break
54c7a01a2cc7 Imported from capsule None
devteam
parents:
diff changeset
25 if i == 30:
54c7a01a2cc7 Imported from capsule None
devteam
parents:
diff changeset
26 break # Hopefully we'll never get here...
54c7a01a2cc7 Imported from capsule None
devteam
parents:
diff changeset
27
54c7a01a2cc7 Imported from capsule None
devteam
parents:
diff changeset
28 if len( elems )<1:
54c7a01a2cc7 Imported from capsule None
devteam
parents:
diff changeset
29 stop_err( "The data in your input dataset is either missing or not formatted properly." )
54c7a01a2cc7 Imported from capsule None
devteam
parents:
diff changeset
30
54c7a01a2cc7 Imported from capsule None
devteam
parents:
diff changeset
31 y_vals = []
54c7a01a2cc7 Imported from capsule None
devteam
parents:
diff changeset
32 x_vals = []
54c7a01a2cc7 Imported from capsule None
devteam
parents:
diff changeset
33
54c7a01a2cc7 Imported from capsule None
devteam
parents:
diff changeset
34 for k, col in enumerate(x_cols):
54c7a01a2cc7 Imported from capsule None
devteam
parents:
diff changeset
35 x_cols[k] = int(col)-1
54c7a01a2cc7 Imported from capsule None
devteam
parents:
diff changeset
36 x_vals.append([])
54c7a01a2cc7 Imported from capsule None
devteam
parents:
diff changeset
37
54c7a01a2cc7 Imported from capsule None
devteam
parents:
diff changeset
38 NA = 'NA'
54c7a01a2cc7 Imported from capsule None
devteam
parents:
diff changeset
39 for ind, line in enumerate( file( infile ) ):
54c7a01a2cc7 Imported from capsule None
devteam
parents:
diff changeset
40 if line and not line.startswith( '#' ):
54c7a01a2cc7 Imported from capsule None
devteam
parents:
diff changeset
41 try:
54c7a01a2cc7 Imported from capsule None
devteam
parents:
diff changeset
42 fields = line.split("\t")
54c7a01a2cc7 Imported from capsule None
devteam
parents:
diff changeset
43 try:
54c7a01a2cc7 Imported from capsule None
devteam
parents:
diff changeset
44 yval = float(fields[y_col])
54c7a01a2cc7 Imported from capsule None
devteam
parents:
diff changeset
45 except Exception, ey:
54c7a01a2cc7 Imported from capsule None
devteam
parents:
diff changeset
46 yval = r('NA')
54c7a01a2cc7 Imported from capsule None
devteam
parents:
diff changeset
47 y_vals.append(yval)
54c7a01a2cc7 Imported from capsule None
devteam
parents:
diff changeset
48 for k, col in enumerate(x_cols):
54c7a01a2cc7 Imported from capsule None
devteam
parents:
diff changeset
49 try:
54c7a01a2cc7 Imported from capsule None
devteam
parents:
diff changeset
50 xval = float(fields[col])
54c7a01a2cc7 Imported from capsule None
devteam
parents:
diff changeset
51 except Exception, ex:
54c7a01a2cc7 Imported from capsule None
devteam
parents:
diff changeset
52 xval = r('NA')
54c7a01a2cc7 Imported from capsule None
devteam
parents:
diff changeset
53 x_vals[k].append(xval)
54c7a01a2cc7 Imported from capsule None
devteam
parents:
diff changeset
54 except:
54c7a01a2cc7 Imported from capsule None
devteam
parents:
diff changeset
55 pass
54c7a01a2cc7 Imported from capsule None
devteam
parents:
diff changeset
56
54c7a01a2cc7 Imported from capsule None
devteam
parents:
diff changeset
57 response_term = ""
54c7a01a2cc7 Imported from capsule None
devteam
parents:
diff changeset
58
54c7a01a2cc7 Imported from capsule None
devteam
parents:
diff changeset
59 x_vals1 = numpy.asarray(x_vals).transpose()
54c7a01a2cc7 Imported from capsule None
devteam
parents:
diff changeset
60
54c7a01a2cc7 Imported from capsule None
devteam
parents:
diff changeset
61 dat = r.list(x=array(x_vals1), y=y_vals)
54c7a01a2cc7 Imported from capsule None
devteam
parents:
diff changeset
62
54c7a01a2cc7 Imported from capsule None
devteam
parents:
diff changeset
63 r.library("leaps")
54c7a01a2cc7 Imported from capsule None
devteam
parents:
diff changeset
64
54c7a01a2cc7 Imported from capsule None
devteam
parents:
diff changeset
65 set_default_mode(NO_CONVERSION)
54c7a01a2cc7 Imported from capsule None
devteam
parents:
diff changeset
66 try:
54c7a01a2cc7 Imported from capsule None
devteam
parents:
diff changeset
67 leaps = r.regsubsets(r("y ~ x"), data= r.na_exclude(dat))
54c7a01a2cc7 Imported from capsule None
devteam
parents:
diff changeset
68 except RException, rex:
54c7a01a2cc7 Imported from capsule None
devteam
parents:
diff changeset
69 stop_err("Error performing linear regression on the input data.\nEither the response column or one of the predictor columns contain no numeric values.")
54c7a01a2cc7 Imported from capsule None
devteam
parents:
diff changeset
70 set_default_mode(BASIC_CONVERSION)
54c7a01a2cc7 Imported from capsule None
devteam
parents:
diff changeset
71
54c7a01a2cc7 Imported from capsule None
devteam
parents:
diff changeset
72 summary = r.summary(leaps)
54c7a01a2cc7 Imported from capsule None
devteam
parents:
diff changeset
73 tot = len(x_vals)
54c7a01a2cc7 Imported from capsule None
devteam
parents:
diff changeset
74 pattern = "["
54c7a01a2cc7 Imported from capsule None
devteam
parents:
diff changeset
75 for i in range(tot):
54c7a01a2cc7 Imported from capsule None
devteam
parents:
diff changeset
76 pattern = pattern + 'c' + str(int(x_cols[int(i)]) + 1) + ' '
54c7a01a2cc7 Imported from capsule None
devteam
parents:
diff changeset
77 pattern = pattern.strip() + ']'
54c7a01a2cc7 Imported from capsule None
devteam
parents:
diff changeset
78 print >> fout, "#Vars\t%s\tR-sq\tAdj. R-sq\tC-p\tbic" % (pattern)
54c7a01a2cc7 Imported from capsule None
devteam
parents:
diff changeset
79 for ind, item in enumerate(summary['outmat']):
54c7a01a2cc7 Imported from capsule None
devteam
parents:
diff changeset
80 print >> fout, "%s\t%s\t%s\t%s\t%s\t%s" % (str(item).count('*'), item, summary['rsq'][ind], summary['adjr2'][ind], summary['cp'][ind], summary['bic'][ind])
54c7a01a2cc7 Imported from capsule None
devteam
parents:
diff changeset
81
54c7a01a2cc7 Imported from capsule None
devteam
parents:
diff changeset
82
54c7a01a2cc7 Imported from capsule None
devteam
parents:
diff changeset
83 r.pdf( outfile2, 8, 8 )
54c7a01a2cc7 Imported from capsule None
devteam
parents:
diff changeset
84 r.plot(leaps, scale="Cp", main="Best subsets using Cp Criterion")
54c7a01a2cc7 Imported from capsule None
devteam
parents:
diff changeset
85 r.plot(leaps, scale="r2", main="Best subsets using R-sq Criterion")
54c7a01a2cc7 Imported from capsule None
devteam
parents:
diff changeset
86 r.plot(leaps, scale="adjr2", main="Best subsets using Adjusted R-sq Criterion")
54c7a01a2cc7 Imported from capsule None
devteam
parents:
diff changeset
87 r.plot(leaps, scale="bic", main="Best subsets using bic Criterion")
54c7a01a2cc7 Imported from capsule None
devteam
parents:
diff changeset
88
54c7a01a2cc7 Imported from capsule None
devteam
parents:
diff changeset
89 r.dev_off()