def multiReg(x, y, colList, alpha): #d = rpy.r.data_frame(y) newX = [] d = {'Y':y} model_string = "Y~" #names = ["Y"] for i in colList: name = "X" + str(i) #names.append( name ) d[name] = x[i] newX.append(x[i]) model_string = model_string + name + "+" print d #rpy.r.colnames(d) = names model_string = model_string + "1" model = rpy.r(model_string) n = rpy.sqrt( len( y ) ) norm = rpy.r.qnorm( 1. - ( alpha/200. ) ) Rlm = rpy.with_mode(rpy.NO_CONVERSION, rpy.r.lm) reg = Rlm(model, data = d) result = rpy.r.summary(reg) print result coef =result['coefficients'] r2 = result['r.squared'] r2adj = result['adj.r.squared'] ic = result['sigma']*norm/n regressor = coef[:, 0] data = {'regressor':regressor[1:], 'intercept':regressor[0], 'r2':r2, 'adj_r2':r2adj, 'ic':ic, 'x':newX, 'y':y} return data
def regression(x, y, regmodel, alpha): d = rpy.r.data_frame(X=x, Y=y) model = regmodel #reg = rpy.r.lm(model, data = d) n = rpy.sqrt(len(x)) norm = rpy.r.qt(1. - (alpha / 200.), len(x)) Rlm = rpy.with_mode(rpy.NO_CONVERSION, rpy.r.lm) reg2 = Rlm(model, data=d) result = rpy.r.summary(reg2) coef = result['coefficients'] r2 = result['r.squared'] r2adj = result['adj.r.squared'] ic = result['sigma'] * norm / n try: slope = coef[1][0] intercept = coef[0][0] except IndexError: slope = coef[0][0] intercept = 0 data = { 'slope': slope, 'intercept': intercept, 'r2': r2, 'adj_r2': r2adj, 'ic': ic, 'x': x, 'y': y } return data
def multiReg(x, y, colList, alpha): #d = rpy.r.data_frame(y) newX = [] d = {'Y': y} model_string = "Y~" #names = ["Y"] for i in colList: name = "X" + str(i) #names.append( name ) d[name] = x[i] newX.append(x[i]) model_string = model_string + name + "+" print d #rpy.r.colnames(d) = names model_string = model_string + "1" model = rpy.r(model_string) n = rpy.sqrt(len(y)) norm = rpy.r.qnorm(1. - (alpha / 200.)) Rlm = rpy.with_mode(rpy.NO_CONVERSION, rpy.r.lm) reg = Rlm(model, data=d) result = rpy.r.summary(reg) print result coef = result['coefficients'] r2 = result['r.squared'] r2adj = result['adj.r.squared'] ic = result['sigma'] * norm / n regressor = coef[:, 0] data = { 'regressor': regressor[1:], 'intercept': regressor[0], 'r2': r2, 'adj_r2': r2adj, 'ic': ic, 'x': newX, 'y': y } return data
def regression(x, y, regmodel, alpha ): d = rpy.r.data_frame(X=x, Y=y) model = regmodel #reg = rpy.r.lm(model, data = d) n = rpy.sqrt( len( x ) ) norm = rpy.r.qt( 1. - ( alpha/200. ), len(x) ) Rlm = rpy.with_mode(rpy.NO_CONVERSION, rpy.r.lm) reg2 = Rlm(model, data = d) result = rpy.r.summary(reg2) coef = result['coefficients'] r2 = result['r.squared'] r2adj = result['adj.r.squared'] ic = result[ 'sigma' ]*norm/n try: slope = coef[1][0] intercept = coef[0][0] except IndexError : slope = coef[0][0] intercept = 0 data = {'slope':slope, 'intercept':intercept, 'r2':r2, 'adj_r2':r2adj, 'ic':ic, 'x':x, 'y':y} return data