%matplotlib inline %load_ext rmagic import numpy as np import matplotlib.pyplot as plt X = np.array([0,1,2,3,4]) Y = np.array([3,5,4,6,7]) plt.scatter(X, Y) %Rpush X Y %R lm(Y~X)\$coef Xr = X - X.mean(); Yr = Y - Y.mean() slope = (Xr*Yr).sum() / (Xr**2).sum() intercept = Y.mean() - X.mean() * slope (intercept, slope) %R resid(lm(Y~X)); coef(lm(X~Y)) b = %R a=resid(lm(Y~X)) %Rpull a print(a) assert id(b.data) == id(a.data) %R -o a %R d=resid(lm(Y~X)); e=coef(lm(Y~X)) %R -o d -o e %Rpull e print(d) print(e) import numpy as np np.testing.assert_almost_equal(d, a) A = np.arange(20) %R -i A %R mean(A) %Rget A from __future__ import print_function v1 = %R plot(X,Y); print(summary(lm(Y~X))); vv=mean(X)*mean(Y) print('v1 is:', v1) v2 = %R mean(X)*mean(Y) print('v2 is:', v2) v = %R plot(X,Y) assert v == None v = %R print(X) assert v == None v = %R print(summary(X)); X print('v:', v) %R -n X %R X; %%R -i X,Y -o XYcoef XYlm = lm(Y~X) XYcoef = coef(XYlm) print(summary(XYlm)) par(mfrow=c(2,2)) plot(XYlm) seq1 = np.arange(10) %%R -i seq1 -o seq2 seq2 = rep(seq1, 2) print(seq2) seq2[::2] = 0 seq2 %%R print(seq2) seq1[0] = 200 %R print(seq1) print(seq1) %R -i seq1 -o seq1 print(seq1) seq1[0] = 200 %R print(seq1) seq1_view = %R seq1 assert(id(seq1_view.data) == id(seq1.data)) try: %R -n nosuchvar except Exception as e: print(e) pass datapy= np.array([(1, 2.9, 'a'), (2, 3.5, 'b'), (3, 2.1, 'c')], dtype=[('x', '