Only prestep was 'pip install rpy2'


In [5]:
%load_ext rpy2.ipython

In [19]:
%%R
x <- rnorm(100,0, 1)
y <- rnorm(100,0, 2)
my_lm <- lm(y ~ x)
summary(my_lm)


Call:
lm(formula = y ~ x)

Residuals:
    Min      1Q  Median      3Q     Max 
-5.2047 -1.3764 -0.1121  1.4567  6.0852 

Coefficients:
            Estimate Std. Error t value Pr(>|t|)
(Intercept) -0.06891    0.20685  -0.333     0.74
x           -0.17208    0.19101  -0.901     0.37

Residual standard error: 2.033 on 98 degrees of freedom
Multiple R-squared:  0.008214,	Adjusted R-squared:  -0.001906 
F-statistic: 0.8117 on 1 and 98 DF,  p-value: 0.3698


In [13]:
%%R
x


[1] 11.2395

In [15]:
%%R
?rnorm


R Help on ‘rnorm’Normal                  package:stats                  R Documentation

_T_h_e _N_o_r_m_a_l _D_i_s_t_r_i_b_u_t_i_o_n

_D_e_s_c_r_i_p_t_i_o_n:

     Density, distribution function, quantile function and random
     generation for the normal distribution with mean equal to ‘mean’
     and standard deviation equal to ‘sd’.

_U_s_a_g_e:

     dnorm(x, mean = 0, sd = 1, log = FALSE)
     pnorm(q, mean = 0, sd = 1, lower.tail = TRUE, log.p = FALSE)
     qnorm(p, mean = 0, sd = 1, lower.tail = TRUE, log.p = FALSE)
     rnorm(n, mean = 0, sd = 1)
     
_A_r_g_u_m_e_n_t_s:

    x, q: vector of quantiles.

       p: vector of probabilities.

       n: number of observations. If ‘length(n) > 1’, the length is
          taken to be the number required.

    mean: vector of means.

      sd: vector of standard deviations.

log, log.p: logical; if TRUE, probabilities p are given as log(p).

lower.tail: logical; if TRUE (default), probabilities are P[X <= x]
          otherwise, P[X > x].

_D_e_t_a_i_l_s:

     If ‘mean’ or ‘sd’ are not specified they assume the default values
     of ‘0’ and ‘1’, respectively.

     The normal distribution has density

            f(x) = 1/(sqrt(2 pi) sigma) e^-((x - mu)^2/(2 sigma^2))     
     
     where mu is the mean of the distribution and sigma the standard
     deviation.

_V_a_l_u_e:

     ‘dnorm’ gives the density, ‘pnorm’ gives the distribution
     function, ‘qnorm’ gives the quantile function, and ‘rnorm’
     generates random deviates.

     The length of the result is determined by ‘n’ for ‘rnorm’, and is
     the maximum of the lengths of the numerical arguments for the
     other functions.

     The numerical arguments other than ‘n’ are recycled to the length
     of the result.  Only the first elements of the logical arguments
     are used.

     For ‘sd = 0’ this gives the limit as ‘sd’ decreases to 0, a point
     mass at ‘mu’.  ‘sd < 0’ is an error and returns ‘NaN’.

_S_o_u_r_c_e:

     For ‘pnorm’, based on

     Cody, W. D. (1993) Algorithm 715: SPECFUN - A portable FORTRAN
     package of special function routines and test drivers.  _ACM
     Transactions on Mathematical Software_ *19*, 22-32.

     For ‘qnorm’, the code is a C translation of

     Wichura, M. J. (1988) Algorithm AS 241: The percentage points of
     the normal distribution.  _Applied Statistics_, *37*, 477-484.

     which provides precise results up to about 16 digits.

     For ‘rnorm’, see RNG for how to select the algorithm and for
     references to the supplied methods.

_R_e_f_e_r_e_n_c_e_s:

     Becker, R. A., Chambers, J. M. and Wilks, A. R. (1988) _The New S
     Language_.  Wadsworth & Brooks/Cole.

     Johnson, N. L., Kotz, S. and Balakrishnan, N. (1995) _Continuous
     Univariate Distributions_, volume 1, chapter 13.  Wiley, New York.

_S_e_e _A_l_s_o:

     Distributions for other standard distributions, including ‘dlnorm’
     for the _Log_normal distribution.

_E_x_a_m_p_l_e_s:

     require(graphics)
     
     dnorm(0) == 1/sqrt(2*pi)
     dnorm(1) == exp(-1/2)/sqrt(2*pi)
     dnorm(1) == 1/sqrt(2*pi*exp(1))
     
     ## Using "log = TRUE" for an extended range :
     par(mfrow = c(2,1))
     plot(function(x) dnorm(x, log = TRUE), -60, 50,
          main = "log { Normal density }")
     curve(log(dnorm(x)), add = TRUE, col = "red", lwd = 2)
     mtext("dnorm(x, log=TRUE)", adj = 0)
     mtext("log(dnorm(x))", col = "red", adj = 1)
     
     plot(function(x) pnorm(x, log.p = TRUE), -50, 10,
          main = "log { Normal Cumulative }")
     curve(log(pnorm(x)), add = TRUE, col = "red", lwd = 2)
     mtext("pnorm(x, log=TRUE)", adj = 0)
     mtext("log(pnorm(x))", col = "red", adj = 1)
     
     ## if you want the so-called 'error function'
     erf <- function(x) 2 * pnorm(x * sqrt(2)) - 1
     ## (see Abramowitz and Stegun 29.2.29)
     ## and the so-called 'complementary error function'
     erfc <- function(x) 2 * pnorm(x * sqrt(2), lower = FALSE)
     ## and the inverses
     erfinv <- function (x) qnorm((1 + x)/2)/sqrt(2)
     erfcinv <- function (x) qnorm(x/2, lower = FALSE)/sqrt(2)