<jupyter_start><jupyter_text>Workbook follows Section 2.12 of [Faraway 2002](https://cran.r-project.org/doc/contrib/Faraway-PRA.pdf)Galapagos Tortoise Data taken from [the accompanying R package](https://cran.r-project.org/web/packages/faraway/)Originally from [<NAME> (1973)](http://science.sciencemag.org/content/179/4076/893.long)<jupyter_code># global change of figure sizes
# following: http://blog.revolutionanalytics.com/2015/09/resizing-plots-in-the-r-kernel-for-jupyter-notebooks.html

library(repr)

options(repr.plot.width = 4, repr.plot.height = 3)<jupyter_output><empty_output><jupyter_text>**Step 1.** Load data and pre-process<jupyter_code>data(gala)
gala
summary(gala) # summary statistics
# fitting linear model
gfit <- lm(Species ~ Area + Elevation + Nearest + Scruz + Adjacent, data=gala)
summary(gfit)
# X matrix
x <- cbind(1, gala[,-c(1,2)])
x <- as.matrix(x) # force intro matrix form
# response
y <- gala$Species
# construct X^T.X
# t() for transpose
# %*% for matrix multiplication
t(x) %*% x
# solve() for inverse (i.e. (X^T X)^{-1})
# x-T-x-inverse
xtxi <- solve(t(x) %*% x)
xtxi
# more direct way to get (X^T X)^{-1}
gs <- summary(gfit)
gs$cov.unscaled
# use names() to see components of an object
names(gs)
names(gfit)
# beta-hat (LSE) directly
xtxi %*% t(x) %*% y<jupyter_output><empty_output>
{'fraction_non_alphanumeric': 0.1664145234493192, 'fraction_numerical': 0.0264750378214826, 'mean_word_length': 5.153488372093023, 'pattern_counts': {'":': 0, '<': 16, '<?xml version=': 0, '>': 10, 'https://': 2, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'chain_length': '2', 'dir': 'jupyter-structured-clean-dedup', 'id': '652729', 'license': 'MIT', 'n_tokens_mistral': 550, 'n_tokens_neox': 509, 'n_words': 112, 'path': 'Faraway s2.12 - Estimation example.ipynb', 'repo_name': 'clin-projects/faraway-regression-R-workbooks', 'size': '1333'}