02. Introduction to R in SageMath Jupyter IPython Notebook

Generalised Linear Models

©2018 Raazesh Sainudiin. Attribution 4.0 International (CC BY 4.0)

  1. How to run R commands in SageMath

    • installing packages
    • loading libraries
    • loading data
    • running glm model on the beetles data
    • making PNG images of the plots
  2. Why SageMath for R?

Use %%r to denote that the Code cell is of language R

In [1]:
%%r
install.packages("Flury")
library(Flury)
data(dead.beetles)
Installing package into ‘/home/raazesh/R/x86_64-pc-linux-gnu-library/3.4’
(as ‘lib’ is unspecified)
trying URL 'http://cran.r-project.org/src/contrib/Flury_0.1-3.tar.gz'
Content type 'application/x-gzip' length 50358 bytes (49 KB)
==================================================
downloaded 49 KB

* installing *source* package ‘Flury’ ...
** package ‘Flury’ successfully unpacked and MD5 sums checked
** data
** help
*** installing help indices
** building package indices
** testing if installed package can be loaded
* DONE (Flury)

The downloaded source packages are in
	‘/tmp/RtmpTwwYwT/downloaded_packages’


Additional Packages

One often needs several additional packages to run certain desired R commands. Let's get some such packages.

Note: Once a package is installed on a particular machine using install.packages("wantedpackage") then you only need to load that library using library(wantedpackage) when you are using the same machine.

In other words, you don't have to install packages that are already installed and thus can be automatically found by R in the default location it will be installed at. In the case below, you can see where the package was installed from the following line:

  • Installing package into ‘/home/raazesh/R/x86_64-pc-linux-gnu-library/3.4’
In [2]:
%%r
install.packages("faraway")
library(faraway)
?ilogit
Installing package into ‘/home/raazesh/R/x86_64-pc-linux-gnu-library/3.4’
(as ‘lib’ is unspecified)
trying URL 'http://cran.r-project.org/src/contrib/faraway_1.0.7.tar.gz'
Content type 'application/x-gzip' length 450183 bytes (439 KB)
==================================================
downloaded 439 KB

* installing *source* package ‘faraway’ ...
** package ‘faraway’ successfully unpacked and MD5 sums checked
** R
** data
*** moving datasets to lazyload DB
** preparing package for lazy loading
** help
*** installing help indices
** building package indices
** testing if installed package can be loaded
* DONE (faraway)

The downloaded source packages are in
	‘/tmp/RtmpTwwYwT/downloaded_packages’

ilogit                 package:faraway                 R Documentation

_I_n_v_e_r_s_e _L_o_g_i_t _T_r_a_n_s_f_o_r_m_a_t_i_o_n

_D_e_s_c_r_i_p_t_i_o_n:

     Computes the inverse logit transformation

_U_s_a_g_e:

     ilogit(x)
     
_A_r_g_u_m_e_n_t_s:

       x: a numeric vector

_V_a_l_u_e:

     exp(x)/(1+exp(x))

_A_u_t_h_o_r(_s):

     Julian Faraway

_S_e_e _A_l_s_o:

     logit

_E_x_a_m_p_l_e_s:

     ilogit(1:3)
     #[1] 0.7310586 0.8807971 0.9525741
     

In [3]:
%%r
m <- glm(data=dead.beetles, cbind(died, tested-died) ~ Dose, family=binomial(link=logit))

In [4]:
%%r
x <- seq(1.6, 1.9, by=0.01)
y <- ilogit(coef(m)["(Intercept)"] + x * coef(m)["Dose"])

In [5]:
%%r
coef(m)["Dose"]
    Dose 
34.27033 
In [6]:
%%r
coef(m)["(Intercept)"]
(Intercept) 
  -60.71745 
In [7]:
%%sh
pwd
ls -lh
/home/raazesh/all/git/scalable-data-science/_glm/2018/jp
total 852K
-rw-rw-r-- 1 raazesh raazesh 267K Nov  6 09:24 00.html
-rw-r--r-- 1 raazesh raazesh  12K Nov  6 09:28 00.ipynb
-rw-r--r-- 1 raazesh raazesh  411 Nov  6 09:35 00.md
-rw-rw-r-- 1 raazesh raazesh 261K Nov  6 09:30 01.html
-rw-r--r-- 1 raazesh raazesh  19K Nov  6 09:30 01.ipynb
-rw-r--r-- 1 raazesh raazesh  411 Nov  6 09:36 01.md
-rw-rw-r-- 1 raazesh raazesh 252K Nov  6 09:30 02.html
-rw-r--r-- 1 raazesh raazesh 7.8K Nov  6 09:43 02.ipynb
-rw-r--r-- 1 raazesh raazesh  411 Nov  6 09:36 02.md
drwxr-xr-x 2 raazesh raazesh 4.0K Nov  6 09:27 mydir
In [13]:
%%sh
mkdir -p imagesFromR
ls -lh
total 860K
-rw-rw-r-- 1 raazesh raazesh 267K Nov  6 09:24 00.html
-rw-r--r-- 1 raazesh raazesh  12K Nov  6 09:28 00.ipynb
-rw-r--r-- 1 raazesh raazesh  411 Nov  6 09:35 00.md
-rw-rw-r-- 1 raazesh raazesh 261K Nov  6 09:30 01.html
-rw-r--r-- 1 raazesh raazesh  19K Nov  6 09:30 01.ipynb
-rw-r--r-- 1 raazesh raazesh  411 Nov  6 09:36 01.md
-rw-rw-r-- 1 raazesh raazesh 252K Nov  6 09:30 02.html
-rw-r--r-- 1 raazesh raazesh  11K Nov  6 09:47 02.ipynb
-rw-r--r-- 1 raazesh raazesh  411 Nov  6 09:36 02.md
drwxr-xr-x 2 raazesh raazesh 4.0K Nov  6 09:47 imagesFromR
drwxr-xr-x 2 raazesh raazesh 4.0K Nov  6 09:27 mydir
In [14]:
%%r
#with(dead.beetles, plot(Dose, died/tested))
png("imagesFromR/plot1.png")
plot(dead.beetles$Dose, dead.beetles$died/dead.beetles$tested)
dev.off()


null device 
          1 
In [16]:
%%sh
pwd
ls -lh imagesFromR
/home/raazesh/all/git/scalable-data-science/_glm/2018/jp
total 4.0K
-rw-r--r-- 1 raazesh raazesh 2.6K Nov  6 09:48 plot1.png
In [ ]: