---
title: "Trees Data Analysis"
author: "Milica Cudina"
output:
  pdf_document: default
---

The data set `trees` is built in. Let's take a look at it. 

```{r}
names(trees)
dim(trees)
```

It should contain measurements of $31$ cherry trees, namely, their `Girth`, `Height`, and `Volume`. 



Again, we undertake a rudimentary exploratory data analysis. It's natural to be interested in pairwise interactions. So, we create an array of scatterplots with which we can visually assess the shape of the dependence and the correlations of each pair of variables.

```{r}
plot(trees,
     col="blue", pch=20)
```

We might be interested in looking at, say, `Girth` as the explanatory and `Volume` as the response. This would be a simple linear regression. 

```{r}
attach(trees)
lm.fit.g<-lm(Volume ~ Girth, data=trees)
summary(lm.fit.g)
plot(Girth, Volume, 
     pch=20, col="blue",
     main="Dependence of Volume on 'Girth'")
abline(lm.fit.g, col="red", lwd=2)

lm.fit.h<-lm(Volume ~ Height, data=trees)
summary(lm.fit.h)
plot(Height, Volume, 
     pch=20, col="blue",
     main="Dependence of Volume on Height")
abline(lm.fit.h, col="red", lwd=2)
```


Now, let's see what happens when we add `Height` as an additional explanatory variable, thus creating a multiple linear regression. 

```{r}
lm.fit.m<-lm(Volume ~ Girth + Height)
summary(lm.fit.m)
```

Let's compare the **coefficient of determination** $R^2$ for the above two fits. 


For anyone who has ever encountered trees, it's natural to suspect that there is a correlation between `Height` and `Girth`. Let's check

```{r}
cor(Height, Girth)
```

So, it might be a good idea to introduce the interaction term in our multiple linear regression. 

```{r}
lm.fit.mi<-lm(Volume ~ Height*Girth)
summary(lm.fit.mi)
```

We should take note, again, of any changes (improvements?) in the $R^2$ and/or the $p-$values. 

Now, we can decide that we are reasonably happy, or we can go back to middle-school math and remember the formulae for volumes of cylinders. Which explanatory should we choose? 

```{r}
lm.fit.geom<-lm(Volume ~ 0 + I(Girth^2):Height)
summary(lm.fit.geom)
```

With the hierarchy principle:

```{r}
lm.fit.geom.h<-lm(Volume ~ I(Girth^2)*Height)
summary(lm.fit.geom.h)
```