---
title: "Getting started with BorderEffect"
output: rmarkdown::html_vignette
vignette: >
  %\VignetteIndexEntry{Getting started with BorderEffect}
  %\VignetteEngine{knitr::rmarkdown}
  %\VignetteEncoding{UTF-8}
---

```{r knitr-opts, include = FALSE}
knitr::opts_chunk$set(
  collapse = TRUE, comment = "#>",
  fig.width = 7.2, fig.height = 4.4, out.width = "100%",
  dpi = 110, fig.align = "center"
)
```

```{r setup}
library(BorderEffect)
data(field_trial)
head(field_trial)
```

## Estimate kappa

```{r}
be <- border_effect(response ~ trt + blk, data = field_trial, coords = field_trial)
be
```

## Decide presence/absence of the edge effect

```{r}
bt <- border_test(be, pattern = "outer1", nsim = 300, seed = 42)
bt
plot(bt)
```

## Spatial diagnostic

```{r}
moran_border(be)
```

## Analyze your own trial

The bundled `field_trial` is **simulated** (via `rnorm`) using the exact design of
the motivating dataset in the paper (a 12x8 staggered "tres bolillos" layout, 3
treatments, 2 blocks, 550 x 210 cm, border mean 2.21 vs interior 1.74). The
original field-recorded values are not distributed with the paper, but any data
with the same structure is analyzed the same way.

To analyze real data, provide a data frame (or CSV) with the plot coordinates
`x`, `y`, the `trt` and `blk` factors, and the `response`. An example CSV in this
exact format ships with the package:

```{r}
csv <- system.file("extdata", "field_trial.csv", package = "BorderEffect")
d <- read.csv(csv)
names(d)[names(d) == "block"] <- "blk"   # the CSV names the block column `block`
be2 <- border_effect(response ~ trt + blk, data = d, coords = d)
border_test(be2, pattern = "outer1", nsim = 300, seed = 1)
```

To reproduce the paper's exact layout and plug in your own 96 recorded yields (in
plot order), rebuild the layout and attach the response:

```{r eval=FALSE}
fl <- field_layout(nx = 12, ny = 8, ntrt = 3, nblk = 2,
                   arrangement = "triangular", width = 550, height = 210,
                   seed = 3000)
fl$response <- my_recorded_yields          # length-96 numeric vector, plot order
be <- border_effect(response ~ trt + blk, data = fl, coords = fl)
border_test(be, pattern = "outer1")
```
