---
title: "PP Hypothesis Tests"
output: pdf_document
---

```{r setup, include=FALSE}
knitr::opts_chunk$set(echo = FALSE)
knitr::opts_chunk$set(warning = FALSE)
knitr::opts_chunk$set(message = FALSE)
knitr::opts_chunk$set(fig.align= 'center')
knitr::opts_chunk$set(fig.height= 4)
knitr::opts_chunk$set(fig.width = 6)
library(tidyverse)
library(gridExtra)
```

### Hypothesis Tests for CSR

\vfill

\vfill

\vfill

```{r}
set.seed(03312021)
n <- rpois(4, 50)

x <- c(rbeta(n[1], 1, 1), rbeta(n[2], 1, 1), rbeta(n[3], 3, 1),rbeta(n[4], 3, 3))
y <- c(rbeta(n[1], 1, 1), rbeta(n[2], 1, 1), rbeta(n[3], 3, 1),rbeta(n[4], 3, 3))

data.frame(group = c(rep(1, n[1]), rep(2, n[2]), rep(3, n[3]), rep(4, n[4])), x = x, y = y) %>% ggplot(aes(x=x, y=y)) + geom_point(alpha=.6) + facet_wrap(~group) + theme_bw()
```
\vfill
\vfill
\vfill
\newpage

#### G and F Functions

One way to describe a spatial point process, is to consider the probability of being a certain distance from a point or similarly, the number of points expected in a distance from a point.
\vfill


\vfill



\vfill
\vfill

\vfill
A similar statistic is the $F(d)$ function. Whereas $G(d)$ is centered at the observed $\boldsymbol{s}_i$, $F(d)$ is defined at any arbitrary point. 
\vfill

\newpage

Discuss how to create an empirical estimate of $\hat{G}(d)$, given a realization of a point process.

\vfill

With bounded area, edge correction procedures are necessary. 

$$\hat{G}(d) = \frac{\sum_i 1(d_i \leq d < b_i)}{\sum_i 1(d < b_i)},$$

\vfill
The empirical estimates of $G$ or $H$ can be compared with $G$ or $F$ using a QQ-plot.

\vfill

- __Discuss:__ What would be the implications of shorter tails or longer tails than expected under CSR?

\vfill

Describe a natural process that might cluster and another than might repel
\vfill


## spatstat

Consider a dataset with medieval grave site information.

```{r, fig.height=6, fig.height=6}
library(spatstat)
library(smacpod)

data(grave)
summary(grave)
plot(grave)
```


\newpage

```{r, fig.height=6, fig.height=6}
plot(envelope(grave, Fest, verbose = F))
plot(envelope(grave, Gest, verbose = F))
```
\vfill


\newpage

### Estimating the intensity Function 

- With CSR, the intensity function is trivial 
\vfill
- __Discuss:__ given a realization of a point process, how could an intensity function be estimated?
\vfill

\vfill

\vfill

Now using the `plot(density(.))` function, plot and interpret the empirical intensity for the grave dataset along with the four synthetic examples.

```{r, fig.height=6, fig.width=6}
plot(density(grave))
```

\newpage

Now let's return to the four datasets we looked at earlier. You can use `envelope` and `density` but first need to create `ppp` objects.