---
title: "prostate"
author: "Nancy"
date: "14/09/2022"
output:
  html_document: default
  pdf_document: default
---

```{r setup, include=FALSE}
knitr::opts_chunk$set(echo = TRUE)
library(faraway)
data(prostate)
options(digits=4)
``` 
## R Markdown

This is an R Markdown document. Markdown is a simple formatting syntax for authoring HTML, PDF, and MS Word documents. For more details on using R Markdown see <http://rmarkdown.rstudio.com>.

When you click the **Knit** button a document will be generated that includes both content as well as the output of any embedded R code chunks within the document. You can embed an R code chunk like this:

```{r prostate}
summary(prostate)
```

## Including Plots

You can also embed plots, for example:

```{r plot-example, echo=FALSE}
plot(prostate[,3])
```

Note that the `echo = FALSE` parameter was added to the code chunk to prevent printing of the R code that generated the plot.

## Preliminary Analysis
From `summary` above, nothing looks too out of the ordinary. A few more details (but not many) are provided by `help(prostate)` or `?prostate`. Unfortunately, simply `knit`ing the .Rmd file does not put the data and other objects into your workspace. (Does anyone know a workaround?)
I had to type `data(prostate)` in my console window, before using `?prostate`.

I'll leave further plotting to you, and move onto

## Linear Regression 
```{r regression}
model1 <- lm(lpsa ~ ., data = prostate)
summary(model1)
coef(model1)
sqrt(diag(vcov(model1)))
```

And now for the questions in Exercise 2.4 of Faraway:
```{r Exercise 2.4}
model2 <- lm(lpsa ~ lcavol, data = prostate)
summary(model2)$r.squared; summary(model2)$sigma # I deduced these from attributes(summary(model2)) 
```
And I'll leave it to you to figure out the rest of Exercise 2.4: "Add `lweight`, `svi`, `lbph`, `age`, `lcp`, `pgg45` and `gleason` to the model one at a time. For each model record the residual standard error and the $R^2$. Plot the trends in these two statistics."

A free coffee for the most elegant solution.

