Source code

Download source archive ↓

upstream-extended/community/tools/rpy_statistics_collection/linear_regression.py

1#!/usr/bin/env python
2
3import sys
4
5import rpy2.rlike.container as rlc
6import rpy2.robjects as robjects
7from rpy2.robjects.packages import importr
8
9r = robjects.r
10grdevices = importr("grDevices")
11#  from rpy import *
12
13
14def stop_err(msg):
15    sys.stderr.write(msg)
16    sys.exit()
17
18
19infile = sys.argv[1]
20y_col = int(sys.argv[2]) - 1
21x_cols = sys.argv[3].split(",")
22outfile = sys.argv[4]
23outfile2 = sys.argv[5]
24
25print("Predictor columns: %s; Response column: %d" % (x_cols, y_col + 1))
26fout = open(outfile, "w")
27elems = []
28for i, line in enumerate(file(infile)):  # noqa F821
29    line = line.rstrip("\r\n")
30    if len(line) > 0 and not line.startswith("#"):
31        elems = line.split("\t")
32        break
33    if i == 30:
34        break  # Hopefully we'll never get here...
35
36if len(elems) < 1:
37    stop_err(
38        "The data in your input dataset is either missing or not formatted properly."
39    )
40
41y_vals = []
42x_vals = []
43
44for k, col in enumerate(x_cols):
45    x_cols[k] = int(col) - 1
46    # x_vals.append([])
47
48NA = "NA"
49for ind, line in enumerate(file(infile)):  # noqa F821
50    if line and not line.startswith("#"):
51        try:
52            fields = line.split("\t")
53            try:
54                yval = float(fields[y_col])
55            except Exception:
56                yval = r("NA")
57            y_vals.append(yval)
58            for k, col in enumerate(x_cols):
59                try:
60                    xval = float(fields[col])
61                except Exception:
62                    xval = r("NA")
63                # x_vals[k].append(xval)
64                x_vals.append(xval)
65        except Exception:
66            pass
67# x_vals1 = numpy.asarray(x_vals).transpose()
68# dat= r.list(x=array(x_vals1), y=y_vals)
69fv = robjects.FloatVector(x_vals)
70m = r["matrix"](fv, ncol=len(x_cols), byrow=True)
71# ensure order for generating formula
72od = rlc.OrdDict([("y", robjects.FloatVector(y_vals)), ("x", m)])
73dat = robjects.DataFrame(od)
74# convert dat.names: ["y","x.1","x.2"] to formula string: 'y ~ x.1 + x.2'
75formula = " + ".join(dat.names).replace("+", "~", 1)
76
77# set_default_mode(NO_CONVERSION)
78try:
79    # linear_model = r.lm(r("y ~ x"), data = r.na_exclude(dat))
80    linear_model = r.lm(formula, data=r["na.exclude"](dat))
81except Exception:
82    stop_err(
83        "Error performing linear regression on the input data.\nEither the response column or one of the predictor columns contain only non-numeric or invalid values."
84    )
85# set_default_mode(BASIC_CONVERSION)
86
87# coeffs=linear_model.as_py()['coefficients']
88# yintercept= coeffs['(Intercept)']
89coeffs = linear_model.rx2("coefficients")
90yintercept = coeffs.rx2("(Intercept)")[0]
91summary = r.summary(linear_model)
92
93# co = summary.get('coefficients', 'NA')
94co = summary.rx2("coefficients")
95
96"""
97if len(co) != len(x_vals)+1:
98    stop_err("Stopped performing linear regression on the input data, since one of the predictor columns contains only non-numeric or invalid values.")
99"""
100# print >>fout, "p-value (Y-intercept)\t%s" %(co[0][3])
101print("p-value (Y-intercept)\t%s" % (co.rx(1, 4)[0]), file=fout)
102
103if len(x_vals) == 1:  # Simple linear  regression case with 1 predictor variable
104    try:
105        # slope = coeffs['x']
106        slope = r.round(float(coeffs.rx2("x")[0]), digits=10)
107    except Exception:
108        slope = "NA"
109    try:
110        # pval = co[1][3]
111        pval = r.round(float(co.rx(2, 4)[0]), digits=10)
112    except Exception:
113        pval = "NA"
114    print("Slope (c%d)\t%s" % (x_cols[0] + 1, slope), file=fout)
115    print("p-value (c%d)\t%s" % (x_cols[0] + 1, pval), file=fout)
116else:  # Multiple regression case with >1 predictors
117    ind = 1
118    # while ind < len(coeffs.keys()):
119    while ind < len(coeffs.names):
120        # print >>fout, "Slope (c%d)\t%s" %(x_cols[ind-1]+1,coeffs['x'+str(ind)])
121        print(
122            "Slope (c%d)\t%s"
123            % (
124                x_cols[ind - 1] + 1,
125                coeffs.rx2(coeffs.names[ind])[0],
126            ),
127            file=fout,
128        )
129        try:
130            # pval = co[ind][3]
131            pval = r.round(float(co.rx(ind + 1, 4)[0]), digits=10)
132        except Exception:
133            pval = "NA"
134        print("p-value (c%d)\t%s" % (x_cols[ind - 1] + 1, pval), file=fout)
135        ind += 1
136
137rsq = summary.rx2("r.squared")[0]
138adjrsq = summary.rx2("adj.r.squared")[0]
139fstat = summary.rx2("fstatistic").rx2("value")[0]
140sigma = summary.rx2("sigma")[0]
141
142try:
143    rsq = r.round(float(rsq), digits=5)
144    adjrsq = r.round(float(adjrsq), digits=5)
145    fval = r.round(fstat["value"], digits=5)
146    fstat["value"] = str(fval)
147    sigma = r.round(float(sigma), digits=10)
148except Exception:
149    pass
150
151print("R-squared\t%s" % (rsq), file=fout)
152print("Adjusted R-squared\t%s" % (adjrsq), file=fout)
153print("F-statistic\t%s" % (fstat), file=fout)
154print("Sigma\t%s" % (sigma), file=fout)
155
156r.pdf(outfile2, 8, 8)
157if len(x_vals) == 1:  # Simple linear  regression case with 1 predictor variable
158    sub_title = "Slope = %s; Y-int = %s" % (slope, yintercept)
159    try:
160        r.plot(
161            x=x_vals[0],
162            y=y_vals,
163            xlab="X",
164            ylab="Y",
165            sub=sub_title,
166            main="Scatterplot with regression",
167        )
168        r.abline(a=yintercept, b=slope, col="red")
169    except Exception:
170        pass
171else:
172    r.pairs(dat, main="Scatterplot Matrix", col="blue")
173try:
174    r.plot(linear_model)
175except Exception:
176    pass
177# r.dev_off()
178grdevices.dev_off()