<jupyter_start><jupyter_text>02. Introduction to R in SageMath Jupyter IPython Notebook [Generalised Linear Models](https://lamastex.github.io/scalable-data-science/glm/2018/)&copy;2018 <NAME>. [Attribution 4.0 International (CC BY 4.0)](https://creativecommons.org/licenses/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`<jupyter_code>%%r
install.packages("Flury")
library(Flury)
data(dead.beetles)<jupyter_output>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’<jupyter_text>Additional PackagesOne 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’`<jupyter_code>%%r
install.packages("faraway")
library(faraway)
?ilogit
%%r
m <- glm(data=dead.beetles, cbind(died, tested-died) ~ Dose, family=binomial(link=logit))
%%r
x <- seq(1.6, 1.9, by=0.01)
y <- ilogit(coef(m)["(Intercept)"] + x * coef(m)["Dose"])
%%r
coef(m)["Dose"]
%%r
coef(m)["(Intercept)"]
%%sh
pwd
ls -lh
%%sh
mkdir -p imagesFromR
ls -lh 
%%r
#with(dead.beetles, plot(Dose, died/tested))
png("imagesFromR/plot1.png")
plot(dead.beetles$Dose, dead.beetles$died/dead.beetles$tested)
dev.off()
%%sh
pwd
ls -lh imagesFromR<jupyter_output>/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
{'fraction_non_alphanumeric': 0.14754098360655737, 'fraction_numerical': 0.025162028211971027, 'mean_word_length': 5.074074074074074, 'pattern_counts': {'":': 0, '<': 11, '<?xml version=': 0, '>': 8, 'https://': 2, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 3, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'chain_length': '2', 'dir': 'jupyter-structured-clean-dedup', 'id': '393358', 'license': 'Unlicense', 'n_tokens_mistral': 959, 'n_tokens_neox': 892, 'n_words': 273, 'path': '_glm/2018/jp/02.ipynb', 'repo_name': 'r-e-x-a-g-o-n/scalable-data-science', 'size': '2634'}