---
title: "Understanding polynomial evaluation of linearity"
author: "Vishesh Shrivastav"
date: "`r Sys.Date()`"
output: rmarkdown::html_vignette
vignette: >
  %\VignetteIndexEntry{Understanding polynomial evaluation of linearity}
  %\VignetteEngine{knitr::rmarkdown}
  %\VignetteEncoding{UTF-8}
---

```{r setup, include = FALSE}
knitr::opts_chunk$set(
  collapse = TRUE,
  comment = "#>"
)
```

# lin.eval
R package for polynomial evaluation of linearity.

## About  
`lin.eval` is a R package for performing polynomial evaluation of linearity.
  
## How it works

Polynomial evaluation of linearity is a technique of assessing if the best way to describe the relationship between two vectors.

1) Fit three models - linear, second-order polynomial and third-order polynomial
2) Find out best-fitting model among the three by comparing their p-values. Model with the lowest p-value out of the three is the best-fitting one.
3) If the best-fitting model is linear, linearity is established and no further steps need to be carried out. This is called Linear 1 type.
4) Else, best-fitting model is either second or third order polynomoal model. In this case, calculate average deviation from linearity (adl). This is given by:  

$$adl\ =\ \frac{1}{n} * (\sum_{1}^{n}|\frac{l_i - p_i}{l_i}| * 100)$$      

where, `l` is the vector of predictions from linear model and `p` is the vector of predictions from best-fitting polynomial model.

5) If `adl` is greater than or equal to the threshold value for deviation from linearity, conclude that the relationship is non-linear.
6) Else if `adl` is less than the threshold value for deviation from linearity, conclude that although the best-fitting model is not linear, deviation from linearity is not significant and hence, it is still a linear relationship. This is called a Linear 2 type.

## Usage  
Call the `poly_eval()` function with the following parameters:  
`y`: vector of response values  
`x`: vector of predictor values  
`threshold`: threshold value for average deviation from linearity as percentage. Defaults to 5.

```{r}
library(lin.eval)
foo <- c(165.3929, 165.3929, 1119.5714, 1119.5714, 2073.7500, 2073.7500, 3027.9286, 3027.9286, 3982.1071, 3982.1071, 4936.2857, 4936.2857, 5890.4643, 5890.4643)
bar <- c(386.2143,  386.2143, 840.6548, 840.6548, 1829.6905, 1829.6905, 3074.4048, 3074.4048, 4295.8810, 4295.8810, 5215.2024, 5215.2024, 5553.4524, 5553.4524)
derp <- poly_eval(bar, foo, 30)
```  

You can check the values stored in the result variable:  
```{r}
derp$p1
derp$p2
derp$p3
```

## More examples 

Usage without passing in optional argument for adl:  
```{r}
foo <- c(0, 1, 2, 4, 6, 8, 9, 10, 11, 12, 13, 14, 15, 16, 18, 19, 20, 21, 22, 24, 25, 26, 27, 28, 29, 30)
bar <- c(126.6, 101.8, 71.6, 101.6, 68.1, 62.9, 45.5, 41.9, 46.3, 34.1, 38.2, 41.7, 24.7, 41.5, 36.6, 19.6, 22.8, 29.6, 23.5, 15.3, 13.4, 26.8, 9.8, 18.8, 25.9, 19.3)
poly_eval(bar, foo)
```
