Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
# R.abline(lm,col='color') # add regression line to EXISTING plot
# print m['coefficients'][0][0] # intercept
print m
# print m['coefficients'][0][1] # slope
#Histogram: mid-points:
# print h.r['mids'][0],
# counts:
# print h.r['counts'][0]
# Standard deviation:
print R.r.sd(R.IntVector([1,2,3,4]))[0]
# matrix
my_mat = R.r.matrix(my_vec,nrow=2)
a = [1,2,3]
b = [4,5,6]
m = R.r.matrix(a+b,nrow=2,dimnames=[ ['r1','r2'], ['c1','c2','c3'] ])
print R.r.rownames(m)
print R.r.colnames(m)
# chisq.test
result = R.r['chisq.test'](my_mat)
print R.r['chisq.test'](my_mat)
print result
# print result.r
print result.r['p.value'][0][0]
# binomial test
otherTranscript.extend([x for x in values[utr_bins:] if x > 0.5])
# estimation for
# 5% chance of transiting to otherTranscript
transitions[1][2] = transitions[1][1] * 0.05
# 10% chance of remaining in otherTranscript
transitions[2][1] = 900
transitions[2][2] = 100
E.info("counting: (n,mean): within utr=%i,%f, "
"outside utr=%i,%f, otherTranscript=%i,%f" %
(len(within_utr), np.mean(within_utr),
len(outside_utr), np.mean(outside_utr),
len(otherTranscript), np.mean(otherTranscript)))
ro.globalenv['transitions'] = R.matrix(transitions, nrow=3, ncol=3)
R('''transitions = transitions / rowSums( transitions )''')
ro.globalenv['within_utr'] = ro.FloatVector(within_utr[:10000])
ro.globalenv['outside_utr'] = ro.FloatVector(outside_utr[:10000])
ro.globalenv['otherTranscript'] = ro.FloatVector(
otherTranscript[:10000])
# estimate beta distribution parameters
R('''doFit = function( data ) {
data[data == 0] = data[data == 0] + 0.001
data[data == 1] = data[data == 1] - 0.001
f = fitdistr( data, dbeta, list( shape1=0.5, shape2=0.5 ) )
return (f) }''')
fit_within_utr = R(
'''fit_within_utr = suppressMessages(doFit( within_utr))''')
fit_outside_utr = R(
L=[]
for i in testarray:
for j in i:
for k in j:
L.append(k)
#THIS WORKS WITH L!!!!
if printverbose:
pstring=str(L)
log.write(pstring)
log.write("\n")
if len(L):
if len(L) == 2:
return(1)
else:
m = robjects.r.matrix(robjects.IntVector(L), ncol=2)
#figure out a way to clear the matrix in case there's an issue
resnosim= chisqtest(m)
all=0
newall=float(all)
g5=0
newg5=float(g5)
for i in range(0,len(resnosim[6])):
newall+=1
if i > 5:newg5+=1
percent=newg5/newall
if percent < 0.80:
#check to see if the parent OTU i1 has a stored similated value
if printverbose: log.write("percent too low (%f), simulate\n" % percent)
res= chisqtest(m,simulate=True, B=reps)
if printverbose: log.write("Done chisq.test with simulate %f\n" % res[2][0])
gc.collect()
:param invariants: sample data of market invariants
:type invariants: pd.Dataframe
:param frequency: time horizon of projection, default set ot 252 days
:type frequency: int
:return: sample skew dataframe
"""
importr('PerformanceAnalytics')
if not isinstance(invariants, pd.DataFrame):
warnings.warn("invariants not a pd.Dataframe", RuntimeWarning)
invariants = pd.DataFrame(invariants)
p = invariants.shape[1]
coskew_function = robjects.r('M4.MM')
r_inv_vec = robjects.FloatVector(np.concatenate(invariants.values))
r_invariants = robjects.r.matrix(r_inv_vec,nrow=p,ncol=p)
r_M4 = coskew_function(r_invariants)
return np.matrix(r_M4)
lam = as.matrix(lambda)[1,1]
n = nrow(X)
fit = glmnet(X, y, standardize=TRUE, intercept=FALSE, thresh=1.e-10)
estimate = coef(fit, s=lam, exact=TRUE, x=X, y=y)[-1]
fit.cv = cv.glmnet(X, y, standardize=TRUE, intercept=FALSE, thresh=1.e-10)
estimate.1se = coef(fit.cv, s='lambda.1se', exact=TRUE, x=X, y=y)[-1]
estimate.min = coef(fit.cv, s='lambda.min', exact=TRUE, x=X, y=y)[-1]
return(list(estimate = estimate, estimate.1se = estimate.1se, estimate.min = estimate.min, lam.min = fit.cv$lambda.min, lam.1se = fit.cv$lambda.1se))
}''')
lambda_R = robjects.globalenv['glmnet_LASSO']
n, p = X.shape
r_X = robjects.r.matrix(X, nrow=n, ncol=p)
r_y = robjects.r.matrix(y, nrow=n, ncol=1)
r_lam = robjects.r.matrix(lambda_val, nrow=1, ncol=1)
estimate = np.array(lambda_R(r_X, r_y, r_lam).rx2('estimate'))
estimate_1se = np.array(lambda_R(r_X, r_y, r_lam).rx2('estimate.1se'))
estimate_min = np.array(lambda_R(r_X, r_y, r_lam).rx2('estimate.min'))
lam_min = np.asscalar(np.array(lambda_R(r_X, r_y, r_lam).rx2('lam.min')))
lam_1se = np.asscalar(np.array(lambda_R(r_X, r_y, r_lam).rx2('lam.1se')))
return estimate, estimate_1se, estimate_min, lam_min, lam_1se
lam_1SE = G_CV$lambda.1se
lam_minCV = G_CV$lambda.min
n = nrow(X)
lam_minCV = lam_minCV*n
lam_1SE = lam_1SE*n
lam_seq = G_CV$lambda*n
result = list(lam_minCV=lam_minCV, lam_1SE=lam_1SE, lam_seq = lam_seq, CV_err=G_CV$cvm, SD=G_CV$cvsd)
return(result)
}''')
if loss is None:
loss = self.loss
X, y = loss.data
r_glmnet_cv = robjects.globalenv['glmnet_cv']
n, p = X.shape
r_X = robjects.r.matrix(X, nrow=n, ncol=p)
r_y = robjects.r.matrix(y, nrow=n, ncol=1)
if not hasattr(self, 'lam_seq'):
result = r_glmnet_cv(r_X, r_y, self.family)
else:
r_lam_seq = robjects.r.matrix(np.true_divide(self.lam_seq, n), nrow=self.lam_seq.shape[0], ncol=1)
result = r_glmnet_cv(r_X, r_y, self.family, r_lam_seq)
lam_minCV = result[0][0]
lam_1SE = result[1][0]
lam_seq = np.array(result[2])
if not hasattr(self, 'lam_seq'):
self.lam_seq = lam_seq
CV_err = np.array(result[3])
# this is stupid but glmnet sometime cuts my given seq of lambdas
if CV_err.shape[0] < self.lam_seq.shape[0]:
CV_err_longer = np.ones(self.lam_seq.shape[0])*np.max(CV_err)
def as_matrix(scores, a, b, transposed=False):
score_type = type(scores)
if score_type == robjects.vectors.Matrix:
return scores
base = importr('base')
if transposed:
a, b = b, a
if score_type in (rinterface.FloatSexpVector,
robjects.vectors.FloatVector):
return robjects.r.matrix(scores, **{
# We expect <a> to be the column vector and <b> to be the row vector.
'ncol': len(base.names(a)),
'nrow': len(base.names(b)),
# The first dimnames element are rows, the second are columns.
'dimnames': robjects.r('list')(base.names(b), base.names(a)),
})
raise ValueError("Can't convert to matrix, unknown type: {}".format(score_type))
</b></a>
if(Type == 1){
type = "full"} else{
type = "partial"}
inf = fixedLassoInf(x = X, y = y, beta = beta, lambda=lam, family = "gaussian",
intercept=FALSE, sigma=sigma, alpha=alpha, type=type)
return(list(ci = inf$ci, pvalue = inf$pv))}
''')
inf_R = robjects.globalenv['selInf']
n, p = X.shape
r_X = robjects.r.matrix(X, nrow=n, ncol=p)
r_y = robjects.r.matrix(y, nrow=n, ncol=1)
r_beta = robjects.r.matrix(beta, nrow=p, ncol=1)
r_lam = robjects.r.matrix(lam, nrow=1, ncol=1)
r_sigma = robjects.r.matrix(sigma, nrow=1, ncol=1)
r_Type = robjects.r.matrix(Type, nrow=1, ncol=1)
output = inf_R(r_X, r_y, r_beta, r_lam, r_sigma, r_Type)
ci = np.array(output.rx2('ci'))
pvalue = np.array(output.rx2('pvalue'))
return ci, pvalue
beta = as.matrix(beta)
lam = as.matrix(lam)[1,1]
sigma = as.matrix(sigma)[1,1]
Type = as.matrix(Type)[1,1]
if(Type == 1){
type = "full"} else{
type = "partial"}
inf = fixedLassoInf(x = X, y = y, beta = beta, lambda=lam, family = "gaussian",
intercept=FALSE, sigma=sigma, alpha=alpha, type=type)
return(list(ci = inf$ci, pvalue = inf$pv))}
''')
inf_R = robjects.globalenv['selInf']
n, p = X.shape
r_X = robjects.r.matrix(X, nrow=n, ncol=p)
r_y = robjects.r.matrix(y, nrow=n, ncol=1)
r_beta = robjects.r.matrix(beta, nrow=p, ncol=1)
r_lam = robjects.r.matrix(lam, nrow=1, ncol=1)
r_sigma = robjects.r.matrix(sigma, nrow=1, ncol=1)
r_Type = robjects.r.matrix(Type, nrow=1, ncol=1)
output = inf_R(r_X, r_y, r_beta, r_lam, r_sigma, r_Type)
ci = np.array(output.rx2('ci'))
pvalue = np.array(output.rx2('pvalue'))
return ci, pvalue
def create_matrix(v_list):
vec_list = []
nrow = 0
ncol = -1
for v_sublist in v_list:
if ncol == -1:
ncol = len(v_sublist)
elif ncol != len(v_sublist):
raise TypeException("Matrix must be rectangular")
nrow += 1
vec_list.extend(v_sublist)
vec = create_vector(vec_list)
return robjects.r.matrix(vec, nrow=nrow)